-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3aa0af
commit df2f140
Showing
5 changed files
with
192 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
use dropshot::{endpoint, HttpError, HttpResponseOk, Path, RequestContext, TypedBody}; | ||
use rfd_model::{AccessGroup, NewAccessGroup}; | ||
use schemars::JsonSchema; | ||
use serde::Deserialize; | ||
use trace_request::trace_request; | ||
use tracing::instrument; | ||
use uuid::Uuid; | ||
|
||
use crate::{ | ||
context::ApiContext, error::ApiError, permissions::ApiPermission, util::response::unauthorized, | ||
ApiPermissions, | ||
}; | ||
|
||
#[trace_request] | ||
#[endpoint { | ||
method = GET, | ||
path = "/group", | ||
}] | ||
#[instrument(skip(rqctx), fields(request_id = rqctx.request_id), err(Debug))] | ||
pub async fn get_groups( | ||
rqctx: RequestContext<ApiContext>, | ||
) -> Result<HttpResponseOk<Vec<AccessGroup<ApiPermission>>>, HttpError> { | ||
let ctx = rqctx.context(); | ||
let auth = ctx.authn_token(&rqctx).await; | ||
let caller = ctx.get_caller(&auth?).await?; | ||
|
||
if caller.can(&ApiPermission::ListGroups) { | ||
Ok(HttpResponseOk( | ||
ctx.get_groups().await.map_err(ApiError::Storage)?, | ||
)) | ||
} else { | ||
Err(unauthorized()) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Deserialize, JsonSchema)] | ||
pub struct AccessGroupUpdateParams { | ||
name: String, | ||
permissions: ApiPermissions, | ||
} | ||
|
||
#[trace_request] | ||
#[endpoint { | ||
method = POST, | ||
path = "/group", | ||
}] | ||
#[instrument(skip(rqctx), fields(request_id = rqctx.request_id), err(Debug))] | ||
pub async fn create_group( | ||
rqctx: RequestContext<ApiContext>, | ||
body: TypedBody<AccessGroupUpdateParams>, | ||
) -> Result<HttpResponseOk<AccessGroup<ApiPermission>>, HttpError> { | ||
let ctx = rqctx.context(); | ||
let auth = ctx.authn_token(&rqctx).await; | ||
let caller = ctx.get_caller(&auth?).await?; | ||
|
||
if caller.can(&ApiPermission::CreateGroup) { | ||
let body = body.into_inner(); | ||
Ok(HttpResponseOk( | ||
ctx.create_group(NewAccessGroup { | ||
id: Uuid::new_v4(), | ||
name: body.name, | ||
permissions: body.permissions, | ||
}) | ||
.await | ||
.map_err(ApiError::Storage)?, | ||
)) | ||
} else { | ||
Err(unauthorized()) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Deserialize, JsonSchema)] | ||
pub struct AccessGroupPath { | ||
group_id: Uuid, | ||
} | ||
|
||
#[trace_request] | ||
#[endpoint { | ||
method = PUT, | ||
path = "/group/{group_id}", | ||
}] | ||
#[instrument(skip(rqctx), fields(request_id = rqctx.request_id), err(Debug))] | ||
pub async fn update_group( | ||
rqctx: RequestContext<ApiContext>, | ||
path: Path<AccessGroupPath>, | ||
body: TypedBody<AccessGroupUpdateParams>, | ||
) -> Result<HttpResponseOk<AccessGroup<ApiPermission>>, HttpError> { | ||
let ctx = rqctx.context(); | ||
let auth = ctx.authn_token(&rqctx).await; | ||
let caller = ctx.get_caller(&auth?).await?; | ||
let path = path.into_inner(); | ||
|
||
if caller.can(&ApiPermission::UpdateGroup(path.group_id)) { | ||
let body = body.into_inner(); | ||
Ok(HttpResponseOk( | ||
ctx.update_group(NewAccessGroup { | ||
id: path.group_id, | ||
name: body.name, | ||
permissions: body.permissions, | ||
}) | ||
.await | ||
.map_err(ApiError::Storage)?, | ||
)) | ||
} else { | ||
Err(unauthorized()) | ||
} | ||
} | ||
|
||
#[trace_request] | ||
#[endpoint { | ||
method = DELETE, | ||
path = "/group/{group_id}", | ||
}] | ||
#[instrument(skip(rqctx), fields(request_id = rqctx.request_id), err(Debug))] | ||
pub async fn delete_group( | ||
rqctx: RequestContext<ApiContext>, | ||
path: Path<AccessGroupPath>, | ||
) -> Result<HttpResponseOk<Option<AccessGroup<ApiPermission>>>, HttpError> { | ||
let ctx = rqctx.context(); | ||
let auth = ctx.authn_token(&rqctx).await; | ||
let caller = ctx.get_caller(&auth?).await?; | ||
let path = path.into_inner(); | ||
|
||
if caller.can(&ApiPermission::DeleteGroup(path.group_id)) { | ||
Ok(HttpResponseOk( | ||
ctx.delete_group(&path.group_id) | ||
.await | ||
.map_err(ApiError::Storage)?, | ||
)) | ||
} else { | ||
Err(unauthorized()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod api_user; | ||
pub mod groups; | ||
pub mod login; | ||
pub mod rfd; | ||
pub mod webhook; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters