use crate::domain::common::EmailAddress;
use std::fmt;
#[derive(Debug, Clone, PartialEq)]
pub enum AuthorizationError {
Forbidden {
actor: EmailAddress,
action: String,
resource: String,
},
}
impl fmt::Display for AuthorizationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AuthorizationError::Forbidden {
actor,
action,
resource,
} => write!(
f,
"ユーザー {} には {} を {} する権限がありません",
actor.as_str(),
resource,
action
),
}
}
}
impl std::error::Error for AuthorizationError {}
pub trait AuthorizationPolicy<T> {
fn authorize_update(
&self,
actor: &EmailAddress,
resource: &T,
) -> Result<(), AuthorizationError>;
fn authorize_delete(
&self,
actor: &EmailAddress,
resource: &T,
) -> Result<(), AuthorizationError>;
fn authorize_read(
&self,
_actor: &EmailAddress,
_resource: &T,
) -> Result<(), AuthorizationError> {
Ok(())
}
}