permissions.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. mod class;
  2. mod curator_group;
  3. mod entity;
  4. mod entity_creation_voucher;
  5. pub use class::*;
  6. pub use curator_group::*;
  7. pub use entity::*;
  8. pub use entity_creation_voucher::*;
  9. pub use crate::errors::*;
  10. use crate::*;
  11. pub use codec::{Codec, Decode, Encode};
  12. use core::fmt::Debug;
  13. use runtime_primitives::traits::{MaybeSerializeDeserialize, Member, SimpleArithmetic};
  14. #[cfg(feature = "std")]
  15. pub use serde::{Deserialize, Serialize};
  16. use srml_support::{dispatch, ensure, Parameter};
  17. /// Model of authentication manager.
  18. pub trait ActorAuthenticator: system::Trait + Debug {
  19. /// Curator identifier
  20. type CuratorId: Parameter
  21. + Member
  22. + SimpleArithmetic
  23. + Codec
  24. + Default
  25. + Copy
  26. + Clone
  27. + MaybeSerializeDeserialize
  28. + Eq
  29. + PartialEq
  30. + Ord;
  31. /// Member identifier
  32. type MemberId: Parameter
  33. + Member
  34. + SimpleArithmetic
  35. + Codec
  36. + Default
  37. + Copy
  38. + Clone
  39. + MaybeSerializeDeserialize
  40. + Eq
  41. + PartialEq
  42. + Ord;
  43. /// Curator group identifier
  44. type CuratorGroupId: Parameter
  45. + Member
  46. + SimpleArithmetic
  47. + Codec
  48. + One
  49. + Default
  50. + Copy
  51. + Clone
  52. + MaybeSerializeDeserialize
  53. + Eq
  54. + PartialEq
  55. + Ord;
  56. /// Authorize actor as lead
  57. fn is_lead(account_id: &Self::AccountId) -> bool;
  58. /// Authorize actor as curator
  59. fn is_curator(curator_id: &Self::CuratorId, account_id: &Self::AccountId) -> bool;
  60. /// Authorize actor as member
  61. fn is_member(member_id: &Self::MemberId, account_id: &Self::AccountId) -> bool;
  62. }
  63. /// Ensure curator authorization performed succesfully
  64. pub fn ensure_curator_auth_success<T: ActorAuthenticator>(
  65. curator_id: &T::CuratorId,
  66. account_id: &T::AccountId,
  67. ) -> dispatch::Result {
  68. ensure!(
  69. T::is_curator(curator_id, account_id),
  70. ERROR_CURATOR_AUTH_FAILED
  71. );
  72. Ok(())
  73. }
  74. /// Ensure member authorization performed succesfully
  75. pub fn ensure_member_auth_success<T: ActorAuthenticator>(
  76. member_id: &T::MemberId,
  77. account_id: &T::AccountId,
  78. ) -> dispatch::Result {
  79. ensure!(
  80. T::is_member(member_id, account_id),
  81. ERROR_MEMBER_AUTH_FAILED
  82. );
  83. Ok(())
  84. }
  85. /// Ensure lead authorization performed succesfully
  86. pub fn ensure_lead_auth_success<T: ActorAuthenticator>(
  87. account_id: &T::AccountId,
  88. ) -> dispatch::Result {
  89. ensure!(T::is_lead(account_id), ERROR_LEAD_AUTH_FAILED);
  90. Ok(())
  91. }
  92. /// Ensure given `Origin` is lead
  93. pub fn ensure_is_lead<T: ActorAuthenticator>(origin: T::Origin) -> dispatch::Result {
  94. let account_id = ensure_signed(origin)?;
  95. ensure_lead_auth_success::<T>(&account_id)
  96. }
  97. /// Enum, representing all possible `Actor`s
  98. #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
  99. #[derive(Encode, Decode, Eq, PartialEq, Clone, Copy, Debug)]
  100. pub enum Actor<T: Trait> {
  101. Curator(T::CuratorGroupId, T::CuratorId),
  102. Member(T::MemberId),
  103. Lead,
  104. }
  105. impl<T: Trait> Default for Actor<T> {
  106. fn default() -> Self {
  107. Self::Lead
  108. }
  109. }