Skip to content

Commit 590d671

Browse files
committed
role hierarchy with guardians
1 parent 224f619 commit 590d671

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

content/stellar-contracts/access/access-control.mdx

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,46 +36,47 @@ This allows for creating complex organizational structures with chains of comman
3636
Here's how to establish and use role hierarchies in practice:
3737

3838
```rust
39-
use soroban_sdk::{contract, contractimpl, symbol_short, Address, Env};
39+
use soroban_sdk::{contract, contractimpl, symbol_short, Address, Env, Symbol};
4040
use stellar_access::access_control::{self as access_control, AccessControl};
4141

42+
const MANAGER_ROLE: Symbol = symbol_short!("manager");
43+
const GUARDIAN_ROLE: Symbol = symbol_short!("guardian");
44+
4245
#[contract]
4346
pub struct MyContract;
4447

4548
#[contractimpl]
4649
impl MyContract {
47-
pub fn __constructor(e: &Env, admin: Address) {
50+
pub fn __constructor(e: &Env, admin: Address, manager: Address) {
4851
// Set the contract admin
4952
access_control::set_admin(e, &admin);
5053

51-
// Define role hierarchy: MANAGER_ROLE can manage USER_ROLE
52-
let manager_role = symbol_short!("manager");
53-
let user_role = symbol_short!("user");
54-
55-
// Set MANAGER_ROLE as the admin role for USER_ROLE
56-
access_control::set_role_admin(e, &admin, &user_role, &manager_role);
54+
// 1. Set MANAGER_ROLE as the admin role for GUARDIAN_ROLE:
55+
// accounts with MANAGER_ROLE can manage accounts with GUARDIAN_ROLE
56+
access_control::set_role_admin_no_auth(e, &admin, &GUARDIAN_ROLE, &MANAGER_ROLE);
57+
58+
// 2. Admin grants MANAGER_ROLE to the manager account
59+
access_control::grant_role_no_auth(e, &admin, &manager, &MANAGER_ROLE);
5760
}
58-
59-
pub fn setup_roles(e: &Env, admin: Address, manager: Address, user: Address) {
60-
let manager_role = symbol_short!("manager");
61-
let user_role = symbol_short!("user");
62-
63-
// Admin grants MANAGER_ROLE to the manager account
64-
access_control::grant_role(e, &admin, &manager, &manager_role);
61+
62+
pub fn manage_guardians(e: &Env, manager: Address, guardian1: Address, guardian2: Address) {
63+
// Manager must be authorized
64+
manager.require_auth();
6565

66-
// Now the manager can grant USER_ROLE to other accounts
67-
access_control::grant_role(e, &manager, &user, &user_role);
66+
// 3. Now the manager can grant GUARDIAN_ROLE to other accounts
67+
access_control::grant_role_no_auth(e, &manager, &guardian1, &GUARDIAN_ROLE);
68+
access_control::grant_role_no_auth(e, &manager, &guardian2, &GUARDIAN_ROLE);
6869

69-
// Manager can also revoke USER_ROLE
70-
access_control::revoke_role(e, &manager, &user, &user_role);
70+
// Manager can also revoke GUARDIAN_ROLE
71+
access_control::revoke_role_no_auth(e, &manager, &guardian1, &GUARDIAN_ROLE);
7172
}
7273
}
7374
```
7475

7576
In this example:
76-
1. The contract admin sets `manager` as the admin role for `user` using `set_role_admin()`
77-
2. The admin grants the `manager` role to a manager account
78-
3. The manager can now grant/revoke the `user` role to other accounts without requiring admin intervention
77+
1. The `admin` sets `MANAGER_ROLE` as the admin role for `GUARDIAN_ROLE` using `set_role_admin()`
78+
2. The `admin` grants the `MANAGER_ROLE` role to the `manager` account
79+
3. The `manager` can now grant/revoke the `GUARDIAN_ROLE` role to other accounts without requiring admin intervention
7980

8081
### Role Enumeration
8182

0 commit comments

Comments
 (0)