in preparation to moving userdata into app, we switched to arbitrary groups

This commit is contained in:
2023-12-23 21:27:52 +01:00
parent e4da952a62
commit c7d7d0ca83
29 changed files with 396 additions and 256 deletions

View File

@ -1,7 +1,7 @@
use crate::model::{
boat::{Boat, BoatToAdd, BoatToUpdate},
location::Location,
user::{AdminUser, User},
user::{AdminUser, User, UserWithRoles},
};
use rocket::{
form::Form,
@ -30,7 +30,10 @@ async fn index(
context.insert("boats", &boats);
context.insert("locations", &locations);
context.insert("users", &users);
context.insert("loggedin_user", &admin.user);
context.insert(
"loggedin_user",
&UserWithRoles::from_user(admin.user, db).await,
);
Template::render("admin/boat/index", context.into_json())
}

View File

@ -1,4 +1,10 @@
use crate::model::user::{AdminUser, User};
use std::collections::HashMap;
use crate::model::{
role::Role,
user::{AdminUser, User, UserWithRoles},
};
use futures::future::join_all;
use rocket::{
form::Form,
get, post,
@ -15,14 +21,26 @@ async fn index(
admin: AdminUser,
flash: Option<FlashMessage<'_>>,
) -> Template {
let users = User::all(db).await;
let user_futures: Vec<_> = User::all(db)
.await
.into_iter()
.map(|u| async move { UserWithRoles::from_user(u, db).await })
.collect();
let users: Vec<UserWithRoles> = join_all(user_futures).await;
let roles = Role::all(db).await;
let mut context = Context::new();
if let Some(msg) = flash {
context.insert("flash", &msg.into_inner());
}
context.insert("users", &users);
context.insert("loggedin_user", &admin.user);
context.insert("roles", &roles);
context.insert(
"loggedin_user",
&UserWithRoles::from_user(admin.user, db).await,
);
Template::render("admin/user/index", context.into_json())
}
@ -57,16 +75,13 @@ async fn delete(db: &State<SqlitePool>, _admin: AdminUser, user: i32) -> Flash<R
}
}
#[derive(FromForm)]
#[derive(FromForm, Debug)]
pub struct UserEditForm {
pub(crate) id: i32,
pub(crate) is_guest: bool,
pub(crate) is_cox: bool,
pub(crate) is_admin: bool,
pub(crate) is_tech: bool,
pub(crate) dob: Option<String>,
pub(crate) weight: Option<String>,
pub(crate) sex: Option<String>,
pub(crate) roles: HashMap<String, String>,
}
#[post("/user", data = "<data>")]
@ -91,7 +106,6 @@ async fn update(
#[derive(FromForm)]
struct UserAddForm<'r> {
name: &'r str,
is_guest: bool,
}
#[post("/user/new", data = "<data>")]
@ -100,7 +114,7 @@ async fn create(
data: Form<UserAddForm<'_>>,
_admin: AdminUser,
) -> Flash<Redirect> {
if User::create(db, data.name, data.is_guest).await {
if User::create(db, data.name).await {
Flash::success(Redirect::to("/admin/user"), "Successfully created user")
} else {
Flash::error(