18
18
19
19
use std:: collections:: HashSet ;
20
20
21
- use actix_web:: { Responder , web} ;
22
- use tokio :: sync :: Mutex ;
21
+ use actix_web:: { HttpResponse , web} ;
22
+ use http :: StatusCode ;
23
23
24
24
use crate :: {
25
- handlers:: http:: { modal:: utils:: rbac_utils:: get_metadata, rbac:: RBACError } ,
25
+ handlers:: http:: {
26
+ modal:: utils:: rbac_utils:: get_metadata,
27
+ rbac:: { RBACError , UPDATE_LOCK } ,
28
+ } ,
26
29
rbac:: {
27
30
Users ,
28
31
map:: roles,
@@ -31,18 +34,14 @@ use crate::{
31
34
storage,
32
35
} ;
33
36
34
- // async aware lock for updating storage metadata and user map atomicically
35
- static UPDATE_LOCK : Mutex < ( ) > = Mutex :: const_new ( ( ) ) ;
36
-
37
37
// Handler for POST /api/v1/user/{username}
38
38
// Creates a new user by username if it does not exists
39
39
pub async fn post_user (
40
40
username : web:: Path < String > ,
41
41
body : Option < web:: Json < serde_json:: Value > > ,
42
- ) -> Result < impl Responder , RBACError > {
42
+ ) -> Result < HttpResponse , RBACError > {
43
43
let username = username. into_inner ( ) ;
44
44
45
- let generated_password = String :: default ( ) ;
46
45
let metadata = get_metadata ( ) . await ?;
47
46
if let Some ( body) = body {
48
47
let user: ParseableUser = serde_json:: from_value ( body. into_inner ( ) ) ?;
@@ -52,37 +51,38 @@ pub async fn post_user(
52
51
Users . add_roles ( & username, created_role. clone ( ) ) ;
53
52
}
54
53
55
- Ok ( generated_password )
54
+ Ok ( HttpResponse :: Ok ( ) . status ( StatusCode :: OK ) . finish ( ) )
56
55
}
57
56
58
- // Handler for DELETE /api/v1/user/delete/{username }
59
- pub async fn delete_user ( username : web:: Path < String > ) -> Result < impl Responder , RBACError > {
60
- let username = username . into_inner ( ) ;
61
- let _ = UPDATE_LOCK . lock ( ) . await ;
57
+ // Handler for DELETE /api/v1/user/delete/{userid }
58
+ pub async fn delete_user ( userid : web:: Path < String > ) -> Result < HttpResponse , RBACError > {
59
+ let userid = userid . into_inner ( ) ;
60
+ let _guard = UPDATE_LOCK . lock ( ) . await ;
62
61
// fail this request if the user does not exists
63
- if !Users . contains ( & username ) {
62
+ if !Users . contains ( & userid ) {
64
63
return Err ( RBACError :: UserDoesNotExist ) ;
65
64
} ;
65
+
66
66
// delete from parseable.json first
67
67
let mut metadata = get_metadata ( ) . await ?;
68
- metadata. users . retain ( |user| user. username ( ) != username ) ;
68
+ metadata. users . retain ( |user| user. userid ( ) != userid ) ;
69
69
70
70
let _ = storage:: put_staging_metadata ( & metadata) ;
71
71
72
72
// update in mem table
73
- Users . delete_user ( & username ) ;
74
- Ok ( format ! ( "deleted user: {username}" ) )
73
+ Users . delete_user ( & userid ) ;
74
+ Ok ( HttpResponse :: Ok ( ) . status ( StatusCode :: OK ) . finish ( ) )
75
75
}
76
76
77
- // Handler PATCH /user/{username }/role/sync/add => Add roles to a user
77
+ // Handler PATCH /user/{userid }/role/sync/add => Add roles to a user
78
78
pub async fn add_roles_to_user (
79
- username : web:: Path < String > ,
79
+ userid : web:: Path < String > ,
80
80
roles_to_add : web:: Json < HashSet < String > > ,
81
- ) -> Result < String , RBACError > {
82
- let username = username . into_inner ( ) ;
81
+ ) -> Result < HttpResponse , RBACError > {
82
+ let userid = userid . into_inner ( ) ;
83
83
let roles_to_add = roles_to_add. into_inner ( ) ;
84
84
85
- if !Users . contains ( & username ) {
85
+ if !Users . contains ( & userid ) {
86
86
return Err ( RBACError :: UserDoesNotExist ) ;
87
87
} ;
88
88
@@ -103,7 +103,7 @@ pub async fn add_roles_to_user(
103
103
if let Some ( user) = metadata
104
104
. users
105
105
. iter_mut ( )
106
- . find ( |user| user. username ( ) == username )
106
+ . find ( |user| user. userid ( ) == userid )
107
107
{
108
108
user. roles . extend ( roles_to_add. clone ( ) ) ;
109
109
} else {
@@ -113,20 +113,19 @@ pub async fn add_roles_to_user(
113
113
114
114
let _ = storage:: put_staging_metadata ( & metadata) ;
115
115
// update in mem table
116
- Users . add_roles ( & username. clone ( ) , roles_to_add. clone ( ) ) ;
117
-
118
- Ok ( format ! ( "Roles updated successfully for {username}" ) )
116
+ Users . add_roles ( & userid. clone ( ) , roles_to_add. clone ( ) ) ;
117
+ Ok ( HttpResponse :: Ok ( ) . status ( StatusCode :: OK ) . finish ( ) )
119
118
}
120
119
121
- // Handler PATCH /user/{username }/role/sync/add => Add roles to a user
120
+ // Handler PATCH /user/{userid }/role/sync/remove => Remove roles to a user
122
121
pub async fn remove_roles_from_user (
123
- username : web:: Path < String > ,
122
+ userid : web:: Path < String > ,
124
123
roles_to_remove : web:: Json < HashSet < String > > ,
125
- ) -> Result < String , RBACError > {
126
- let username = username . into_inner ( ) ;
124
+ ) -> Result < HttpResponse , RBACError > {
125
+ let userid = userid . into_inner ( ) ;
127
126
let roles_to_remove = roles_to_remove. into_inner ( ) ;
128
127
129
- if !Users . contains ( & username ) {
128
+ if !Users . contains ( & userid ) {
130
129
return Err ( RBACError :: UserDoesNotExist ) ;
131
130
} ;
132
131
@@ -143,7 +142,7 @@ pub async fn remove_roles_from_user(
143
142
}
144
143
145
144
// check that user actually has these roles
146
- let user_roles: HashSet < String > = HashSet :: from_iter ( Users . get_role ( & username ) ) ;
145
+ let user_roles: HashSet < String > = HashSet :: from_iter ( Users . get_role ( & userid ) ) ;
147
146
let roles_not_with_user: HashSet < String > =
148
147
HashSet :: from_iter ( roles_to_remove. difference ( & user_roles) . cloned ( ) ) ;
149
148
@@ -153,12 +152,12 @@ pub async fn remove_roles_from_user(
153
152
) ) ) ;
154
153
}
155
154
156
- // update parseable.json first
155
+ // update parseable.json in staging first
157
156
let mut metadata = get_metadata ( ) . await ?;
158
157
if let Some ( user) = metadata
159
158
. users
160
159
. iter_mut ( )
161
- . find ( |user| user. username ( ) == username )
160
+ . find ( |user| user. userid ( ) == userid )
162
161
{
163
162
let diff: HashSet < String > =
164
163
HashSet :: from_iter ( user. roles . difference ( & roles_to_remove) . cloned ( ) ) ;
@@ -170,14 +169,14 @@ pub async fn remove_roles_from_user(
170
169
171
170
let _ = storage:: put_staging_metadata ( & metadata) ;
172
171
// update in mem table
173
- Users . remove_roles ( & username . clone ( ) , roles_to_remove. clone ( ) ) ;
172
+ Users . remove_roles ( & userid . clone ( ) , roles_to_remove. clone ( ) ) ;
174
173
175
- Ok ( format ! ( "Roles updated successfully for {username}" ) )
174
+ Ok ( HttpResponse :: Ok ( ) . status ( StatusCode :: OK ) . finish ( ) )
176
175
}
177
176
178
177
// Handler for POST /api/v1/user/{username}/generate-new-password
179
178
// Resets password for the user to a newly generated one and returns it
180
- pub async fn post_gen_password ( username : web:: Path < String > ) -> Result < impl Responder , RBACError > {
179
+ pub async fn post_gen_password ( username : web:: Path < String > ) -> Result < HttpResponse , RBACError > {
181
180
let username = username. into_inner ( ) ;
182
181
let mut new_hash = String :: default ( ) ;
183
182
let mut metadata = get_metadata ( ) . await ?;
@@ -197,6 +196,5 @@ pub async fn post_gen_password(username: web::Path<String>) -> Result<impl Respo
197
196
return Err ( RBACError :: UserDoesNotExist ) ;
198
197
}
199
198
Users . change_password_hash ( & username, & new_hash) ;
200
-
201
- Ok ( "Updated" )
199
+ Ok ( HttpResponse :: Ok ( ) . status ( StatusCode :: OK ) . finish ( ) )
202
200
}
0 commit comments