Skip to main content

AccessControl Enumerable

This example shows how you can reuse the implementation of AccessControl with AccessControlEnumerable extension, which enables an easier overview of access control roles.

First, you should implement basic version of AccessControl.

Step 1: Add imports and enable unstable feature

Import everything from openbrush::contracts::access_control::extensions::enumerable.

#![cfg_attr(not(feature = "std"), no_std)]
#![feature(min_specialization)]

#[openbrush::contract]
pub mod my_access_control {
use openbrush::contracts::access_control::extensions::enumerable::*;
...

Step 2: Define storage

Pass enumerable::Members into access_control::Data to be able to use AcessControlEnumerable extension in your AccessControl implementation.

#[derive(Default, SpreadAllocate, Storage)]
#[ink(storage)]
pub struct Contract {
#[storage_field]
access: access_control::Data<enumerable::Members>,
}

Step 3: Inherit logic

Inherit implementation of the AccessControlEnumerable trait. You can customize (override) methods in this impl block.


impl AccessControl for Contract {}

impl AccessControlEnumerable for Contract {}

Final code

#![cfg_attr(not(feature = "std"), no_std)]
#![feature(min_specialization)]

#[openbrush::contract]
pub mod my_access_control {
use ink_storage::traits::SpreadAllocate;
use openbrush::{
contracts::access_control::extensions::enumerable::*,
traits::Storage,
};

#[derive(Default, SpreadAllocate, Storage)]
#[ink(storage)]
pub struct Contract {
#[storage_field]
access: access_control::Data<enumerable::Members>,
}

impl AccessControl for Contract {}
impl AccessControlEnumerable for Contract {}

impl Contract {
#[ink(constructor)]
pub fn new() -> Self {
ink_lang::codegen::initialize_contract(|_instance: &mut Self| {})
}
}
}

And that's it! Your AccessControl is now extended by the AccessControlEnumerable extension and ready to use its functions! You can check an example of the usage of AccessControl Enumerable.

You can also check the documentation for the basic implementation of AccessControl.