Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add access level to user table #97

Merged
merged 2 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions migrations/20240411150555_init.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ CREATE TABLE IF NOT EXISTS studies(
date_modified TIMESTAMP with time zone NOT NULL
);

CREATE TYPE accesslevel AS ENUM ('organization_admin', 'system_admin', 'user');

CREATE TABLE IF NOT EXISTS users(
id TEXT PRIMARY KEY,
user_name TEXT NOT NULL UNIQUE,
Expand All @@ -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,
Expand Down
12 changes: 11 additions & 1 deletion src/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
OrganizationAdmin,
SystemAdmin,
User,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub struct UserInDb {
Expand All @@ -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<Utc>,
pub date_modified: DateTime<Utc>,
}
Expand All @@ -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(),
})
Expand Down
19 changes: 13 additions & 6 deletions src/services/user_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -96,19 +96,21 @@ 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,
first_name,
last_name,
email,
hashed_password,
organization_id,
active,
organization_id,
access_level AS "access_level: AccessLevel",
date_added,
date_modified
"#,
Expand All @@ -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,
)
Expand Down Expand Up @@ -171,8 +174,9 @@ pub async fn get_user_service(pool: &PgPool, user_id: &str) -> Result<Option<Use
last_name,
email,
hashed_password,
active,
organization_id,
active,
access_level AS "access_level: AccessLevel",
date_added,
date_modified
FROM users
Expand Down Expand Up @@ -272,8 +276,9 @@ pub async fn get_users_service(pool: &PgPool) -> Result<Vec<User>> {
last_name,
email,
hashed_password,
active,
organization_id,
active,
access_level AS "access_level: AccessLevel",
date_added,
date_modified
FROM users
Expand Down Expand Up @@ -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
"#,
Expand Down Expand Up @@ -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
"#,
Expand Down