admin: added list of users

This commit is contained in:
Philippe Loctaux 2023-04-16 22:00:29 +02:00
parent e3bda01eca
commit 5153d057b0
13 changed files with 313 additions and 3 deletions

View file

@ -40,7 +40,8 @@ impl Icon {
"id-badge-2", IdBadge2, r#"<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-id-badge-2" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M7 12h3v4h-3z"></path><path d="M10 6h-6a1 1 0 0 0 -1 1v12a1 1 0 0 0 1 1h16a1 1 0 0 0 1 -1v-12a1 1 0 0 0 -1 -1h-6"></path><path d="M10 3m0 1a1 1 0 0 1 1 -1h2a1 1 0 0 1 1 1v3a1 1 0 0 1 -1 1h-2a1 1 0 0 1 -1 -1z"></path><path d="M14 16h2"></path><path d="M14 12h4"></path></svg>"#,
"user", User, r#"<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-user" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M8 7a4 4 0 1 0 8 0a4 4 0 0 0 -8 0"></path><path d="M6 21v-2a4 4 0 0 1 4 -4h4a4 4 0 0 1 4 4v2"></path></svg>"#,
"at", At, r#"<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-at" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M12 12m-4 0a4 4 0 1 0 8 0a4 4 0 1 0 -8 0"></path><path d="M16 12v1.5a2.5 2.5 0 0 0 5 0v-1.5a9 9 0 1 0 -5.5 8.28"></path></svg>"#,
"paperclip-large", PaperclipLarge, r#"<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-lg icon-tabler icon-tabler-paperclip" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M15 7l-6.5 6.5a1.5 1.5 0 0 0 3 3l6.5 -6.5a3 3 0 0 0 -6 -6l-6.5 6.5a4.5 4.5 0 0 0 9 9l6.5 -6.5"></path></svg>"#
"paperclip-large", PaperclipLarge, r#"<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-lg icon-tabler icon-tabler-paperclip" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M15 7l-6.5 6.5a1.5 1.5 0 0 0 3 3l6.5 -6.5a3 3 0 0 0 -6 -6l-6.5 6.5a4.5 4.5 0 0 0 9 9l6.5 -6.5"></path></svg>"#,
"users", Users, r#"<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-users" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M9 7m-4 0a4 4 0 1 0 8 0a4 4 0 1 0 -8 0"></path><path d="M3 21v-2a4 4 0 0 1 4 -4h4a4 4 0 0 1 4 4v2"></path><path d="M16 3.13a4 4 0 0 1 0 7.75"></path><path d="M21 21v-2a4 4 0 0 0 -3 -3.85"></path></svg>"#
}
}
@ -62,6 +63,7 @@ pub fn icons_to_templates(tera: &mut Tera) {
Icon::User,
Icon::At,
Icon::PaperclipLarge,
Icon::Users,
];
// For each icon, it will output: ("icons/name", "<svg>...</svg>")

View file

@ -7,6 +7,7 @@ pub enum AdminMenu {
Exit,
Dashboard,
Apps,
Users,
Settings,
}
@ -16,6 +17,7 @@ impl AdminMenu {
AdminMenu::Exit => "exit",
AdminMenu::Dashboard => "dashboard",
AdminMenu::Apps => "apps",
AdminMenu::Users => "users",
AdminMenu::Settings => "settings",
}
}
@ -42,6 +44,13 @@ impl AdminMenu {
icon: Icon::Apps.svg,
sub: None,
},
MainItem {
id: AdminMenu::Users.id(),
label: "Users",
link: uri!(routes::admin::users::admin_users_list).to_string(),
icon: Icon::Users.svg,
sub: None,
},
MainItem {
id: AdminMenu::Settings.id(),
label: "Server settings",

View file

@ -24,6 +24,7 @@ pub enum Page {
UserPersonalSettings(UserPersonalSettings),
UserSecuritySettings(UserSecuritySettings),
UserVisualSettings(UserVisualSettings),
AdminUsersList(AdminUsersList),
}
impl Page {
@ -44,6 +45,7 @@ impl Page {
Page::UserPersonalSettings(_) => "pages/settings/personal",
Page::UserSecuritySettings(_) => "pages/settings/security",
Page::UserVisualSettings(_) => "pages/settings/visual",
Page::AdminUsersList(_) => "pages/admin/users/list",
}
}
@ -64,6 +66,7 @@ impl Page {
Page::UserPersonalSettings(_) => "Personal settings",
Page::UserSecuritySettings(_) => "Security settings",
Page::UserVisualSettings(_) => "Visual settings",
Page::AdminUsersList(_) => "Users",
}
}
@ -86,6 +89,7 @@ impl Page {
Page::UserPersonalSettings(_) => Some(UserMenu::Settings.into()),
Page::UserSecuritySettings(_) => Some(UserMenu::Settings.into()),
Page::UserVisualSettings(_) => Some(UserMenu::Settings.into()),
Page::AdminUsersList(_) => Some(AdminMenu::Users.into()),
}
}
@ -106,6 +110,7 @@ impl Page {
Page::UserPersonalSettings(personal) => Box::new(personal),
Page::UserSecuritySettings(security) => Box::new(security),
Page::UserVisualSettings(visual) => Box::new(visual),
Page::AdminUsersList(list) => Box::new(list),
}
}
}

