init
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
use crate::core::state::AppState;
|
||||
use crate::http::context::Superuser;
|
||||
use crate::http::error::HTTPError;
|
||||
use crate::routes::channel::dto::{ChannelResponse, CreateChannelRequest, UpdateChannelRequest};
|
||||
use crate::routes::channel::dto::{
|
||||
ChannelResponse, ChannelRolePermissionResponse, ChannelUserPermissionResponse,
|
||||
CreateChannelRequest, SetChannelPermissionRequest, UpdateChannelRequest,
|
||||
};
|
||||
use crate::routes::channel::mapper;
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
@@ -195,3 +198,217 @@ pub async fn delete(
|
||||
Err(HTTPError::NotFound)
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère les permissions directes d'un utilisateur dans un canal.
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/channels/{channel_id}/permissions/users/{user_id}",
|
||||
params(
|
||||
("channel_id" = Uuid, Path, description = "ID du canal"),
|
||||
("user_id" = Uuid, Path, description = "ID de l'utilisateur")
|
||||
),
|
||||
responses(
|
||||
(status = 200, body = ChannelUserPermissionResponse),
|
||||
(status = 404, description = "Permission introuvable"),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
tag = "Channel Permissions"
|
||||
)]
|
||||
pub async fn get_user_permission(
|
||||
State(state): State<AppState>,
|
||||
Path((channel_id, user_id)): Path<(Uuid, Uuid)>,
|
||||
) -> Result<Json<ChannelUserPermissionResponse>, HTTPError> {
|
||||
let permission = state
|
||||
.repositories
|
||||
.channel
|
||||
.get_user_permission(channel_id, user_id)
|
||||
.await?
|
||||
.ok_or(HTTPError::NotFound)?;
|
||||
|
||||
Ok(Json(mapper::channel_user_permission_to_response(
|
||||
permission,
|
||||
)))
|
||||
}
|
||||
|
||||
/// Définit ou remplace les permissions directes d'un utilisateur dans un canal.
|
||||
#[utoipa::path(
|
||||
put,
|
||||
path = "/channels/{channel_id}/permissions/users/{user_id}",
|
||||
request_body = SetChannelPermissionRequest,
|
||||
params(
|
||||
("channel_id" = Uuid, Path, description = "ID du canal"),
|
||||
("user_id" = Uuid, Path, description = "ID de l'utilisateur")
|
||||
),
|
||||
responses(
|
||||
(status = 200, body = ChannelUserPermissionResponse),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
tag = "Channel Permissions"
|
||||
)]
|
||||
pub async fn set_user_permission(
|
||||
State(state): State<AppState>,
|
||||
Path((channel_id, user_id)): Path<(Uuid, Uuid)>,
|
||||
Json(payload): Json<SetChannelPermissionRequest>,
|
||||
) -> Result<Json<ChannelUserPermissionResponse>, HTTPError> {
|
||||
state
|
||||
.repositories
|
||||
.channel
|
||||
.set_user_permission(channel_id, user_id, payload.permissions)
|
||||
.await?;
|
||||
|
||||
let permission = state
|
||||
.repositories
|
||||
.channel
|
||||
.get_user_permission(channel_id, user_id)
|
||||
.await?
|
||||
.ok_or(HTTPError::NotFound)?;
|
||||
|
||||
Ok(Json(mapper::channel_user_permission_to_response(
|
||||
permission,
|
||||
)))
|
||||
}
|
||||
|
||||
/// Supprime les permissions directes d'un utilisateur dans un canal.
|
||||
#[utoipa::path(
|
||||
delete,
|
||||
path = "/channels/{channel_id}/permissions/users/{user_id}",
|
||||
params(
|
||||
("channel_id" = Uuid, Path, description = "ID du canal"),
|
||||
("user_id" = Uuid, Path, description = "ID de l'utilisateur")
|
||||
),
|
||||
responses(
|
||||
(status = 204, description = "Permission supprimée"),
|
||||
(status = 404, description = "Permission introuvable"),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
tag = "Channel Permissions"
|
||||
)]
|
||||
pub async fn remove_user_permission(
|
||||
State(state): State<AppState>,
|
||||
Path((channel_id, user_id)): Path<(Uuid, Uuid)>,
|
||||
) -> Result<StatusCode, HTTPError> {
|
||||
if state
|
||||
.repositories
|
||||
.channel
|
||||
.get_user_permission(channel_id, user_id)
|
||||
.await?
|
||||
.is_none()
|
||||
{
|
||||
return Err(HTTPError::NotFound);
|
||||
}
|
||||
|
||||
state
|
||||
.repositories
|
||||
.channel
|
||||
.remove_user_permission(channel_id, user_id)
|
||||
.await?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
/// Récupère les permissions d'un rôle dans un canal.
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/channels/{channel_id}/permissions/roles/{role_id}",
|
||||
params(
|
||||
("channel_id" = Uuid, Path, description = "ID du canal"),
|
||||
("role_id" = Uuid, Path, description = "ID du rôle")
|
||||
),
|
||||
responses(
|
||||
(status = 200, body = ChannelRolePermissionResponse),
|
||||
(status = 404, description = "Permission introuvable"),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
tag = "Channel Permissions"
|
||||
)]
|
||||
pub async fn get_role_permission(
|
||||
State(state): State<AppState>,
|
||||
Path((channel_id, role_id)): Path<(Uuid, Uuid)>,
|
||||
) -> Result<Json<ChannelRolePermissionResponse>, HTTPError> {
|
||||
let permission = state
|
||||
.repositories
|
||||
.channel
|
||||
.get_role_permission(channel_id, role_id)
|
||||
.await?
|
||||
.ok_or(HTTPError::NotFound)?;
|
||||
|
||||
Ok(Json(mapper::channel_role_permission_to_response(
|
||||
permission,
|
||||
)))
|
||||
}
|
||||
|
||||
/// Définit ou remplace les permissions d'un rôle dans un canal.
|
||||
#[utoipa::path(
|
||||
put,
|
||||
path = "/channels/{channel_id}/permissions/roles/{role_id}",
|
||||
request_body = SetChannelPermissionRequest,
|
||||
params(
|
||||
("channel_id" = Uuid, Path, description = "ID du canal"),
|
||||
("role_id" = Uuid, Path, description = "ID du rôle")
|
||||
),
|
||||
responses(
|
||||
(status = 200, body = ChannelRolePermissionResponse),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
tag = "Channel Permissions"
|
||||
)]
|
||||
pub async fn set_role_permission(
|
||||
State(state): State<AppState>,
|
||||
Path((channel_id, role_id)): Path<(Uuid, Uuid)>,
|
||||
Json(payload): Json<SetChannelPermissionRequest>,
|
||||
) -> Result<Json<ChannelRolePermissionResponse>, HTTPError> {
|
||||
state
|
||||
.repositories
|
||||
.channel
|
||||
.set_role_permission(channel_id, role_id, payload.permissions)
|
||||
.await?;
|
||||
|
||||
let permission = state
|
||||
.repositories
|
||||
.channel
|
||||
.get_role_permission(channel_id, role_id)
|
||||
.await?
|
||||
.ok_or(HTTPError::NotFound)?;
|
||||
|
||||
Ok(Json(mapper::channel_role_permission_to_response(
|
||||
permission,
|
||||
)))
|
||||
}
|
||||
|
||||
/// Supprime les permissions d'un rôle dans un canal.
|
||||
#[utoipa::path(
|
||||
delete,
|
||||
path = "/channels/{channel_id}/permissions/roles/{role_id}",
|
||||
params(
|
||||
("channel_id" = Uuid, Path, description = "ID du canal"),
|
||||
("role_id" = Uuid, Path, description = "ID du rôle")
|
||||
),
|
||||
responses(
|
||||
(status = 204, description = "Permission supprimée"),
|
||||
(status = 404, description = "Permission introuvable"),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
tag = "Channel Permissions"
|
||||
)]
|
||||
pub async fn remove_role_permission(
|
||||
State(state): State<AppState>,
|
||||
Path((channel_id, role_id)): Path<(Uuid, Uuid)>,
|
||||
) -> Result<StatusCode, HTTPError> {
|
||||
if state
|
||||
.repositories
|
||||
.channel
|
||||
.get_role_permission(channel_id, role_id)
|
||||
.await?
|
||||
.is_none()
|
||||
{
|
||||
return Err(HTTPError::NotFound);
|
||||
}
|
||||
|
||||
state
|
||||
.repositories
|
||||
.channel
|
||||
.remove_role_permission(channel_id, role_id)
|
||||
.await?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user