This repository has been archived on 2024-08-23. You can view files and clone it, but cannot push or open issues or pull requests.

58 lines
1.5 KiB
Rust

use crate::{db::permissions::PermissionRaw, prelude::*};
#[derive(Deserialize, Debug)]
pub struct Params {
folder_id: Uuid,
permission_type: PermissionRaw,
user_id: i32,
}
pub async fn set(
claims: Claims,
State(pool): State<Pool>,
Json(params): Json<Params>,
) -> GeneralResult<EmptyResponse> {
let root = db::folder::get_root(claims.user_id, &pool)
.await
.handle_internal("Error getting the root folder")?;
if params.folder_id == root {
return Err(GeneralError::message(
StatusCode::BAD_REQUEST,
"Cannot set permissions for the root folder",
));
}
db::folder::get_permissions(params.folder_id, claims.user_id, &pool)
.await
.can_manage_guard()?;
if params.user_id == claims.user_id {
return Err(GeneralError::message(
StatusCode::BAD_REQUEST,
"Cannot set your own permissions",
));
}
let folder_info = db::folder::get_by_id(params.folder_id, &pool)
.await
.handle_internal("Error getting folder info")?
.item_not_found()?;
if folder_info.owner_id == params.user_id {
return Err(GeneralError::message(
StatusCode::BAD_REQUEST,
"Cannot set permissions of the folder's owner",
));
}
db::permissions::insert(
params.user_id,
params.folder_id,
params.permission_type,
&pool,
)
.await
.handle_internal("Error writing to the database")?;
Ok(EmptyResponse)
}