pallet_permission0/permission/
curator.rs

1use bitflags::bitflags;
2use codec::{Decode, Encode, MaxEncodedLen};
3use polkadot_sdk::{
4    frame_support::{CloneNoBound, DebugNoBound, EqNoBound, PartialEqNoBound},
5    polkadot_sdk_frame::prelude::BlockNumberFor,
6    sp_runtime::{BoundedBTreeMap, BoundedBTreeSet},
7};
8use scale_info::TypeInfo;
9
10use crate::{Config, Permissions};
11
12use super::PermissionId;
13
14#[derive(
15    CloneNoBound,
16    Copy,
17    DebugNoBound,
18    Encode,
19    Decode,
20    EqNoBound,
21    PartialEqNoBound,
22    TypeInfo,
23    MaxEncodedLen,
24)]
25pub struct CuratorPermissions(u32);
26
27bitflags! {
28    impl CuratorPermissions: u32 {
29        /// Able to appoint other curators. Though not used at the moment,
30        /// it will be valuable when we remove the SUDO key/multisig.
31        const ROOT                        = 0b0000_0001;
32        /// Permission to review and process agent applications
33        const APPLICATION_REVIEW          = 0b0000_0010;
34        /// Permission to manage the whitelist (add/remove accounts)
35        const WHITELIST_MANAGE            = 0b0000_0100;
36        /// Permission to apply penalty factors to agents
37        const PENALTY_CONTROL             = 0b0000_1000;
38        /// Permission to toggle agent freezing
39        const AGENT_FREEZING_TOGGLING     = 0b0001_0000;
40        /// Permission to toggle namespace freezing
41        const NAMESPACE_FREEZING_TOGGLING = 0b0010_0000;
42    }
43}
44
45#[derive(Encode, Decode, CloneNoBound, PartialEq, TypeInfo, MaxEncodedLen, DebugNoBound)]
46#[scale_info(skip_type_params(T))]
47pub struct CuratorScope<T: Config> {
48    pub recipient: T::AccountId,
49    pub flags: BoundedBTreeMap<
50        Option<PermissionId>,
51        CuratorPermissions,
52        T::MaxCuratorSubpermissionsPerPermission,
53    >,
54    pub cooldown: Option<BlockNumberFor<T>>,
55    /// Maximum number of instances of this permission
56    pub max_instances: u32,
57    /// Children permissions
58    pub children: BoundedBTreeSet<PermissionId, T::MaxChildrenPerPermission>,
59}
60
61impl<T: Config> CuratorScope<T> {
62    pub fn has_permission(&self, permission: CuratorPermissions) -> bool {
63        self.flags.iter().any(|(_, p)| p.contains(permission))
64    }
65}
66
67impl<T: Config> CuratorScope<T> {
68    /// Cleanup operations when permission is revoked or expired
69    pub(crate) fn cleanup(
70        &self,
71        permission_id: polkadot_sdk::sp_core::H256,
72        _last_execution: &Option<crate::BlockNumberFor<T>>,
73        _delegator: &T::AccountId,
74    ) {
75        for pid in self.flags.keys().cloned().flatten() {
76            Permissions::<T>::mutate_extant(pid, |parent| {
77                if let Some(children) = parent.children_mut() {
78                    children.remove(&permission_id);
79                }
80            });
81        }
82    }
83}