mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-08-20 00:02:19 +00:00
331 lines
8.0 KiB
Rust
331 lines
8.0 KiB
Rust
/*
|
|
* Author & Copyright: Ruben Fiszel 2021
|
|
* This file and its contents are licensed under the AGPLv3 License.
|
|
* Please see the included NOTICE for copyright information and
|
|
* LICENSE-AGPL for a copy of the license.
|
|
*/
|
|
|
|
use crate::{
|
|
audit::{audit_log, ActionKind},
|
|
db::{UserDB, DB},
|
|
error::{Error, JsonResult, Result},
|
|
users::{owner_to_token_owner, Authed},
|
|
utils::Pagination,
|
|
};
|
|
use axum::{
|
|
extract::{Extension, Path, Query},
|
|
routing::{delete, get, post},
|
|
Json, Router,
|
|
};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
use sqlx::{FromRow, Postgres, Transaction};
|
|
|
|
pub fn workspaced_service() -> Router {
|
|
Router::new()
|
|
.route("/list", get(list_groups))
|
|
.route("/listnames", get(list_group_names))
|
|
.route("/create", post(create_group))
|
|
.route("/get/:name", get(get_group))
|
|
.route("/update/:name", post(update_group))
|
|
.route("/delete/:name", delete(delete_group))
|
|
.route("/adduser/:name", post(add_user))
|
|
.route("/removeuser/:name", post(remove_user))
|
|
}
|
|
|
|
#[derive(FromRow, Serialize, Deserialize)]
|
|
pub struct Group {
|
|
pub workspace_id: String,
|
|
pub name: String,
|
|
pub summary: Option<String>,
|
|
pub extra_perms: serde_json::Value,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct NewGroup {
|
|
pub name: String,
|
|
pub summary: Option<String>,
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
pub struct GroupInfo {
|
|
pub workspace_id: String,
|
|
pub name: String,
|
|
pub summary: Option<String>,
|
|
pub members: Vec<String>,
|
|
pub extra_perms: serde_json::Value,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct EditGroup {
|
|
pub summary: Option<String>,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct Username {
|
|
pub username: String,
|
|
}
|
|
|
|
async fn list_groups(
|
|
Extension(db): Extension<DB>,
|
|
Path(w_id): Path<String>,
|
|
Query(pagination): Query<Pagination>,
|
|
) -> JsonResult<Vec<Group>> {
|
|
let (per_page, offset) = crate::utils::paginate(pagination);
|
|
|
|
let rows = sqlx::query_as!(
|
|
Group,
|
|
"SELECT * FROM group_ WHERE workspace_id = $1 ORDER BY name desc LIMIT $2 OFFSET $3",
|
|
w_id,
|
|
per_page as i64,
|
|
offset as i64
|
|
)
|
|
.fetch_all(&db)
|
|
.await?;
|
|
|
|
Ok(Json(rows))
|
|
}
|
|
|
|
async fn list_group_names(
|
|
Extension(db): Extension<DB>,
|
|
Path(w_id): Path<String>,
|
|
) -> JsonResult<Vec<String>> {
|
|
let rows = sqlx::query_scalar!(
|
|
"SELECT name FROM group_ WHERE workspace_id = $1 ORDER BY name desc",
|
|
w_id
|
|
)
|
|
.fetch_all(&db)
|
|
.await?;
|
|
|
|
Ok(Json(rows))
|
|
}
|
|
|
|
async fn create_group(
|
|
authed: Authed,
|
|
Extension(user_db): Extension<UserDB>,
|
|
Path(w_id): Path<String>,
|
|
Json(ng): Json<NewGroup>,
|
|
) -> Result<String> {
|
|
let mut tx = user_db.begin(&authed).await?;
|
|
|
|
sqlx::query_as!(
|
|
Group,
|
|
"INSERT INTO group_ (workspace_id, name, summary, extra_perms) VALUES ($1, $2, $3, $4)",
|
|
w_id,
|
|
ng.name,
|
|
ng.summary,
|
|
serde_json::json!({owner_to_token_owner(&authed.username, false): true})
|
|
)
|
|
.execute(&mut tx)
|
|
.await?;
|
|
|
|
audit_log(
|
|
&mut tx,
|
|
&authed.username,
|
|
"group.create",
|
|
ActionKind::Create,
|
|
&w_id,
|
|
Some(&ng.name.to_string()),
|
|
None,
|
|
)
|
|
.await?;
|
|
|
|
tx.commit().await?;
|
|
Ok(format!("Created group {}", ng.name))
|
|
}
|
|
|
|
pub async fn get_group_opt<'c>(
|
|
db: &mut Transaction<'c, Postgres>,
|
|
w_id: &str,
|
|
name: &str,
|
|
) -> Result<Option<Group>> {
|
|
let group_opt = sqlx::query_as!(
|
|
Group,
|
|
"SELECT * FROM group_ WHERE name = $1 AND workspace_id = $2",
|
|
name,
|
|
w_id
|
|
)
|
|
.fetch_optional(db)
|
|
.await?;
|
|
Ok(group_opt)
|
|
}
|
|
|
|
async fn get_group(
|
|
authed: Authed,
|
|
Extension(user_db): Extension<UserDB>,
|
|
Path((w_id, name)): Path<(String, String)>,
|
|
) -> JsonResult<GroupInfo> {
|
|
let mut tx = user_db.begin(&authed).await?;
|
|
|
|
let group = crate::utils::not_found_if_none(
|
|
get_group_opt(&mut tx, &w_id, &name).await?,
|
|
"Group",
|
|
&name,
|
|
)?;
|
|
|
|
let members = sqlx::query_scalar!(
|
|
"SELECT usr.username
|
|
FROM usr_to_group LEFT JOIN usr ON usr_to_group.usr = usr.username
|
|
WHERE group_ = $1 AND usr.workspace_id = $2 AND usr_to_group.workspace_id = $2",
|
|
name,
|
|
w_id
|
|
)
|
|
.fetch_all(&mut tx)
|
|
.await?;
|
|
|
|
tx.commit().await?;
|
|
Ok(Json(GroupInfo {
|
|
workspace_id: group.workspace_id,
|
|
name: group.name,
|
|
summary: group.summary,
|
|
members,
|
|
extra_perms: group.extra_perms,
|
|
}))
|
|
}
|
|
|
|
async fn delete_group(
|
|
authed: Authed,
|
|
Extension(user_db): Extension<UserDB>,
|
|
Path((w_id, name)): Path<(String, String)>,
|
|
) -> Result<String> {
|
|
let mut tx = user_db.begin(&authed).await?;
|
|
|
|
crate::utils::not_found_if_none(get_group_opt(&mut tx, &w_id, &name).await?, "Group", &name)?;
|
|
|
|
sqlx::query!(
|
|
"DELETE FROM usr_to_group WHERE group_ = $1 AND workspace_id = $2",
|
|
name,
|
|
w_id
|
|
)
|
|
.execute(&mut tx)
|
|
.await?;
|
|
sqlx::query!(
|
|
"DELETE FROM group_ WHERE name = $1 AND workspace_id = $2",
|
|
name,
|
|
w_id
|
|
)
|
|
.execute(&mut tx)
|
|
.await?;
|
|
audit_log(
|
|
&mut tx,
|
|
&authed.username,
|
|
"group.delete",
|
|
ActionKind::Delete,
|
|
&w_id,
|
|
Some(&name.to_string()),
|
|
None,
|
|
)
|
|
.await?;
|
|
tx.commit().await?;
|
|
Ok(format!("delete group at name {}", name))
|
|
}
|
|
|
|
async fn update_group(
|
|
authed: Authed,
|
|
Extension(user_db): Extension<UserDB>,
|
|
Path((w_id, name)): Path<(String, String)>,
|
|
Json(eg): Json<EditGroup>,
|
|
) -> Result<String> {
|
|
let mut tx = user_db.begin(&authed).await?;
|
|
|
|
crate::utils::not_found_if_none(get_group_opt(&mut tx, &w_id, &name).await?, "Group", &name)?;
|
|
|
|
sqlx::query_as!(
|
|
Group,
|
|
"UPDATE group_ SET summary = $1 WHERE name = $2 AND workspace_id = $3",
|
|
eg.summary,
|
|
&name,
|
|
&w_id
|
|
)
|
|
.execute(&mut tx)
|
|
.await?;
|
|
|
|
audit_log(
|
|
&mut tx,
|
|
&authed.username,
|
|
"group.edit",
|
|
ActionKind::Update,
|
|
&w_id,
|
|
Some(&name.to_string()),
|
|
None,
|
|
)
|
|
.await?;
|
|
tx.commit().await?;
|
|
Ok(format!("Edited group {}", name))
|
|
}
|
|
|
|
async fn add_user(
|
|
authed: Authed,
|
|
Extension(user_db): Extension<UserDB>,
|
|
Path((w_id, name)): Path<(String, String)>,
|
|
Json(Username {
|
|
username: user_username,
|
|
}): Json<Username>,
|
|
) -> Result<String> {
|
|
let mut tx = user_db.begin(&authed).await?;
|
|
|
|
crate::utils::not_found_if_none(get_group_opt(&mut tx, &w_id, &name).await?, "Group", &name)?;
|
|
|
|
sqlx::query_as!(
|
|
Group,
|
|
"INSERT INTO usr_to_group (workspace_id, usr, group_) VALUES ($1, $2, $3)",
|
|
&w_id,
|
|
user_username,
|
|
name,
|
|
)
|
|
.execute(&mut tx)
|
|
.await?;
|
|
|
|
audit_log(
|
|
&mut tx,
|
|
&authed.username,
|
|
"group.adduser",
|
|
ActionKind::Update,
|
|
&w_id,
|
|
Some(&name.to_string()),
|
|
Some([("user", user_username.as_str())].into()),
|
|
)
|
|
.await?;
|
|
tx.commit().await?;
|
|
Ok(format!("Added {} to group {}", user_username, name))
|
|
}
|
|
|
|
async fn remove_user(
|
|
authed: Authed,
|
|
Extension(user_db): Extension<UserDB>,
|
|
Path((w_id, name)): Path<(String, String)>,
|
|
Json(Username {
|
|
username: user_username,
|
|
}): Json<Username>,
|
|
) -> Result<String> {
|
|
let mut tx = user_db.begin(&authed).await?;
|
|
|
|
crate::utils::not_found_if_none(get_group_opt(&mut tx, &w_id, &name).await?, "Group", &name)?;
|
|
if &name == "all" {
|
|
return Err(Error::BadRequest(format!("Cannot delete users from all")));
|
|
}
|
|
sqlx::query_as!(
|
|
Group,
|
|
"DELETE FROM usr_to_group WHERE usr = $1 AND group_ = $2 AND workspace_id = $3",
|
|
user_username,
|
|
name,
|
|
&w_id,
|
|
)
|
|
.execute(&mut tx)
|
|
.await?;
|
|
|
|
audit_log(
|
|
&mut tx,
|
|
&authed.username,
|
|
"group.removeuser",
|
|
ActionKind::Update,
|
|
&w_id,
|
|
Some(&name.to_string()),
|
|
Some([("user", user_username.as_str())].into()),
|
|
)
|
|
.await?;
|
|
|
|
tx.commit().await?;
|
|
Ok(format!("Removed {} to group {}", user_username, name))
|
|
}
|