astrox_macros 0.2.2

add user management and canister relation management for canister
Documentation
#[macro_export]
macro_rules! inject_canister_users {
    () => {
        thread_local! {
          pub static USER: RefCell<User> = RefCell::new(User::default());
        }

        use astrox_macros::user::User;
        use ic_cdk::caller;
        use ic_cdk::export::Principal;
        use ic_cdk::{id, trap};
        use std::cell::RefCell;

        /* owner relative methods */
        #[inline(always)]
        pub fn owner_guard() -> Result<(), String> {
            let caller = caller();
            let ret = is_owner(caller);
            if ret {
                Ok(())
            } else {
                trap(&format!("{} unauthorized", caller));
            }
        }

        pub fn is_owner(user_id: Principal) -> bool {
            USER.with(|b| b.borrow().is_owner(user_id))
        }

        pub fn owners_set(users: BTreeMap<Principal, String>) {
            USER.with(|s| s.borrow_mut().owners_set(users));
        }

        pub fn owners() -> Option<BTreeMap<Principal, String>> {
            USER.with(|s| s.borrow().owners())
        }


        pub fn owner_add(user_id: Principal) {
            USER.with(|s| s.borrow_mut().owner_add(user_id.to_text(), user_id));
        }


        pub fn owner_add_with_name(name: String, user_id: Principal) {
            USER.with(|s| s.borrow_mut().owner_add(name, user_id));
        }


        pub fn owner_remove(user_id: Principal) {
            USER.with(|s| s.borrow_mut().owner_remove(user_id));
        }

        /* user relative methods */
        #[inline(always)]
        pub fn user_guard() -> Result<(), String> {
            let caller = caller();
            let ret = USER.with(|b| b.borrow().is_user(caller));
            if ret {
                Ok(())
            } else {
                trap(&format!("{} unauthorized", caller));
            }
        }

        pub fn users_set(users: BTreeMap<Principal, String>)  {
            USER.with(|s| s.borrow_mut().users_set(users));
        }


        pub fn users() -> Option<BTreeMap<Principal, String>> {
            USER.with(|s| s.borrow().users())
        }

        pub fn user_add(user_id: Principal) {
            USER.with(|s| s.borrow_mut().user_add(user_id.to_text(), user_id));
        }


        pub fn user_add_with_name(name: String, user_id: Principal) {
            USER.with(|s| s.borrow_mut().user_add(name, user_id));
        }

        pub fn user_remove(user_id: Principal) {
            USER.with(|s| s.borrow_mut().user_remove(user_id));
        }

        /* op relative methods */
        #[inline(always)]
        pub fn op_guard() -> Result<(), String> {
            let caller = caller();
            let ret = USER.with(|b| b.borrow().is_op(caller));
            if ret {
                Ok(())
            } else {
                trap(&format!("{} unauthorized", caller));
            }
        }

        pub fn ops_set(users: BTreeMap<Principal, String>)  {
            USER.with(|s| s.borrow_mut().ops_set(users));
        }

        pub fn ops() -> Option<BTreeMap<Principal, String>> {
            USER.with(|s| s.borrow().ops())
        }


        pub fn op_add(user_id: Principal) {
            USER.with(|s| s.borrow_mut().op_add(user_id.to_text(), user_id));
        }


        pub fn op_add_with_name(name: String, user_id: Principal) {
            USER.with(|s| s.borrow_mut().op_add(name, user_id));
        }

        pub fn op_remove(user_id: Principal) {
            USER.with(|s| s.borrow_mut().op_remove(user_id));
        }


        pub fn users_pre_upgrade() -> User {
            USER.with(|s| s.take().into())
        }

        pub fn users_post_upgrade(stable_state: User) {
            USER.with(|s| s.replace(stable_state));
        }
    };
}