use std::future::Future;
pub use super::router::ViewAction;
use crate::{AppResult, AppState, auth::CurrentPrincipal};
use che_orm::{Expr, Model, SelectQuery, Transaction, ValidatedWrite};
#[derive(Clone, Copy)]
pub struct PermissionContext<'a> {
pub state: &'a AppState,
pub transaction: Option<&'a Transaction>,
}
impl<'a> From<&'a AppState> for PermissionContext<'a> {
fn from(state: &'a AppState) -> Self {
Self {
state,
transaction: None,
}
}
}
impl<'a> PermissionContext<'a> {
pub fn in_transaction(state: &'a AppState, transaction: &'a Transaction) -> Self {
Self {
state,
transaction: Some(transaction),
}
}
}
pub trait Permission<M: Model>: Clone + Send + Sync + Default + 'static {
fn check_request(
&self,
context: PermissionContext<'_>,
current: Option<&CurrentPrincipal>,
action: ViewAction,
) -> impl Future<Output = AppResult<()>> + Send;
fn scope(
&self,
context: PermissionContext<'_>,
current: Option<&CurrentPrincipal>,
action: ViewAction,
) -> impl Future<Output = AppResult<Option<Expr>>> + Send {
async move {
self.check_request(context, current, action).await?;
if self.object_scoped() {
return Err(crate::AppError::Forbidden(
"object-scoped permissions must provide an explicit SQL scope".into(),
));
}
Ok(None)
}
}
fn check_object(
&self,
context: PermissionContext<'_>,
current: Option<&CurrentPrincipal>,
action: ViewAction,
model: &M,
) -> impl Future<Output = AppResult<()>> + Send
where
M: Send + Sync + 'static,
{
async move {
if let Some(scope) = self.scope(context, current, action).await? {
let rows = context
.fetch_all(
M::query()
.filter(M::primary_key().eq(model.primary_key_value()))
.filter(scope)
.limit(1),
)
.await?;
if rows.is_empty() {
return Err(crate::AppError::Forbidden(
"object permission required".into(),
));
}
}
Ok(())
}
}
fn check_write(
&self,
context: PermissionContext<'_>,
current: Option<&CurrentPrincipal>,
action: ViewAction,
model: Option<&M>,
_write: Option<&ValidatedWrite<M>>,
) -> impl Future<Output = AppResult<()>> + Send
where
M: Send + Sync + 'static,
{
async move {
self.check_request(context, current, action).await?;
if let Some(model) = model {
self.check_object(context, current, action, model).await?;
}
Ok(())
}
}
fn after_write(
&self,
_context: PermissionContext<'_>,
_current: Option<&CurrentPrincipal>,
_action: ViewAction,
_model: &M,
) -> impl Future<Output = AppResult<()>> + Send {
async { Ok(()) }
}
fn validate(&self, _fields: &[che_orm::SerializerField]) -> AppResult<()> {
Ok(())
}
fn model_access_key(&self) -> Option<&'static str> {
None
}
fn object_scoped(&self) -> bool {
true
}
}
#[derive(Clone, Copy, Default)]
pub struct AllowAny;
impl<M: Model> Permission<M> for AllowAny {
fn object_scoped(&self) -> bool {
false
}
async fn check_request(
&self,
_: PermissionContext<'_>,
_: Option<&CurrentPrincipal>,
_: ViewAction,
) -> AppResult<()> {
Ok(())
}
}
impl PermissionContext<'_> {
pub async fn fetch_all<M: Model + Send + 'static>(
&self,
query: SelectQuery<M>,
) -> AppResult<Vec<M>> {
Ok(match self.transaction {
Some(tx) => tx.fetch_all(query).await?,
None => self.state.database().fetch_all(query).await?,
})
}
}