Start to build edit user form

This commit is contained in:
Pierre HUBERT 2022-04-07 17:04:05 +02:00
parent 587758f4ed
commit af903de7c2
6 changed files with 128 additions and 6 deletions

View File

@ -1,3 +1,5 @@
use std::ops::Deref;
use actix::Addr;
use actix_web::{HttpResponse, Responder, web};
use askama::Template;
@ -23,6 +25,14 @@ struct UsersListTemplate {
users: Vec<User>,
}
#[derive(Template)]
#[template(path = "settings/edit_user.html")]
struct EditUserTemplate {
_parent: BaseSettingsPage,
u: User,
clients: Vec<Client>,
}
pub async fn clients_route(user: CurrentUser, clients: web::Data<ClientManager>) -> impl Responder {
HttpResponse::Ok().body(ClientsListTemplate {
@ -48,4 +58,12 @@ pub async fn users_route(user: CurrentUser, users: web::Data<Addr<UsersActor>>)
),
users,
}.render().unwrap())
}
pub async fn create_user(user: CurrentUser, clients: web::Data<ClientManager>) -> impl Responder {
HttpResponse::Ok().body(EditUserTemplate {
_parent: BaseSettingsPage::get("Create a new user", user.deref(), None, None),
u: Default::default(),
clients: clients.cloned(),
}.render().unwrap())
}

View File

@ -1,6 +1,5 @@
pub mod app_config;
pub mod entity_manager;
pub mod service;
pub mod session_identity;
pub mod user;
pub mod client;

View File

@ -1,2 +0,0 @@
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct ServiceID(String);

View File

@ -1,5 +1,5 @@
use crate::data::client::ClientID;
use crate::data::entity_manager::EntityManager;
use crate::data::service::ServiceID;
use crate::utils::err::Res;
pub type UserID = String;
@ -18,10 +18,17 @@ pub struct User {
/// None = all services
/// Some([]) = no service
pub authorized_services: Option<Vec<ServiceID>>,
pub authorized_services: Option<Vec<ClientID>>,
}
impl User {
pub fn can_access_app(&self, id: &ClientID) -> bool {
match &self.authorized_services {
None => true,
Some(c) => c.contains(id)
}
}
pub fn verify_password<P: AsRef<[u8]>>(&self, pass: P) -> bool {
verify_password(pass, &self.password)
}
@ -47,7 +54,7 @@ impl Default for User {
need_reset_password: false,
enabled: true,
admin: false,
authorized_services: None,
authorized_services: Some(Vec::new()),
}
}
}

View File

@ -119,6 +119,7 @@ async fn main() -> std::io::Result<()> {
.to(|| async { HttpResponse::Found().append_header(("Location", "/settings")).finish() }))
.route("/admin/clients", web::get().to(admin_controller::clients_route))
.route("/admin/users", web::get().to(admin_controller::users_route))
.route("/admin/create_user", web::get().to(admin_controller::create_user))
})
.bind(listen_address)?
.run()

View File

@ -0,0 +1,99 @@
{% extends "base_settings_page.html" %}
{% block content %}
<form method="post" target="/admin/users">
<!-- User ID -->
<div class="form-group">
<label class="form-label mt-4" for="userID">User ID</label>
<input class="form-control" id="userID" type="text" readonly=""
name="uid" value="{{ u.uid }}"/>
</div>
<!-- User name -->
<div class="form-group">
<label class="form-label mt-4" for="username">User name</label>
<input class="form-control" id="username" type="text"
name="username" value="{{ u.username }}" required/>
</div>
<!-- First name -->
<div class="form-group">
<label class="form-label mt-4" for="first_name">First name</label>
<input class="form-control" id="first_name" type="text"
name="first_name" value="{{ u.first_name }}"/>
</div>
<!-- Last name -->
<div class="form-group">
<label class="form-label mt-4" for="last_name">Last name</label>
<input class="form-control" id="last_name" type="text"
name="last_name" value="{{ u.last_name }}"/>
</div>
<!-- Email -->
<div class="form-group">
<label class="form-label mt-4" for="email">Email address</label>
<input class="form-control" id="email" type="email"
name="email" value="{{ u.email }}"/>
</div>
<div class="form-group mt-4">
<!-- Generate new password -->
<div class="form-check">
<input class="form-check-input" type="checkbox" name="gen_new_password" id="gen_new_password" {% if
u.password.is_empty() %} checked="" {% endif %}>
<label class="form-check-label" for="gen_new_password">
Generate a new temporary password
</label>
</div>
<!-- Enabled -->
<div class="form-check">
<input class="form-check-input" type="checkbox" name="enabled" id="enabled" {% if u.enabled %} checked="" {%
endif %}>
<label class="form-check-label" for="enabled">
Enabled
</label>
</div>
<!-- Admin -->
<div class="form-check">
<input class="form-check-input" type="checkbox" name="admin" id="admin" {% if u.admin %} checked="" {% endif
%}>
<label class="form-check-label" for="admin">
Grant admin privileges
</label>
</div>
</div>
<!-- Granted services -->
<fieldset class="form-group">
<legend class="mt-4">Granted services</legend>
<div class="form-check">
<label class="form-check-label">
<input type="radio" class="form-check-input" name="granted_services"
value="all_services" {% if u.authorized_services== None %} checked="" {% endif %}>
Grant all services
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<input type="radio" class="form-check-input" name="granted_services"
value="custom_services" {% if u.authorized_services !=None %} checked="checked" {% endif %}>
Manually specify allowed services
</label>
</div>
{% for c in clients %}
<div class="form-check">
<input class="form-check-input" type="checkbox" class="authorize_client" data-id="{{ c.id.0 }}"
{% if u.can_access_app(c.id) %} checked="" {% endif %}>
<label class="form-check-label" for="admin">
{{ c.name }}
</label>
</div>
{% endfor %}
</fieldset>
</form>
{% endblock content %}