chore: import MCTF services
- Added all seven issued service trees without omissions. - Preserved the supplied runtime configuration, binaries, and assets.
This commit is contained in:
commit
13f936bee9
1222 files changed
+21280
No files matched your search
+112
@@ -0,0 +1,112 @@
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
dto::{
|
||||
AddUserRequest, MyWorkspaceResponse, NewWorkspace, WorkspaceParticipantResponse,
|
||||
WorkspaceResponse,
|
||||
},
|
||||
error::{ApiError, ApiResult},
|
||||
models::{Role, User},
|
||||
pam,
|
||||
repositories::{AuthPolicyRepository, UserRepository, WorkspaceRepository},
|
||||
};
|
||||
|
||||
const WORKSPACE_NAME_MAX_LEN: usize = 100;
|
||||
|
||||
pub async fn create(pool: &PgPool, owner: User, req: NewWorkspace) -> ApiResult<WorkspaceResponse> {
|
||||
let name = req.name.trim();
|
||||
|
||||
if name.is_empty() {
|
||||
return Err(ApiError::BadRequest(
|
||||
"workspace name must not be empty".to_string(),
|
||||
));
|
||||
}
|
||||
if name.len() > WORKSPACE_NAME_MAX_LEN {
|
||||
return Err(ApiError::BadRequest(format!(
|
||||
"workspace name must be at most {WORKSPACE_NAME_MAX_LEN} characters"
|
||||
)));
|
||||
}
|
||||
|
||||
let workspace = WorkspaceRepository::create(pool, name, req.description.as_deref()).await?;
|
||||
|
||||
UserRepository::attach_to_workspace(pool, owner.id, workspace.id, Role::WorkspaceAdmin).await?;
|
||||
pam::memberships::append(&owner.username, workspace.id)?;
|
||||
|
||||
Ok(WorkspaceResponse {
|
||||
id: workspace.id,
|
||||
name: workspace.title,
|
||||
description: workspace.description,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn get_my(pool: &PgPool, user: User) -> ApiResult<MyWorkspaceResponse> {
|
||||
let workspace_id = user.workspace_id.ok_or(ApiError::NotFound)?;
|
||||
|
||||
let workspace = WorkspaceRepository::get(pool, workspace_id).await?;
|
||||
let participants = WorkspaceRepository::get_participants(pool, workspace_id).await?;
|
||||
let auth_policy = AuthPolicyRepository::get(pool, workspace_id)
|
||||
.await?
|
||||
.map(|policy| policy.name);
|
||||
|
||||
Ok(MyWorkspaceResponse {
|
||||
id: workspace.id,
|
||||
name: workspace.title,
|
||||
description: workspace.description,
|
||||
participants: participants
|
||||
.into_iter()
|
||||
.map(|participant| WorkspaceParticipantResponse {
|
||||
id: participant.id,
|
||||
username: participant.username,
|
||||
role: participant.role,
|
||||
})
|
||||
.collect(),
|
||||
auth_policy,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn add_user(
|
||||
pool: &PgPool,
|
||||
admin: User,
|
||||
workspace_id: Uuid,
|
||||
req: AddUserRequest,
|
||||
) -> ApiResult<()> {
|
||||
super::ensure_workspace_admin(&admin, workspace_id)?;
|
||||
|
||||
let username = req.username.trim();
|
||||
if username.is_empty() {
|
||||
return Err(ApiError::BadRequest(
|
||||
"username must not be empty".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
let user = UserRepository::find_by_username(pool, username)
|
||||
.await?
|
||||
.ok_or(ApiError::NotFound)?;
|
||||
|
||||
if let Some(previous) = user.workspace_id {
|
||||
if previous != workspace_id {
|
||||
pam::memberships::remove(&user.username, previous)?;
|
||||
}
|
||||
}
|
||||
|
||||
UserRepository::attach_to_workspace(pool, user.id, workspace_id, Role::User).await?;
|
||||
pam::memberships::append(user.username.trim(), workspace_id)
|
||||
}
|
||||
|
||||
pub async fn remove_user(
|
||||
pool: &PgPool,
|
||||
admin: User,
|
||||
workspace_id: Uuid,
|
||||
user_id: Uuid,
|
||||
) -> ApiResult<()> {
|
||||
super::ensure_workspace_admin(&admin, workspace_id)?;
|
||||
|
||||
if UserRepository::detach_member(pool, user_id, workspace_id).await? {
|
||||
let user = UserRepository::get(pool, user_id).await?;
|
||||
pam::memberships::remove(&user.username, workspace_id)?;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ApiError::NotFound)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user