/// A generic module for role-based access control (RBAC). module std::role { use std::error; use std::signer; const EROLE: u64 = 0; struct Role has key {} /// Assign the role to the account. The caller must pass a witness, so is /// expected to be a function of the module that defines `Type`. public fun assign_role(to: &signer, _witness: &Type) { assert!(!has_role(signer::address_of(to)), error::already_exists(EROLE)); move_to>(to, Role{}); } /// Revoke the role from the account. The caller must pass a witness, so is /// expected to be a function of the module that defines `Type`. public fun revoke_role(from: &signer, _witness: &Type) acquires Role { assert!(has_role(signer::address_of(from)), error::not_found(EROLE)); let Role{} = move_from>(signer::address_of(from)); } /// Return true iff the address has the role. public fun has_role(addr: address): bool { exists>(addr) } /// assert! that the account has the role. public fun assert_has_role(account: &signer) { assert!(has_role(signer::address_of(account)), error::not_found(EROLE)); } }