Show More
@@ -1,818 +1,822 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2011-2017 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import logging |
|
22 | 22 | |
|
23 | 23 | from rhodecode.api import ( |
|
24 | 24 | jsonrpc_method, JSONRPCError, JSONRPCForbidden, JSONRPCValidationError) |
|
25 | 25 | from rhodecode.api.utils import ( |
|
26 | 26 | Optional, OAttr, store_update, has_superadmin_permission, get_origin, |
|
27 | 27 | get_user_or_error, get_user_group_or_error, get_perm_or_error) |
|
28 | 28 | from rhodecode.lib import audit_logger |
|
29 | 29 | from rhodecode.lib.auth import HasUserGroupPermissionAnyApi, HasPermissionAnyApi |
|
30 | 30 | from rhodecode.lib.exceptions import UserGroupAssignedException |
|
31 | 31 | from rhodecode.model.db import Session |
|
32 | 32 | from rhodecode.model.scm import UserGroupList |
|
33 | 33 | from rhodecode.model.user_group import UserGroupModel |
|
34 | 34 | from rhodecode.model import validation_schema |
|
35 | 35 | from rhodecode.model.validation_schema.schemas import user_group_schema |
|
36 | 36 | |
|
37 | 37 | log = logging.getLogger(__name__) |
|
38 | 38 | |
|
39 | 39 | |
|
40 | 40 | @jsonrpc_method() |
|
41 | 41 | def get_user_group(request, apiuser, usergroupid): |
|
42 | 42 | """ |
|
43 | 43 | Returns the data of an existing user group. |
|
44 | 44 | |
|
45 | 45 | This command can only be run using an |authtoken| with admin rights to |
|
46 | 46 | the specified repository. |
|
47 | 47 | |
|
48 | 48 | :param apiuser: This is filled automatically from the |authtoken|. |
|
49 | 49 | :type apiuser: AuthUser |
|
50 | 50 | :param usergroupid: Set the user group from which to return data. |
|
51 | 51 | :type usergroupid: str or int |
|
52 | 52 | |
|
53 | 53 | Example error output: |
|
54 | 54 | |
|
55 | 55 | .. code-block:: bash |
|
56 | 56 | |
|
57 | 57 | { |
|
58 | 58 | "error": null, |
|
59 | 59 | "id": <id>, |
|
60 | 60 | "result": { |
|
61 | 61 | "active": true, |
|
62 | 62 | "group_description": "group description", |
|
63 | 63 | "group_name": "group name", |
|
64 | 64 | "members": [ |
|
65 | 65 | { |
|
66 | 66 | "name": "owner-name", |
|
67 | 67 | "origin": "owner", |
|
68 | 68 | "permission": "usergroup.admin", |
|
69 | 69 | "type": "user" |
|
70 | 70 | }, |
|
71 | 71 | { |
|
72 | 72 | { |
|
73 | 73 | "name": "user name", |
|
74 | 74 | "origin": "permission", |
|
75 | 75 | "permission": "usergroup.admin", |
|
76 | 76 | "type": "user" |
|
77 | 77 | }, |
|
78 | 78 | { |
|
79 | 79 | "name": "user group name", |
|
80 | 80 | "origin": "permission", |
|
81 | 81 | "permission": "usergroup.write", |
|
82 | 82 | "type": "user_group" |
|
83 | 83 | } |
|
84 | 84 | ], |
|
85 | 85 | "owner": "owner name", |
|
86 | 86 | "users": [], |
|
87 | 87 | "users_group_id": 2 |
|
88 | 88 | } |
|
89 | 89 | } |
|
90 | 90 | |
|
91 | 91 | """ |
|
92 | 92 | |
|
93 | 93 | user_group = get_user_group_or_error(usergroupid) |
|
94 | 94 | if not has_superadmin_permission(apiuser): |
|
95 | 95 | # check if we have at least read permission for this user group ! |
|
96 | 96 | _perms = ('usergroup.read', 'usergroup.write', 'usergroup.admin',) |
|
97 | 97 | if not HasUserGroupPermissionAnyApi(*_perms)( |
|
98 | 98 | user=apiuser, user_group_name=user_group.users_group_name): |
|
99 | 99 | raise JSONRPCError('user group `%s` does not exist' % ( |
|
100 | 100 | usergroupid,)) |
|
101 | 101 | |
|
102 | 102 | permissions = [] |
|
103 | 103 | for _user in user_group.permissions(): |
|
104 | 104 | user_data = { |
|
105 | 105 | 'name': _user.username, |
|
106 | 106 | 'permission': _user.permission, |
|
107 | 107 | 'origin': get_origin(_user), |
|
108 | 108 | 'type': "user", |
|
109 | 109 | } |
|
110 | 110 | permissions.append(user_data) |
|
111 | 111 | |
|
112 | 112 | for _user_group in user_group.permission_user_groups(): |
|
113 | 113 | user_group_data = { |
|
114 | 114 | 'name': _user_group.users_group_name, |
|
115 | 115 | 'permission': _user_group.permission, |
|
116 | 116 | 'origin': get_origin(_user_group), |
|
117 | 117 | 'type': "user_group", |
|
118 | 118 | } |
|
119 | 119 | permissions.append(user_group_data) |
|
120 | 120 | |
|
121 | 121 | data = user_group.get_api_data() |
|
122 | 122 | data['members'] = permissions |
|
123 | 123 | |
|
124 | 124 | return data |
|
125 | 125 | |
|
126 | 126 | |
|
127 | 127 | @jsonrpc_method() |
|
128 | 128 | def get_user_groups(request, apiuser): |
|
129 | 129 | """ |
|
130 | 130 | Lists all the existing user groups within RhodeCode. |
|
131 | 131 | |
|
132 | 132 | This command can only be run using an |authtoken| with admin rights to |
|
133 | 133 | the specified repository. |
|
134 | 134 | |
|
135 | 135 | This command takes the following options: |
|
136 | 136 | |
|
137 | 137 | :param apiuser: This is filled automatically from the |authtoken|. |
|
138 | 138 | :type apiuser: AuthUser |
|
139 | 139 | |
|
140 | 140 | Example error output: |
|
141 | 141 | |
|
142 | 142 | .. code-block:: bash |
|
143 | 143 | |
|
144 | 144 | id : <id_given_in_input> |
|
145 | 145 | result : [<user_group_obj>,...] |
|
146 | 146 | error : null |
|
147 | 147 | """ |
|
148 | 148 | |
|
149 | 149 | include_secrets = has_superadmin_permission(apiuser) |
|
150 | 150 | |
|
151 | 151 | result = [] |
|
152 | 152 | _perms = ('usergroup.read', 'usergroup.write', 'usergroup.admin',) |
|
153 | 153 | extras = {'user': apiuser} |
|
154 | 154 | for user_group in UserGroupList(UserGroupModel().get_all(), |
|
155 | 155 | perm_set=_perms, extra_kwargs=extras): |
|
156 | 156 | result.append( |
|
157 | 157 | user_group.get_api_data(include_secrets=include_secrets)) |
|
158 | 158 | return result |
|
159 | 159 | |
|
160 | 160 | |
|
161 | 161 | @jsonrpc_method() |
|
162 | 162 | def create_user_group( |
|
163 | 163 | request, apiuser, group_name, description=Optional(''), |
|
164 | 164 | owner=Optional(OAttr('apiuser')), active=Optional(True)): |
|
165 | 165 | """ |
|
166 | 166 | Creates a new user group. |
|
167 | 167 | |
|
168 | 168 | This command can only be run using an |authtoken| with admin rights to |
|
169 | 169 | the specified repository. |
|
170 | 170 | |
|
171 | 171 | This command takes the following options: |
|
172 | 172 | |
|
173 | 173 | :param apiuser: This is filled automatically from the |authtoken|. |
|
174 | 174 | :type apiuser: AuthUser |
|
175 | 175 | :param group_name: Set the name of the new user group. |
|
176 | 176 | :type group_name: str |
|
177 | 177 | :param description: Give a description of the new user group. |
|
178 | 178 | :type description: str |
|
179 | 179 | :param owner: Set the owner of the new user group. |
|
180 | 180 | If not set, the owner is the |authtoken| user. |
|
181 | 181 | :type owner: Optional(str or int) |
|
182 | 182 | :param active: Set this group as active. |
|
183 | 183 | :type active: Optional(``True`` | ``False``) |
|
184 | 184 | |
|
185 | 185 | Example output: |
|
186 | 186 | |
|
187 | 187 | .. code-block:: bash |
|
188 | 188 | |
|
189 | 189 | id : <id_given_in_input> |
|
190 | 190 | result: { |
|
191 | 191 | "msg": "created new user group `<groupname>`", |
|
192 | 192 | "user_group": <user_group_object> |
|
193 | 193 | } |
|
194 | 194 | error: null |
|
195 | 195 | |
|
196 | 196 | Example error output: |
|
197 | 197 | |
|
198 | 198 | .. code-block:: bash |
|
199 | 199 | |
|
200 | 200 | id : <id_given_in_input> |
|
201 | 201 | result : null |
|
202 | 202 | error : { |
|
203 | 203 | "user group `<group name>` already exist" |
|
204 | 204 | or |
|
205 | 205 | "failed to create group `<group name>`" |
|
206 | 206 | } |
|
207 | 207 | |
|
208 | 208 | """ |
|
209 | 209 | |
|
210 | 210 | if not has_superadmin_permission(apiuser): |
|
211 | 211 | if not HasPermissionAnyApi('hg.usergroup.create.true')(user=apiuser): |
|
212 | 212 | raise JSONRPCForbidden() |
|
213 | 213 | |
|
214 | 214 | if UserGroupModel().get_by_name(group_name): |
|
215 | 215 | raise JSONRPCError("user group `%s` already exist" % (group_name,)) |
|
216 | 216 | |
|
217 | 217 | if isinstance(owner, Optional): |
|
218 | 218 | owner = apiuser.user_id |
|
219 | 219 | |
|
220 | 220 | owner = get_user_or_error(owner) |
|
221 | 221 | active = Optional.extract(active) |
|
222 | 222 | description = Optional.extract(description) |
|
223 | 223 | |
|
224 | 224 | schema = user_group_schema.UserGroupSchema().bind( |
|
225 | 225 | # user caller |
|
226 | 226 | user=apiuser) |
|
227 | 227 | try: |
|
228 | 228 | schema_data = schema.deserialize(dict( |
|
229 | 229 | user_group_name=group_name, |
|
230 | 230 | user_group_description=description, |
|
231 | 231 | user_group_owner=owner.username, |
|
232 | 232 | user_group_active=active, |
|
233 | 233 | )) |
|
234 | 234 | except validation_schema.Invalid as err: |
|
235 | 235 | raise JSONRPCValidationError(colander_exc=err) |
|
236 | 236 | |
|
237 | 237 | try: |
|
238 | 238 | user_group = UserGroupModel().create( |
|
239 | 239 | name=schema_data['user_group_name'], |
|
240 | 240 | description=schema_data['user_group_description'], |
|
241 | 241 | owner=owner, |
|
242 | 242 | active=schema_data['user_group_active']) |
|
243 | 243 | Session().flush() |
|
244 | 244 | creation_data = user_group.get_api_data() |
|
245 | 245 | audit_logger.store_api( |
|
246 | 246 | 'user_group.create', action_data={'data': creation_data}, |
|
247 | 247 | user=apiuser) |
|
248 | 248 | Session().commit() |
|
249 | 249 | return { |
|
250 | 250 | 'msg': 'created new user group `%s`' % group_name, |
|
251 | 251 | 'user_group': creation_data |
|
252 | 252 | } |
|
253 | 253 | except Exception: |
|
254 | 254 | log.exception("Error occurred during creation of user group") |
|
255 | 255 | raise JSONRPCError('failed to create group `%s`' % (group_name,)) |
|
256 | 256 | |
|
257 | 257 | |
|
258 | 258 | @jsonrpc_method() |
|
259 | 259 | def update_user_group(request, apiuser, usergroupid, group_name=Optional(''), |
|
260 | 260 | description=Optional(''), owner=Optional(None), |
|
261 | 261 | active=Optional(True)): |
|
262 | 262 | """ |
|
263 | 263 | Updates the specified `user group` with the details provided. |
|
264 | 264 | |
|
265 | 265 | This command can only be run using an |authtoken| with admin rights to |
|
266 | 266 | the specified repository. |
|
267 | 267 | |
|
268 | 268 | :param apiuser: This is filled automatically from the |authtoken|. |
|
269 | 269 | :type apiuser: AuthUser |
|
270 | 270 | :param usergroupid: Set the id of the `user group` to update. |
|
271 | 271 | :type usergroupid: str or int |
|
272 | 272 | :param group_name: Set the new name the `user group` |
|
273 | 273 | :type group_name: str |
|
274 | 274 | :param description: Give a description for the `user group` |
|
275 | 275 | :type description: str |
|
276 | 276 | :param owner: Set the owner of the `user group`. |
|
277 | 277 | :type owner: Optional(str or int) |
|
278 | 278 | :param active: Set the group as active. |
|
279 | 279 | :type active: Optional(``True`` | ``False``) |
|
280 | 280 | |
|
281 | 281 | Example output: |
|
282 | 282 | |
|
283 | 283 | .. code-block:: bash |
|
284 | 284 | |
|
285 | 285 | id : <id_given_in_input> |
|
286 | 286 | result : { |
|
287 | 287 | "msg": 'updated user group ID:<user group id> <user group name>', |
|
288 | 288 | "user_group": <user_group_object> |
|
289 | 289 | } |
|
290 | 290 | error : null |
|
291 | 291 | |
|
292 | 292 | Example error output: |
|
293 | 293 | |
|
294 | 294 | .. code-block:: bash |
|
295 | 295 | |
|
296 | 296 | id : <id_given_in_input> |
|
297 | 297 | result : null |
|
298 | 298 | error : { |
|
299 | 299 | "failed to update user group `<user group name>`" |
|
300 | 300 | } |
|
301 | 301 | |
|
302 | 302 | """ |
|
303 | 303 | |
|
304 | 304 | user_group = get_user_group_or_error(usergroupid) |
|
305 | 305 | include_secrets = False |
|
306 | 306 | if not has_superadmin_permission(apiuser): |
|
307 | 307 | # check if we have admin permission for this user group ! |
|
308 | 308 | _perms = ('usergroup.admin',) |
|
309 | 309 | if not HasUserGroupPermissionAnyApi(*_perms)( |
|
310 | 310 | user=apiuser, user_group_name=user_group.users_group_name): |
|
311 | 311 | raise JSONRPCError( |
|
312 | 312 | 'user group `%s` does not exist' % (usergroupid,)) |
|
313 | 313 | else: |
|
314 | 314 | include_secrets = True |
|
315 | 315 | |
|
316 | 316 | if not isinstance(owner, Optional): |
|
317 | 317 | owner = get_user_or_error(owner) |
|
318 | 318 | |
|
319 | 319 | old_data = user_group.get_api_data() |
|
320 | 320 | updates = {} |
|
321 | 321 | store_update(updates, group_name, 'users_group_name') |
|
322 | 322 | store_update(updates, description, 'user_group_description') |
|
323 | 323 | store_update(updates, owner, 'user') |
|
324 | 324 | store_update(updates, active, 'users_group_active') |
|
325 | 325 | try: |
|
326 | 326 | UserGroupModel().update(user_group, updates) |
|
327 | 327 | audit_logger.store_api( |
|
328 | 328 | 'user_group.edit', action_data={'old_data': old_data}, |
|
329 | 329 | user=apiuser) |
|
330 | 330 | Session().commit() |
|
331 | 331 | return { |
|
332 | 332 | 'msg': 'updated user group ID:%s %s' % ( |
|
333 | 333 | user_group.users_group_id, user_group.users_group_name), |
|
334 | 334 | 'user_group': user_group.get_api_data( |
|
335 | 335 | include_secrets=include_secrets) |
|
336 | 336 | } |
|
337 | 337 | except Exception: |
|
338 | 338 | log.exception("Error occurred during update of user group") |
|
339 | 339 | raise JSONRPCError( |
|
340 | 340 | 'failed to update user group `%s`' % (usergroupid,)) |
|
341 | 341 | |
|
342 | 342 | |
|
343 | 343 | @jsonrpc_method() |
|
344 | 344 | def delete_user_group(request, apiuser, usergroupid): |
|
345 | 345 | """ |
|
346 | 346 | Deletes the specified `user group`. |
|
347 | 347 | |
|
348 | 348 | This command can only be run using an |authtoken| with admin rights to |
|
349 | 349 | the specified repository. |
|
350 | 350 | |
|
351 | 351 | This command takes the following options: |
|
352 | 352 | |
|
353 | 353 | :param apiuser: filled automatically from apikey |
|
354 | 354 | :type apiuser: AuthUser |
|
355 | 355 | :param usergroupid: |
|
356 | 356 | :type usergroupid: int |
|
357 | 357 | |
|
358 | 358 | Example output: |
|
359 | 359 | |
|
360 | 360 | .. code-block:: bash |
|
361 | 361 | |
|
362 | 362 | id : <id_given_in_input> |
|
363 | 363 | result : { |
|
364 | 364 | "msg": "deleted user group ID:<user_group_id> <user_group_name>" |
|
365 | 365 | } |
|
366 | 366 | error : null |
|
367 | 367 | |
|
368 | 368 | Example error output: |
|
369 | 369 | |
|
370 | 370 | .. code-block:: bash |
|
371 | 371 | |
|
372 | 372 | id : <id_given_in_input> |
|
373 | 373 | result : null |
|
374 | 374 | error : { |
|
375 | 375 | "failed to delete user group ID:<user_group_id> <user_group_name>" |
|
376 | 376 | or |
|
377 | 377 | "RepoGroup assigned to <repo_groups_list>" |
|
378 | 378 | } |
|
379 | 379 | |
|
380 | 380 | """ |
|
381 | 381 | |
|
382 | 382 | user_group = get_user_group_or_error(usergroupid) |
|
383 | 383 | if not has_superadmin_permission(apiuser): |
|
384 | 384 | # check if we have admin permission for this user group ! |
|
385 | 385 | _perms = ('usergroup.admin',) |
|
386 | 386 | if not HasUserGroupPermissionAnyApi(*_perms)( |
|
387 | 387 | user=apiuser, user_group_name=user_group.users_group_name): |
|
388 | 388 | raise JSONRPCError( |
|
389 | 389 | 'user group `%s` does not exist' % (usergroupid,)) |
|
390 | 390 | |
|
391 | 391 | old_data = user_group.get_api_data() |
|
392 | 392 | try: |
|
393 | 393 | UserGroupModel().delete(user_group) |
|
394 | 394 | audit_logger.store_api( |
|
395 | 395 | 'user_group.delete', action_data={'old_data': old_data}, |
|
396 | 396 | user=apiuser) |
|
397 | 397 | Session().commit() |
|
398 | 398 | return { |
|
399 | 399 | 'msg': 'deleted user group ID:%s %s' % ( |
|
400 | 400 | user_group.users_group_id, user_group.users_group_name), |
|
401 | 401 | 'user_group': None |
|
402 | 402 | } |
|
403 | 403 | except UserGroupAssignedException as e: |
|
404 | 404 | log.exception("UserGroupAssigned error") |
|
405 | 405 | raise JSONRPCError(str(e)) |
|
406 | 406 | except Exception: |
|
407 | 407 | log.exception("Error occurred during deletion of user group") |
|
408 | 408 | raise JSONRPCError( |
|
409 | 409 | 'failed to delete user group ID:%s %s' %( |
|
410 | 410 | user_group.users_group_id, user_group.users_group_name)) |
|
411 | 411 | |
|
412 | 412 | |
|
413 | 413 | @jsonrpc_method() |
|
414 | 414 | def add_user_to_user_group(request, apiuser, usergroupid, userid): |
|
415 | 415 | """ |
|
416 | 416 | Adds a user to a `user group`. If the user already exists in the group |
|
417 | 417 | this command will return false. |
|
418 | 418 | |
|
419 | 419 | This command can only be run using an |authtoken| with admin rights to |
|
420 | 420 | the specified user group. |
|
421 | 421 | |
|
422 | 422 | This command takes the following options: |
|
423 | 423 | |
|
424 | 424 | :param apiuser: This is filled automatically from the |authtoken|. |
|
425 | 425 | :type apiuser: AuthUser |
|
426 | 426 | :param usergroupid: Set the name of the `user group` to which a |
|
427 | 427 | user will be added. |
|
428 | 428 | :type usergroupid: int |
|
429 | 429 | :param userid: Set the `user_id` of the user to add to the group. |
|
430 | 430 | :type userid: int |
|
431 | 431 | |
|
432 | 432 | Example output: |
|
433 | 433 | |
|
434 | 434 | .. code-block:: bash |
|
435 | 435 | |
|
436 | 436 | id : <id_given_in_input> |
|
437 | 437 | result : { |
|
438 | 438 | "success": True|False # depends on if member is in group |
|
439 | 439 | "msg": "added member `<username>` to user group `<groupname>` | |
|
440 | 440 | User is already in that group" |
|
441 | 441 | |
|
442 | 442 | } |
|
443 | 443 | error : null |
|
444 | 444 | |
|
445 | 445 | Example error output: |
|
446 | 446 | |
|
447 | 447 | .. code-block:: bash |
|
448 | 448 | |
|
449 | 449 | id : <id_given_in_input> |
|
450 | 450 | result : null |
|
451 | 451 | error : { |
|
452 | 452 | "failed to add member to user group `<user_group_name>`" |
|
453 | 453 | } |
|
454 | 454 | |
|
455 | 455 | """ |
|
456 | 456 | |
|
457 | 457 | user = get_user_or_error(userid) |
|
458 | 458 | user_group = get_user_group_or_error(usergroupid) |
|
459 | 459 | if not has_superadmin_permission(apiuser): |
|
460 | 460 | # check if we have admin permission for this user group ! |
|
461 | 461 | _perms = ('usergroup.admin',) |
|
462 | 462 | if not HasUserGroupPermissionAnyApi(*_perms)( |
|
463 | 463 | user=apiuser, user_group_name=user_group.users_group_name): |
|
464 | 464 | raise JSONRPCError('user group `%s` does not exist' % ( |
|
465 | 465 | usergroupid,)) |
|
466 | 466 | |
|
467 | old_values = user_group.get_api_data() | |
|
467 | 468 | try: |
|
468 | 469 | ugm = UserGroupModel().add_user_to_group(user_group, user) |
|
469 | 470 | success = True if ugm is not True else False |
|
470 | 471 | msg = 'added member `%s` to user group `%s`' % ( |
|
471 | 472 | user.username, user_group.users_group_name |
|
472 | 473 | ) |
|
473 | 474 | msg = msg if success else 'User is already in that group' |
|
474 | 475 | if success: |
|
475 | 476 | user_data = user.get_api_data() |
|
476 | 477 | audit_logger.store_api( |
|
477 |
'user_group.edit.member.add', |
|
|
478 | 'user_group.edit.member.add', | |
|
479 | action_data={'user': user_data, 'old_data': old_values}, | |
|
478 | 480 | user=apiuser) |
|
479 | 481 | |
|
480 | 482 | Session().commit() |
|
481 | 483 | |
|
482 | 484 | return { |
|
483 | 485 | 'success': success, |
|
484 | 486 | 'msg': msg |
|
485 | 487 | } |
|
486 | 488 | except Exception: |
|
487 | 489 | log.exception("Error occurred during adding a member to user group") |
|
488 | 490 | raise JSONRPCError( |
|
489 | 491 | 'failed to add member to user group `%s`' % ( |
|
490 | 492 | user_group.users_group_name, |
|
491 | 493 | ) |
|
492 | 494 | ) |
|
493 | 495 | |
|
494 | 496 | |
|
495 | 497 | @jsonrpc_method() |
|
496 | 498 | def remove_user_from_user_group(request, apiuser, usergroupid, userid): |
|
497 | 499 | """ |
|
498 | 500 | Removes a user from a user group. |
|
499 | 501 | |
|
500 | 502 | * If the specified user is not in the group, this command will return |
|
501 | 503 | `false`. |
|
502 | 504 | |
|
503 | 505 | This command can only be run using an |authtoken| with admin rights to |
|
504 | 506 | the specified user group. |
|
505 | 507 | |
|
506 | 508 | :param apiuser: This is filled automatically from the |authtoken|. |
|
507 | 509 | :type apiuser: AuthUser |
|
508 | 510 | :param usergroupid: Sets the user group name. |
|
509 | 511 | :type usergroupid: str or int |
|
510 | 512 | :param userid: The user you wish to remove from |RCE|. |
|
511 | 513 | :type userid: str or int |
|
512 | 514 | |
|
513 | 515 | Example output: |
|
514 | 516 | |
|
515 | 517 | .. code-block:: bash |
|
516 | 518 | |
|
517 | 519 | id : <id_given_in_input> |
|
518 | 520 | result: { |
|
519 | 521 | "success": True|False, # depends on if member is in group |
|
520 | 522 | "msg": "removed member <username> from user group <groupname> | |
|
521 | 523 | User wasn't in group" |
|
522 | 524 | } |
|
523 | 525 | error: null |
|
524 | 526 | |
|
525 | 527 | """ |
|
526 | 528 | |
|
527 | 529 | user = get_user_or_error(userid) |
|
528 | 530 | user_group = get_user_group_or_error(usergroupid) |
|
529 | 531 | if not has_superadmin_permission(apiuser): |
|
530 | 532 | # check if we have admin permission for this user group ! |
|
531 | 533 | _perms = ('usergroup.admin',) |
|
532 | 534 | if not HasUserGroupPermissionAnyApi(*_perms)( |
|
533 | 535 | user=apiuser, user_group_name=user_group.users_group_name): |
|
534 | 536 | raise JSONRPCError( |
|
535 | 537 | 'user group `%s` does not exist' % (usergroupid,)) |
|
536 | 538 | |
|
539 | old_values = user_group.get_api_data() | |
|
537 | 540 | try: |
|
538 | 541 | success = UserGroupModel().remove_user_from_group(user_group, user) |
|
539 | 542 | msg = 'removed member `%s` from user group `%s`' % ( |
|
540 | 543 | user.username, user_group.users_group_name |
|
541 | 544 | ) |
|
542 | 545 | msg = msg if success else "User wasn't in group" |
|
543 | 546 | if success: |
|
544 | 547 | user_data = user.get_api_data() |
|
545 | 548 | audit_logger.store_api( |
|
546 |
'user_group.edit.member.delete', |
|
|
549 | 'user_group.edit.member.delete', | |
|
550 | action_data={'user': user_data, 'old_data': old_values}, | |
|
547 | 551 | user=apiuser) |
|
548 | 552 | |
|
549 | 553 | Session().commit() |
|
550 | 554 | return {'success': success, 'msg': msg} |
|
551 | 555 | except Exception: |
|
552 | 556 | log.exception("Error occurred during removing an member from user group") |
|
553 | 557 | raise JSONRPCError( |
|
554 | 558 | 'failed to remove member from user group `%s`' % ( |
|
555 | 559 | user_group.users_group_name, |
|
556 | 560 | ) |
|
557 | 561 | ) |
|
558 | 562 | |
|
559 | 563 | |
|
560 | 564 | @jsonrpc_method() |
|
561 | 565 | def grant_user_permission_to_user_group( |
|
562 | 566 | request, apiuser, usergroupid, userid, perm): |
|
563 | 567 | """ |
|
564 | 568 | Set permissions for a user in a user group. |
|
565 | 569 | |
|
566 | 570 | :param apiuser: This is filled automatically from the |authtoken|. |
|
567 | 571 | :type apiuser: AuthUser |
|
568 | 572 | :param usergroupid: Set the user group to edit permissions on. |
|
569 | 573 | :type usergroupid: str or int |
|
570 | 574 | :param userid: Set the user from whom you wish to set permissions. |
|
571 | 575 | :type userid: str |
|
572 | 576 | :param perm: (usergroup.(none|read|write|admin)) |
|
573 | 577 | :type perm: str |
|
574 | 578 | |
|
575 | 579 | Example output: |
|
576 | 580 | |
|
577 | 581 | .. code-block:: bash |
|
578 | 582 | |
|
579 | 583 | id : <id_given_in_input> |
|
580 | 584 | result : { |
|
581 | 585 | "msg": "Granted perm: `<perm_name>` for user: `<username>` in user group: `<user_group_name>`", |
|
582 | 586 | "success": true |
|
583 | 587 | } |
|
584 | 588 | error : null |
|
585 | 589 | """ |
|
586 | 590 | |
|
587 | 591 | user_group = get_user_group_or_error(usergroupid) |
|
588 | 592 | |
|
589 | 593 | if not has_superadmin_permission(apiuser): |
|
590 | 594 | # check if we have admin permission for this user group ! |
|
591 | 595 | _perms = ('usergroup.admin',) |
|
592 | 596 | if not HasUserGroupPermissionAnyApi(*_perms)( |
|
593 | 597 | user=apiuser, user_group_name=user_group.users_group_name): |
|
594 | 598 | raise JSONRPCError( |
|
595 | 599 | 'user group `%s` does not exist' % (usergroupid,)) |
|
596 | 600 | |
|
597 | 601 | user = get_user_or_error(userid) |
|
598 | 602 | perm = get_perm_or_error(perm, prefix='usergroup.') |
|
599 | 603 | |
|
600 | 604 | try: |
|
601 | 605 | UserGroupModel().grant_user_permission( |
|
602 | 606 | user_group=user_group, user=user, perm=perm) |
|
603 | 607 | Session().commit() |
|
604 | 608 | return { |
|
605 | 609 | 'msg': |
|
606 | 610 | 'Granted perm: `%s` for user: `%s` in user group: `%s`' % ( |
|
607 | 611 | perm.permission_name, user.username, |
|
608 | 612 | user_group.users_group_name |
|
609 | 613 | ), |
|
610 | 614 | 'success': True |
|
611 | 615 | } |
|
612 | 616 | except Exception: |
|
613 | 617 | log.exception("Error occurred during editing permissions " |
|
614 | 618 | "for user in user group") |
|
615 | 619 | raise JSONRPCError( |
|
616 | 620 | 'failed to edit permission for user: ' |
|
617 | 621 | '`%s` in user group: `%s`' % ( |
|
618 | 622 | userid, user_group.users_group_name)) |
|
619 | 623 | |
|
620 | 624 | |
|
621 | 625 | @jsonrpc_method() |
|
622 | 626 | def revoke_user_permission_from_user_group( |
|
623 | 627 | request, apiuser, usergroupid, userid): |
|
624 | 628 | """ |
|
625 | 629 | Revoke a users permissions in a user group. |
|
626 | 630 | |
|
627 | 631 | :param apiuser: This is filled automatically from the |authtoken|. |
|
628 | 632 | :type apiuser: AuthUser |
|
629 | 633 | :param usergroupid: Set the user group from which to revoke the user |
|
630 | 634 | permissions. |
|
631 | 635 | :type: usergroupid: str or int |
|
632 | 636 | :param userid: Set the userid of the user whose permissions will be |
|
633 | 637 | revoked. |
|
634 | 638 | :type userid: str |
|
635 | 639 | |
|
636 | 640 | Example output: |
|
637 | 641 | |
|
638 | 642 | .. code-block:: bash |
|
639 | 643 | |
|
640 | 644 | id : <id_given_in_input> |
|
641 | 645 | result : { |
|
642 | 646 | "msg": "Revoked perm for user: `<username>` in user group: `<user_group_name>`", |
|
643 | 647 | "success": true |
|
644 | 648 | } |
|
645 | 649 | error : null |
|
646 | 650 | """ |
|
647 | 651 | |
|
648 | 652 | user_group = get_user_group_or_error(usergroupid) |
|
649 | 653 | |
|
650 | 654 | if not has_superadmin_permission(apiuser): |
|
651 | 655 | # check if we have admin permission for this user group ! |
|
652 | 656 | _perms = ('usergroup.admin',) |
|
653 | 657 | if not HasUserGroupPermissionAnyApi(*_perms)( |
|
654 | 658 | user=apiuser, user_group_name=user_group.users_group_name): |
|
655 | 659 | raise JSONRPCError( |
|
656 | 660 | 'user group `%s` does not exist' % (usergroupid,)) |
|
657 | 661 | |
|
658 | 662 | user = get_user_or_error(userid) |
|
659 | 663 | |
|
660 | 664 | try: |
|
661 | 665 | UserGroupModel().revoke_user_permission( |
|
662 | 666 | user_group=user_group, user=user) |
|
663 | 667 | Session().commit() |
|
664 | 668 | return { |
|
665 | 669 | 'msg': 'Revoked perm for user: `%s` in user group: `%s`' % ( |
|
666 | 670 | user.username, user_group.users_group_name |
|
667 | 671 | ), |
|
668 | 672 | 'success': True |
|
669 | 673 | } |
|
670 | 674 | except Exception: |
|
671 | 675 | log.exception("Error occurred during editing permissions " |
|
672 | 676 | "for user in user group") |
|
673 | 677 | raise JSONRPCError( |
|
674 | 678 | 'failed to edit permission for user: `%s` in user group: `%s`' |
|
675 | 679 | % (userid, user_group.users_group_name)) |
|
676 | 680 | |
|
677 | 681 | |
|
678 | 682 | @jsonrpc_method() |
|
679 | 683 | def grant_user_group_permission_to_user_group( |
|
680 | 684 | request, apiuser, usergroupid, sourceusergroupid, perm): |
|
681 | 685 | """ |
|
682 | 686 | Give one user group permissions to another user group. |
|
683 | 687 | |
|
684 | 688 | :param apiuser: This is filled automatically from the |authtoken|. |
|
685 | 689 | :type apiuser: AuthUser |
|
686 | 690 | :param usergroupid: Set the user group on which to edit permissions. |
|
687 | 691 | :type usergroupid: str or int |
|
688 | 692 | :param sourceusergroupid: Set the source user group to which |
|
689 | 693 | access/permissions will be granted. |
|
690 | 694 | :type sourceusergroupid: str or int |
|
691 | 695 | :param perm: (usergroup.(none|read|write|admin)) |
|
692 | 696 | :type perm: str |
|
693 | 697 | |
|
694 | 698 | Example output: |
|
695 | 699 | |
|
696 | 700 | .. code-block:: bash |
|
697 | 701 | |
|
698 | 702 | id : <id_given_in_input> |
|
699 | 703 | result : { |
|
700 | 704 | "msg": "Granted perm: `<perm_name>` for user group: `<source_user_group_name>` in user group: `<user_group_name>`", |
|
701 | 705 | "success": true |
|
702 | 706 | } |
|
703 | 707 | error : null |
|
704 | 708 | """ |
|
705 | 709 | |
|
706 | 710 | user_group = get_user_group_or_error(sourceusergroupid) |
|
707 | 711 | target_user_group = get_user_group_or_error(usergroupid) |
|
708 | 712 | perm = get_perm_or_error(perm, prefix='usergroup.') |
|
709 | 713 | |
|
710 | 714 | if not has_superadmin_permission(apiuser): |
|
711 | 715 | # check if we have admin permission for this user group ! |
|
712 | 716 | _perms = ('usergroup.admin',) |
|
713 | 717 | if not HasUserGroupPermissionAnyApi(*_perms)( |
|
714 | 718 | user=apiuser, |
|
715 | 719 | user_group_name=target_user_group.users_group_name): |
|
716 | 720 | raise JSONRPCError( |
|
717 | 721 | 'to user group `%s` does not exist' % (usergroupid,)) |
|
718 | 722 | |
|
719 | 723 | # check if we have at least read permission for source user group ! |
|
720 | 724 | _perms = ('usergroup.read', 'usergroup.write', 'usergroup.admin',) |
|
721 | 725 | if not HasUserGroupPermissionAnyApi(*_perms)( |
|
722 | 726 | user=apiuser, user_group_name=user_group.users_group_name): |
|
723 | 727 | raise JSONRPCError( |
|
724 | 728 | 'user group `%s` does not exist' % (sourceusergroupid,)) |
|
725 | 729 | |
|
726 | 730 | try: |
|
727 | 731 | UserGroupModel().grant_user_group_permission( |
|
728 | 732 | target_user_group=target_user_group, |
|
729 | 733 | user_group=user_group, perm=perm) |
|
730 | 734 | Session().commit() |
|
731 | 735 | |
|
732 | 736 | return { |
|
733 | 737 | 'msg': 'Granted perm: `%s` for user group: `%s` ' |
|
734 | 738 | 'in user group: `%s`' % ( |
|
735 | 739 | perm.permission_name, user_group.users_group_name, |
|
736 | 740 | target_user_group.users_group_name |
|
737 | 741 | ), |
|
738 | 742 | 'success': True |
|
739 | 743 | } |
|
740 | 744 | except Exception: |
|
741 | 745 | log.exception("Error occurred during editing permissions " |
|
742 | 746 | "for user group in user group") |
|
743 | 747 | raise JSONRPCError( |
|
744 | 748 | 'failed to edit permission for user group: `%s` in ' |
|
745 | 749 | 'user group: `%s`' % ( |
|
746 | 750 | sourceusergroupid, target_user_group.users_group_name |
|
747 | 751 | ) |
|
748 | 752 | ) |
|
749 | 753 | |
|
750 | 754 | |
|
751 | 755 | @jsonrpc_method() |
|
752 | 756 | def revoke_user_group_permission_from_user_group( |
|
753 | 757 | request, apiuser, usergroupid, sourceusergroupid): |
|
754 | 758 | """ |
|
755 | 759 | Revoke the permissions that one user group has to another. |
|
756 | 760 | |
|
757 | 761 | :param apiuser: This is filled automatically from the |authtoken|. |
|
758 | 762 | :type apiuser: AuthUser |
|
759 | 763 | :param usergroupid: Set the user group on which to edit permissions. |
|
760 | 764 | :type usergroupid: str or int |
|
761 | 765 | :param sourceusergroupid: Set the user group from which permissions |
|
762 | 766 | are revoked. |
|
763 | 767 | :type sourceusergroupid: str or int |
|
764 | 768 | |
|
765 | 769 | Example output: |
|
766 | 770 | |
|
767 | 771 | .. code-block:: bash |
|
768 | 772 | |
|
769 | 773 | id : <id_given_in_input> |
|
770 | 774 | result : { |
|
771 | 775 | "msg": "Revoked perm for user group: `<user_group_name>` in user group: `<target_user_group_name>`", |
|
772 | 776 | "success": true |
|
773 | 777 | } |
|
774 | 778 | error : null |
|
775 | 779 | """ |
|
776 | 780 | |
|
777 | 781 | user_group = get_user_group_or_error(sourceusergroupid) |
|
778 | 782 | target_user_group = get_user_group_or_error(usergroupid) |
|
779 | 783 | |
|
780 | 784 | if not has_superadmin_permission(apiuser): |
|
781 | 785 | # check if we have admin permission for this user group ! |
|
782 | 786 | _perms = ('usergroup.admin',) |
|
783 | 787 | if not HasUserGroupPermissionAnyApi(*_perms)( |
|
784 | 788 | user=apiuser, |
|
785 | 789 | user_group_name=target_user_group.users_group_name): |
|
786 | 790 | raise JSONRPCError( |
|
787 | 791 | 'to user group `%s` does not exist' % (usergroupid,)) |
|
788 | 792 | |
|
789 | 793 | # check if we have at least read permission |
|
790 | 794 | # for the source user group ! |
|
791 | 795 | _perms = ('usergroup.read', 'usergroup.write', 'usergroup.admin',) |
|
792 | 796 | if not HasUserGroupPermissionAnyApi(*_perms)( |
|
793 | 797 | user=apiuser, user_group_name=user_group.users_group_name): |
|
794 | 798 | raise JSONRPCError( |
|
795 | 799 | 'user group `%s` does not exist' % (sourceusergroupid,)) |
|
796 | 800 | |
|
797 | 801 | try: |
|
798 | 802 | UserGroupModel().revoke_user_group_permission( |
|
799 | 803 | target_user_group=target_user_group, user_group=user_group) |
|
800 | 804 | Session().commit() |
|
801 | 805 | |
|
802 | 806 | return { |
|
803 | 807 | 'msg': 'Revoked perm for user group: ' |
|
804 | 808 | '`%s` in user group: `%s`' % ( |
|
805 | 809 | user_group.users_group_name, |
|
806 | 810 | target_user_group.users_group_name |
|
807 | 811 | ), |
|
808 | 812 | 'success': True |
|
809 | 813 | } |
|
810 | 814 | except Exception: |
|
811 | 815 | log.exception("Error occurred during editing permissions " |
|
812 | 816 | "for user group in user group") |
|
813 | 817 | raise JSONRPCError( |
|
814 | 818 | 'failed to edit permission for user group: ' |
|
815 | 819 | '`%s` in user group: `%s`' % ( |
|
816 | 820 | sourceusergroupid, target_user_group.users_group_name |
|
817 | 821 | ) |
|
818 | 822 | ) |
General Comments 0
You need to be logged in to leave comments.
Login now