pallet_permission0/permission/
namespace.rs

1use codec::{Decode, Encode, MaxEncodedLen};
2use pallet_torus0_api::NamespacePath;
3use polkadot_sdk::{
4    frame_support::{CloneNoBound, DebugNoBound},
5    sp_runtime::{BoundedBTreeMap, BoundedBTreeSet},
6};
7use scale_info::TypeInfo;
8
9use crate::{Config, Permissions};
10
11use super::PermissionId;
12
13/// Scope for namespace permissions
14#[derive(Encode, Decode, CloneNoBound, TypeInfo, MaxEncodedLen, DebugNoBound)]
15#[scale_info(skip_type_params(T))]
16pub struct NamespaceScope<T: Config> {
17    pub recipient: T::AccountId,
18    /// Set of namespace paths this permission delegates access to
19    pub paths: BoundedBTreeMap<
20        Option<PermissionId>,
21        BoundedBTreeSet<NamespacePath, T::MaxNamespacesPerPermission>,
22        T::MaxNamespacesPerPermission,
23    >,
24    /// Maximum number of instances of this permission
25    pub max_instances: u32,
26    /// Children permissions
27    pub children: BoundedBTreeSet<PermissionId, T::MaxChildrenPerPermission>,
28}
29
30impl<T: Config> NamespaceScope<T> {
31    /// Cleanup operations when permission is revoked or expired
32    pub(super) fn cleanup(
33        &self,
34        permission_id: polkadot_sdk::sp_core::H256,
35        _last_execution: &Option<crate::BlockNumberFor<T>>,
36        _delegator: &T::AccountId,
37    ) {
38        for pid in self.paths.keys().cloned().flatten() {
39            Permissions::<T>::mutate_extant(pid, |parent| {
40                if let Some(children) = parent.children_mut() {
41                    children.remove(&permission_id);
42                }
43            });
44        }
45    }
46}