2026-02-14 01:29:34 +01:00
|
|
|
use crate::application::dtos::folder_dto::{
|
|
|
|
|
CreateFolderDto, FolderDto, MoveFolderDto, RenameFolderDto,
|
|
|
|
|
};
|
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,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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-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())
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-20 09:22:31 +01:00
|
|
|
async fn get_folder_by_path(&self, _path: &str) -> Result<FolderDto, DomainError> {
|
|
|
|
|
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-02-15 23:45:11 +01:00
|
|
|
async fn list_folders_for_owner(
|
|
|
|
|
&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-02-16 00:22:42 +01:00
|
|
|
async fn list_folders_for_owner_paginated(
|
|
|
|
|
&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-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-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-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-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-03-07 18:05:52 +01:00
|
|
|
.create_home_folder(user_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-02-12 09:41:25 +01:00
|
|
|
/// Gets a folder by its path
|
2025-03-19 00:44:27 +01:00
|
|
|
async fn get_folder_by_path(&self, path: &str) -> Result<FolderDto, DomainError> {
|
2026-02-12 09:41:25 +01:00
|
|
|
// Convert the string path to StoragePath
|
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
|
|
|
|
|
.get_folder_by_path(&storage_path)
|
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| {
|
|
|
|
|
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-02-21 16:51:50 -08:00
|
|
|
/// Self-healing: if listing root folders and none exist, creates a home folder.
|
2026-02-15 23:45:11 +01:00
|
|
|
async fn list_folders_for_owner(
|
|
|
|
|
&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> {
|
|
|
|
|
let folders = self
|
|
|
|
|
.folder_storage
|
2026-03-07 18:05:52 +01:00
|
|
|
.list_folders_by_owner(parent_id, owner_id)
|
2026-02-15 23:45:11 +01:00
|
|
|
.await
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
DomainError::internal_error(
|
|
|
|
|
"FolderStorage",
|
|
|
|
|
format!(
|
|
|
|
|
"Failed to list folders for owner '{}' in parent {:?}: {}",
|
|
|
|
|
owner_id, parent_id, e
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
|
2026-02-21 16:51:50 -08:00
|
|
|
// Self-healing: if listing root folders and none exist, create a home folder
|
|
|
|
|
// This ensures the frontend always gets a valid userHomeFolderId
|
|
|
|
|
if parent_id.is_none() && folders.is_empty() {
|
|
|
|
|
tracing::info!(
|
|
|
|
|
"No root folders found for user {}, creating home folder automatically",
|
|
|
|
|
owner_id
|
|
|
|
|
);
|
2026-05-20 15:39:53 +02:00
|
|
|
let owner_id_short = {
|
|
|
|
|
let s = owner_id.to_string();
|
|
|
|
|
s[..8.min(s.len())].to_string()
|
|
|
|
|
};
|
2026-03-07 18:05:52 +01:00
|
|
|
let folder_name = format!("My Folder - {}", owner_id_short);
|
2026-02-21 16:51:50 -08:00
|
|
|
match self
|
|
|
|
|
.folder_storage
|
2026-03-07 18:05:52 +01:00
|
|
|
.create_home_folder(owner_id, folder_name.clone())
|
2026-02-21 16:51:50 -08:00
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
Ok(home_folder) => {
|
|
|
|
|
tracing::info!(
|
|
|
|
|
"Created home folder '{}' for user {}",
|
|
|
|
|
folder_name,
|
|
|
|
|
owner_id
|
|
|
|
|
);
|
|
|
|
|
return Ok(vec![FolderDto::from(home_folder)]);
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
tracing::warn!("Failed to create home folder for user {}: {}", owner_id, e);
|
|
|
|
|
// Return empty list rather than failing - user might not have storage quota, etc.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-15 23:45:11 +01:00
|
|
|
Ok(folders.into_iter().map(FolderDto::from).collect())
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
|
|
|
|
async fn list_folders_for_owner_paginated(
|
|
|
|
|
&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();
|
|
|
|
|
|
|
|
|
|
let (folders, total_items) = self
|
|
|
|
|
.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
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
let total = total_items.unwrap_or(folders.len());
|
|
|
|
|
|
|
|
|
|
let response = crate::application::dtos::pagination::PaginatedResponseDto::new(
|
|
|
|
|
folders.into_iter().map(FolderDto::from).collect(),
|
|
|
|
|
pagination.page,
|
|
|
|
|
pagination.page_size,
|
|
|
|
|
total,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Ok(response)
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|