Browse Source

Content_directory module: Fix remaining class related type definitions

iorveth 4 years ago
parent
commit
94ba801cd2

+ 4 - 4
runtime-modules/content-directory/src/class.rs

@@ -4,7 +4,7 @@ use super::*;
 #[derive(Encode, Decode, Eq, PartialEq, Default, Clone)]
 pub struct Class<
     EntityId: Default + BaseArithmetic,
-    ClassId: Default + BaseArithmetic,
+    ClassId: Default + BaseArithmetic + Clone,
     CuratorGroupId: Ord + Default,
 > {
     /// Permissions for an instance of a Class.
@@ -48,7 +48,7 @@ pub struct Class<
 
 impl<
         EntityId: Default + BaseArithmetic,
-        ClassId: Default + BaseArithmetic,
+        ClassId: Default + BaseArithmetic + Clone,
         CuratorGroupId: Ord + Default,
     > Class<EntityId, ClassId, CuratorGroupId>
 {
@@ -224,7 +224,7 @@ impl<
     /// Ensure `Property` under given `PropertyId` is unlocked from actor with given `EntityAccessLevel`
     /// return corresponding `Property` by value
     pub fn ensure_class_property_type_unlocked_from<T: Trait>(
-        &self,
+        self,
         in_class_schema_property_id: PropertyId,
         entity_access_level: EntityAccessLevel,
     ) -> Result<Property<ClassId>, Error<T>> {
@@ -242,7 +242,7 @@ impl<
         // Ensure Property is unlocked from Actor with given EntityAccessLevel
         class_property.ensure_unlocked_from::<T>(entity_access_level)?;
 
-        Ok(class_property.to_owned())
+        Ok(class_property.clone())
     }
 
     /// Ensure property values were not locked on `Class` level

+ 36 - 2
runtime-modules/content-directory/src/lib.rs

@@ -974,7 +974,7 @@ decl_module! {
             class_permissions.ensure_entity_creation_not_blocked::<T>()?;
 
             // Ensure actor can create entities
-            class_permissions.ensure_can_create_entities(&account_id, &actor)?;
+            Self::ensure_can_create_entities(&class_permissions, &account_id, &actor)?;
 
             let entity_controller = EntityController::from_actor(&actor);
 
@@ -2131,7 +2131,7 @@ impl<T: Trait> Module<T> {
         // Ensure Entity under given id exists, retrieve corresponding one
         let entity = Self::ensure_known_entity_id(entity_id)?;
 
-        let class = ClassById::get(entity.get_class_id());
+        let class = ClassById::<T>::get(entity.get_class_id());
         Ok((entity, class))
     }
 
@@ -2183,6 +2183,40 @@ impl<T: Trait> Module<T> {
         Ok(())
     }
 
+    /// Ensure provided actor can create entities of current `Class`
+    pub fn ensure_can_create_entities(
+        class_permissions: &ClassPermissions<T::CuratorGroupId>,
+        account_id: &T::AccountId,
+        actor: &Actor<T>,
+    ) -> Result<(), Error<T>> {
+        let can_create = match &actor {
+            Actor::Lead => {
+                // Ensure lead authorization performed succesfully
+                ensure_lead_auth_success::<T>(account_id)?;
+                true
+            }
+            Actor::Member(member_id) if class_permissions.any_member_status() => {
+                // Ensure member authorization performed succesfully
+                ensure_member_auth_success::<T>(member_id, account_id)?;
+                true
+            }
+            Actor::Curator(curator_group_id, curator_id)
+                if class_permissions.is_maintainer(curator_group_id) =>
+            {
+                // Authorize curator, performing all checks to ensure curator can act
+                CuratorGroup::<T>::perform_curator_in_group_auth(
+                    curator_id,
+                    curator_group_id,
+                    account_id,
+                )?;
+                true
+            }
+            _ => false,
+        };
+        ensure!(can_create, Error::<T>::ActorCanNotCreateEntities);
+        Ok(())
+    }
+
     /// Ensure all provided `new_property_value_references_with_same_owner_flag_set` are valid
     fn ensure_are_valid_references_with_same_owner_flag_set(
         new_property_value_references_with_same_owner_flag_set: InputValuesForExistingProperties<T>,

+ 0 - 34
runtime-modules/content-directory/src/permissions/class.rs

@@ -83,40 +83,6 @@ impl<CuratorGroupId: Ord + Default> ClassPermissions<CuratorGroupId> {
         self.maintainers = maintainers
     }
 
-    /// Ensure provided actor can create entities of current `Class`
-    pub fn ensure_can_create_entities<T: Trait>(
-        &self,
-        account_id: &T::AccountId,
-        actor: &Actor<T>,
-    ) -> Result<(), Error<T>> {
-        let can_create = match &actor {
-            Actor::Lead => {
-                // Ensure lead authorization performed succesfully
-                ensure_lead_auth_success::<T>(account_id)?;
-                true
-            }
-            Actor::Member(member_id) if self.any_member => {
-                // Ensure member authorization performed succesfully
-                ensure_member_auth_success::<T>(member_id, account_id)?;
-                true
-            }
-            Actor::Curator(curator_group_id, curator_id)
-                if self.maintainers.contains(curator_group_id) =>
-            {
-                // Authorize curator, performing all checks to ensure curator can act
-                CuratorGroup::<T>::perform_curator_in_group_auth(
-                    curator_id,
-                    curator_group_id,
-                    account_id,
-                )?;
-                true
-            }
-            _ => false,
-        };
-        ensure!(can_create, Error::<T>::ActorCanNotCreateEntities);
-        Ok(())
-    }
-
     /// Ensure entities creation is not blocked on `Class` level
     pub fn ensure_entity_creation_not_blocked<T: Trait>(&self) -> Result<(), Error<T>> {
         ensure!(

+ 24 - 13
runtime-modules/content-directory/src/schema/property.rs

@@ -253,7 +253,7 @@ impl<ClassId: Default + BaseArithmetic> Property<ClassId> {
     }
 
     /// Ensure property vector length after value inserted is valid
-    fn validate_property_vector_length_after_value_insert<V>(
+    fn validate_property_vector_length_after_value_insert<T: Trait, V>(
         vec: &[V],
         max_len: VecMaxLength,
     ) -> Result<(), Error<T>> {
@@ -290,29 +290,35 @@ impl<ClassId: Default + BaseArithmetic> Property<ClassId> {
         ) {
             // Single values
             (InputValue::Bool(_), VecStoredValue::Bool(vec), Type::Bool) => {
-                Self::validate_property_vector_length_after_value_insert(vec, max_vec_len)
+                Self::validate_property_vector_length_after_value_insert::<T, bool>(
+                    vec,
+                    max_vec_len,
+                )
             }
             (InputValue::Uint16(_), VecStoredValue::Uint16(vec), Type::Uint16) => {
-                Self::validate_property_vector_length_after_value_insert(vec, max_vec_len)
+                Self::validate_property_vector_length_after_value_insert::<T, u16>(vec, max_vec_len)
             }
             (InputValue::Uint32(_), VecStoredValue::Uint32(vec), Type::Uint32) => {
-                Self::validate_property_vector_length_after_value_insert(vec, max_vec_len)
+                Self::validate_property_vector_length_after_value_insert::<T, u32>(vec, max_vec_len)
             }
             (InputValue::Uint64(_), VecStoredValue::Uint64(vec), Type::Uint64) => {
-                Self::validate_property_vector_length_after_value_insert(vec, max_vec_len)
+                Self::validate_property_vector_length_after_value_insert::<T, u64>(vec, max_vec_len)
             }
             (InputValue::Int16(_), VecStoredValue::Int16(vec), Type::Int16) => {
-                Self::validate_property_vector_length_after_value_insert(vec, max_vec_len)
+                Self::validate_property_vector_length_after_value_insert::<T, i16>(vec, max_vec_len)
             }
             (InputValue::Int32(_), VecStoredValue::Int32(vec), Type::Int32) => {
-                Self::validate_property_vector_length_after_value_insert(vec, max_vec_len)
+                Self::validate_property_vector_length_after_value_insert::<T, i32>(vec, max_vec_len)
             }
             (InputValue::Int64(_), VecStoredValue::Int64(vec), Type::Int64) => {
-                Self::validate_property_vector_length_after_value_insert(vec, max_vec_len)
+                Self::validate_property_vector_length_after_value_insert::<T, i64>(vec, max_vec_len)
             }
             (InputValue::Text(text_item), VecStoredValue::Text(vec), Type::Text(text_max_len)) => {
                 Self::validate_max_len_of_text(text_item, *text_max_len)?;
-                Self::validate_property_vector_length_after_value_insert(vec, max_vec_len)
+                Self::validate_property_vector_length_after_value_insert::<T, Vec<u8>>(
+                    vec,
+                    max_vec_len,
+                )
             }
             (
                 InputValue::TextToHash(text_item),
@@ -322,7 +328,10 @@ impl<ClassId: Default + BaseArithmetic> Property<ClassId> {
                 if let Some(text_max_len) = text_max_len {
                     Self::validate_max_len_of_text_to_be_hashed(text_item, *text_max_len)?;
                 }
-                Self::validate_property_vector_length_after_value_insert(vec, max_vec_len)
+                Self::validate_property_vector_length_after_value_insert::<T, T::Hash>(
+                    vec,
+                    max_vec_len,
+                )
             }
             (
                 InputValue::Reference(entity_id),
@@ -339,7 +348,10 @@ impl<ClassId: Default + BaseArithmetic> Property<ClassId> {
                     *same_controller_status,
                     current_entity_controller,
                 )?;
-                Self::validate_property_vector_length_after_value_insert(vec, max_vec_len)
+                Self::validate_property_vector_length_after_value_insert::<T, T::EntityId>(
+                    vec,
+                    max_vec_len,
+                )
             }
             _ => Err(Error::<T>::PropertyValueTypeDoesNotMatchInternalVectorType),
         }
@@ -568,9 +580,8 @@ impl<ClassId: Default + BaseArithmetic> Property<ClassId> {
         class_id: ClassId,
     ) -> Result<Entity<T>, Error<T>> {
         // Ensure Entity under given id exists
-        Module::<T>::ensure_known_entity_id(entity_id)?;
+        let entity = Module::<T>::ensure_known_entity_id(entity_id)?;
 
-        let entity = Module::<T>::entity_by_id(entity_id);
         ensure!(
             entity.get_class_id() == class_id,
             Error::<T>::ReferencedEntityDoesNotMatchItsClass