proper permission checking
This commit is contained in:
@@ -3,6 +3,7 @@ use sqlx::PgConnection;
|
|||||||
use crate::{database::DatabaseError, users::User};
|
use crate::{database::DatabaseError, users::User};
|
||||||
|
|
||||||
/// Infradmin and systemuser have all permissions.
|
/// Infradmin and systemuser have all permissions.
|
||||||
|
#[derive(strum::IntoStaticStr)]
|
||||||
pub enum Permission {
|
pub enum Permission {
|
||||||
// All Users have the right to observe their own sessions
|
// All Users have the right to observe their own sessions
|
||||||
ListOthersSessions,
|
ListOthersSessions,
|
||||||
@@ -16,26 +17,41 @@ pub enum Permission {
|
|||||||
CreateTags,
|
CreateTags,
|
||||||
RenameTags,
|
RenameTags,
|
||||||
DeleteTags,
|
DeleteTags,
|
||||||
#[allow(unused)]
|
CreateQuotes,
|
||||||
DeleteQuotes,
|
DeleteQuotes,
|
||||||
ChangePersonPrimaryName,
|
ChangePersonPrimaryName,
|
||||||
#[allow(unused)]
|
|
||||||
BrowseServerLogs,
|
BrowseServerLogs,
|
||||||
ConfigureInstance,
|
ConfigureInstance,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Permission {
|
||||||
|
pub fn is_default_permission(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
Self::CreateTags | Self::CreateQuotes => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl User {
|
impl User {
|
||||||
pub async fn has_permission(
|
pub async fn has_permission(
|
||||||
&self,
|
&self,
|
||||||
#[allow(unused)] conn: &mut PgConnection,
|
conn: &mut PgConnection,
|
||||||
#[allow(unused)] permission: Permission,
|
permission: Permission,
|
||||||
) -> Result<bool, DatabaseError> {
|
) -> Result<bool, DatabaseError> {
|
||||||
// Infradmin and systemuser have all permissions
|
|
||||||
if self.is_infradmin() || self.is_systemuser() {
|
if self.is_infradmin() || self.is_systemuser() {
|
||||||
return Ok(true);
|
return Ok(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(false)
|
let permission_key: &'static str = (&permission).into();
|
||||||
// todo!("Do the permission checking here once permissions are modeled in the DB")
|
let state: Option<bool> = sqlx::query_scalar(
|
||||||
|
"SELECT state FROM user_permissions WHERE user_id = $1 AND permission = $2",
|
||||||
|
)
|
||||||
|
.bind(self.id)
|
||||||
|
.bind(permission_key)
|
||||||
|
.fetch_optional(&mut *conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(state.unwrap_or_else(|| permission.is_default_permission()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user