179 lines
5.1 KiB
Rust
179 lines
5.1 KiB
Rust
|
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,
|
||
|
nav_state::{Breadcrumb, NavState},
|
||
|
projects::{Project, DEFAULT_PROJECT_NAME},
|
||
|
schema::{team_memberships, teams},
|
||
|
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))
|
||
|
.route("/new-team", get(new_team_page))
|
||
|
.route("/new-team", post(post_new_team))
|
||
|
}
|
||
|
|
||
|
async fn teams_page(
|
||
|
State(Settings { base_path, .. }): State<Settings>,
|
||
|
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())?
|
||
|
};
|
||
|
let nav_state = NavState::new()
|
||
|
.set_base_path(&base_path)
|
||
|
.push_slug(Breadcrumb {
|
||
|
href: "teams".to_string(),
|
||
|
label: "Teams".to_string(),
|
||
|
})
|
||
|
.set_navbar_active_item("teams");
|
||
|
#[derive(Template)]
|
||
|
#[template(path = "teams.html")]
|
||
|
struct ResponseTemplate {
|
||
|
base_path: String,
|
||
|
teams: Vec<Team>,
|
||
|
nav_state: NavState,
|
||
|
}
|
||
|
Ok(Html(
|
||
|
ResponseTemplate {
|
||
|
base_path,
|
||
|
nav_state,
|
||
|
teams,
|
||
|
}
|
||
|
.render()?,
|
||
|
))
|
||
|
}
|
||
|
|
||
|
async fn team_page(
|
||
|
State(Settings { base_path, .. }): State<Settings>,
|
||
|
Path(team_id): Path<Uuid>,
|
||
|
) -> impl IntoResponse {
|
||
|
Redirect::to(&format!("{}/teams/{}/projects", base_path, team_id))
|
||
|
}
|
||
|
|
||
|
#[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!(
|
||
|
"{}/teams/{}/projects",
|
||
|
base_path,
|
||
|
team.id.hyphenated()
|
||
|
))
|
||
|
.into_response())
|
||
|
}
|
||
|
|
||
|
async fn new_team_page(
|
||
|
State(Settings { base_path, .. }): State<Settings>,
|
||
|
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?;
|
||
|
|
||
|
let nav_state = NavState::new()
|
||
|
.set_base_path(&base_path)
|
||
|
.push_slug(Breadcrumb {
|
||
|
href: "new-team".to_string(),
|
||
|
label: "New Team".to_string(),
|
||
|
})
|
||
|
.set_navbar_active_item("teams");
|
||
|
#[derive(Template)]
|
||
|
#[template(path = "new-team.html")]
|
||
|
struct ResponseTemplate {
|
||
|
base_path: String,
|
||
|
csrf_token: String,
|
||
|
nav_state: NavState,
|
||
|
}
|
||
|
Ok(Html(
|
||
|
ResponseTemplate {
|
||
|
base_path,
|
||
|
csrf_token,
|
||
|
nav_state,
|
||
|
}
|
||
|
.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?;
|
||
|
Ok(Redirect::to(&format!("{}/teams/{}/projects", base_path, team_id)).into_response())
|
||
|
}
|