61 lines
1.8 KiB
Rust
61 lines
1.8 KiB
Rust
|
|
use serde::{Deserialize,Serialize,Deserializer};
|
||
|
|
|
||
|
|
// #[derive(Error, Debug)]
|
||
|
|
// pub enum AuthError {
|
||
|
|
// #[error("error")]
|
||
|
|
// SomeError(),
|
||
|
|
// #[error("no authorization header found")]
|
||
|
|
// NoAuthHeaderFoundError,
|
||
|
|
// #[error("wrong authorization header format")]
|
||
|
|
// InvalidAuthHeaderFormatError,
|
||
|
|
// #[error("no user found for this token")]
|
||
|
|
// InvalidTokenError,
|
||
|
|
// #[error("error during authorization")]
|
||
|
|
// AuthorizationError,
|
||
|
|
// #[error("user is not unauthorized")]
|
||
|
|
// UnauthorizedError,
|
||
|
|
// #[error("no user found with this name")]
|
||
|
|
// UserNotFoundError,
|
||
|
|
// }
|
||
|
|
|
||
|
|
#[derive(Eq, PartialEq, Clone, Serialize, Debug, Deserialize)]
|
||
|
|
pub enum UserRole {
|
||
|
|
SuperUser,
|
||
|
|
Developer,
|
||
|
|
User,
|
||
|
|
Anonymous,
|
||
|
|
}
|
||
|
|
impl Default for UserRole {
|
||
|
|
fn default() -> Self {
|
||
|
|
UserRole::Anonymous
|
||
|
|
}
|
||
|
|
}
|
||
|
|
impl std::fmt::Display for UserRole {
|
||
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||
|
|
match self {
|
||
|
|
UserRole::SuperUser => write!(f,"superuser"),
|
||
|
|
UserRole::Developer => write!(f,"developer"),
|
||
|
|
UserRole::User => write!(f,"user"),
|
||
|
|
UserRole::Anonymous => write!(f,"anonymous"),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
impl UserRole {
|
||
|
|
#[allow(dead_code)]
|
||
|
|
pub fn from_str(value: &str) -> UserRole {
|
||
|
|
match value {
|
||
|
|
"superuser" | "SuperUser" | "superUser" | "admin" => UserRole::SuperUser,
|
||
|
|
"developer" | "Developer" => UserRole::Developer,
|
||
|
|
"user" | "User" => UserRole::User,
|
||
|
|
"anonymous" | "Anonymous" => UserRole::Anonymous,
|
||
|
|
_ => UserRole::default(),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
#[allow(dead_code)]
|
||
|
|
pub fn deserialize_user_role<'de, D>(deserializer: D) -> Result<UserRole, D::Error>
|
||
|
|
where D: Deserializer<'de> {
|
||
|
|
let buf = String::deserialize(deserializer)?;
|
||
|
|
Ok(UserRole::from_str(&buf))
|
||
|
|
}
|