Access control list (acl) module. An acl is a list of account addresses who
have the access permission to a certain object.
This module uses a vector
to represent the list, but can be refactored to
use a "set" instead when it's available in the language in the future.
- Struct
ACL
- Constants
- Function
empty
- Function
add
- Function
remove
- Function
contains
- Function
assert_contains
use 0x1::Errors;
use 0x1::Vector;
struct ACL has copy, drop, store
Fields
-
list: vector<address>
The ACL already contains the address.
const ECONTAIN: u64 = 0;
The ACL does not contain the address.
const ENOT_CONTAIN: u64 = 1;
Return an empty ACL.
Implementation
public fun empty(): ACL {
ACL{ list: Vector::empty<address>() }
}
Add the address to the ACL.
Implementation
public fun add(acl: &mut ACL, addr: address) {
assert!(!Vector::contains(&mut acl.list, &addr), Errors::invalid_argument(ECONTAIN));
Vector::push_back(&mut acl.list, addr);
}
Remove the address from the ACL.
Implementation
public fun remove(acl: &mut ACL, addr: address) {
let (found, index) = Vector::index_of(&mut acl.list, &addr);
assert!(found, Errors::invalid_argument(ENOT_CONTAIN));
Vector::remove(&mut acl.list, index);
}
Return true iff the ACL contains the address.
Implementation
public fun contains(acl: &ACL, addr: address): bool {
Vector::contains(&acl.list, &addr)
}
assert! that the ACL has the address.
public fun assert_contains(acl: &ACL::ACL, addr: address)
Implementation
public fun assert_contains(acl: &ACL, addr: address) {
assert!(contains(acl, addr), Errors::invalid_argument(ENOT_CONTAIN));
}