dialtone_axum 0.1.0

Dialtone Axum Back-end
Documentation
use dialtone_common::{
    rest::users::{user_login::NameHostPair, web_user::SystemRole},
    utils::parse_acct::parse_user_acct,
};

use crate::{
    err_unauthorized,
    response::response_error::ResponseError,
    start_server::{Claims, SharedState},
};

use super::authz_sysrole::authz_sysrole;

pub async fn authz_acct_or_sysrole(
    acct: &str,
    claims: &Claims,
    state: &SharedState,
    role: &SystemRole,
) -> Result<NameHostPair, ResponseError> {
    let owner_user_host = parse_user_acct(acct)?;
    if claims.sub != acct {
        authz_sysrole(&state, role, &owner_user_host.host_name)
            .await
            .map_or_else(|_| err_unauthorized!(), |_| Ok(owner_user_host))
    } else {
        Ok(owner_user_host)
    }
}

pub async fn authz_acct_or_contentadmin(
    acct: &str,
    claims: &Claims,
    state: &SharedState,
) -> Result<NameHostPair, ResponseError> {
    authz_acct_or_sysrole(acct, claims, state, &SystemRole::ContentAdmin).await
}

pub async fn authz_acct_or_actoradmin(
    acct: &str,
    claims: &Claims,
    state: &SharedState,
) -> Result<NameHostPair, ResponseError> {
    authz_acct_or_sysrole(acct, claims, state, &SystemRole::ActorAdmin).await
}

pub async fn authz_acct_or_useradmin(
    acct: &str,
    claims: &Claims,
    state: &SharedState,
) -> Result<NameHostPair, ResponseError> {
    authz_acct_or_sysrole(acct, claims, state, &SystemRole::UserAdmin).await
}