class.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. use super::*;
  2. /// Permissions for an instance of a `Class` in the versioned store.
  3. #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
  4. #[derive(Encode, Decode, Eq, PartialEq, Clone, Debug, Default)]
  5. pub struct ClassPermissions<CuratorGroupId: Ord + Default> {
  6. /// For this permission, the individual member is allowed to create the entity and become controller.
  7. any_member: bool,
  8. /// Whether to prevent everyone from creating an entity.
  9. ///
  10. /// This could be useful in order to quickly, and possibly temporarily, block new entity creation, without
  11. /// having to tear down `can_create_entities`.
  12. entity_creation_blocked: bool,
  13. /// Whether to prevent everyone from updating entity properties.
  14. ///
  15. /// This could be useful in order to quickly, and probably temporarily, block any editing of entities,
  16. /// rather than for example having to set, and later clear.
  17. all_entity_property_values_locked: bool,
  18. /// Current class maintainer curator groups
  19. maintainers: BTreeSet<CuratorGroupId>,
  20. }
  21. impl<CuratorGroupId: Ord + Default> ClassPermissions<CuratorGroupId> {
  22. /// Retieve `all_entity_property_values_locked` status
  23. pub fn all_entity_property_values_locked(&self) -> bool {
  24. self.all_entity_property_values_locked
  25. }
  26. /// Retieve `any_member` status
  27. pub fn any_member_status(&self) -> bool {
  28. self.any_member
  29. }
  30. /// Check if given `curator_group_id` is maintainer of current `Class`
  31. pub fn is_maintainer(&self, curator_group_id: &CuratorGroupId) -> bool {
  32. self.maintainers.contains(curator_group_id)
  33. }
  34. /// Get `Class` maintainers by reference
  35. pub fn get_maintainers(&self) -> &BTreeSet<CuratorGroupId> {
  36. &self.maintainers
  37. }
  38. /// Get `Class` maintainers by mutable reference
  39. pub fn get_maintainers_mut(&mut self) -> &mut BTreeSet<CuratorGroupId> {
  40. &mut self.maintainers
  41. }
  42. /// Set `entity_creation_blocked` flag, as provided
  43. pub fn set_entity_creation_blocked(&mut self, entity_creation_blocked: bool) {
  44. self.entity_creation_blocked = entity_creation_blocked
  45. }
  46. /// Set `all_entity_property_values_locked` flag, as provided
  47. pub fn set_all_entity_property_values_locked(
  48. &mut self,
  49. all_entity_property_values_locked: bool,
  50. ) {
  51. self.all_entity_property_values_locked = all_entity_property_values_locked
  52. }
  53. /// Set `any_member` flag, as provided
  54. pub fn set_any_member_status(&mut self, any_member: bool) {
  55. self.any_member = any_member;
  56. }
  57. /// Update `maintainers` set with provided one
  58. pub fn set_maintainers(&mut self, maintainers: BTreeSet<CuratorGroupId>) {
  59. self.maintainers = maintainers
  60. }
  61. /// Ensure entities creation is not blocked on `Class` level
  62. pub fn ensure_entity_creation_not_blocked<T: Trait>(&self) -> Result<(), Error<T>> {
  63. ensure!(
  64. !self.entity_creation_blocked,
  65. Error::<T>::EntitiesCreationBlocked
  66. );
  67. Ok(())
  68. }
  69. /// Ensure maintainer, associated with given `curator_group_id` is already added to `maintainers` set
  70. pub fn ensure_maintainer_exists<T: Trait>(
  71. &self,
  72. curator_group_id: &CuratorGroupId,
  73. ) -> Result<(), Error<T>> {
  74. ensure!(
  75. self.maintainers.contains(curator_group_id),
  76. Error::<T>::MaintainerDoesNotExist
  77. );
  78. Ok(())
  79. }
  80. /// Ensure maintainer, associated with given `curator_group_id` is not yet added to `maintainers` set
  81. pub fn ensure_maintainer_does_not_exist<T: Trait>(
  82. &self,
  83. curator_group_id: &CuratorGroupId,
  84. ) -> Result<(), Error<T>> {
  85. ensure!(
  86. !self.maintainers.contains(curator_group_id),
  87. Error::<T>::MaintainerAlreadyExists
  88. );
  89. Ok(())
  90. }
  91. }