feat: add user roles and storage quota management

This commit is contained in:
2025-11-02 17:17:13 +01:00
parent 596313b8c5
commit f49d9179f5
10 changed files with 183 additions and 30 deletions

View File

@@ -0,0 +1,25 @@
use uuid::Uuid;
use crate::models::{Album, Media, Role, User};
pub trait Ownable {
fn owner_id(&self) -> Uuid;
}
impl Ownable for Media {
fn owner_id(&self) -> Uuid {
self.owner_id
}
}
impl Ownable for Album {
fn owner_id(&self) -> Uuid {
self.owner_id
}
}
pub fn is_admin(user: &User) -> bool {
user.role == Role::Admin
}
pub fn is_owner(user_id: Uuid, entity: &impl Ownable) -> bool {
user_id == entity.owner_id()
}

View File

@@ -1,3 +1,4 @@
pub mod authz;
pub mod config;
pub mod error;
pub mod models;

View File

@@ -1,3 +1,20 @@
#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
#[sqlx(rename_all = "lowercase")]
#[sqlx(type_name = "TEXT")]
pub enum Role {
User,
Admin,
}
impl Role {
pub fn as_str(&self) -> &'static str {
match self {
Role::User => "user",
Role::Admin => "admin",
}
}
}
pub struct Media {
pub id: uuid::Uuid,
pub owner_id: uuid::Uuid,
@@ -11,7 +28,7 @@ pub struct Media {
pub height: Option<i32>,
}
#[derive(Clone)]
#[derive(Clone, sqlx::FromRow)]
pub struct User {
pub id: uuid::Uuid,
pub username: String,
@@ -19,6 +36,10 @@ pub struct User {
pub hashed_password: String,
pub created_at: chrono::DateTime<chrono::Utc>,
pub updated_at: chrono::DateTime<chrono::Utc>,
pub role: Role,
pub storage_quota: i64, // in bytes
pub storage_used: i64, // in bytes
}
pub struct Album {

View File

@@ -27,6 +27,7 @@ pub trait UserRepository: Send + Sync {
async fn find_by_email(&self, email: &str) -> CoreResult<Option<User>>;
async fn find_by_username(&self, username: &str) -> CoreResult<Option<User>>;
async fn find_by_id(&self, id: Uuid) -> CoreResult<Option<User>>;
async fn update_storage_used(&self, user_id: Uuid, bytes: i64) -> CoreResult<()>;
}
#[async_trait]