use super::dependency_unavailable;
use crate::{
access::AccessError,
cdk::types::Principal,
ops::{config::ConfigOps, runtime::env::EnvOps, storage::children::CanisterChildrenOps},
};
use ic_cdk::api::{canister_self, is_controller as caller_is_controller};
#[expect(clippy::unused_async)]
pub(super) async fn is_controller(caller: Principal) -> Result<(), AccessError> {
if caller_is_controller(&caller) {
Ok(())
} else {
Err(AccessError::ControllerRequired)
}
}
#[expect(clippy::unused_async)]
pub(super) async fn is_whitelisted(caller: Principal) -> Result<(), AccessError> {
let whitelisted = ConfigOps::is_whitelisted(&caller).map_err(dependency_unavailable)?;
if !whitelisted {
return Err(AccessError::WhitelistRequired);
}
Ok(())
}
#[expect(clippy::unused_async)]
pub(super) async fn is_child(caller: Principal) -> Result<(), AccessError> {
if CanisterChildrenOps::contains_pid(&caller) {
Ok(())
} else {
Err(AccessError::DirectChildRequired)
}
}
#[expect(clippy::unused_async)]
pub(super) async fn is_parent(caller: Principal) -> Result<(), AccessError> {
let parent_pid = EnvOps::parent_pid().map_err(dependency_unavailable)?;
if parent_pid == caller {
Ok(())
} else {
Err(AccessError::ParentRequired)
}
}
#[expect(clippy::unused_async)]
pub(super) async fn is_root(caller: Principal) -> Result<(), AccessError> {
let root_pid = EnvOps::root_pid().map_err(dependency_unavailable)?;
if caller == root_pid {
Ok(())
} else {
Err(AccessError::RootRequired)
}
}
#[expect(clippy::unused_async)]
pub(super) async fn is_same_canister(caller: Principal) -> Result<(), AccessError> {
if caller == canister_self() {
Ok(())
} else {
Err(AccessError::SelfRequired)
}
}