proper permission checking

This commit is contained in:
2026-05-06 03:07:03 +02:00
parent 7d284f0777
commit 1f07952973

View File

@@ -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()))
} }
} }