che_rest/rest/
permissions.rs1use std::future::Future;
3
4pub use super::router::ViewAction;
5use crate::{AppResult, AppState, auth::CurrentPrincipal};
6use che_orm::{Expr, Model, SelectQuery, Transaction, ValidatedWrite};
7
8#[derive(Clone, Copy)]
10pub struct PermissionContext<'a> {
11 pub state: &'a AppState,
12 pub transaction: Option<&'a Transaction>,
13}
14impl<'a> From<&'a AppState> for PermissionContext<'a> {
15 fn from(state: &'a AppState) -> Self {
16 Self {
17 state,
18 transaction: None,
19 }
20 }
21}
22impl<'a> PermissionContext<'a> {
23 pub fn in_transaction(state: &'a AppState, transaction: &'a Transaction) -> Self {
24 Self {
25 state,
26 transaction: Some(transaction),
27 }
28 }
29}
30
31pub trait Permission<M: Model>: Clone + Send + Sync + Default + 'static {
32 fn check_request(
33 &self,
34 context: PermissionContext<'_>,
35 current: Option<&CurrentPrincipal>,
36 action: ViewAction,
37 ) -> impl Future<Output = AppResult<()>> + Send;
38
39 fn scope(
42 &self,
43 context: PermissionContext<'_>,
44 current: Option<&CurrentPrincipal>,
45 action: ViewAction,
46 ) -> impl Future<Output = AppResult<Option<Expr>>> + Send {
47 async move {
48 self.check_request(context, current, action).await?;
49 if self.object_scoped() {
50 return Err(crate::AppError::Forbidden(
51 "object-scoped permissions must provide an explicit SQL scope".into(),
52 ));
53 }
54 Ok(None)
55 }
56 }
57 fn check_object(
58 &self,
59 context: PermissionContext<'_>,
60 current: Option<&CurrentPrincipal>,
61 action: ViewAction,
62 model: &M,
63 ) -> impl Future<Output = AppResult<()>> + Send
64 where
65 M: Send + Sync + 'static,
66 {
67 async move {
68 if let Some(scope) = self.scope(context, current, action).await? {
69 let rows = context
70 .fetch_all(
71 M::query()
72 .filter(M::primary_key().eq(model.primary_key_value()))
73 .filter(scope)
74 .limit(1),
75 )
76 .await?;
77 if rows.is_empty() {
78 return Err(crate::AppError::Forbidden(
79 "object permission required".into(),
80 ));
81 }
82 }
83 Ok(())
84 }
85 }
86 fn check_write(
88 &self,
89 context: PermissionContext<'_>,
90 current: Option<&CurrentPrincipal>,
91 action: ViewAction,
92 model: Option<&M>,
93 _write: Option<&ValidatedWrite<M>>,
94 ) -> impl Future<Output = AppResult<()>> + Send
95 where
96 M: Send + Sync + 'static,
97 {
98 async move {
99 self.check_request(context, current, action).await?;
100 if let Some(model) = model {
101 self.check_object(context, current, action, model).await?;
102 }
103 Ok(())
104 }
105 }
106 fn after_write(
108 &self,
109 _context: PermissionContext<'_>,
110 _current: Option<&CurrentPrincipal>,
111 _action: ViewAction,
112 _model: &M,
113 ) -> impl Future<Output = AppResult<()>> + Send {
114 async { Ok(()) }
115 }
116 fn validate(&self, _fields: &[che_orm::SerializerField]) -> AppResult<()> {
117 Ok(())
118 }
119 fn model_access_key(&self) -> Option<&'static str> {
121 None
122 }
123 fn object_scoped(&self) -> bool {
126 true
127 }
128}
129
130#[derive(Clone, Copy, Default)]
131pub struct AllowAny;
132impl<M: Model> Permission<M> for AllowAny {
133 fn object_scoped(&self) -> bool {
134 false
135 }
136 async fn check_request(
137 &self,
138 _: PermissionContext<'_>,
139 _: Option<&CurrentPrincipal>,
140 _: ViewAction,
141 ) -> AppResult<()> {
142 Ok(())
143 }
144}
145
146impl PermissionContext<'_> {
147 pub async fn fetch_all<M: Model + Send + 'static>(
148 &self,
149 query: SelectQuery<M>,
150 ) -> AppResult<Vec<M>> {
151 Ok(match self.transaction {
152 Some(tx) => tx.fetch_all(query).await?,
153 None => self.state.database().fetch_all(query).await?,
154 })
155 }
156}