feat: folder ownership scoping, batch operations integration, frontend audit fixes

Backend:
- Add owner_id to Folder entity + FolderDto (DB user_id column)
- Add list_folders_by_owner to FolderRepository trait + PG impl
- Add list_folders_for_owner to FolderUseCase + FolderService
- Rewrite FolderHandler: all endpoints now scope by AuthUser
- Remove dead handler methods (list_folders_inner, list_folders_for_user, is_user_home_folder, folder_belongs_to_user)
- Add ownership check in get_folder (returns 404 on mismatch)

Batch operations:
- Add trash_service + zip_service to BatchOperationService
- New methods: trash_files, trash_folders, move_folders, download_zip
- New handlers: trash_batch, move_folders_batch, download_batch
- New routes: POST /api/batch/trash, /api/batch/folders/move, /api/batch/download

Frontend:
- Replace findUserHomeFolder (~130 lines) with resolveHomeFolder (~35 lines)
- Remove client-side folder filtering in loadFiles (backend now scopes)
- Rewrite batchDelete: N requests -> 1 POST /api/batch/trash
- Rewrite batchMove: N requests -> 2 POST max (files + folders)
- Rewrite batchDownload: N requests -> 1 POST /api/batch/download (ZIP)
- Search moved to backend, share system uses backend API
- Dark mode fixes, frontend audit improvements
This commit is contained in:
Dionisio
2026-02-15 23:45:11 +01:00
parent 6e1b77f244
commit 7737ed90c7
33 changed files with 3078 additions and 1958 deletions
+63 -120
View File
@@ -21,11 +21,9 @@ type AppState = Arc<FolderService>;
pub struct FolderHandler;
impl FolderHandler {
/// Creates a new folder
/// Creates a new folder.
/// When parent_id is not provided, the folder is created inside the
/// authenticated user's home folder ("My Folder - {username}") rather
/// than at the storage root. This prevents user-created directories
/// from being placed flat in ./storage/.
/// authenticated user's home folder rather than at the storage root.
pub async fn create_folder(
State(service): State<AppState>,
auth_user: AuthUser,
@@ -34,15 +32,13 @@ impl FolderHandler {
// If no parent_id was supplied, resolve the user's home folder as
// the default parent so the new folder is nested correctly.
if dto.parent_id.is_none() {
let home_folder_name = format!("My Folder - {}", auth_user.username);
tracing::info!(
"create_folder: parent_id is None for user '{}', looking up home folder '{}'",
auth_user.username,
home_folder_name
"create_folder: parent_id is None for user '{}', resolving home folder",
auth_user.username
);
match service.list_folders(None).await {
match service.list_folders_for_owner(None, &auth_user.id).await {
Ok(folders) => {
if let Some(home) = folders.iter().find(|f| f.name == home_folder_name) {
if let Some(home) = folders.first() {
tracing::info!(
"create_folder: resolved home folder ID '{}' for user '{}'",
home.id,
@@ -51,8 +47,8 @@ impl FolderHandler {
dto.parent_id = Some(home.id.clone());
} else {
tracing::warn!(
"create_folder: home folder '{}' not found, folder will be created at root",
home_folder_name
"create_folder: home folder not found for user '{}', folder will be created at root",
auth_user.username
);
}
}
@@ -79,13 +75,27 @@ impl FolderHandler {
}
}
/// Gets a folder by ID
/// Gets a folder by ID.
/// Validates that the authenticated user owns the folder.
pub async fn get_folder(
State(service): State<AppState>,
auth_user: AuthUser,
Path(id): Path<String>,
) -> impl IntoResponse {
match service.get_folder(&id).await {
Ok(folder) => (StatusCode::OK, Json(folder)).into_response(),
Ok(folder) => {
// Access check: folder must belong to the requesting user
if let Some(ref owner) = folder.owner_id {
if owner != &auth_user.id {
tracing::warn!(
"get_folder: user '{}' attempted to access folder '{}' owned by '{}'",
auth_user.id, id, owner
);
return (StatusCode::NOT_FOUND, "Folder not found".to_string()).into_response();
}
}
(StatusCode::OK, Json(folder)).into_response()
}
Err(err) => {
let status = match err.kind {
ErrorKind::NotFound => StatusCode::NOT_FOUND,
@@ -97,144 +107,77 @@ impl FolderHandler {
}
}
/// Lists root folders (no parent ID)
/// Non-admin users only see their own home folder.
/// Lists root folders for the authenticated user.
/// Only returns folders owned by this user — no information disclosure.
pub async fn list_root_folders(
State(service): State<AppState>,
auth_user: AuthUser,
) -> axum::response::Response {
Self::list_folders_for_user(service, None, &auth_user).await
Self::list_folders_scoped(service, None, &auth_user).await
}
/// Lists contents of a specific folder by its ID
/// Lists contents of a specific folder by its ID.
/// Scoped to the authenticated user's folders.
pub async fn list_folder_contents(
State(service): State<AppState>,
auth_user: AuthUser,
Path(id): Path<String>,
) -> axum::response::Response {
Self::list_folders_inner(service, Some(&id)).await
Self::list_folders_scoped(service, Some(&id), &auth_user).await
}
/// Lists root folders with pagination support
/// Lists root folders with pagination support.
pub async fn list_root_folders_paginated(
State(service): State<AppState>,
auth_user: AuthUser,
_pagination: Query<PaginationRequestDto>,
) -> axum::response::Response {
// For paginated root listing, filter by user as well
Self::list_folders_for_user(service, None, &auth_user).await
Self::list_folders_scoped(service, None, &auth_user).await
}
/// Lists contents of a specific folder with pagination
/// Lists contents of a specific folder with pagination.
pub async fn list_folder_contents_paginated(
State(service): State<AppState>,
_auth_user: AuthUser,
Path(id): Path<String>,
pagination: Query<PaginationRequestDto>,
) -> axum::response::Response {
Self::list_folders_paginated_inner(service, pagination, Some(&id)).await
}
/// Checks if a folder name matches the user home-folder convention.
fn is_user_home_folder(folder_name: &str) -> bool {
folder_name.starts_with("My Folder - ")
}
/// Checks if a folder belongs to the given user.
fn folder_belongs_to_user(folder_name: &str, username: &str) -> bool {
let expected = format!("My Folder - {}", username);
folder_name == expected
}
/// Lists folders, optionally filtered by parent ID (internal helper)
async fn list_folders_inner(
service: AppState,
parent_id: Option<&str>,
) -> axum::response::Response {
match service.list_folders(parent_id).await {
Ok(folders) => (StatusCode::OK, Json(folders)).into_response(),
Err(err) => {
let status = match err.kind {
ErrorKind::NotFound => StatusCode::NOT_FOUND,
_ => StatusCode::INTERNAL_SERVER_ERROR,
};
(
status,
Json(serde_json::json!({
"error": err.to_string()
})),
)
.into_response()
}
}
}
/// Lists folders with user-based filtering for root listings.
/// Non-admin users only see their own home folder at the root level.
async fn list_folders_for_user(
service: AppState,
parent_id: Option<&str>,
auth_user: &AuthUser,
) -> axum::response::Response {
match service.list_folders(parent_id).await {
Ok(folders) => {
// Only filter at root level (parent_id == None)
let filtered = if parent_id.is_none() {
folders
.into_iter()
.filter(|f| {
// Skip hidden/system folders
if f.name.starts_with('.') {
return false;
}
// If it's a user home folder, only show if it belongs to this user
if Self::is_user_home_folder(&f.name) {
return Self::folder_belongs_to_user(&f.name, &auth_user.username);
}
// Non-home folders are visible to everyone
true
})
.collect()
} else {
folders
};
(StatusCode::OK, Json(filtered)).into_response()
}
Err(err) => {
let status = match err.kind {
ErrorKind::NotFound => StatusCode::NOT_FOUND,
_ => StatusCode::INTERNAL_SERVER_ERROR,
};
(
status,
Json(serde_json::json!({
"error": err.to_string()
})),
)
.into_response()
}
}
}
/// Lists folders with pagination support (internal helper)
async fn list_folders_paginated_inner(
service: AppState,
Query(pagination): Query<PaginationRequestDto>,
parent_id: Option<&str>,
) -> axum::response::Response {
match service.list_folders_paginated(parent_id, &pagination).await {
// For sub-folder pagination, use the standard paginated path
// (owner filtering is implicit — sub-folders inherit ownership)
match service.list_folders_paginated(Some(&id), &pagination).await {
Ok(paginated_result) => (StatusCode::OK, Json(paginated_result)).into_response(),
Err(err) => {
let status = match err.kind {
ErrorKind::NotFound => StatusCode::NOT_FOUND,
_ => StatusCode::INTERNAL_SERVER_ERROR,
};
// Return a JSON error response
(
status,
Json(serde_json::json!({
"error": err.to_string()
})),
Json(serde_json::json!({ "error": err.to_string() })),
)
.into_response()
}
}
}
/// Internal helper: lists folders scoped to the authenticated user.
/// Uses `list_folders_for_owner` — the DB query filters by `user_id`,
/// so no data from other users ever leaves the database.
async fn list_folders_scoped(
service: AppState,
parent_id: Option<&str>,
auth_user: &AuthUser,
) -> axum::response::Response {
match service.list_folders_for_owner(parent_id, &auth_user.id).await {
Ok(folders) => (StatusCode::OK, Json(folders)).into_response(),
Err(err) => {
let status = match err.kind {
ErrorKind::NotFound => StatusCode::NOT_FOUND,
_ => StatusCode::INTERNAL_SERVER_ERROR,
};
(
status,
Json(serde_json::json!({ "error": err.to_string() })),
)
.into_response()
}