2025-03-14 13:04:57 -07:00
|
|
|
use anyhow::Context as _;
|
|
|
|
use askama::Template;
|
|
|
|
use axum::{
|
|
|
|
extract::{Path, State},
|
|
|
|
response::{Html, IntoResponse, Redirect},
|
|
|
|
routing::{get, post},
|
|
|
|
Router,
|
|
|
|
};
|
|
|
|
use axum_extra::extract::Form;
|
|
|
|
use diesel::prelude::*;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
api_keys::ApiKey,
|
|
|
|
app_error::AppError,
|
|
|
|
app_state::{AppState, DbConn},
|
|
|
|
csrf::generate_csrf_token,
|
|
|
|
guards,
|
2025-04-04 13:42:10 -07:00
|
|
|
nav::{BreadcrumbTrail, Navbar, NavbarBuilder, NAVBAR_ITEM_TEAMS},
|
2025-03-14 13:04:57 -07:00
|
|
|
projects::{Project, DEFAULT_PROJECT_NAME},
|
2025-04-04 14:22:44 -07:00
|
|
|
schema::{api_keys, team_memberships, teams},
|
2025-03-14 13:04:57 -07:00
|
|
|
settings::Settings,
|
|
|
|
team_memberships::TeamMembership,
|
|
|
|
teams::Team,
|
|
|
|
users::CurrentUser,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn new_router() -> Router<AppState> {
|
|
|
|
Router::new()
|
|
|
|
.route("/teams", get(teams_page))
|
|
|
|
.route("/teams/{team_id}", get(team_page))
|
|
|
|
.route("/teams/{team_id}/new-api-key", post(post_new_api_key))
|
2025-04-04 14:22:44 -07:00
|
|
|
.route("/teams/{team_id}/remove-api-key", post(remove_api_key))
|
2025-03-14 13:04:57 -07:00
|
|
|
.route("/new-team", get(new_team_page))
|
|
|
|
.route("/new-team", post(post_new_team))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn teams_page(
|
|
|
|
State(Settings { base_path, .. }): State<Settings>,
|
2025-04-04 13:42:10 -07:00
|
|
|
State(navbar_template): State<NavbarBuilder>,
|
2025-03-14 13:04:57 -07:00
|
|
|
DbConn(conn): DbConn,
|
|
|
|
CurrentUser(current_user): CurrentUser,
|
|
|
|
) -> Result<impl IntoResponse, AppError> {
|
|
|
|
let teams: Vec<Team> = {
|
|
|
|
let current_user = current_user.clone();
|
|
|
|
conn.interact(move |conn| current_user.team_memberships().load(conn))
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.context("failed to load team memberships")
|
|
|
|
.map(|memberships| memberships.into_iter().map(|(_, team)| team).collect())?
|
|
|
|
};
|
|
|
|
#[derive(Template)]
|
|
|
|
#[template(path = "teams.html")]
|
|
|
|
struct ResponseTemplate {
|
|
|
|
base_path: String,
|
2025-04-04 13:42:10 -07:00
|
|
|
breadcrumbs: BreadcrumbTrail,
|
|
|
|
navbar: Navbar,
|
2025-03-14 13:04:57 -07:00
|
|
|
teams: Vec<Team>,
|
|
|
|
}
|
|
|
|
Ok(Html(
|
|
|
|
ResponseTemplate {
|
2025-04-04 13:42:10 -07:00
|
|
|
breadcrumbs: BreadcrumbTrail::from_base_path(&base_path)
|
|
|
|
.with_i18n_slug("en")
|
|
|
|
.push_slug("Teams", "teams"),
|
2025-03-14 13:04:57 -07:00
|
|
|
base_path,
|
2025-04-04 13:42:10 -07:00
|
|
|
navbar: navbar_template.with_active_item(NAVBAR_ITEM_TEAMS).build(),
|
2025-03-14 13:04:57 -07:00
|
|
|
teams,
|
|
|
|
}
|
|
|
|
.render()?,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn team_page(
|
|
|
|
State(Settings { base_path, .. }): State<Settings>,
|
|
|
|
Path(team_id): Path<Uuid>,
|
|
|
|
) -> impl IntoResponse {
|
2025-03-25 11:40:02 -07:00
|
|
|
Redirect::to(&format!("{}/en/teams/{}/projects", base_path, team_id))
|
2025-03-14 13:04:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct PostNewApiKeyForm {
|
|
|
|
csrf_token: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn post_new_api_key(
|
|
|
|
State(Settings { base_path, .. }): State<Settings>,
|
|
|
|
DbConn(db_conn): DbConn,
|
|
|
|
Path(team_id): Path<Uuid>,
|
|
|
|
CurrentUser(current_user): CurrentUser,
|
|
|
|
Form(form): Form<PostNewApiKeyForm>,
|
|
|
|
) -> Result<impl IntoResponse, AppError> {
|
|
|
|
guards::require_valid_csrf_token(&form.csrf_token, ¤t_user, &db_conn).await?;
|
|
|
|
let team = guards::require_team_membership(¤t_user, &team_id, &db_conn).await?;
|
|
|
|
|
|
|
|
ApiKey::generate_for_team(&db_conn, team.id).await?;
|
|
|
|
Ok(Redirect::to(&format!(
|
2025-03-25 11:40:02 -07:00
|
|
|
"{}/en/teams/{}/projects",
|
2025-03-14 13:04:57 -07:00
|
|
|
base_path,
|
2025-04-04 14:22:44 -07:00
|
|
|
team.id.simple()
|
2025-03-14 13:04:57 -07:00
|
|
|
))
|
|
|
|
.into_response())
|
|
|
|
}
|
|
|
|
|
2025-04-04 14:22:44 -07:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct RemoveApiKeyForm {
|
|
|
|
csrf_token: String,
|
|
|
|
key_id: Uuid,
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn remove_api_key(
|
|
|
|
State(Settings { base_path, .. }): State<Settings>,
|
|
|
|
DbConn(db_conn): DbConn,
|
|
|
|
Path(team_id): Path<Uuid>,
|
|
|
|
CurrentUser(current_user): CurrentUser,
|
|
|
|
Form(form): Form<RemoveApiKeyForm>,
|
|
|
|
) -> Result<impl IntoResponse, AppError> {
|
|
|
|
guards::require_valid_csrf_token(&form.csrf_token, ¤t_user, &db_conn).await?;
|
|
|
|
let team = guards::require_team_membership(¤t_user, &team_id, &db_conn).await?;
|
|
|
|
|
|
|
|
let n_deleted = {
|
|
|
|
let team_id = team.id;
|
|
|
|
db_conn
|
|
|
|
.interact::<_, Result<usize, AppError>>(move |conn| {
|
|
|
|
diesel::delete(
|
|
|
|
api_keys::table
|
|
|
|
.filter(ApiKey::with_team(&team_id))
|
|
|
|
.filter(ApiKey::with_id(&form.key_id)),
|
|
|
|
)
|
|
|
|
.execute(conn)
|
|
|
|
.context("failed to delete API key from database")
|
|
|
|
.map_err(Into::into)
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap()?
|
|
|
|
};
|
|
|
|
assert!(
|
|
|
|
n_deleted < 2,
|
|
|
|
"there should never be more than 1 API key with the same ID"
|
|
|
|
);
|
|
|
|
if n_deleted == 0 {
|
|
|
|
Err(AppError::NotFoundError(
|
|
|
|
"no API key with that ID and team found".to_owned(),
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Ok(Redirect::to(&format!(
|
|
|
|
"{}/en/teams/{}/projects",
|
|
|
|
base_path,
|
|
|
|
team.id.simple()
|
|
|
|
))
|
|
|
|
.into_response())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-14 13:04:57 -07:00
|
|
|
async fn new_team_page(
|
|
|
|
State(Settings { base_path, .. }): State<Settings>,
|
2025-04-04 13:42:10 -07:00
|
|
|
State(navbar_template): State<NavbarBuilder>,
|
2025-03-14 13:04:57 -07:00
|
|
|
DbConn(db_conn): DbConn,
|
|
|
|
CurrentUser(current_user): CurrentUser,
|
|
|
|
) -> Result<impl IntoResponse, AppError> {
|
|
|
|
let csrf_token = generate_csrf_token(&db_conn, Some(current_user.id)).await?;
|
|
|
|
|
|
|
|
#[derive(Template)]
|
|
|
|
#[template(path = "new-team.html")]
|
|
|
|
struct ResponseTemplate {
|
|
|
|
base_path: String,
|
2025-04-04 13:42:10 -07:00
|
|
|
breadcrumbs: BreadcrumbTrail,
|
2025-03-14 13:04:57 -07:00
|
|
|
csrf_token: String,
|
2025-04-04 13:42:10 -07:00
|
|
|
navbar: Navbar,
|
2025-03-14 13:04:57 -07:00
|
|
|
}
|
|
|
|
Ok(Html(
|
|
|
|
ResponseTemplate {
|
2025-04-04 13:42:10 -07:00
|
|
|
breadcrumbs: BreadcrumbTrail::from_base_path(&base_path)
|
|
|
|
.with_i18n_slug("en")
|
|
|
|
.push_slug("New Team", "new-team"),
|
2025-03-14 13:04:57 -07:00
|
|
|
base_path,
|
|
|
|
csrf_token,
|
2025-04-04 13:42:10 -07:00
|
|
|
navbar: navbar_template.with_active_item(NAVBAR_ITEM_TEAMS).build(),
|
2025-03-14 13:04:57 -07:00
|
|
|
}
|
|
|
|
.render()?,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct PostNewTeamForm {
|
|
|
|
name: String,
|
|
|
|
csrf_token: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn post_new_team(
|
|
|
|
DbConn(db_conn): DbConn,
|
|
|
|
State(Settings { base_path, .. }): State<Settings>,
|
|
|
|
CurrentUser(current_user): CurrentUser,
|
|
|
|
Form(form): Form<PostNewTeamForm>,
|
|
|
|
) -> Result<impl IntoResponse, AppError> {
|
|
|
|
guards::require_valid_csrf_token(&form.csrf_token, ¤t_user, &db_conn).await?;
|
|
|
|
|
|
|
|
let team_id = Uuid::now_v7();
|
|
|
|
let team = Team {
|
|
|
|
id: team_id,
|
|
|
|
name: form.name,
|
|
|
|
};
|
|
|
|
let team_membership = TeamMembership {
|
|
|
|
team_id,
|
|
|
|
user_id: current_user.id,
|
|
|
|
};
|
|
|
|
db_conn
|
|
|
|
.interact::<_, Result<(), AppError>>(move |conn| {
|
|
|
|
conn.transaction::<(), AppError, _>(move |conn| {
|
|
|
|
diesel::insert_into(teams::table)
|
|
|
|
.values(&team)
|
|
|
|
.execute(conn)?;
|
|
|
|
diesel::insert_into(team_memberships::table)
|
|
|
|
.values(&team_membership)
|
|
|
|
.execute(conn)?;
|
|
|
|
Project::insert_new(conn, &team.id, DEFAULT_PROJECT_NAME)?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
ApiKey::generate_for_team(&db_conn, team_id).await?;
|
2025-03-25 11:40:02 -07:00
|
|
|
Ok(Redirect::to(&format!("{}/en/teams/{}/projects", base_path, team_id)).into_response())
|
2025-03-14 13:04:57 -07:00
|
|
|
}
|