Skip to content

Commit d74816b

Browse files
code rabbit suggestions
1 parent 2d8c655 commit d74816b

File tree

7 files changed

+39
-17
lines changed

7 files changed

+39
-17
lines changed

src/handlers/http/modal/ingest/ingestor_rbac.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub async fn post_user(
5858
// Handler for DELETE /api/v1/user/delete/{username}
5959
pub async fn delete_user(username: web::Path<String>) -> Result<impl Responder, RBACError> {
6060
let username = username.into_inner();
61-
let _ = UPDATE_LOCK.lock().await;
61+
let _guard = UPDATE_LOCK.lock().await;
6262
// fail this request if the user does not exists
6363
if !Users.contains(&username) {
6464
return Err(RBACError::UserDoesNotExist);

src/handlers/http/modal/ingest/ingestor_role.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub async fn put(
5050
let mut session_refresh_users: HashSet<String> = HashSet::new();
5151
for user_group in read_user_groups().values() {
5252
if user_group.roles.contains(&name) {
53-
session_refresh_users.extend(user_group.get_usernames());
53+
session_refresh_users.extend(user_group.users.iter().map(|u| u.userid().to_string()));
5454
}
5555
}
5656

src/handlers/http/modal/query/querier_rbac.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ use crate::{
3232
},
3333
rbac::{
3434
Users,
35-
map::{roles, write_user_groups},
36-
user,
35+
map::{roles, users, write_user_groups},
36+
user::{self, UserType},
3737
},
3838
validator,
3939
};
@@ -74,7 +74,7 @@ pub async fn post_user(
7474
return Err(RBACError::RolesDoNotExist(non_existant_roles));
7575
}
7676
}
77-
let _ = UPDATE_LOCK.lock().await;
77+
let _guard = UPDATE_LOCK.lock().await;
7878
if Users.contains(&username)
7979
|| metadata
8080
.users
@@ -106,7 +106,7 @@ pub async fn post_user(
106106
// Handler for DELETE /api/v1/user/{username}
107107
pub async fn delete_user(username: web::Path<String>) -> Result<impl Responder, RBACError> {
108108
let username = username.into_inner();
109-
let _ = UPDATE_LOCK.lock().await;
109+
let _guard = UPDATE_LOCK.lock().await;
110110
// fail this request if the user does not exists
111111
if !Users.contains(&username) {
112112
return Err(RBACError::UserDoesNotExist);
@@ -120,11 +120,21 @@ pub async fn delete_user(username: web::Path<String>) -> Result<impl Responder,
120120
let mut groups_to_update = Vec::new();
121121
for user_group in user_groups {
122122
if let Some(ug) = write_user_groups().get_mut(&user_group) {
123-
ug.remove_users_by_user_ids(HashSet::from_iter([username.clone()]))?;
124-
groups_to_update.push(ug.clone());
123+
// Look up the user by display name to get the correct userid
124+
if let Some(user) = users().get(&username) {
125+
let user_id = match &user.ty {
126+
UserType::Native(basic) => basic.username.clone(),
127+
UserType::OAuth(oauth) => oauth.userid.clone(),
128+
};
129+
ug.remove_users_by_user_ids(HashSet::from_iter([user_id]))?;
130+
groups_to_update.push(ug.clone());
131+
} else {
132+
// User not found, skip or log as needed
133+
continue;
134+
}
125135
} else {
126136
continue;
127-
};
137+
}
128138
}
129139

130140
// For each updated group, replace in place if found; otherwise push
@@ -134,7 +144,7 @@ pub async fn delete_user(username: web::Path<String>) -> Result<impl Responder,
134144
.iter_mut()
135145
.find(|ug| ug.name == updated_group.name)
136146
{
137-
*existing = updated_group.clone();
147+
existing.clone_from(updated_group);
138148
} else {
139149
metadata.user_groups.push(updated_group.clone());
140150
}
@@ -262,7 +272,7 @@ pub async fn post_gen_password(username: web::Path<String>) -> Result<impl Respo
262272
let mut new_hash = String::default();
263273
let mut metadata = get_metadata().await?;
264274

265-
let _ = UPDATE_LOCK.lock().await;
275+
let _guard = UPDATE_LOCK.lock().await;
266276
let user::PassCode { password, hash } = user::Basic::gen_new_password();
267277
new_password.clone_from(&password);
268278
new_hash.clone_from(&hash);

src/handlers/http/modal/query/querier_role.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub async fn put(
5353
let mut session_refresh_users: HashSet<String> = HashSet::new();
5454
for user_group in read_user_groups().values() {
5555
if user_group.roles.contains(&name) {
56-
session_refresh_users.extend(user_group.get_usernames());
56+
session_refresh_users.extend(user_group.users.iter().map(|u| u.userid().to_string()));
5757
}
5858
}
5959

src/handlers/http/rbac.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub async fn post_user(
124124
return Err(RBACError::RolesDoNotExist(non_existent_roles));
125125
}
126126
}
127-
let _ = UPDATE_LOCK.lock().await;
127+
let _guard = UPDATE_LOCK.lock().await;
128128
if Users.contains(&username)
129129
|| metadata
130130
.users
@@ -159,7 +159,7 @@ pub async fn post_gen_password(username: web::Path<String>) -> Result<impl Respo
159159
let mut new_hash = String::default();
160160
let mut metadata = get_metadata().await?;
161161

162-
let _ = UPDATE_LOCK.lock().await;
162+
let _guard = UPDATE_LOCK.lock().await;
163163
let user::PassCode { password, hash } = user::Basic::gen_new_password();
164164
new_password.clone_from(&password);
165165
new_hash.clone_from(&hash);
@@ -235,7 +235,7 @@ pub async fn delete_user(username: web::Path<String>) -> Result<impl Responder,
235235
if !Users.contains(&username) {
236236
return Err(RBACError::UserDoesNotExist);
237237
};
238-
let _ = UPDATE_LOCK.lock().await;
238+
let _guard = UPDATE_LOCK.lock().await;
239239

240240
// delete from parseable.json first
241241
let mut metadata = get_metadata().await?;

src/handlers/http/role.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub async fn put(
5252
let mut session_refresh_users: HashSet<String> = HashSet::new();
5353
for user_group in read_user_groups().values() {
5454
if user_group.roles.contains(&name) {
55-
session_refresh_users.extend(user_group.get_usernames());
55+
session_refresh_users.extend(user_group.users.iter().map(|u| u.userid().to_string()));
5656
}
5757
}
5858

src/rbac/user.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,25 @@ impl From<openid::Userinfo> for UserInfo {
204204
}
205205

206206
/// Represents a user in a UserGroup - simplified structure for both user types
207-
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
207+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
208208
pub struct GroupUser {
209209
pub userid: String,
210210
pub username: String,
211211
pub method: String,
212212
}
213213

214+
impl PartialEq for GroupUser {
215+
fn eq(&self, other: &Self) -> bool {
216+
self.userid == other.userid
217+
}
218+
}
219+
impl Eq for GroupUser {}
220+
impl std::hash::Hash for GroupUser {
221+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
222+
self.userid.hash(state)
223+
}
224+
}
225+
214226
impl GroupUser {
215227
pub fn username(&self) -> &str {
216228
&self.username

0 commit comments

Comments
 (0)