Skip to content

Commit f334e88

Browse files
committed
bugfixes
- user and role deletion while still being used - renamed resource types
1 parent 601b6cf commit f334e88

File tree

3 files changed

+23
-37
lines changed

3 files changed

+23
-37
lines changed

src/handlers/http/rbac.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,19 @@ pub async fn get_role(username: web::Path<String>) -> Result<impl Responder, RBA
228228
// Handler for DELETE /api/v1/user/delete/{username}
229229
pub async fn delete_user(username: web::Path<String>) -> Result<impl Responder, RBACError> {
230230
let username = username.into_inner();
231-
let _ = UPDATE_LOCK.lock().await;
231+
232+
// if user is a part of any groups then don't allow deletion
233+
if !Users.get_user_groups(&username).is_empty() {
234+
return Err(RBACError::InvalidDeletionRequest(format!(
235+
"User: {username} should not be a part of any groups"
236+
)));
237+
}
232238
// fail this request if the user does not exists
233239
if !Users.contains(&username) {
234240
return Err(RBACError::UserDoesNotExist);
235241
};
242+
let _ = UPDATE_LOCK.lock().await;
243+
236244
// delete from parseable.json first
237245
let mut metadata = get_metadata().await?;
238246
metadata.users.retain(|user| user.username() != username);
@@ -408,6 +416,8 @@ pub enum RBACError {
408416
UserGroupNotEmpty(String),
409417
#[error("Resource in use: {0}")]
410418
ResourceInUse(String),
419+
#[error("{0}")]
420+
InvalidDeletionRequest(String),
411421
}
412422

413423
impl actix_web::ResponseError for RBACError {
@@ -429,6 +439,7 @@ impl actix_web::ResponseError for RBACError {
429439
Self::InvalidSyncOperation(_) => StatusCode::BAD_REQUEST,
430440
Self::UserGroupNotEmpty(_) => StatusCode::BAD_REQUEST,
431441
Self::ResourceInUse(_) => StatusCode::BAD_REQUEST,
442+
Self::InvalidDeletionRequest(_) => StatusCode::BAD_REQUEST,
432443
}
433444
}
434445

src/handlers/http/role.rs

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ use actix_web::{
2222
HttpResponse, Responder,
2323
};
2424
use http::StatusCode;
25-
use itertools::Itertools;
2625

2726
use crate::{
2827
parseable::PARSEABLE,
2928
rbac::{
30-
map::{mut_roles, read_user_groups, write_user_groups, DEFAULT_ROLE},
29+
map::{mut_roles, DEFAULT_ROLE},
3130
role::model::DefaultPrivilege,
3231
},
3332
storage::{self, ObjectStorageError, StorageMetadata},
@@ -78,46 +77,22 @@ pub async fn list_roles() -> Result<impl Responder, RoleError> {
7877
// Delete existing role
7978
pub async fn delete(name: web::Path<String>) -> Result<impl Responder, RoleError> {
8079
let name = name.into_inner();
80+
// check if the role is being used by any user or group
8181
let mut metadata = get_metadata().await?;
8282
if metadata.users.iter().any(|user| user.roles.contains(&name)) {
8383
return Err(RoleError::RoleInUse);
8484
}
85+
if metadata
86+
.user_groups
87+
.iter()
88+
.any(|user_group| user_group.roles.contains(&name))
89+
{
90+
return Err(RoleError::RoleInUse);
91+
}
8592
metadata.roles.remove(&name);
8693
put_metadata(&metadata).await?;
8794
mut_roles().remove(&name);
8895

89-
// also delete from user groups
90-
let groups = read_user_groups().keys().cloned().collect_vec();
91-
let mut group_names = Vec::new();
92-
93-
for user_group in groups {
94-
if let Some(ug) = read_user_groups().get(&user_group) {
95-
if ug.roles.contains(&name) {
96-
return Err(RoleError::RoleInUse);
97-
}
98-
group_names.push(ug.name.clone());
99-
} else {
100-
continue;
101-
};
102-
}
103-
104-
// remove role from all user groups that have it
105-
let mut groups_to_update = Vec::new();
106-
for group in write_user_groups().values_mut() {
107-
if group.roles.remove(&name) {
108-
groups_to_update.push(group.clone());
109-
}
110-
}
111-
112-
// update metadata only if there are changes
113-
if !groups_to_update.is_empty() {
114-
metadata
115-
.user_groups
116-
.retain(|x| !groups_to_update.contains(x));
117-
metadata.user_groups.extend(groups_to_update);
118-
}
119-
put_metadata(&metadata).await?;
120-
12196
Ok(HttpResponse::Ok().finish())
12297
}
12398

src/rbac/role.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ pub enum Action {
7979

8080
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
8181
pub enum ParseableResourceType {
82-
#[serde(rename = "stream")]
82+
#[serde(rename = "dataset")]
8383
Stream(String),
84-
#[serde(rename = "llm")]
84+
#[serde(rename = "llmKey")]
8585
Llm(String),
8686
#[serde(rename = "all")]
8787
All,

0 commit comments

Comments
 (0)