use serde::{Deserialize, Serialize};
use std::ops::BitOr;
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Copy)]
pub enum Role {
Super,
Admin,
Controller,
Viewer,
Audit,
None,
}
impl BitOr for Role {
type Output = bool;
fn bitor(self, rhs: Role) -> Self::Output {
matches!(self, Role::Admin)
|| matches!(rhs, Role::Admin)
|| matches!(self, Role::Controller)
|| matches!(rhs, Role::Controller)
|| matches!(self, Role::Viewer)
|| matches!(rhs, Role::Viewer)
|| matches!(self, Role::Audit)
|| matches!(rhs, Role::Audit)
|| matches!(self, Role::Super)
|| matches!(rhs, Role::Super)
}
}
impl Role {
pub fn from_str(role: &str) -> Self {
match role {
"admin" => Self::Admin,
"controller" => Self::Controller,
"viewer" => Self::Viewer,
"SUPER" => Self::Super,
"audit" => Self::Audit,
"none" => Self::None,
_ => Self::None,
}
}
pub fn to_str(&self) -> &'static str {
match self {
Self::Admin => "admin",
Self::Controller => "controller",
Self::Viewer => "viewer",
Self::Super => "SUPER",
Self::Audit => "audit",
Self::None => "none",
}
}
}
pub fn has_org_permission(current_role: Role, required_role: Role) -> bool {
match (current_role, required_role) {
(Role::Admin, _) => true, (Role::Controller, Role::Viewer) => true, (Role::Controller, Role::Audit) => true, (Role::Viewer, Role::Audit) => true, _ if current_role == required_role => true, _ => false, }
}