From 5285f10ea6891d0fdd51892cb0d6f28fad05550d Mon Sep 17 00:00:00 2001 From: Paul Sanders Date: Sat, 13 Apr 2024 21:32:18 -0400 Subject: [PATCH 1/2] Add access level to user table --- migrations/20240411150555_init.up.sql | 4 ++++ src/models/user.rs | 12 +++++++++++- src/services/user_services.rs | 19 +++++++++++++------ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/migrations/20240411150555_init.up.sql b/migrations/20240411150555_init.up.sql index e7aa3c2..a07017b 100644 --- a/migrations/20240411150555_init.up.sql +++ b/migrations/20240411150555_init.up.sql @@ -16,6 +16,8 @@ CREATE TABLE IF NOT EXISTS studies( date_modified TIMESTAMP with time zone NOT NULL ); +CREATE TYPE accesslevel AS ENUM ('org_admin', 'system_admin', 'user'); + CREATE TABLE IF NOT EXISTS users( id TEXT PRIMARY KEY, user_name TEXT NOT NULL UNIQUE, @@ -25,11 +27,13 @@ CREATE TABLE IF NOT EXISTS users( hashed_password TEXT NOT NULL, organization_id TEXT REFERENCES organizations(id) ON DELETE CASCADE NOT NULL, active BOOLEAN NOT NULL, + access_level accesslevel NOT NULL, date_added TIMESTAMP with time zone NOT NULL, date_modified TIMESTAMP with time zone NOT NULL ); CREATE INDEX ON users(email); +CREATE INDEX ON users(access_level); CREATE TABLE IF NOT EXISTS user_studies( id TEXT PRIMARY KEY, diff --git a/src/models/user.rs b/src/models/user.rs index 5b8ff36..156629d 100644 --- a/src/models/user.rs +++ b/src/models/user.rs @@ -8,6 +8,14 @@ use crate::{ utils::{generate_db_id, hash_password}, }; +#[derive(Debug, Deserialize, Serialize, sqlx::Type)] +#[sqlx(rename_all = "snake_case")] +pub enum AccessLevel { + Organization, + System, + User, +} + #[derive(Debug, Deserialize, Serialize)] #[serde(rename_all = "snake_case")] pub struct UserInDb { @@ -19,6 +27,7 @@ pub struct UserInDb { pub hashed_password: String, pub organization_id: String, pub active: bool, + pub access_level: AccessLevel, pub date_added: DateTime, pub date_modified: DateTime, } @@ -40,8 +49,9 @@ impl UserInDb { last_name, email, hashed_password, - active: true, organization_id, + active: true, + access_level: AccessLevel::User, date_added: Utc::now(), date_modified: Utc::now(), }) diff --git a/src/services/user_services.rs b/src/services/user_services.rs index 947217c..d6e43e3 100644 --- a/src/services/user_services.rs +++ b/src/services/user_services.rs @@ -5,7 +5,7 @@ use sqlx::postgres::PgPool; use crate::{ models::{ study::{Study, StudyInDb}, - user::{User, UserCreate, UserInDb, UserUpdate}, + user::{AccessLevel, User, UserCreate, UserInDb, UserUpdate}, }, services::{ organization_services::get_organization_service, study_services::get_study_service, @@ -96,10 +96,11 @@ pub async fn create_user_service(pool: &PgPool, new_user: &UserCreate) -> Result hashed_password, organization_id, active, + access_level, date_added, date_modified ) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING id, user_name, @@ -107,8 +108,9 @@ pub async fn create_user_service(pool: &PgPool, new_user: &UserCreate) -> Result last_name, email, hashed_password, - organization_id, active, + organization_id, + access_level AS "access_level: AccessLevel", date_added, date_modified "#, @@ -120,6 +122,7 @@ pub async fn create_user_service(pool: &PgPool, new_user: &UserCreate) -> Result prepped_user.hashed_password, prepped_user.organization_id, prepped_user.active, + prepped_user.access_level as AccessLevel, prepped_user.date_added, prepped_user.date_modified, ) @@ -171,8 +174,9 @@ pub async fn get_user_service(pool: &PgPool, user_id: &str) -> Result Result> { last_name, email, hashed_password, - active, organization_id, + active, + access_level AS "access_level: AccessLevel", date_added, date_modified FROM users @@ -374,8 +379,9 @@ pub async fn update_user_service(pool: &PgPool, updated_user: &UserUpdate) -> Re last_name, email, hashed_password, - active, organization_id, + active, + access_level AS "access_level: AccessLevel", date_added, date_modified "#, @@ -414,6 +420,7 @@ pub async fn update_user_service(pool: &PgPool, updated_user: &UserUpdate) -> Re hashed_password, organization_id, active, + access_level AS "access_level: AccessLevel", date_added, date_modified "#, From 1bfb74ddea970652649b881f73ca9f6222ebdcee Mon Sep 17 00:00:00 2001 From: Paul Sanders Date: Sat, 13 Apr 2024 21:34:19 -0400 Subject: [PATCH 2/2] Fix emum types --- migrations/20240411150555_init.up.sql | 2 +- src/models/user.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/migrations/20240411150555_init.up.sql b/migrations/20240411150555_init.up.sql index a07017b..8a24658 100644 --- a/migrations/20240411150555_init.up.sql +++ b/migrations/20240411150555_init.up.sql @@ -16,7 +16,7 @@ CREATE TABLE IF NOT EXISTS studies( date_modified TIMESTAMP with time zone NOT NULL ); -CREATE TYPE accesslevel AS ENUM ('org_admin', 'system_admin', 'user'); +CREATE TYPE accesslevel AS ENUM ('organization_admin', 'system_admin', 'user'); CREATE TABLE IF NOT EXISTS users( id TEXT PRIMARY KEY, diff --git a/src/models/user.rs b/src/models/user.rs index 156629d..9ab09e1 100644 --- a/src/models/user.rs +++ b/src/models/user.rs @@ -11,8 +11,8 @@ use crate::{ #[derive(Debug, Deserialize, Serialize, sqlx::Type)] #[sqlx(rename_all = "snake_case")] pub enum AccessLevel { - Organization, - System, + OrganizationAdmin, + SystemAdmin, User, }