Change user role display to show names instead of ids
This commit is contained in:
parent
e7d0780b1b
commit
f73b5c16b9
|
|
@ -9,5 +9,4 @@ INSERT INTO "main"."role_permissions" ("id", "created_at", "created_by", "update
|
|||
INSERT INTO "main"."role_permissions" ("id", "created_at", "created_by", "updated_at", "updated_by", "role_id", "item") VALUES ('7', '0', '0', '0', '0', '4', '/users');
|
||||
|
||||
-- First user is an admin
|
||||
INSERT INTO "main"."user_roles" ("id", "created_at", "created_by", "updated_at", "updated_by", "user_id", "role_id") VALUES ('1', '1728247301', '0', '1728247301', '0', '1', '1');
|
||||
INSERT INTO "main"."user_roles" ("id", "created_at", "created_by", "updated_at", "updated_by", "user_id", "role_id") VALUES ('2', '0', '0', '0', '0', '1', '4');
|
||||
|
|
|
|||
|
|
@ -37,10 +37,10 @@ async fn main() {
|
|||
|
||||
static MIGRATOR: Migrator = sqlx::migrate!();
|
||||
|
||||
/* MIGRATOR
|
||||
MIGRATOR
|
||||
.run(&app_state.db_pool)
|
||||
.await
|
||||
.expect("Failed to run migrations"); */
|
||||
.expect("Failed to run migrations");
|
||||
|
||||
let user_data: Option<UserData> = None;
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use axum::{extract::{Path, State}, response::{Html, IntoResponse, Redirect}, Ext
|
|||
use http::StatusCode;
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use crate::{middlewares::is_authorized, user::get_user_roles, UserData};
|
||||
use crate::{middlewares::is_authorized, user::{get_user_roles, get_user_roles_display}, UserData};
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "profile.html")]
|
||||
|
|
@ -19,7 +19,7 @@ struct UserProfileTemplate {
|
|||
logged_in: bool,
|
||||
name: String,
|
||||
user: UserData,
|
||||
user_roles: Vec<crate::user::UserRoles>
|
||||
user_roles: Vec<crate::user::UserRolesDisplay>
|
||||
}
|
||||
|
||||
struct HtmlTemplate<T>(T);
|
||||
|
|
@ -134,7 +134,7 @@ pub async fn profile(
|
|||
|
||||
if is_authorized("/users", user_data, db_pool.clone()).await {
|
||||
// Get user roles
|
||||
let user_roles = get_user_roles(user_id, &db_pool.clone()).await;
|
||||
let user_roles = get_user_roles_display(user_id, &db_pool.clone()).await;
|
||||
|
||||
// Create the profile template.
|
||||
let template = UserProfileTemplate { logged_in, name, user: user, user_roles };
|
||||
|
|
|
|||
|
|
@ -37,6 +37,16 @@ pub struct UserRoles {
|
|||
pub role_id: i64,
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Debug, FromRow, Serialize, Deserialize)]
|
||||
pub struct UserRolesDisplay {
|
||||
pub id: i64,
|
||||
pub created_at: i64,
|
||||
pub created_by: i64,
|
||||
pub updated_at: i64,
|
||||
pub updated_by: i64,
|
||||
pub role: String,
|
||||
}
|
||||
|
||||
pub async fn get_user_roles(user_id: i64, db_pool: &SqlitePool) -> Vec<UserRoles> {
|
||||
// Get user roles
|
||||
let user_roles = sqlx::query_as(
|
||||
|
|
@ -47,5 +57,18 @@ pub async fn get_user_roles(user_id: i64, db_pool: &SqlitePool) -> Vec<UserRoles
|
|||
.await
|
||||
.unwrap();
|
||||
|
||||
user_roles
|
||||
}
|
||||
|
||||
pub async fn get_user_roles_display(user_id: i64, db_pool: &SqlitePool) -> Vec<UserRolesDisplay> {
|
||||
// Get user roles
|
||||
let user_roles = sqlx::query_as(
|
||||
r#"select r.id, r.name as role, r.created_at, r.created_by, r.updated_at, r.updated_by from roles r join user_roles ur on ur.role_id = r.id WHERE ur.user_id = ?"#
|
||||
)
|
||||
.bind(user_id)
|
||||
.fetch_all(db_pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
user_roles
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
{% extends "authorized.html" %}
|
||||
{% block title %}User Profile{% endblock %}
|
||||
{% block content %}
|
||||
{% block center %}
|
||||
<h1>User Profile</h1>
|
||||
Full name: {{ user.name }}<br/>
|
||||
Given name: {{ user.given_name }}<br/>
|
||||
|
|
@ -19,9 +19,9 @@ Your email address: {{ user.email }}<br/>
|
|||
{% for user_role in user_roles %}
|
||||
<tr>
|
||||
<td><a href="/roles/{{ user_role.id }}">{{ user_role.id }}</a></td>
|
||||
<td>{{ user_role.role_id }}</td>
|
||||
<td>{{ user_role.role }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% endblock %}
|
||||
{% endblock center %}
|
||||
|
|
|
|||
Loading…
Reference in New Issue