Skip to content

Commit

Permalink
Merge pull request #165 from nightly-labs/change-user-privileges
Browse files Browse the repository at this point in the history
Additional getters endpoints
  • Loading branch information
Giems authored Apr 9, 2024
2 parents e45360d + 0a4b3ba commit 9131b82
Show file tree
Hide file tree
Showing 16 changed files with 1,281 additions and 5 deletions.
3 changes: 1 addition & 2 deletions database/src/tables/user_app_privileges/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ impl Db {
let query = format!(
"SELECT uap.* FROM {USER_APP_PRIVILEGES_TABLE_NAME} uap
JOIN {REGISTERED_APPS_TABLE_NAME} ra ON uap.app_id = ra.app_id
WHERE ra.team_id = $1
GROUP BY uap.app_id, uap.user_id, uap.creation_timestamp, uap.privilege_level"
WHERE ra.team_id = $1"
);
let typed_query = sqlx::query_as::<_, UserAppPrivilege>(&query);

Expand Down
46 changes: 46 additions & 0 deletions database/src/tables/user_app_privileges/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,52 @@ impl Db {

Ok(())
}

pub async fn update_user_privilege(
&self,
tx: &mut Transaction<'_, sqlx::Postgres>,
user_id: &String,
app_id: &String,
new_privilege_level: PrivilegeLevel,
) -> Result<(), DbError> {
let query_body = format!(
"UPDATE {USER_APP_PRIVILEGES_TABLE_NAME} SET privilege_level = $1 WHERE user_id = $2 AND app_id = $3"
);

let query_result = query(&query_body)
.bind(&new_privilege_level)
.bind(user_id)
.bind(app_id)
.execute(&mut **tx)
.await;

match query_result {
Ok(_) => Ok(()),
Err(e) => Err(e).map_err(|e| e.into()),
}
}

pub async fn remove_user_privilege(
&self,
tx: &mut Transaction<'_, sqlx::Postgres>,
user_id: &String,
app_id: &String,
) -> Result<(), DbError> {
let query_body = format!(
"DELETE FROM {USER_APP_PRIVILEGES_TABLE_NAME} WHERE user_id = $1 AND app_id = $2"
);

let query_result = query(&query_body)
.bind(user_id)
.bind(app_id)
.execute(&mut **tx)
.await;

match query_result {
Ok(_) => Ok(()),
Err(e) => Err(e).map_err(|e| e.into()),
}
}
}

#[cfg(feature = "cloud_db_tests")]
Expand Down
39 changes: 38 additions & 1 deletion database/src/tables/users/select.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use super::table_struct::User;
use std::collections::HashMap;

use super::table_struct::{User, UserIdEmail};
use crate::db::Db;
use crate::structs::db_error::DbError;
use crate::tables::users::table_struct::USERS_TABLE_NAME;
Expand Down Expand Up @@ -26,4 +28,39 @@ impl Db {
.await
.map_err(|e| e.into());
}

pub async fn get_users_ids_by_emails(
&self,
emails: &Vec<String>,
) -> Result<HashMap<String, String>, DbError> {
// User email to user id
let query = format!("SELECT user_id, email FROM {USERS_TABLE_NAME} WHERE email = ANY($1)");
let typed_query = query_as::<_, UserIdEmail>(&query);

let data_vec = typed_query
.bind(&emails)
.fetch_all(&self.connection_pool)
.await
.map_err(|e| DbError::from(e))?;

return Ok(data_vec.into_iter().map(|x| (x.email, x.user_id)).collect());
}

pub async fn get_users_emails_by_ids(
&self,
user_ids: &Vec<String>,
) -> Result<HashMap<String, String>, DbError> {
// User id to user email
let query =
format!("SELECT user_id, email FROM {USERS_TABLE_NAME} WHERE user_id = ANY($1)");
let typed_query = query_as::<_, UserIdEmail>(&query);

let data_vec = typed_query
.bind(&user_ids)
.fetch_all(&self.connection_pool)
.await
.map_err(|e| DbError::from(e))?;

return Ok(data_vec.into_iter().map(|x| (x.user_id, x.email)).collect());
}
}
15 changes: 15 additions & 0 deletions database/src/tables/users/table_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,18 @@ impl FromRow<'_, PgRow> for User {
})
}
}

#[derive(Clone, Debug, PartialEq)]
pub struct UserIdEmail {
pub user_id: String,
pub email: String,
}

impl FromRow<'_, PgRow> for UserIdEmail {
fn from_row(row: &sqlx::postgres::PgRow) -> std::result::Result<Self, sqlx::Error> {
Ok(UserIdEmail {
user_id: row.get("user_id"),
email: row.get("email"),
})
}
}
2 changes: 0 additions & 2 deletions infra/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.9'

services:
timescaledb:
image: timescale/timescaledb-ha:pg16
Expand Down
Loading

0 comments on commit 9131b82

Please sign in to comment.