diff --git a/backend/migrations/20241007024816_initial_data.up.sql b/backend/migrations/20241007024816_initial_data.up.sql index f881b88..9632bfc 100644 --- a/backend/migrations/20241007024816_initial_data.up.sql +++ b/backend/migrations/20241007024816_initial_data.up.sql @@ -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'); diff --git a/backend/src/main.rs b/backend/src/main.rs index 09516ac..bda3347 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -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 = None; diff --git a/backend/src/routes.rs b/backend/src/routes.rs index a52a719..c1a513a 100644 --- a/backend/src/routes.rs +++ b/backend/src/routes.rs @@ -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 + user_roles: Vec } struct HtmlTemplate(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 }; diff --git a/backend/src/user.rs b/backend/src/user.rs index b915afa..9da1c6f 100644 --- a/backend/src/user.rs +++ b/backend/src/user.rs @@ -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 { // 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 Vec { + // 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 } \ No newline at end of file diff --git a/backend/templates/user.html b/backend/templates/user.html index 4379b3e..4243b52 100644 --- a/backend/templates/user.html +++ b/backend/templates/user.html @@ -1,6 +1,6 @@ {% extends "authorized.html" %} {% block title %}User Profile{% endblock %} -{% block content %} +{% block center %}

User Profile

Full name: {{ user.name }}
Given name: {{ user.given_name }}
@@ -19,9 +19,9 @@ Your email address: {{ user.email }}
{% for user_role in user_roles %} {{ user_role.id }} - {{ user_role.role_id }} + {{ user_role.role }} {% endfor %} -{% endblock %} +{% endblock center %}