#[macro_export]
macro_rules! inject_canister_users {
() => {
thread_local! {
pub static USER: RefCell<User> = RefCell::new(User::default());
}
use astrox_macros::user::OwnerTrait;
use astrox_macros::user::User;
use astrox_macros::user::UserTrait;
use ic_cdk::caller;
use ic_cdk::export::Principal;
use ic_cdk::{id, trap};
use std::cell::RefCell;
#[inline(always)]
pub fn owner_guard() -> Result<(), String> {
let caller = caller();
let ret = USER.with(|b| b.borrow().check_owner(caller));
if ret {
Ok(())
} else {
trap(&format!("{} unauthorized", caller));
}
}
#[update(name = "role_owner_set", guard = "owner_guard")]
#[candid_method(update, rename = "role_owner_set")]
pub fn role_owner_set(principals: Vec<Principal>) -> Result<(), String> {
USER.with(|s| s.borrow_mut().role_owner_set(principals));
Ok(())
}
#[update(name = "role_owner_add", guard = "owner_guard")]
#[candid_method(update, rename = "role_owner_add")]
pub fn role_owner_add(principal: Principal) -> Result<(), String> {
USER.with(|s| s.borrow_mut().role_owner_add(principal));
Ok(())
}
#[update(name = "role_owner_remove", guard = "owner_guard")]
#[candid_method(update, rename = "role_owner_remove")]
pub fn role_owner_remove(principal: Principal) -> Result<(), String> {
USER.with(|s| s.borrow_mut().role_owner_remove(principal));
Ok(())
}
#[inline(always)]
pub fn user_guard() -> Result<(), String> {
let caller = caller();
let ret = USER.with(|b| b.borrow().check_user(caller));
if ret {
Ok(())
} else {
trap(&format!("{} unauthorized", caller));
}
}
#[update(name = "role_user_set", guard = "owner_guard")]
#[candid_method(update, rename = "role_user_set")]
pub fn role_user_set(principals: Vec<Principal>) -> Result<(), String> {
USER.with(|s| s.borrow_mut().role_user_set(principals));
Ok(())
}
#[update(name = "role_user_add", guard = "owner_guard")]
#[candid_method(update, rename = "role_user_add")]
pub fn role_user_add(principal: Principal) -> Result<(), String> {
USER.with(|s| s.borrow_mut().role_user_add(principal));
Ok(())
}
#[update(name = "role_user_remove", guard = "owner_guard")]
#[candid_method(update, rename = "role_user_remove")]
pub fn role_user_remove(principal: Principal) -> Result<(), String> {
USER.with(|s| s.borrow_mut().role_user_remove(principal));
Ok(())
}
pub fn users_init(caller: Principal) {
USER.with(|s| {
let mut s = s.borrow_mut();
s.role_owner_add(caller)
});
}
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));
}
};
}