2026-05-28 00:44:20 +02:00
|
|
|
use crate::application::dtos::cursor::PageCursor;
|
2026-02-14 01:29:34 +01:00
|
|
|
use crate::application::dtos::folder_dto::{
|
2026-05-28 00:44:20 +02:00
|
|
|
CreateFolderDto, FolderDto, FolderResourceCursor, FolderResourceRow, ListResourcesOptions,
|
|
|
|
|
MoveFolderDto, RenameFolderDto,
|
2026-02-14 01:29:34 +01:00
|
|
|
};
|
2026-05-20 22:56:00 +02:00
|
|
|
use crate::application::ports::authorization_ports::AuthorizationEngine;
|
2026-05-20 15:39:53 +02:00
|
|
|
use crate::application::ports::folder_ports::FolderUseCase;
|
2025-03-26 18:33:22 +01:00
|
|
|
use crate::common::errors::{DomainError, ErrorKind};
|
2026-03-04 23:55:08 +01:00
|
|
|
use crate::domain::repositories::folder_repository::FolderRepository;
|
2026-05-20 22:56:00 +02:00
|
|
|
use crate::domain::services::authorization::{Permission, Resource, Subject};
|
2026-05-08 00:11:02 +02:00
|
|
|
use crate::domain::services::path_service::{StoragePath, validate_storage_name};
|
2026-03-03 15:36:42 +00:00
|
|
|
use crate::infrastructure::repositories::pg::folder_db_repository::FolderDbRepository;
|
2026-05-20 22:56:00 +02:00
|
|
|
use crate::infrastructure::services::pg_acl_engine::PgAclEngine;
|
2026-03-04 23:55:08 +01:00
|
|
|
use std::sync::Arc;
|
2026-03-07 14:59:32 +01:00
|
|
|
use uuid::Uuid;
|
2025-03-17 21:28:08 +01:00
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Implementation of the use case for folder operations
|
2025-03-17 21:28:08 +01:00
|
|
|
pub struct FolderService {
|
2026-03-03 15:36:42 +00:00
|
|
|
folder_storage: Arc<FolderDbRepository>,
|
2026-05-20 22:56:00 +02:00
|
|
|
authz: Arc<PgAclEngine>,
|
2025-03-17 21:28:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FolderService {
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Creates a new folder service
|
2026-05-20 22:56:00 +02:00
|
|
|
pub fn new(folder_storage: Arc<FolderDbRepository>, authz: Arc<PgAclEngine>) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
folder_storage,
|
|
|
|
|
authz,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-19 11:27:11 +00:00
|
|
|
/// Batch counterpart of `get_folder`: resolve many folder ids in ONE
|
|
|
|
|
/// query instead of one per id. Like `get_folder` it performs no
|
|
|
|
|
/// per-folder authorization — both current callers (ACL grant listing,
|
|
|
|
|
/// NextCloud favorites REPORT) resolve ids already vetted by the
|
|
|
|
|
/// authorization engine or the favorites table. Missing or trashed ids
|
|
|
|
|
/// are absent from the result; callers re-associate by `id`.
|
|
|
|
|
pub async fn get_folders_by_ids(&self, ids: &[String]) -> Result<Vec<FolderDto>, DomainError> {
|
|
|
|
|
let folders = self.folder_storage.get_folders_by_ids(ids).await?;
|
|
|
|
|
Ok(folders.into_iter().map(FolderDto::from).collect())
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-20 22:56:00 +02:00
|
|
|
/// Helper: parse a folder id string into a `Resource::Folder`. Returns
|
|
|
|
|
/// `DomainError::not_found` on parse error (anti-enumeration — the same
|
|
|
|
|
/// error as "folder does not exist").
|
|
|
|
|
fn folder_resource(id: &str) -> Result<Resource, DomainError> {
|
|
|
|
|
Uuid::parse_str(id)
|
|
|
|
|
.map(Resource::Folder)
|
|
|
|
|
.map_err(|_| DomainError::not_found("Folder", id))
|
2025-03-17 21:28:08 +01:00
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2025-03-20 09:22:31 +01:00
|
|
|
/// Creates a stub implementation for testing and middleware
|
|
|
|
|
pub fn new_stub() -> impl FolderUseCase {
|
|
|
|
|
struct FolderServiceStub;
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2025-03-20 09:22:31 +01:00
|
|
|
impl FolderUseCase for FolderServiceStub {
|
2026-05-21 21:50:42 +02:00
|
|
|
async fn require_permission(
|
2026-05-21 11:07:04 +02:00
|
|
|
&self,
|
|
|
|
|
_caller_id: Uuid,
|
|
|
|
|
_permission: Permission,
|
|
|
|
|
_folder_id: &str,
|
|
|
|
|
) -> Result<(), DomainError> {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2026-05-20 15:39:53 +02:00
|
|
|
async fn create_folder_with_perms(
|
2026-05-20 12:48:06 +02:00
|
|
|
&self,
|
|
|
|
|
_dto: CreateFolderDto,
|
|
|
|
|
_user_id: Uuid,
|
|
|
|
|
) -> Result<FolderDto, DomainError> {
|
2025-03-20 09:22:31 +01:00
|
|
|
Ok(FolderDto::empty())
|
|
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2025-03-20 09:22:31 +01:00
|
|
|
async fn get_folder(&self, _id: &str) -> Result<FolderDto, DomainError> {
|
|
|
|
|
Ok(FolderDto::empty())
|
|
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-05-20 15:39:53 +02:00
|
|
|
async fn get_folder_with_perms(
|
2026-03-05 21:28:51 +01:00
|
|
|
&self,
|
|
|
|
|
_id: &str,
|
2026-03-07 14:59:32 +01:00
|
|
|
_caller_id: Uuid,
|
2026-03-05 21:28:51 +01:00
|
|
|
) -> Result<FolderDto, DomainError> {
|
2026-03-05 10:30:39 +01:00
|
|
|
Ok(FolderDto::empty())
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-18 23:02:17 +02:00
|
|
|
async fn get_folder_by_path(
|
|
|
|
|
&self,
|
|
|
|
|
_path: &str,
|
|
|
|
|
_user_id: Uuid,
|
|
|
|
|
) -> Result<FolderDto, DomainError> {
|
2025-03-20 09:22:31 +01:00
|
|
|
Ok(FolderDto::empty())
|
|
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
|
|
|
|
async fn list_folders(
|
|
|
|
|
&self,
|
|
|
|
|
_parent_id: Option<&str>,
|
|
|
|
|
) -> Result<Vec<FolderDto>, DomainError> {
|
2025-03-20 09:22:31 +01:00
|
|
|
Ok(vec![])
|
|
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-05-21 11:07:04 +02:00
|
|
|
async fn list_folders_with_perms(
|
2026-02-15 23:45:11 +01:00
|
|
|
&self,
|
|
|
|
|
_parent_id: Option<&str>,
|
2026-03-07 14:59:32 +01:00
|
|
|
_owner_id: Uuid,
|
2026-02-15 23:45:11 +01:00
|
|
|
) -> Result<Vec<FolderDto>, DomainError> {
|
|
|
|
|
Ok(vec![])
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-20 09:22:31 +01:00
|
|
|
async fn list_folders_paginated(
|
2026-02-14 01:29:34 +01:00
|
|
|
&self,
|
2025-03-20 09:22:31 +01:00
|
|
|
_parent_id: Option<&str>,
|
2026-02-14 01:29:34 +01:00
|
|
|
_pagination: &crate::application::dtos::pagination::PaginationRequestDto,
|
|
|
|
|
) -> Result<
|
|
|
|
|
crate::application::dtos::pagination::PaginatedResponseDto<FolderDto>,
|
|
|
|
|
DomainError,
|
|
|
|
|
> {
|
|
|
|
|
Ok(
|
|
|
|
|
crate::application::dtos::pagination::PaginatedResponseDto::new(
|
|
|
|
|
vec![],
|
|
|
|
|
0,
|
|
|
|
|
10,
|
|
|
|
|
0,
|
|
|
|
|
),
|
|
|
|
|
)
|
2025-03-20 09:22:31 +01:00
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-05-21 11:07:04 +02:00
|
|
|
async fn list_folders_paginated_with_perms(
|
2026-02-16 00:22:42 +01:00
|
|
|
&self,
|
|
|
|
|
_parent_id: Option<&str>,
|
2026-03-07 14:59:32 +01:00
|
|
|
_owner_id: Uuid,
|
2026-02-16 00:22:42 +01:00
|
|
|
_pagination: &crate::application::dtos::pagination::PaginationRequestDto,
|
|
|
|
|
) -> Result<
|
|
|
|
|
crate::application::dtos::pagination::PaginatedResponseDto<FolderDto>,
|
|
|
|
|
DomainError,
|
|
|
|
|
> {
|
|
|
|
|
Ok(
|
|
|
|
|
crate::application::dtos::pagination::PaginatedResponseDto::new(
|
|
|
|
|
vec![],
|
|
|
|
|
0,
|
|
|
|
|
10,
|
|
|
|
|
0,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-20 15:39:53 +02:00
|
|
|
async fn rename_folder_with_perms(
|
2026-02-14 01:29:34 +01:00
|
|
|
&self,
|
|
|
|
|
_id: &str,
|
|
|
|
|
_dto: RenameFolderDto,
|
2026-03-07 14:59:32 +01:00
|
|
|
_caller_id: Uuid,
|
2026-02-14 01:29:34 +01:00
|
|
|
) -> Result<FolderDto, DomainError> {
|
2025-03-20 09:22:31 +01:00
|
|
|
Ok(FolderDto::empty())
|
|
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-05-20 15:39:53 +02:00
|
|
|
async fn move_folder_with_perms(
|
2026-02-14 01:29:34 +01:00
|
|
|
&self,
|
|
|
|
|
_id: &str,
|
|
|
|
|
_dto: MoveFolderDto,
|
2026-03-07 14:59:32 +01:00
|
|
|
_caller_id: Uuid,
|
2026-02-14 01:29:34 +01:00
|
|
|
) -> Result<FolderDto, DomainError> {
|
2025-03-20 09:22:31 +01:00
|
|
|
Ok(FolderDto::empty())
|
|
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-05-20 15:39:53 +02:00
|
|
|
async fn delete_folder_with_perms(
|
|
|
|
|
&self,
|
|
|
|
|
_id: &str,
|
|
|
|
|
_caller_id: Uuid,
|
|
|
|
|
) -> Result<(), DomainError> {
|
2025-03-20 09:22:31 +01:00
|
|
|
Ok(())
|
|
|
|
|
}
|
2026-02-16 16:18:39 +01:00
|
|
|
|
2026-02-21 13:33:18 +01:00
|
|
|
async fn create_home_folder(
|
|
|
|
|
&self,
|
2026-03-07 14:59:32 +01:00
|
|
|
_user_id: Uuid,
|
2026-06-18 13:29:41 +02:00
|
|
|
_drive_id: Uuid,
|
2026-02-21 13:33:18 +01:00
|
|
|
_name: String,
|
|
|
|
|
) -> Result<FolderDto, DomainError> {
|
2026-02-16 16:18:39 +01:00
|
|
|
Ok(FolderDto::empty())
|
|
|
|
|
}
|
2025-03-20 09:22:31 +01:00
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2025-03-20 09:22:31 +01:00
|
|
|
FolderServiceStub
|
|
|
|
|
}
|
2025-03-19 00:44:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FolderUseCase for FolderService {
|
2026-05-21 11:07:04 +02:00
|
|
|
/// Verifies the caller has the given permition on a resource
|
|
|
|
|
/// `folder_id`. `None` is the caller's root namespace and always allowed.
|
|
|
|
|
///
|
|
|
|
|
/// Returns `Ok(())` when permitted, `DomainError::not_found(...)` when not
|
|
|
|
|
/// (anti-enumeration — same error as "folder doesn't exist").
|
|
|
|
|
///
|
|
|
|
|
/// Used by handlers that need a fail-fast pre-check BEFORE spooling
|
|
|
|
|
/// large request bodies (file upload, chunked upload). The authoritative
|
|
|
|
|
/// check happens again inside the upload/management services before any
|
|
|
|
|
/// DB write — this is a UX/resource optimization, not a security boundary.
|
2026-05-21 21:50:42 +02:00
|
|
|
async fn require_permission(
|
2026-05-21 11:07:04 +02:00
|
|
|
&self,
|
|
|
|
|
caller_id: Uuid,
|
|
|
|
|
permission: Permission,
|
|
|
|
|
folder_id: &str,
|
|
|
|
|
) -> Result<(), DomainError> {
|
|
|
|
|
let resource = Self::folder_resource(folder_id)?;
|
|
|
|
|
self.authz
|
|
|
|
|
.require(Subject::User(caller_id), permission, resource)
|
|
|
|
|
.await
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Creates a new folder
|
2026-05-20 15:39:53 +02:00
|
|
|
async fn create_folder_with_perms(
|
2026-05-20 12:48:06 +02:00
|
|
|
&self,
|
|
|
|
|
dto: CreateFolderDto,
|
|
|
|
|
caller_id: Uuid,
|
|
|
|
|
) -> Result<FolderDto, DomainError> {
|
2026-05-08 00:11:02 +02:00
|
|
|
if let Err(reason) = validate_storage_name(&dto.name) {
|
|
|
|
|
return Err(DomainError::validation_error(format!(
|
|
|
|
|
"Invalid folder name '{}': {reason}",
|
|
|
|
|
dto.name
|
|
|
|
|
)));
|
2025-03-19 00:44:27 +01:00
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-05-20 12:48:06 +02:00
|
|
|
let Some(parent_id) = dto.parent_id.as_deref() else {
|
|
|
|
|
return Err(DomainError::validation_error(
|
|
|
|
|
"Root folder creation is reserved for registration",
|
|
|
|
|
));
|
|
|
|
|
};
|
2026-05-20 22:56:00 +02:00
|
|
|
let parent_resource = Self::folder_resource(parent_id)?;
|
|
|
|
|
self.authz
|
|
|
|
|
.require(
|
|
|
|
|
Subject::User(caller_id),
|
|
|
|
|
Permission::Create,
|
|
|
|
|
parent_resource,
|
|
|
|
|
)
|
2026-05-20 12:48:06 +02:00
|
|
|
.await?;
|
2026-02-14 01:29:34 +01:00
|
|
|
|
|
|
|
|
let folder = self
|
|
|
|
|
.folder_storage
|
|
|
|
|
.create_folder(dto.name, dto.parent_id)
|
2026-05-18 23:01:55 +02:00
|
|
|
.await?;
|
2025-03-17 21:28:08 +01:00
|
|
|
Ok(FolderDto::from(folder))
|
|
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-02-16 16:18:39 +01:00
|
|
|
/// Creates a root-level home folder for a user during registration.
|
2026-06-18 13:29:41 +02:00
|
|
|
/// `drive_id` is the user's personal drive — the wrapper folder lives
|
|
|
|
|
/// inside it during the D0 dual-write window (M2b retires the wrapper
|
|
|
|
|
/// later).
|
2026-02-21 13:33:18 +01:00
|
|
|
async fn create_home_folder(
|
|
|
|
|
&self,
|
2026-03-07 14:59:32 +01:00
|
|
|
user_id: Uuid,
|
2026-06-18 13:29:41 +02:00
|
|
|
drive_id: Uuid,
|
2026-02-21 13:33:18 +01:00
|
|
|
name: String,
|
|
|
|
|
) -> Result<FolderDto, DomainError> {
|
2026-02-16 16:18:39 +01:00
|
|
|
let folder = self
|
|
|
|
|
.folder_storage
|
2026-06-18 13:29:41 +02:00
|
|
|
.create_home_folder(user_id, drive_id, name)
|
2026-02-16 16:18:39 +01:00
|
|
|
.await
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
DomainError::internal_error(
|
|
|
|
|
"FolderStorage",
|
|
|
|
|
format!("Failed to create home folder: {}", e),
|
|
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
Ok(FolderDto::from(folder))
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-25 10:28:34 +01:00
|
|
|
async fn list_subtree_folders(&self, folder_id: &str) -> Result<Vec<FolderDto>, DomainError> {
|
2026-02-24 12:18:38 +01:00
|
|
|
let folders = self.folder_storage.list_subtree_folders(folder_id).await?;
|
|
|
|
|
Ok(folders.into_iter().map(FolderDto::from).collect())
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Gets a folder by its ID
|
2025-03-19 00:44:27 +01:00
|
|
|
async fn get_folder(&self, id: &str) -> Result<FolderDto, DomainError> {
|
2026-02-14 01:29:34 +01:00
|
|
|
let folder = self.folder_storage.get_folder(id).await.map_err(|e| {
|
|
|
|
|
DomainError::internal_error(
|
|
|
|
|
"FolderStorage",
|
|
|
|
|
format!("Failed to get folder with ID: {}: {}", id, e),
|
|
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
|
2025-03-17 21:28:08 +01:00
|
|
|
Ok(FolderDto::from(folder))
|
|
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-05-20 22:56:00 +02:00
|
|
|
/// Gets a folder by its ID, enforcing that `caller_id` has `Read` access
|
|
|
|
|
/// (via ownership or a grant — including cascading from ancestor folders).
|
2026-05-20 15:39:53 +02:00
|
|
|
async fn get_folder_with_perms(
|
|
|
|
|
&self,
|
|
|
|
|
id: &str,
|
|
|
|
|
caller_id: Uuid,
|
|
|
|
|
) -> Result<FolderDto, DomainError> {
|
2026-05-20 22:56:00 +02:00
|
|
|
self.authz
|
|
|
|
|
.require(
|
|
|
|
|
Subject::User(caller_id),
|
|
|
|
|
Permission::Read,
|
|
|
|
|
Self::folder_resource(id)?,
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
self.get_folder(id).await
|
2026-03-05 10:30:39 +01:00
|
|
|
}
|
|
|
|
|
|
2026-06-18 23:02:17 +02:00
|
|
|
/// Gets a folder by its path, scoped to the caller's tree.
|
|
|
|
|
async fn get_folder_by_path(
|
|
|
|
|
&self,
|
|
|
|
|
path: &str,
|
|
|
|
|
user_id: Uuid,
|
|
|
|
|
) -> Result<FolderDto, DomainError> {
|
2025-03-19 00:44:27 +01:00
|
|
|
let storage_path = StoragePath::from_string(path);
|
2026-02-14 01:29:34 +01:00
|
|
|
|
|
|
|
|
let folder = self
|
|
|
|
|
.folder_storage
|
2026-06-18 23:02:17 +02:00
|
|
|
.get_folder_by_path(&storage_path, user_id)
|
2025-03-19 00:44:27 +01:00
|
|
|
.await
|
2026-02-14 01:29:34 +01:00
|
|
|
.map_err(|e| {
|
|
|
|
|
DomainError::internal_error(
|
|
|
|
|
"FolderStorage",
|
|
|
|
|
format!("Failed to get folder at path: {}: {}", path, e),
|
|
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
|
2025-03-17 21:28:08 +01:00
|
|
|
Ok(FolderDto::from(folder))
|
|
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Lists folders within a parent folder
|
2025-03-19 00:44:27 +01:00
|
|
|
async fn list_folders(&self, parent_id: Option<&str>) -> Result<Vec<FolderDto>, DomainError> {
|
2026-02-14 01:29:34 +01:00
|
|
|
let folders = self
|
|
|
|
|
.folder_storage
|
|
|
|
|
.list_folders(parent_id)
|
2025-03-19 00:44:27 +01:00
|
|
|
.await
|
2026-02-14 01:29:34 +01:00
|
|
|
.map_err(|e| {
|
2026-05-21 11:07:04 +02:00
|
|
|
tracing::warn!("errror while fetching folders {}", e);
|
2026-02-14 01:29:34 +01:00
|
|
|
DomainError::internal_error(
|
|
|
|
|
"FolderStorage",
|
|
|
|
|
format!("Failed to list folders in parent: {:?}: {}", parent_id, e),
|
|
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Convert to DTOs
|
2025-03-17 21:28:08 +01:00
|
|
|
Ok(folders.into_iter().map(FolderDto::from).collect())
|
|
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-02-15 23:45:11 +01:00
|
|
|
/// Lists folders scoped to a specific owner.
|
2026-06-01 16:05:02 +02:00
|
|
|
///
|
|
|
|
|
/// **Note (post PR 3):** the self-heal block that auto-created a
|
|
|
|
|
/// home folder when listing returned empty has been removed.
|
|
|
|
|
/// `HomeFolderLifecycleHook` (registered on `UserLifecycleService`)
|
|
|
|
|
/// now provisions the folder on `on_user_created` / `on_user_login`,
|
|
|
|
|
/// idempotently, so the listing path no longer needs to self-heal.
|
2026-05-21 11:07:04 +02:00
|
|
|
async fn list_folders_with_perms(
|
2026-02-15 23:45:11 +01:00
|
|
|
&self,
|
|
|
|
|
parent_id: Option<&str>,
|
2026-05-21 11:07:04 +02:00
|
|
|
caller_id: Uuid,
|
2026-02-15 23:45:11 +01:00
|
|
|
) -> Result<Vec<FolderDto>, DomainError> {
|
2026-05-21 11:07:04 +02:00
|
|
|
if let Some(parent_id_unwrapped) = parent_id {
|
|
|
|
|
// check authorisation
|
|
|
|
|
self.authz
|
|
|
|
|
.require(
|
|
|
|
|
Subject::User(caller_id),
|
|
|
|
|
Permission::Read,
|
|
|
|
|
Self::folder_resource(parent_id_unwrapped)?,
|
2026-02-15 23:45:11 +01:00
|
|
|
)
|
2026-05-21 11:07:04 +02:00
|
|
|
.await?;
|
|
|
|
|
return self.list_folders(parent_id).await;
|
2026-02-21 16:51:50 -08:00
|
|
|
}
|
2026-06-01 16:05:02 +02:00
|
|
|
// No parent → list the user's root folders.
|
|
|
|
|
let folders = self
|
|
|
|
|
.folder_storage
|
|
|
|
|
.list_folders_by_owner(parent_id, caller_id)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
DomainError::internal_error(
|
|
|
|
|
"FolderStorage",
|
|
|
|
|
format!(
|
|
|
|
|
"Failed to list folders for owner '{}' in parent {:?}: {}",
|
|
|
|
|
caller_id, parent_id, e
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
Ok(folders.into_iter().map(FolderDto::from).collect())
|
2026-02-15 23:45:11 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Lists folders with pagination
|
2025-03-19 00:44:27 +01:00
|
|
|
async fn list_folders_paginated(
|
2026-02-14 01:29:34 +01:00
|
|
|
&self,
|
2025-03-19 00:44:27 +01:00
|
|
|
parent_id: Option<&str>,
|
2026-02-14 01:29:34 +01:00
|
|
|
pagination: &crate::application::dtos::pagination::PaginationRequestDto,
|
|
|
|
|
) -> Result<crate::application::dtos::pagination::PaginatedResponseDto<FolderDto>, DomainError>
|
|
|
|
|
{
|
2025-03-19 00:44:27 +01:00
|
|
|
let pagination = pagination.validate_and_adjust();
|
2026-02-14 01:29:34 +01:00
|
|
|
|
|
|
|
|
let (folders, total_items) = self
|
|
|
|
|
.folder_storage
|
2026-02-21 13:33:18 +01:00
|
|
|
.list_folders_paginated(parent_id, pagination.offset(), pagination.limit(), true)
|
2026-02-14 01:29:34 +01:00
|
|
|
.await
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
DomainError::internal_error(
|
|
|
|
|
"FolderStorage",
|
|
|
|
|
format!(
|
|
|
|
|
"Failed to list folders with pagination in parent: {:?}: {}",
|
|
|
|
|
parent_id, e
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
|
2025-03-19 00:44:27 +01:00
|
|
|
let total = total_items.unwrap_or(folders.len());
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2025-03-19 00:44:27 +01:00
|
|
|
let response = crate::application::dtos::pagination::PaginatedResponseDto::new(
|
|
|
|
|
folders.into_iter().map(FolderDto::from).collect(),
|
|
|
|
|
pagination.page,
|
|
|
|
|
pagination.page_size,
|
2026-02-14 01:29:34 +01:00
|
|
|
total,
|
2025-03-19 00:44:27 +01:00
|
|
|
);
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2025-03-19 00:44:27 +01:00
|
|
|
Ok(response)
|
|
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-02-16 00:22:42 +01:00
|
|
|
/// Lists folders with pagination, scoped to a specific owner.
|
2026-05-21 11:07:04 +02:00
|
|
|
async fn list_folders_paginated_with_perms(
|
2026-02-16 00:22:42 +01:00
|
|
|
&self,
|
|
|
|
|
parent_id: Option<&str>,
|
2026-03-07 14:59:32 +01:00
|
|
|
owner_id: Uuid,
|
2026-02-16 00:22:42 +01:00
|
|
|
pagination: &crate::application::dtos::pagination::PaginationRequestDto,
|
|
|
|
|
) -> Result<crate::application::dtos::pagination::PaginatedResponseDto<FolderDto>, DomainError>
|
|
|
|
|
{
|
|
|
|
|
let pagination = pagination.validate_and_adjust();
|
|
|
|
|
|
2026-05-21 11:07:04 +02:00
|
|
|
if let Some(parent_id_unwrapped) = parent_id {
|
|
|
|
|
self.authz
|
|
|
|
|
.require(
|
|
|
|
|
Subject::User(owner_id),
|
|
|
|
|
Permission::Read,
|
|
|
|
|
Self::folder_resource(parent_id_unwrapped)?,
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
return self.list_folders_paginated(parent_id, &pagination).await;
|
|
|
|
|
} else {
|
|
|
|
|
let (folders, total_items) = self
|
2026-02-16 00:22:42 +01:00
|
|
|
.folder_storage
|
|
|
|
|
.list_folders_by_owner_paginated(
|
|
|
|
|
parent_id,
|
2026-03-07 18:05:52 +01:00
|
|
|
owner_id,
|
2026-02-16 00:22:42 +01:00
|
|
|
pagination.offset(),
|
|
|
|
|
pagination.limit(),
|
|
|
|
|
true,
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
DomainError::internal_error(
|
|
|
|
|
"FolderStorage",
|
|
|
|
|
format!(
|
|
|
|
|
"Failed to list folders for owner '{}' with pagination in parent {:?}: {}",
|
|
|
|
|
owner_id, parent_id, e
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
|
2026-05-21 11:07:04 +02:00
|
|
|
let total = total_items.unwrap_or(folders.len());
|
2026-02-16 00:22:42 +01:00
|
|
|
|
2026-05-21 11:07:04 +02:00
|
|
|
let response = crate::application::dtos::pagination::PaginatedResponseDto::new(
|
|
|
|
|
folders.into_iter().map(FolderDto::from).collect(),
|
|
|
|
|
pagination.page,
|
|
|
|
|
pagination.page_size,
|
|
|
|
|
total,
|
|
|
|
|
);
|
2026-02-16 00:22:42 +01:00
|
|
|
|
2026-05-21 11:07:04 +02:00
|
|
|
Ok(response)
|
|
|
|
|
}
|
2026-02-16 00:22:42 +01:00
|
|
|
}
|
|
|
|
|
|
2026-05-20 22:56:00 +02:00
|
|
|
/// Renames a folder after verifying the caller has `Update` permission.
|
2026-05-20 15:39:53 +02:00
|
|
|
async fn rename_folder_with_perms(
|
2026-02-14 01:29:34 +01:00
|
|
|
&self,
|
|
|
|
|
id: &str,
|
|
|
|
|
dto: RenameFolderDto,
|
2026-03-07 14:59:32 +01:00
|
|
|
caller_id: Uuid,
|
2026-02-14 01:29:34 +01:00
|
|
|
) -> Result<FolderDto, DomainError> {
|
2026-05-08 00:11:02 +02:00
|
|
|
if let Err(reason) = validate_storage_name(&dto.name) {
|
|
|
|
|
return Err(DomainError::validation_error(format!(
|
|
|
|
|
"Invalid folder name '{}': {reason}",
|
|
|
|
|
dto.name
|
|
|
|
|
)));
|
2025-03-19 00:44:27 +01:00
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-05-20 22:56:00 +02:00
|
|
|
self.authz
|
|
|
|
|
.require(
|
|
|
|
|
Subject::User(caller_id),
|
|
|
|
|
Permission::Update,
|
|
|
|
|
Self::folder_resource(id)?,
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
2026-02-16 00:22:42 +01:00
|
|
|
|
2026-03-03 01:49:18 +01:00
|
|
|
let folder = self
|
|
|
|
|
.folder_storage
|
|
|
|
|
.rename_folder(id, dto.name)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
DomainError::internal_error(
|
|
|
|
|
"FolderStorage",
|
|
|
|
|
format!("Failed to rename folder with ID: {}: {}", id, e),
|
|
|
|
|
)
|
|
|
|
|
})?;
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2025-03-17 21:28:08 +01:00
|
|
|
Ok(FolderDto::from(folder))
|
|
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-05-20 22:56:00 +02:00
|
|
|
/// Moves a folder to a new parent. Requires `Update` on the source and
|
|
|
|
|
/// `Create` on the destination parent (if any).
|
2026-05-20 15:39:53 +02:00
|
|
|
async fn move_folder_with_perms(
|
2026-02-21 13:33:18 +01:00
|
|
|
&self,
|
|
|
|
|
id: &str,
|
|
|
|
|
dto: MoveFolderDto,
|
2026-03-07 14:59:32 +01:00
|
|
|
caller_id: Uuid,
|
2026-02-21 13:33:18 +01:00
|
|
|
) -> Result<FolderDto, DomainError> {
|
2026-05-20 22:56:00 +02:00
|
|
|
let source_resource = Self::folder_resource(id)?;
|
|
|
|
|
self.authz
|
|
|
|
|
.require(
|
|
|
|
|
Subject::User(caller_id),
|
|
|
|
|
Permission::Update,
|
|
|
|
|
source_resource,
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
2026-02-16 00:22:42 +01:00
|
|
|
|
2025-03-19 00:44:27 +01:00
|
|
|
if let Some(parent_id) = &dto.parent_id {
|
2026-05-20 22:56:00 +02:00
|
|
|
// Cannot move a folder into itself (cycle guard).
|
2025-03-19 00:44:27 +01:00
|
|
|
if parent_id == id {
|
|
|
|
|
return Err(DomainError::new(
|
|
|
|
|
ErrorKind::InvalidInput,
|
|
|
|
|
"Folder",
|
2026-02-14 01:29:34 +01:00
|
|
|
"Cannot move a folder into itself",
|
2025-03-19 00:44:27 +01:00
|
|
|
));
|
|
|
|
|
}
|
2026-05-20 22:56:00 +02:00
|
|
|
let parent_resource = Self::folder_resource(parent_id)?;
|
|
|
|
|
self.authz
|
|
|
|
|
.require(
|
|
|
|
|
Subject::User(caller_id),
|
|
|
|
|
Permission::Create,
|
|
|
|
|
parent_resource,
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
// TODO: full descendant-cycle check (moving a folder into one of its own descendants)
|
2025-03-19 00:44:27 +01:00
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-03-02 01:30:34 +01:00
|
|
|
let parent_ref = dto.parent_id.as_deref();
|
2026-03-03 01:49:18 +01:00
|
|
|
let folder = self
|
|
|
|
|
.folder_storage
|
|
|
|
|
.move_folder(id, parent_ref)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
DomainError::internal_error(
|
|
|
|
|
"FolderStorage",
|
|
|
|
|
format!("Failed to move folder with ID: {}: {}", id, e),
|
|
|
|
|
)
|
|
|
|
|
})?;
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2025-03-17 21:28:08 +01:00
|
|
|
Ok(FolderDto::from(folder))
|
|
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-05-20 22:56:00 +02:00
|
|
|
/// Deletes a folder after verifying the caller has `Delete` permission.
|
|
|
|
|
/// The DB trigger `trg_cleanup_grants_folder` cleans up `access_grants`
|
|
|
|
|
/// rows targeting the deleted folder automatically.
|
2026-05-20 15:39:53 +02:00
|
|
|
async fn delete_folder_with_perms(&self, id: &str, caller_id: Uuid) -> Result<(), DomainError> {
|
2026-05-20 22:56:00 +02:00
|
|
|
self.authz
|
|
|
|
|
.require(
|
|
|
|
|
Subject::User(caller_id),
|
|
|
|
|
Permission::Delete,
|
|
|
|
|
Self::folder_resource(id)?,
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
2026-02-14 01:29:34 +01:00
|
|
|
|
|
|
|
|
self.folder_storage.delete_folder(id).await.map_err(|e| {
|
|
|
|
|
DomainError::internal_error(
|
|
|
|
|
"FolderStorage",
|
|
|
|
|
format!("Failed to delete folder with ID: {}: {}", id, e),
|
|
|
|
|
)
|
|
|
|
|
})
|
2025-03-17 21:28:08 +01:00
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
}
|
2026-05-28 00:44:20 +02:00
|
|
|
|
|
|
|
|
// ── FolderService — cursor-paginated resource listing ────────────────────────
|
|
|
|
|
|
|
|
|
|
impl FolderService {
|
|
|
|
|
/// Cursor-paginated listing of sub-folders **and** files inside `parent_id`.
|
|
|
|
|
///
|
|
|
|
|
/// Enforces `Permission::Read` on the parent folder before querying.
|
|
|
|
|
/// `order_by` controls both the SQL `ORDER BY` and the cursor encoding.
|
|
|
|
|
/// `kinds` filters the result to only the specified resource types.
|
|
|
|
|
pub async fn list_resources_paged_with_perms(
|
|
|
|
|
&self,
|
|
|
|
|
parent_id: &str,
|
|
|
|
|
caller_id: Uuid,
|
|
|
|
|
opts: ListResourcesOptions<'_>,
|
|
|
|
|
) -> Result<(Vec<FolderResourceRow>, Option<String>), DomainError> {
|
|
|
|
|
// 1. AuthZ — same check as list_folders_with_perms
|
|
|
|
|
self.authz
|
|
|
|
|
.require(
|
|
|
|
|
Subject::User(caller_id),
|
|
|
|
|
Permission::Read,
|
|
|
|
|
Self::folder_resource(parent_id)?,
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
let pid =
|
|
|
|
|
Uuid::parse_str(parent_id).map_err(|_| DomainError::not_found("Folder", parent_id))?;
|
|
|
|
|
|
|
|
|
|
let ListResourcesOptions {
|
|
|
|
|
limit,
|
|
|
|
|
cursor,
|
|
|
|
|
order_by,
|
|
|
|
|
kinds,
|
|
|
|
|
reverse,
|
|
|
|
|
} = opts;
|
|
|
|
|
|
|
|
|
|
// 2. Fetch limit+1 rows so we can detect has_next
|
|
|
|
|
let mut rows = self
|
|
|
|
|
.folder_storage
|
|
|
|
|
.list_resources_paged(pid, limit + 1, cursor.as_ref(), order_by, kinds, reverse)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
// 3. Detect has_next, build encoded next cursor
|
|
|
|
|
let next_cursor = if rows.len() > limit {
|
|
|
|
|
let last = &rows[limit - 1];
|
|
|
|
|
let c = build_folder_resource_cursor(last, order_by, reverse);
|
|
|
|
|
rows.truncate(limit);
|
|
|
|
|
Some(c.encode())
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok((rows, next_cursor))
|
|
|
|
|
}
|
2026-06-01 16:05:02 +02:00
|
|
|
|
|
|
|
|
/// Idempotently provision a home folder for a user.
|
|
|
|
|
///
|
|
|
|
|
/// Returns `Ok(true)` if a folder was newly created, `Ok(false)` if the
|
|
|
|
|
/// user already had at least one root folder.
|
|
|
|
|
///
|
|
|
|
|
/// **System-level operation** — bypasses authz because this runs on
|
|
|
|
|
/// the user's own behalf (during creation or login provisioning) at a
|
|
|
|
|
/// point where the caller may be the engine itself, not an HTTP user.
|
|
|
|
|
/// Callers must be inside trusted code paths (lifecycle hooks).
|
|
|
|
|
///
|
|
|
|
|
/// Used by [`HomeFolderLifecycleHook`] on `on_user_created` and
|
|
|
|
|
/// `on_user_login`. Replaces the old self-heal at the listing path
|
|
|
|
|
/// and the four eager `create_personal_folder` calls in
|
|
|
|
|
/// `AuthApplicationService` (removed in the same PR).
|
|
|
|
|
pub async fn ensure_home_folder(
|
|
|
|
|
&self,
|
|
|
|
|
user_id: Uuid,
|
2026-06-18 13:29:41 +02:00
|
|
|
drive_id: Uuid,
|
2026-06-02 21:21:24 +02:00
|
|
|
username: Option<&str>,
|
2026-06-01 16:05:02 +02:00
|
|
|
) -> Result<bool, DomainError> {
|
|
|
|
|
let existing = self
|
|
|
|
|
.folder_storage
|
|
|
|
|
.list_folders_by_owner(None, user_id)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
DomainError::internal_error(
|
|
|
|
|
"FolderStorage",
|
|
|
|
|
format!("ensure_home_folder: list root folders: {}", e),
|
|
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
if !existing.is_empty() {
|
|
|
|
|
return Ok(false);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-02 21:21:24 +02:00
|
|
|
let folder_name = match username {
|
|
|
|
|
Some(u) => format!("My Folder - {}", u),
|
|
|
|
|
None => format!("My Folder - {}", user_id),
|
|
|
|
|
};
|
2026-06-01 16:05:02 +02:00
|
|
|
self.folder_storage
|
2026-06-18 13:29:41 +02:00
|
|
|
.create_home_folder(user_id, drive_id, folder_name.clone())
|
2026-06-01 16:05:02 +02:00
|
|
|
.await
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
DomainError::internal_error(
|
|
|
|
|
"FolderStorage",
|
|
|
|
|
format!("ensure_home_folder: create: {}", e),
|
|
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
tracing::info!(
|
|
|
|
|
target: "user_lifecycle",
|
|
|
|
|
hook = "home_folder",
|
|
|
|
|
user_id = %user_id,
|
|
|
|
|
folder_name = %folder_name,
|
|
|
|
|
"Home folder provisioned"
|
|
|
|
|
);
|
|
|
|
|
Ok(true)
|
|
|
|
|
}
|
2026-05-28 00:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Build the next-page cursor from the last row of the current page.
|
|
|
|
|
/// `reverse` is stored in the cursor so subsequent pages use the same order.
|
|
|
|
|
fn build_folder_resource_cursor(
|
|
|
|
|
row: &FolderResourceRow,
|
|
|
|
|
order_by: &str,
|
|
|
|
|
reverse: bool,
|
|
|
|
|
) -> FolderResourceCursor {
|
|
|
|
|
match order_by {
|
|
|
|
|
"type" => FolderResourceCursor {
|
|
|
|
|
order_by: "type".to_owned(),
|
|
|
|
|
resource_id: row.id,
|
|
|
|
|
sort_str: Some(row.sort_str.clone()),
|
|
|
|
|
sort_int: Some(row.type_order),
|
|
|
|
|
sort_ts: None,
|
|
|
|
|
reverse,
|
|
|
|
|
},
|
|
|
|
|
"modified_at" => FolderResourceCursor {
|
|
|
|
|
order_by: "modified_at".to_owned(),
|
|
|
|
|
resource_id: row.id,
|
|
|
|
|
sort_str: None,
|
|
|
|
|
sort_int: None,
|
|
|
|
|
sort_ts: Some(row.modified_at),
|
|
|
|
|
reverse,
|
|
|
|
|
},
|
|
|
|
|
"created_at" => FolderResourceCursor {
|
|
|
|
|
order_by: "created_at".to_owned(),
|
|
|
|
|
resource_id: row.id,
|
|
|
|
|
sort_str: None,
|
|
|
|
|
sort_int: None,
|
|
|
|
|
sort_ts: Some(row.created_at),
|
|
|
|
|
reverse,
|
|
|
|
|
},
|
|
|
|
|
"size" => FolderResourceCursor {
|
|
|
|
|
order_by: "size".to_owned(),
|
|
|
|
|
resource_id: row.id,
|
|
|
|
|
sort_str: None,
|
|
|
|
|
sort_int: Some(row.size),
|
|
|
|
|
sort_ts: None,
|
|
|
|
|
reverse,
|
|
|
|
|
},
|
|
|
|
|
_ => FolderResourceCursor {
|
|
|
|
|
// "name" (default): sort_int = folder_first (0 or 1)
|
|
|
|
|
order_by: "name".to_owned(),
|
|
|
|
|
resource_id: row.id,
|
|
|
|
|
sort_str: Some(row.sort_str.clone()),
|
|
|
|
|
sort_int: Some(i64::from(row.folder_first)),
|
|
|
|
|
sort_ts: None,
|
|
|
|
|
reverse,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-01 16:05:02 +02:00
|
|
|
|
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
|
// HomeFolderLifecycleHook
|
|
|
|
|
//
|
|
|
|
|
// Owns home-folder provisioning policy. Replaces:
|
|
|
|
|
// - the 4 eager `create_personal_folder` calls in AuthApplicationService
|
|
|
|
|
// (register / setup_create_admin / admin_create_user / OIDC JIT)
|
|
|
|
|
// - the self-heal at `list_folders_with_perms` when no root folders exist
|
|
|
|
|
//
|
|
|
|
|
// Lives in this file (not under a centralised `lifecycle/` directory)
|
|
|
|
|
// because the folder service owns home-folder policy — see the
|
|
|
|
|
// "owner-located convention" note in
|
|
|
|
|
// `docs/architecture/user-lifecycle.md`.
|
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
use async_trait::async_trait;
|
|
|
|
|
|
|
|
|
|
use crate::application::ports::user_lifecycle::{DeletionMode, LogoutReason, UserLifecycleHook};
|
|
|
|
|
use crate::domain::entities::user::User;
|
|
|
|
|
|
2026-06-18 13:29:41 +02:00
|
|
|
/// Lifecycle hook: provisions a user's default Personal drive at first
|
|
|
|
|
/// login (replaces the legacy `My Folder - <username>` wrapper as of D0).
|
|
|
|
|
///
|
|
|
|
|
/// Two writes happen on first provisioning:
|
|
|
|
|
/// 1. A row in `storage.drives` with `kind='personal'`,
|
|
|
|
|
/// `default_for_user=<uid>`, and the user's quota carried over from
|
|
|
|
|
/// `auth.users.storage_quota_bytes`.
|
|
|
|
|
/// 2. An Owner role grant in `storage.role_grants` so the user can
|
|
|
|
|
/// read/write/manage their own drive (the engine's owner short-
|
|
|
|
|
/// circuit applies to folders/files but not drives — see
|
|
|
|
|
/// `pg_acl_engine::check_inner` D0-6 rewrite).
|
|
|
|
|
///
|
|
|
|
|
/// Both writes are idempotent: `find_default_for_user` short-circuits
|
|
|
|
|
/// when the drive already exists; `set_role` is an UPSERT that no-ops
|
|
|
|
|
/// when the Owner row is already present.
|
|
|
|
|
pub struct PersonalDriveLifecycleHook {
|
|
|
|
|
drive_repo: Arc<dyn crate::domain::repositories::drive_repository::DriveRepository>,
|
|
|
|
|
// The `AuthorizationEngine` trait isn't `dyn`-compatible (native
|
|
|
|
|
// async-fn-in-trait methods are not object-safe), so we hold the
|
|
|
|
|
// concrete engine. This matches the convention already used by
|
2026-06-18 23:02:17 +02:00
|
|
|
// `AppState.authorization`. Only the idempotent-rerun path uses it
|
|
|
|
|
// now; the create path goes through the repo's atomic CTE which
|
|
|
|
|
// writes the role_grant inline.
|
2026-06-18 13:29:41 +02:00
|
|
|
authorization: Arc<crate::infrastructure::services::pg_acl_engine::PgAclEngine>,
|
2026-06-01 16:05:02 +02:00
|
|
|
}
|
|
|
|
|
|
2026-06-18 13:29:41 +02:00
|
|
|
impl PersonalDriveLifecycleHook {
|
|
|
|
|
pub fn new(
|
|
|
|
|
drive_repo: Arc<dyn crate::domain::repositories::drive_repository::DriveRepository>,
|
|
|
|
|
authorization: Arc<crate::infrastructure::services::pg_acl_engine::PgAclEngine>,
|
|
|
|
|
) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
drive_repo,
|
|
|
|
|
authorization,
|
|
|
|
|
}
|
2026-06-01 16:05:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Idempotent provisioning shared by `on_user_created` and
|
|
|
|
|
/// `on_user_login`. External users are skipped per tip #2 in the
|
2026-06-18 13:29:41 +02:00
|
|
|
/// trait docstring — they have no resources of their own, only
|
|
|
|
|
/// grants on other users' resources.
|
2026-06-01 16:05:02 +02:00
|
|
|
async fn provision_if_needed(&self, user: &User) -> Result<(), DomainError> {
|
2026-06-18 23:02:17 +02:00
|
|
|
use crate::domain::repositories::drive_repository::DriveRepositoryError;
|
2026-06-18 13:29:41 +02:00
|
|
|
use crate::domain::services::authorization::{Resource, Role, Subject};
|
|
|
|
|
|
2026-06-01 16:05:02 +02:00
|
|
|
if user.is_external() {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
2026-06-18 13:29:41 +02:00
|
|
|
|
|
|
|
|
// Idempotent shortcut: if the user already has a default drive,
|
2026-06-18 23:02:17 +02:00
|
|
|
// the atomic CTE already ran on a prior turn. The CTE writes
|
|
|
|
|
// the Owner role_grant inline, so there's nothing to repair —
|
|
|
|
|
// but we still re-emit the grant via `set_role` (UPSERT-safe)
|
|
|
|
|
// to cover the historical case where a pre-CTE provisioning
|
|
|
|
|
// path partially completed (drive created, grant missing).
|
2026-06-18 13:29:41 +02:00
|
|
|
match self.drive_repo.find_default_for_user(user.id()).await {
|
2026-06-18 23:02:17 +02:00
|
|
|
Ok(drive_with_name) => {
|
2026-06-18 13:29:41 +02:00
|
|
|
self.authorization
|
|
|
|
|
.set_role(
|
|
|
|
|
user.id(),
|
|
|
|
|
Subject::User(user.id()),
|
|
|
|
|
Role::Owner,
|
2026-06-18 23:02:17 +02:00
|
|
|
Resource::Drive(drive_with_name.drive.id),
|
2026-06-18 13:29:41 +02:00
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.map(|_grant| ())?;
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
Err(DriveRepositoryError::NotFound(_)) => { /* fall through to create */ }
|
|
|
|
|
Err(e) => {
|
|
|
|
|
return Err(DomainError::internal_error(
|
|
|
|
|
"PersonalDriveHook",
|
|
|
|
|
format!("find_default lookup: {e}"),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-18 23:02:17 +02:00
|
|
|
// One atomic CTE — drive row + root folder ("Personal",
|
|
|
|
|
// parent_id=NULL, drive_id pinned) + drives.root_folder_id
|
|
|
|
|
// wire-up + Owner role_grant. Single SQL statement, atomic
|
|
|
|
|
// against server crash mid-sequence (docs/plan/drive.md §3).
|
|
|
|
|
let drive_with_name = self
|
2026-06-18 13:29:41 +02:00
|
|
|
.drive_repo
|
2026-06-18 23:02:17 +02:00
|
|
|
.create_personal_drive_atomic(user.id(), Some(user.storage_quota_bytes()))
|
2026-06-18 13:29:41 +02:00
|
|
|
.await
|
|
|
|
|
.map_err(|e| {
|
2026-06-18 23:02:17 +02:00
|
|
|
DomainError::internal_error(
|
|
|
|
|
"PersonalDriveHook",
|
|
|
|
|
format!("create_personal_drive_atomic: {e}"),
|
|
|
|
|
)
|
2026-06-18 13:29:41 +02:00
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
tracing::info!(
|
|
|
|
|
target: "user_lifecycle",
|
|
|
|
|
hook = "personal_drive",
|
|
|
|
|
user_id = %user.id(),
|
2026-06-18 23:02:17 +02:00
|
|
|
drive_id = %drive_with_name.drive.id,
|
|
|
|
|
root_folder_id = %drive_with_name.drive.root_folder_id,
|
|
|
|
|
"Default personal drive + root folder + owner grant provisioned (atomic CTE)"
|
2026-06-18 13:29:41 +02:00
|
|
|
);
|
|
|
|
|
Ok(())
|
2026-06-01 16:05:02 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
2026-06-18 13:29:41 +02:00
|
|
|
impl UserLifecycleHook for PersonalDriveLifecycleHook {
|
2026-06-01 16:05:02 +02:00
|
|
|
fn name(&self) -> &'static str {
|
2026-06-18 13:29:41 +02:00
|
|
|
"personal_drive"
|
2026-06-01 16:05:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn on_user_created(&self, user: &User) -> Result<(), DomainError> {
|
|
|
|
|
self.provision_if_needed(user).await
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Login is the safety net — if `on_user_created` failed at any
|
|
|
|
|
/// earlier point (or the user was created in a flow that pre-dated
|
|
|
|
|
/// this hook), provisioning happens here on next login.
|
|
|
|
|
async fn on_user_login(&self, user: &User) -> Result<(), DomainError> {
|
|
|
|
|
self.provision_if_needed(user).await
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn on_user_logout(&self, _user: &User, _reason: LogoutReason) -> Result<(), DomainError> {
|
2026-06-18 13:29:41 +02:00
|
|
|
// Drives don't react to logout. Explicit no-op per the
|
2026-06-01 16:05:02 +02:00
|
|
|
// "no defaults" convention.
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-01 16:14:18 +02:00
|
|
|
async fn on_user_deleted(
|
|
|
|
|
&self,
|
|
|
|
|
user: &User,
|
|
|
|
|
mode: DeletionMode,
|
|
|
|
|
_tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
|
|
|
|
) -> Result<(), DomainError> {
|
2026-06-18 13:29:41 +02:00
|
|
|
// `storage.drives.default_for_user` has ON DELETE CASCADE
|
|
|
|
|
// referencing `auth.users(id)`, and `storage.folders.drive_id`
|
|
|
|
|
// / `storage.files.drive_id` both have ON DELETE CASCADE on
|
|
|
|
|
// `storage.drives(id)` (M3). So a user delete cascades:
|
|
|
|
|
// user → drive → folders → files in one transaction.
|
|
|
|
|
//
|
2026-06-01 16:14:18 +02:00
|
|
|
// The hook emits a per-mode tracing event so audit can tell
|
|
|
|
|
// AdminDelete (currently recoverable only via DB-level rollback
|
|
|
|
|
// before commit) from GdprPurge (no sweeper exists yet — the
|
|
|
|
|
// variant is reserved for a future PR that adds retention).
|
|
|
|
|
tracing::info!(
|
|
|
|
|
target: "user_lifecycle",
|
2026-06-18 13:29:41 +02:00
|
|
|
hook = "personal_drive",
|
2026-06-01 16:14:18 +02:00
|
|
|
user_id = %user.id(),
|
|
|
|
|
mode = ?mode,
|
2026-06-18 13:29:41 +02:00
|
|
|
"Personal drive (and tree) will be removed via FK CASCADE on user delete"
|
2026-06-01 16:14:18 +02:00
|
|
|
);
|
2026-06-01 16:05:02 +02:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|