Show More
@@ -283,7 +283,8 b' OUTPUT::' | |||||
283 | add_user_to_users_group |
|
283 | add_user_to_users_group | |
284 | ----------------------- |
|
284 | ----------------------- | |
285 |
|
285 | |||
286 | Adds a user to a users group. This command can be executed only using api_key |
|
286 | Adds a user to a users group. If user exists in that group success will be | |
|
287 | `false`. This command can be executed only using api_key | |||
287 | belonging to user with admin rights |
|
288 | belonging to user with admin rights | |
288 |
|
289 | |||
289 |
|
290 | |||
@@ -300,7 +301,36 b' OUTPUT::' | |||||
300 |
|
301 | |||
301 | result: { |
|
302 | result: { | |
302 | "id": "<newusersgroupmemberid>", |
|
303 | "id": "<newusersgroupmemberid>", | |
303 | "msg": "created new users group member" |
|
304 | "success": True|False # depends on if member is in group | |
|
305 | "msg": "added member <username> to users group <groupname> | | |||
|
306 | User is already in that group" | |||
|
307 | } | |||
|
308 | error: null | |||
|
309 | ||||
|
310 | ||||
|
311 | remove_user_from_users_group | |||
|
312 | ---------------------------- | |||
|
313 | ||||
|
314 | Removes a user from a users group. If user is not in given group success will | |||
|
315 | be `false`. This command can be executed only | |||
|
316 | using api_key belonging to user with admin rights | |||
|
317 | ||||
|
318 | ||||
|
319 | INPUT:: | |||
|
320 | ||||
|
321 | api_key : "<api_key>" | |||
|
322 | method : "remove_user_from_users_group" | |||
|
323 | args: { | |||
|
324 | "group_name" : "<groupname>", | |||
|
325 | "username" : "<username>" | |||
|
326 | } | |||
|
327 | ||||
|
328 | OUTPUT:: | |||
|
329 | ||||
|
330 | result: { | |||
|
331 | "success": True|False, # depends on if member is in group | |||
|
332 | "msg": "removed member <username> from users group <groupname> | | |||
|
333 | User wasn't in group" | |||
304 | } |
|
334 | } | |
305 | error: null |
|
335 | error: null | |
306 |
|
336 |
@@ -92,8 +92,8 b' class ApiController(JSONRPCController):' | |||||
92 | """ |
|
92 | """ | |
93 |
|
93 | |||
94 | user = User.get_by_username(username) |
|
94 | user = User.get_by_username(username) | |
95 |
if |
|
95 | if user is None: | |
96 |
return |
|
96 | return user | |
97 |
|
97 | |||
98 | return dict( |
|
98 | return dict( | |
99 | id=user.user_id, |
|
99 | id=user.user_id, | |
@@ -258,18 +258,52 b' class ApiController(JSONRPCController):' | |||||
258 | if not users_group: |
|
258 | if not users_group: | |
259 | raise JSONRPCError('unknown users group %s' % group_name) |
|
259 | raise JSONRPCError('unknown users group %s' % group_name) | |
260 |
|
260 | |||
261 | try: |
|
261 | user = User.get_by_username(username) | |
262 | user = User.get_by_username(username) |
|
262 | if user is None: | |
263 | except NoResultFound: |
|
|||
264 | raise JSONRPCError('unknown user %s' % username) |
|
263 | raise JSONRPCError('unknown user %s' % username) | |
265 |
|
264 | |||
266 | ugm = UsersGroupModel().add_user_to_group(users_group, user) |
|
265 | ugm = UsersGroupModel().add_user_to_group(users_group, user) | |
|
266 | success = True if ugm != True else False | |||
|
267 | msg = 'added member %s to users group %s' % (username, group_name) | |||
|
268 | msg = msg if success else 'User is already in that group' | |||
267 | Session.commit() |
|
269 | Session.commit() | |
268 | return dict(id=ugm.users_group_member_id, |
|
270 | ||
269 | msg='created new users group member') |
|
271 | return dict( | |
|
272 | id=ugm.users_group_member_id if ugm != True else None, | |||
|
273 | success=success, | |||
|
274 | msg=msg | |||
|
275 | ) | |||
270 | except Exception: |
|
276 | except Exception: | |
271 | log.error(traceback.format_exc()) |
|
277 | log.error(traceback.format_exc()) | |
272 |
raise JSONRPCError('failed to |
|
278 | raise JSONRPCError('failed to add users group member') | |
|
279 | ||||
|
280 | @HasPermissionAllDecorator('hg.admin') | |||
|
281 | def remove_user_from_users_group(self, apiuser, group_name, username): | |||
|
282 | """ | |||
|
283 | Remove user from a group | |||
|
284 | ||||
|
285 | :param apiuser | |||
|
286 | :param group_name | |||
|
287 | :param username | |||
|
288 | """ | |||
|
289 | ||||
|
290 | try: | |||
|
291 | users_group = UsersGroup.get_by_group_name(group_name) | |||
|
292 | if not users_group: | |||
|
293 | raise JSONRPCError('unknown users group %s' % group_name) | |||
|
294 | ||||
|
295 | user = User.get_by_username(username) | |||
|
296 | if user is None: | |||
|
297 | raise JSONRPCError('unknown user %s' % username) | |||
|
298 | ||||
|
299 | success = UsersGroupModel().remove_user_from_group(users_group, user) | |||
|
300 | msg = 'removed member %s from users group %s' % (username, group_name) | |||
|
301 | msg = msg if success else "User wasn't in group" | |||
|
302 | Session.commit() | |||
|
303 | return dict(success=success, msg=msg) | |||
|
304 | except Exception: | |||
|
305 | log.error(traceback.format_exc()) | |||
|
306 | raise JSONRPCError('failed to remove user from group') | |||
273 |
|
307 | |||
274 | @HasPermissionAnyDecorator('hg.admin') |
|
308 | @HasPermissionAnyDecorator('hg.admin') | |
275 | def get_repo(self, apiuser, repo_name): |
|
309 | def get_repo(self, apiuser, repo_name): | |
@@ -386,10 +420,9 b' class ApiController(JSONRPCController):' | |||||
386 | """ |
|
420 | """ | |
387 |
|
421 | |||
388 | try: |
|
422 | try: | |
389 | try: |
|
423 | owner = User.get_by_username(owner_name) | |
390 | owner = User.get_by_username(owner_name) |
|
424 | if owner is None: | |
391 | except NoResultFound: |
|
425 | raise JSONRPCError('unknown user %s' % owner_name) | |
392 | raise JSONRPCError('unknown user %s' % owner) |
|
|||
393 |
|
426 | |||
394 | if Repository.get_by_repo_name(repo_name): |
|
427 | if Repository.get_by_repo_name(repo_name): | |
395 | raise JSONRPCError("repo %s already exist" % repo_name) |
|
428 | raise JSONRPCError("repo %s already exist" % repo_name) |
@@ -29,7 +29,7 b' import traceback' | |||||
29 |
|
29 | |||
30 | from rhodecode.model import BaseModel |
|
30 | from rhodecode.model import BaseModel | |
31 | from rhodecode.model.db import UsersGroupMember, UsersGroup,\ |
|
31 | from rhodecode.model.db import UsersGroupMember, UsersGroup,\ | |
32 | UsersGroupRepoToPerm, Permission, UsersGroupToPerm |
|
32 | UsersGroupRepoToPerm, Permission, UsersGroupToPerm, User | |
33 | from rhodecode.lib.exceptions import UsersGroupsAssignedException |
|
33 | from rhodecode.lib.exceptions import UsersGroupsAssignedException | |
34 |
|
34 | |||
35 | log = logging.getLogger(__name__) |
|
35 | log = logging.getLogger(__name__) | |
@@ -37,6 +37,9 b' log = logging.getLogger(__name__)' | |||||
37 |
|
37 | |||
38 | class UsersGroupModel(BaseModel): |
|
38 | class UsersGroupModel(BaseModel): | |
39 |
|
39 | |||
|
40 | def __get_user(self, user): | |||
|
41 | return self._get_instance(User, user, callback=User.get_by_username) | |||
|
42 | ||||
40 | def __get_users_group(self, users_group): |
|
43 | def __get_users_group(self, users_group): | |
41 | return self._get_instance(UsersGroup, users_group, |
|
44 | return self._get_instance(UsersGroup, users_group, | |
42 | callback=UsersGroup.get_by_group_name) |
|
45 | callback=UsersGroup.get_by_group_name) | |
@@ -111,10 +114,13 b' class UsersGroupModel(BaseModel):' | |||||
111 | raise |
|
114 | raise | |
112 |
|
115 | |||
113 | def add_user_to_group(self, users_group, user): |
|
116 | def add_user_to_group(self, users_group, user): | |
|
117 | users_group = self.__get_users_group(users_group) | |||
|
118 | user = self.__get_user(user) | |||
|
119 | ||||
114 | for m in users_group.members: |
|
120 | for m in users_group.members: | |
115 | u = m.user |
|
121 | u = m.user | |
116 | if u.user_id == user.user_id: |
|
122 | if u.user_id == user.user_id: | |
117 |
return |
|
123 | return True | |
118 |
|
124 | |||
119 | try: |
|
125 | try: | |
120 | users_group_member = UsersGroupMember() |
|
126 | users_group_member = UsersGroupMember() | |
@@ -130,6 +136,28 b' class UsersGroupModel(BaseModel):' | |||||
130 | log.error(traceback.format_exc()) |
|
136 | log.error(traceback.format_exc()) | |
131 | raise |
|
137 | raise | |
132 |
|
138 | |||
|
139 | def remove_user_from_group(self, users_group, user): | |||
|
140 | users_group = self.__get_users_group(users_group) | |||
|
141 | user = self.__get_user(user) | |||
|
142 | ||||
|
143 | users_group_member = None | |||
|
144 | for m in users_group.members: | |||
|
145 | if m.user.user_id == user.user_id: | |||
|
146 | # Found this user's membership row | |||
|
147 | users_group_member = m | |||
|
148 | break | |||
|
149 | ||||
|
150 | if users_group_member: | |||
|
151 | try: | |||
|
152 | self.sa.delete(users_group_member) | |||
|
153 | return True | |||
|
154 | except: | |||
|
155 | log.error(traceback.format_exc()) | |||
|
156 | raise | |||
|
157 | else: | |||
|
158 | # User isn't in that group | |||
|
159 | return False | |||
|
160 | ||||
133 | def has_perm(self, users_group, perm): |
|
161 | def has_perm(self, users_group, perm): | |
134 | users_group = self.__get_users_group(users_group) |
|
162 | users_group = self.__get_users_group(users_group) | |
135 | perm = self.__get_perm(perm) |
|
163 | perm = self.__get_perm(perm) |
General Comments 0
You need to be logged in to leave comments.
Login now