View file

@ -1,11 +1,13 @@
use self::apps::*;
use self::settings::*;
use self::users::*;
use dashboard::*;
use rocket::{routes, Route};
pub mod apps;
pub mod dashboard;
pub mod settings;
pub mod users;
pub fn routes() -> Vec<Route> {
routes![
@ -21,6 +23,7 @@ pub fn routes() -> Vec<Route> {
admin_apps_view_form,
admin_apps_new_secret,
admin_apps_archive,
admin_users_list,
]
}
@ -28,6 +31,7 @@ pub mod content {
use apps::App;
use jwt::JwtClaims;
use rocket::serde::Serialize;
use users::User;
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
@ -74,4 +78,12 @@ pub mod content {
pub user: JwtClaims,
pub app: App,
}
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
#[derive(Clone)]
pub struct AdminUsersList {
pub user: JwtClaims,
pub users: Vec<User>,
}
}

View file

@ -0,0 +1,21 @@
use crate::routes::prelude::*;
use rocket::get;
use users::User;
#[get("/admin/users")]
pub async fn admin_users_list(
mut db: Connection<Database>,
admin: JwtAdmin,
flash: Option<FlashMessage<'_>>,
) -> Result<Template> {
let users = User::get_all(&mut *db).await?;
let page = Page::AdminUsersList(super::content::AdminUsersList {
user: admin.0,
users,
});
Ok(flash
.map(|flash| Page::with_flash(page.clone(), flash))
.unwrap_or_else(|| page.into()))
}

View file

@ -0,0 +1,144 @@
{% extends "shell" %}
{% block content %}
{% set new_user_label = "New user" %}
{% set new_user_link = "users/new" %}
<div class="page-header d-print-none">
<div class="container-xl">
<div class="row align-items-center">
<div class="col">
<div class="page-pretitle">
Admin dashboard
</div>
<h2 class="page-title">
Users
</h2>
</div>
<div class="col-auto ms-auto">
<div class="btn-list">
<a href="{{ new_user_link }}" class="btn btn-primary d-none d-sm-inline-block">
{% include "icons/plus" %}
{{ new_user_label }}
</a>
<a href="{{ new_user_link }}" class="btn btn-primary d-sm-none btn-icon"
aria-label="{{ new_user_label }}">
{% include "icons/plus" %}
</a>
</div>
</div>
</div>
</div>
</div>
<!-- Page body -->
<div class="page-body">
<div class="container-xl">
{% if flash %}
<div class="alert alert-{{flash.0}}" role="alert">
<h4 class="alert-title">{{ flash.1 }}</h4>
</div>
{% endif %}
<div class="card">
<div id="table-default" class="table-responsive">
<table class="table table-hover table-vcenter">
<!-- Table header -->
<thead>
<tr>
<th>
<button class="table-sort" data-sort="sort-name">Name</button>
</th>
<th>
<button class="table-sort" data-sort="sort-status">Status</button>
</th>
<th>
<button class="table-sort" data-sort="sort-email">Email</button>
</th>
<th>
<button class="table-sort" data-sort="sort-creation-date">Creation Date</button>
</th>
<th>Security</th>
<th class="w-1">Actions</th>
</tr>
</thead>
<!-- Table content -->
<tbody class="table-tbody">
{% for user_loop in users %}
<tr>
<td class="sort-name text-nowrap">
{{ user::avatar(username=user_loop.username, name=user_loop.name, size="xs", css="me-1") }}
<span>
{% if user_loop.name %}
{{ user_loop.name }}
{% else %}
{{ user_loop.username }}
{% endif %}
</span>
</td>
<td class="sort-status text-nowrap">
{% if user_loop.is_archived == true %}
<span class="badge bg-danger me-1"></span> Archived
{% else %}
<span class="badge bg-success me-1"></span> Active
{% endif %}
</td>
<td class="sort-email">
{% if user_loop.email %}
{{ user_loop.email }}
{% else %}
<span class="text-muted">N/A</span>
{% endif %}
</td>
<td class="sort-creation-date" data-date='{{ user_loop.created_at | date(format="%s") }}'>
{{ user_loop.created_at | date(format="%F %T", timezone=user.zoneinfo | default(value="UTC")) }}
</td>
<td class="text-nowrap">
{% if user_loop.password %}
{% if user_loop.paper_key %}
<span class="badge bg-success me-1"></span> Good
{% else %}
<span class="badge bg-warning me-1"></span> No paper key
{% endif %}
{% else %}
<span class="badge bg-danger me-1"></span> No password
{% endif %}
</td>
<td>
{% if user_loop.id != user.sub %}
<a href="users/{{ user_loop.id }}">Details</a>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
{% endblock content %}
{% block libs_js %}
<script src="/libs/list.js/list.min.js" defer></script>
{% endblock lib_js %}
{% block additional_js %}
<script>
document.addEventListener("DOMContentLoaded", function () {
const list = new List('table-default', {
sortClass: 'table-sort',
listClass: 'table-tbody',
valueNames: ['sort-name', 'sort-status', 'sort-email',
{attr: 'data-date', name: 'sort-creation-date'},
]
});
})
</script>
{% endblock additional_js %}