Show More
@@ -1,745 +1,745 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2011-2018 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 | import traceback |
|
23 | 23 | |
|
24 | 24 | from rhodecode.lib.utils2 import safe_str, safe_unicode |
|
25 | 25 | from rhodecode.lib.exceptions import ( |
|
26 | 26 | UserGroupAssignedException, RepoGroupAssignmentError) |
|
27 | 27 | from rhodecode.lib.utils2 import ( |
|
28 | 28 | get_current_rhodecode_user, action_logger_generic) |
|
29 | 29 | from rhodecode.model import BaseModel |
|
30 | 30 | from rhodecode.model.scm import UserGroupList |
|
31 | 31 | from rhodecode.model.db import ( |
|
32 | 32 | joinedload, true, func, User, UserGroupMember, UserGroup, |
|
33 | 33 | UserGroupRepoToPerm, Permission, UserGroupToPerm, UserUserGroupToPerm, |
|
34 | 34 | UserGroupUserGroupToPerm, UserGroupRepoGroupToPerm) |
|
35 | 35 | |
|
36 | 36 | |
|
37 | 37 | log = logging.getLogger(__name__) |
|
38 | 38 | |
|
39 | 39 | |
|
40 | 40 | class UserGroupModel(BaseModel): |
|
41 | 41 | |
|
42 | 42 | cls = UserGroup |
|
43 | 43 | |
|
44 | 44 | def _get_user_group(self, user_group): |
|
45 | 45 | return self._get_instance(UserGroup, user_group, |
|
46 | 46 | callback=UserGroup.get_by_group_name) |
|
47 | 47 | |
|
48 | 48 | def _create_default_perms(self, user_group): |
|
49 | 49 | # create default permission |
|
50 | 50 | default_perm = 'usergroup.read' |
|
51 | 51 | def_user = User.get_default_user() |
|
52 | 52 | for p in def_user.user_perms: |
|
53 | 53 | if p.permission.permission_name.startswith('usergroup.'): |
|
54 | 54 | default_perm = p.permission.permission_name |
|
55 | 55 | break |
|
56 | 56 | |
|
57 | 57 | user_group_to_perm = UserUserGroupToPerm() |
|
58 | 58 | user_group_to_perm.permission = Permission.get_by_key(default_perm) |
|
59 | 59 | |
|
60 | 60 | user_group_to_perm.user_group = user_group |
|
61 | 61 | user_group_to_perm.user_id = def_user.user_id |
|
62 | 62 | return user_group_to_perm |
|
63 | 63 | |
|
64 | 64 | def update_permissions( |
|
65 | 65 | self, user_group, perm_additions=None, perm_updates=None, |
|
66 | 66 | perm_deletions=None, check_perms=True, cur_user=None): |
|
67 | 67 | |
|
68 | 68 | from rhodecode.lib.auth import HasUserGroupPermissionAny |
|
69 | 69 | if not perm_additions: |
|
70 | 70 | perm_additions = [] |
|
71 | 71 | if not perm_updates: |
|
72 | 72 | perm_updates = [] |
|
73 | 73 | if not perm_deletions: |
|
74 | 74 | perm_deletions = [] |
|
75 | 75 | |
|
76 | 76 | req_perms = ('usergroup.read', 'usergroup.write', 'usergroup.admin') |
|
77 | 77 | |
|
78 | 78 | changes = { |
|
79 | 79 | 'added': [], |
|
80 | 80 | 'updated': [], |
|
81 | 81 | 'deleted': [] |
|
82 | 82 | } |
|
83 | 83 | change_obj = user_group.get_api_data() |
|
84 | 84 | # update permissions |
|
85 | 85 | for member_id, perm, member_type in perm_updates: |
|
86 | 86 | member_id = int(member_id) |
|
87 | 87 | if member_type == 'user': |
|
88 | 88 | member_name = User.get(member_id).username |
|
89 | 89 | # this updates existing one |
|
90 | 90 | self.grant_user_permission( |
|
91 | 91 | user_group=user_group, user=member_id, perm=perm |
|
92 | 92 | ) |
|
93 | 93 | else: |
|
94 | 94 | # check if we have permissions to alter this usergroup |
|
95 | 95 | member_name = UserGroup.get(member_id).users_group_name |
|
96 | 96 | if not check_perms or HasUserGroupPermissionAny( |
|
97 | 97 | *req_perms)(member_name, user=cur_user): |
|
98 | 98 | self.grant_user_group_permission( |
|
99 | 99 | target_user_group=user_group, user_group=member_id, perm=perm) |
|
100 | 100 | |
|
101 | 101 | changes['updated'].append({ |
|
102 | 102 | 'change_obj': change_obj, |
|
103 | 103 | 'type': member_type, 'id': member_id, |
|
104 | 104 | 'name': member_name, 'new_perm': perm}) |
|
105 | 105 | |
|
106 | 106 | # set new permissions |
|
107 | 107 | for member_id, perm, member_type in perm_additions: |
|
108 | 108 | member_id = int(member_id) |
|
109 | 109 | if member_type == 'user': |
|
110 | 110 | member_name = User.get(member_id).username |
|
111 | 111 | self.grant_user_permission( |
|
112 | 112 | user_group=user_group, user=member_id, perm=perm) |
|
113 | 113 | else: |
|
114 | 114 | # check if we have permissions to alter this usergroup |
|
115 | 115 | member_name = UserGroup.get(member_id).users_group_name |
|
116 | 116 | if not check_perms or HasUserGroupPermissionAny( |
|
117 | 117 | *req_perms)(member_name, user=cur_user): |
|
118 | 118 | self.grant_user_group_permission( |
|
119 | 119 | target_user_group=user_group, user_group=member_id, perm=perm) |
|
120 | 120 | |
|
121 | 121 | changes['added'].append({ |
|
122 | 122 | 'change_obj': change_obj, |
|
123 | 123 | 'type': member_type, 'id': member_id, |
|
124 | 124 | 'name': member_name, 'new_perm': perm}) |
|
125 | 125 | |
|
126 | 126 | # delete permissions |
|
127 | 127 | for member_id, perm, member_type in perm_deletions: |
|
128 | 128 | member_id = int(member_id) |
|
129 | 129 | if member_type == 'user': |
|
130 | 130 | member_name = User.get(member_id).username |
|
131 | 131 | self.revoke_user_permission(user_group=user_group, user=member_id) |
|
132 | 132 | else: |
|
133 | 133 | # check if we have permissions to alter this usergroup |
|
134 | 134 | member_name = UserGroup.get(member_id).users_group_name |
|
135 | 135 | if not check_perms or HasUserGroupPermissionAny( |
|
136 | 136 | *req_perms)(member_name, user=cur_user): |
|
137 | 137 | self.revoke_user_group_permission( |
|
138 | 138 | target_user_group=user_group, user_group=member_id) |
|
139 | 139 | |
|
140 | 140 | changes['deleted'].append({ |
|
141 | 141 | 'change_obj': change_obj, |
|
142 | 142 | 'type': member_type, 'id': member_id, |
|
143 | 143 | 'name': member_name, 'new_perm': perm}) |
|
144 | 144 | |
|
145 | 145 | return changes |
|
146 | 146 | |
|
147 | 147 | def get(self, user_group_id, cache=False): |
|
148 | 148 | return UserGroup.get(user_group_id) |
|
149 | 149 | |
|
150 | 150 | def get_group(self, user_group): |
|
151 | 151 | return self._get_user_group(user_group) |
|
152 | 152 | |
|
153 | 153 | def get_by_name(self, name, cache=False, case_insensitive=False): |
|
154 | 154 | return UserGroup.get_by_group_name(name, cache, case_insensitive) |
|
155 | 155 | |
|
156 | 156 | def create(self, name, description, owner, active=True, group_data=None): |
|
157 | 157 | try: |
|
158 | 158 | new_user_group = UserGroup() |
|
159 | 159 | new_user_group.user = self._get_user(owner) |
|
160 | 160 | new_user_group.users_group_name = name |
|
161 | 161 | new_user_group.user_group_description = description |
|
162 | 162 | new_user_group.users_group_active = active |
|
163 | 163 | if group_data: |
|
164 | 164 | new_user_group.group_data = group_data |
|
165 | 165 | self.sa.add(new_user_group) |
|
166 | 166 | perm_obj = self._create_default_perms(new_user_group) |
|
167 | 167 | self.sa.add(perm_obj) |
|
168 | 168 | |
|
169 | 169 | self.grant_user_permission(user_group=new_user_group, |
|
170 | 170 | user=owner, perm='usergroup.admin') |
|
171 | 171 | |
|
172 | 172 | return new_user_group |
|
173 | 173 | except Exception: |
|
174 | 174 | log.error(traceback.format_exc()) |
|
175 | 175 | raise |
|
176 | 176 | |
|
177 | 177 | def _get_memberships_for_user_ids(self, user_group, user_id_list): |
|
178 | 178 | members = [] |
|
179 | 179 | for user_id in user_id_list: |
|
180 | 180 | member = self._get_membership(user_group.users_group_id, user_id) |
|
181 | 181 | members.append(member) |
|
182 | 182 | return members |
|
183 | 183 | |
|
184 | 184 | def _get_added_and_removed_user_ids(self, user_group, user_id_list): |
|
185 | 185 | current_members = user_group.members or [] |
|
186 | 186 | current_members_ids = [m.user.user_id for m in current_members] |
|
187 | 187 | |
|
188 | 188 | added_members = [ |
|
189 | 189 | user_id for user_id in user_id_list |
|
190 | 190 | if user_id not in current_members_ids] |
|
191 | 191 | if user_id_list == []: |
|
192 | 192 | # all members were deleted |
|
193 | 193 | deleted_members = current_members_ids |
|
194 | 194 | else: |
|
195 | 195 | deleted_members = [ |
|
196 | 196 | user_id for user_id in current_members_ids |
|
197 | 197 | if user_id not in user_id_list] |
|
198 | 198 | |
|
199 | 199 | return added_members, deleted_members |
|
200 | 200 | |
|
201 | 201 | def _set_users_as_members(self, user_group, user_ids): |
|
202 | 202 | user_group.members = [] |
|
203 | 203 | self.sa.flush() |
|
204 | 204 | members = self._get_memberships_for_user_ids( |
|
205 | 205 | user_group, user_ids) |
|
206 | 206 | user_group.members = members |
|
207 | 207 | self.sa.add(user_group) |
|
208 | 208 | |
|
209 | 209 | def _update_members_from_user_ids(self, user_group, user_ids): |
|
210 | 210 | added, removed = self._get_added_and_removed_user_ids( |
|
211 | 211 | user_group, user_ids) |
|
212 | 212 | self._set_users_as_members(user_group, user_ids) |
|
213 | 213 | self._log_user_changes('added to', user_group, added) |
|
214 | 214 | self._log_user_changes('removed from', user_group, removed) |
|
215 | 215 | return added, removed |
|
216 | 216 | |
|
217 | 217 | def _clean_members_data(self, members_data): |
|
218 | 218 | if not members_data: |
|
219 | 219 | members_data = [] |
|
220 | 220 | |
|
221 | 221 | members = [] |
|
222 | 222 | for user in members_data: |
|
223 | 223 | uid = int(user['member_user_id']) |
|
224 | 224 | if uid not in members and user['type'] in ['new', 'existing']: |
|
225 | 225 | members.append(uid) |
|
226 | 226 | return members |
|
227 | 227 | |
|
228 | 228 | def update(self, user_group, form_data, group_data=None): |
|
229 | 229 | user_group = self._get_user_group(user_group) |
|
230 | 230 | if 'users_group_name' in form_data: |
|
231 | 231 | user_group.users_group_name = form_data['users_group_name'] |
|
232 | 232 | if 'users_group_active' in form_data: |
|
233 | 233 | user_group.users_group_active = form_data['users_group_active'] |
|
234 | 234 | if 'user_group_description' in form_data: |
|
235 | 235 | user_group.user_group_description = form_data[ |
|
236 | 236 | 'user_group_description'] |
|
237 | 237 | |
|
238 | 238 | # handle owner change |
|
239 | 239 | if 'user' in form_data: |
|
240 | 240 | owner = form_data['user'] |
|
241 | 241 | if isinstance(owner, basestring): |
|
242 | 242 | owner = User.get_by_username(form_data['user']) |
|
243 | 243 | |
|
244 | 244 | if not isinstance(owner, User): |
|
245 | 245 | raise ValueError( |
|
246 | 246 | 'invalid owner for user group: %s' % form_data['user']) |
|
247 | 247 | |
|
248 | 248 | user_group.user = owner |
|
249 | 249 | |
|
250 | 250 | added_user_ids = [] |
|
251 | 251 | removed_user_ids = [] |
|
252 | 252 | if 'users_group_members' in form_data: |
|
253 | 253 | members_id_list = self._clean_members_data( |
|
254 | 254 | form_data['users_group_members']) |
|
255 | 255 | added_user_ids, removed_user_ids = \ |
|
256 | 256 | self._update_members_from_user_ids(user_group, members_id_list) |
|
257 | 257 | |
|
258 | 258 | if group_data: |
|
259 | 259 | new_group_data = {} |
|
260 | 260 | new_group_data.update(group_data) |
|
261 | 261 | user_group.group_data = new_group_data |
|
262 | 262 | |
|
263 | 263 | self.sa.add(user_group) |
|
264 | 264 | return user_group, added_user_ids, removed_user_ids |
|
265 | 265 | |
|
266 | 266 | def delete(self, user_group, force=False): |
|
267 | 267 | """ |
|
268 | 268 | Deletes repository group, unless force flag is used |
|
269 | 269 | raises exception if there are members in that group, else deletes |
|
270 | 270 | group and users |
|
271 | 271 | |
|
272 | 272 | :param user_group: |
|
273 | 273 | :param force: |
|
274 | 274 | """ |
|
275 | 275 | user_group = self._get_user_group(user_group) |
|
276 | 276 | if not user_group: |
|
277 | 277 | return |
|
278 | 278 | |
|
279 | 279 | try: |
|
280 | 280 | # check if this group is not assigned to repo |
|
281 | 281 | assigned_to_repo = [x.repository for x in UserGroupRepoToPerm.query()\ |
|
282 | 282 | .filter(UserGroupRepoToPerm.users_group == user_group).all()] |
|
283 | 283 | # check if this group is not assigned to repo |
|
284 | 284 | assigned_to_repo_group = [x.group for x in UserGroupRepoGroupToPerm.query()\ |
|
285 | 285 | .filter(UserGroupRepoGroupToPerm.users_group == user_group).all()] |
|
286 | 286 | |
|
287 | 287 | if (assigned_to_repo or assigned_to_repo_group) and not force: |
|
288 | 288 | assigned = ','.join(map(safe_str, |
|
289 | 289 | assigned_to_repo+assigned_to_repo_group)) |
|
290 | 290 | |
|
291 | 291 | raise UserGroupAssignedException( |
|
292 | 292 | 'UserGroup assigned to %s' % (assigned,)) |
|
293 | 293 | self.sa.delete(user_group) |
|
294 | 294 | except Exception: |
|
295 | 295 | log.error(traceback.format_exc()) |
|
296 | 296 | raise |
|
297 | 297 | |
|
298 | 298 | def _log_user_changes(self, action, user_group, user_or_users): |
|
299 | 299 | users = user_or_users |
|
300 | 300 | if not isinstance(users, (list, tuple)): |
|
301 | 301 | users = [users] |
|
302 | 302 | |
|
303 | 303 | group_name = user_group.users_group_name |
|
304 | 304 | |
|
305 | 305 | for user_or_user_id in users: |
|
306 | 306 | user = self._get_user(user_or_user_id) |
|
307 | 307 | log_text = 'User {user} {action} {group}'.format( |
|
308 | 308 | action=action, user=user.username, group=group_name) |
|
309 | 309 | action_logger_generic(log_text) |
|
310 | 310 | |
|
311 | 311 | def _find_user_in_group(self, user, user_group): |
|
312 | 312 | user_group_member = None |
|
313 | 313 | for m in user_group.members: |
|
314 | 314 | if m.user_id == user.user_id: |
|
315 | 315 | # Found this user's membership row |
|
316 | 316 | user_group_member = m |
|
317 | 317 | break |
|
318 | 318 | |
|
319 | 319 | return user_group_member |
|
320 | 320 | |
|
321 | 321 | def _get_membership(self, user_group_id, user_id): |
|
322 | 322 | user_group_member = UserGroupMember(user_group_id, user_id) |
|
323 | 323 | return user_group_member |
|
324 | 324 | |
|
325 | 325 | def add_user_to_group(self, user_group, user): |
|
326 | 326 | user_group = self._get_user_group(user_group) |
|
327 | 327 | user = self._get_user(user) |
|
328 | 328 | user_member = self._find_user_in_group(user, user_group) |
|
329 | 329 | if user_member: |
|
330 | 330 | # user already in the group, skip |
|
331 | 331 | return True |
|
332 | 332 | |
|
333 | 333 | member = self._get_membership( |
|
334 | 334 | user_group.users_group_id, user.user_id) |
|
335 | 335 | user_group.members.append(member) |
|
336 | 336 | |
|
337 | 337 | try: |
|
338 | 338 | self.sa.add(member) |
|
339 | 339 | except Exception: |
|
340 | 340 | # what could go wrong here? |
|
341 | 341 | log.error(traceback.format_exc()) |
|
342 | 342 | raise |
|
343 | 343 | |
|
344 | 344 | self._log_user_changes('added to', user_group, user) |
|
345 | 345 | return member |
|
346 | 346 | |
|
347 | 347 | def remove_user_from_group(self, user_group, user): |
|
348 | 348 | user_group = self._get_user_group(user_group) |
|
349 | 349 | user = self._get_user(user) |
|
350 | 350 | user_group_member = self._find_user_in_group(user, user_group) |
|
351 | 351 | |
|
352 | 352 | if not user_group_member: |
|
353 | 353 | # User isn't in that group |
|
354 | 354 | return False |
|
355 | 355 | |
|
356 | 356 | try: |
|
357 | 357 | self.sa.delete(user_group_member) |
|
358 | 358 | except Exception: |
|
359 | 359 | log.error(traceback.format_exc()) |
|
360 | 360 | raise |
|
361 | 361 | |
|
362 | 362 | self._log_user_changes('removed from', user_group, user) |
|
363 | 363 | return True |
|
364 | 364 | |
|
365 | 365 | def has_perm(self, user_group, perm): |
|
366 | 366 | user_group = self._get_user_group(user_group) |
|
367 | 367 | perm = self._get_perm(perm) |
|
368 | 368 | |
|
369 | 369 | return UserGroupToPerm.query()\ |
|
370 | 370 | .filter(UserGroupToPerm.users_group == user_group)\ |
|
371 | 371 | .filter(UserGroupToPerm.permission == perm).scalar() is not None |
|
372 | 372 | |
|
373 | 373 | def grant_perm(self, user_group, perm): |
|
374 | 374 | user_group = self._get_user_group(user_group) |
|
375 | 375 | perm = self._get_perm(perm) |
|
376 | 376 | |
|
377 | 377 | # if this permission is already granted skip it |
|
378 | 378 | _perm = UserGroupToPerm.query()\ |
|
379 | 379 | .filter(UserGroupToPerm.users_group == user_group)\ |
|
380 | 380 | .filter(UserGroupToPerm.permission == perm)\ |
|
381 | 381 | .scalar() |
|
382 | 382 | if _perm: |
|
383 | 383 | return |
|
384 | 384 | |
|
385 | 385 | new = UserGroupToPerm() |
|
386 | 386 | new.users_group = user_group |
|
387 | 387 | new.permission = perm |
|
388 | 388 | self.sa.add(new) |
|
389 | 389 | return new |
|
390 | 390 | |
|
391 | 391 | def revoke_perm(self, user_group, perm): |
|
392 | 392 | user_group = self._get_user_group(user_group) |
|
393 | 393 | perm = self._get_perm(perm) |
|
394 | 394 | |
|
395 | 395 | obj = UserGroupToPerm.query()\ |
|
396 | 396 | .filter(UserGroupToPerm.users_group == user_group)\ |
|
397 | 397 | .filter(UserGroupToPerm.permission == perm).scalar() |
|
398 | 398 | if obj: |
|
399 | 399 | self.sa.delete(obj) |
|
400 | 400 | |
|
401 | 401 | def grant_user_permission(self, user_group, user, perm): |
|
402 | 402 | """ |
|
403 | 403 | Grant permission for user on given user group, or update |
|
404 | 404 | existing one if found |
|
405 | 405 | |
|
406 | 406 | :param user_group: Instance of UserGroup, users_group_id, |
|
407 | 407 | or users_group_name |
|
408 | 408 | :param user: Instance of User, user_id or username |
|
409 | 409 | :param perm: Instance of Permission, or permission_name |
|
410 | 410 | """ |
|
411 | 411 | changes = { |
|
412 | 412 | 'added': [], |
|
413 | 413 | 'updated': [], |
|
414 | 414 | 'deleted': [] |
|
415 | 415 | } |
|
416 | 416 | |
|
417 | 417 | user_group = self._get_user_group(user_group) |
|
418 | 418 | user = self._get_user(user) |
|
419 | 419 | permission = self._get_perm(perm) |
|
420 | 420 | perm_name = permission.permission_name |
|
421 | 421 | member_id = user.user_id |
|
422 | 422 | member_name = user.username |
|
423 | 423 | |
|
424 | 424 | # check if we have that permission already |
|
425 | 425 | obj = self.sa.query(UserUserGroupToPerm)\ |
|
426 | 426 | .filter(UserUserGroupToPerm.user == user)\ |
|
427 | 427 | .filter(UserUserGroupToPerm.user_group == user_group)\ |
|
428 | 428 | .scalar() |
|
429 | 429 | if obj is None: |
|
430 | 430 | # create new ! |
|
431 | 431 | obj = UserUserGroupToPerm() |
|
432 | 432 | obj.user_group = user_group |
|
433 | 433 | obj.user = user |
|
434 | 434 | obj.permission = permission |
|
435 | 435 | self.sa.add(obj) |
|
436 | 436 | log.debug('Granted perm %s to %s on %s', perm, user, user_group) |
|
437 | 437 | action_logger_generic( |
|
438 | 438 | 'granted permission: {} to user: {} on usergroup: {}'.format( |
|
439 | 439 | perm, user, user_group), namespace='security.usergroup') |
|
440 | 440 | |
|
441 | 441 | changes['added'].append({ |
|
442 | 442 | 'change_obj': user_group.get_api_data(), |
|
443 | 443 | 'type': 'user', 'id': member_id, |
|
444 | 444 | 'name': member_name, 'new_perm': perm_name}) |
|
445 | 445 | |
|
446 | 446 | return changes |
|
447 | 447 | |
|
448 | 448 | def revoke_user_permission(self, user_group, user): |
|
449 | 449 | """ |
|
450 | 450 | Revoke permission for user on given user group |
|
451 | 451 | |
|
452 | 452 | :param user_group: Instance of UserGroup, users_group_id, |
|
453 | 453 | or users_group name |
|
454 | 454 | :param user: Instance of User, user_id or username |
|
455 | 455 | """ |
|
456 | 456 | changes = { |
|
457 | 457 | 'added': [], |
|
458 | 458 | 'updated': [], |
|
459 | 459 | 'deleted': [] |
|
460 | 460 | } |
|
461 | 461 | |
|
462 | 462 | user_group = self._get_user_group(user_group) |
|
463 | 463 | user = self._get_user(user) |
|
464 | 464 | perm_name = 'usergroup.none' |
|
465 | 465 | member_id = user.user_id |
|
466 | 466 | member_name = user.username |
|
467 | 467 | |
|
468 | 468 | obj = self.sa.query(UserUserGroupToPerm)\ |
|
469 | 469 | .filter(UserUserGroupToPerm.user == user)\ |
|
470 | 470 | .filter(UserUserGroupToPerm.user_group == user_group)\ |
|
471 | 471 | .scalar() |
|
472 | 472 | if obj: |
|
473 | 473 | self.sa.delete(obj) |
|
474 | 474 | log.debug('Revoked perm on %s on %s', user_group, user) |
|
475 | 475 | action_logger_generic( |
|
476 | 476 | 'revoked permission from user: {} on usergroup: {}'.format( |
|
477 | 477 | user, user_group), namespace='security.usergroup') |
|
478 | 478 | |
|
479 | 479 | changes['deleted'].append({ |
|
480 | 480 | 'change_obj': user_group.get_api_data(), |
|
481 | 481 | 'type': 'user', 'id': member_id, |
|
482 | 482 | 'name': member_name, 'new_perm': perm_name}) |
|
483 | 483 | |
|
484 | 484 | return changes |
|
485 | 485 | |
|
486 | 486 | def grant_user_group_permission(self, target_user_group, user_group, perm): |
|
487 | 487 | """ |
|
488 | 488 | Grant user group permission for given target_user_group |
|
489 | 489 | |
|
490 | 490 | :param target_user_group: |
|
491 | 491 | :param user_group: |
|
492 | 492 | :param perm: |
|
493 | 493 | """ |
|
494 | 494 | changes = { |
|
495 | 495 | 'added': [], |
|
496 | 496 | 'updated': [], |
|
497 | 497 | 'deleted': [] |
|
498 | 498 | } |
|
499 | 499 | |
|
500 | 500 | target_user_group = self._get_user_group(target_user_group) |
|
501 | 501 | user_group = self._get_user_group(user_group) |
|
502 | 502 | permission = self._get_perm(perm) |
|
503 | 503 | perm_name = permission.permission_name |
|
504 | 504 | member_id = user_group.users_group_id |
|
505 | 505 | member_name = user_group.users_group_name |
|
506 | 506 | |
|
507 | 507 | # forbid assigning same user group to itself |
|
508 | 508 | if target_user_group == user_group: |
|
509 | 509 | raise RepoGroupAssignmentError('target repo:%s cannot be ' |
|
510 | 510 | 'assigned to itself' % target_user_group) |
|
511 | 511 | |
|
512 | 512 | # check if we have that permission already |
|
513 | 513 | obj = self.sa.query(UserGroupUserGroupToPerm)\ |
|
514 | 514 | .filter(UserGroupUserGroupToPerm.target_user_group == target_user_group)\ |
|
515 | 515 | .filter(UserGroupUserGroupToPerm.user_group == user_group)\ |
|
516 | 516 | .scalar() |
|
517 | 517 | if obj is None: |
|
518 | 518 | # create new ! |
|
519 | 519 | obj = UserGroupUserGroupToPerm() |
|
520 | 520 | obj.user_group = user_group |
|
521 | 521 | obj.target_user_group = target_user_group |
|
522 | 522 | obj.permission = permission |
|
523 | 523 | self.sa.add(obj) |
|
524 | 524 | log.debug( |
|
525 | 525 | 'Granted perm %s to %s on %s', perm, target_user_group, user_group) |
|
526 | 526 | action_logger_generic( |
|
527 | 527 | 'granted permission: {} to usergroup: {} on usergroup: {}'.format( |
|
528 | 528 | perm, user_group, target_user_group), |
|
529 | 529 | namespace='security.usergroup') |
|
530 | 530 | |
|
531 | 531 | changes['added'].append({ |
|
532 | 532 | 'change_obj': target_user_group.get_api_data(), |
|
533 | 533 | 'type': 'user_group', 'id': member_id, |
|
534 | 534 | 'name': member_name, 'new_perm': perm_name}) |
|
535 | 535 | |
|
536 | 536 | return changes |
|
537 | 537 | |
|
538 | 538 | def revoke_user_group_permission(self, target_user_group, user_group): |
|
539 | 539 | """ |
|
540 | 540 | Revoke user group permission for given target_user_group |
|
541 | 541 | |
|
542 | 542 | :param target_user_group: |
|
543 | 543 | :param user_group: |
|
544 | 544 | """ |
|
545 | 545 | changes = { |
|
546 | 546 | 'added': [], |
|
547 | 547 | 'updated': [], |
|
548 | 548 | 'deleted': [] |
|
549 | 549 | } |
|
550 | 550 | |
|
551 | 551 | target_user_group = self._get_user_group(target_user_group) |
|
552 | 552 | user_group = self._get_user_group(user_group) |
|
553 | 553 | perm_name = 'usergroup.none' |
|
554 | 554 | member_id = user_group.users_group_id |
|
555 | 555 | member_name = user_group.users_group_name |
|
556 | 556 | |
|
557 | 557 | obj = self.sa.query(UserGroupUserGroupToPerm)\ |
|
558 | 558 | .filter(UserGroupUserGroupToPerm.target_user_group == target_user_group)\ |
|
559 | 559 | .filter(UserGroupUserGroupToPerm.user_group == user_group)\ |
|
560 | 560 | .scalar() |
|
561 | 561 | if obj: |
|
562 | 562 | self.sa.delete(obj) |
|
563 | 563 | log.debug( |
|
564 | 564 | 'Revoked perm on %s on %s', target_user_group, user_group) |
|
565 | 565 | action_logger_generic( |
|
566 | 566 | 'revoked permission from usergroup: {} on usergroup: {}'.format( |
|
567 | 567 | user_group, target_user_group), |
|
568 | 568 | namespace='security.repogroup') |
|
569 | 569 | |
|
570 | 570 | changes['deleted'].append({ |
|
571 | 571 | 'change_obj': target_user_group.get_api_data(), |
|
572 | 572 | 'type': 'user_group', 'id': member_id, |
|
573 | 573 | 'name': member_name, 'new_perm': perm_name}) |
|
574 | 574 | |
|
575 | 575 | return changes |
|
576 | 576 | |
|
577 | 577 | def get_perms_summary(self, user_group_id): |
|
578 | 578 | permissions = { |
|
579 | 579 | 'repositories': {}, |
|
580 | 580 | 'repositories_groups': {}, |
|
581 | 581 | } |
|
582 | 582 | ugroup_repo_perms = UserGroupRepoToPerm.query()\ |
|
583 | 583 | .options(joinedload(UserGroupRepoToPerm.permission))\ |
|
584 | 584 | .options(joinedload(UserGroupRepoToPerm.repository))\ |
|
585 | 585 | .filter(UserGroupRepoToPerm.users_group_id == user_group_id)\ |
|
586 | 586 | .all() |
|
587 | 587 | |
|
588 | 588 | for gr in ugroup_repo_perms: |
|
589 | 589 | permissions['repositories'][gr.repository.repo_name] \ |
|
590 | 590 | = gr.permission.permission_name |
|
591 | 591 | |
|
592 | 592 | ugroup_group_perms = UserGroupRepoGroupToPerm.query()\ |
|
593 | 593 | .options(joinedload(UserGroupRepoGroupToPerm.permission))\ |
|
594 | 594 | .options(joinedload(UserGroupRepoGroupToPerm.group))\ |
|
595 | 595 | .filter(UserGroupRepoGroupToPerm.users_group_id == user_group_id)\ |
|
596 | 596 | .all() |
|
597 | 597 | |
|
598 | 598 | for gr in ugroup_group_perms: |
|
599 | 599 | permissions['repositories_groups'][gr.group.group_name] \ |
|
600 | 600 | = gr.permission.permission_name |
|
601 | 601 | return permissions |
|
602 | 602 | |
|
603 | 603 | def enforce_groups(self, user, groups, extern_type=None): |
|
604 | 604 | user = self._get_user(user) |
|
605 | 605 | current_groups = user.group_member |
|
606 | 606 | |
|
607 | 607 | # find the external created groups, i.e automatically created |
|
608 | 608 | log.debug('Enforcing user group set `%s` on user %s', groups, user) |
|
609 | 609 | # calculate from what groups user should be removed |
|
610 | 610 | # external_groups that are not in groups |
|
611 | 611 | for gr in [x.users_group for x in current_groups]: |
|
612 | 612 | managed = gr.group_data.get('extern_type') |
|
613 | 613 | if managed: |
|
614 | 614 | if gr.users_group_name not in groups: |
|
615 | 615 | log.debug('Removing user %s from user group %s. ' |
|
616 | 616 | 'Group sync managed by: %s', user, gr, managed) |
|
617 | 617 | self.remove_user_from_group(gr, user) |
|
618 | 618 | else: |
|
619 | 619 | log.debug('Skipping removal from group %s since it is ' |
|
620 | 620 | 'not set to be automatically synchronized' % gr) |
|
621 | 621 | |
|
622 | 622 | # now we calculate in which groups user should be == groups params |
|
623 | 623 | owner = User.get_first_super_admin().username |
|
624 | 624 | for gr in set(groups): |
|
625 | 625 | existing_group = UserGroup.get_by_group_name(gr) |
|
626 | 626 | if not existing_group: |
|
627 | 627 | desc = 'Automatically created from plugin:%s' % extern_type |
|
628 | 628 | # we use first admin account to set the owner of the group |
|
629 | 629 | existing_group = UserGroupModel().create( |
|
630 | 630 | gr, desc, owner, group_data={'extern_type': extern_type}) |
|
631 | 631 | |
|
632 | 632 | # we can only add users to groups which have set sync flag via |
|
633 | 633 | # extern_type attribute. |
|
634 | 634 | # This is either set and created via plugins, or manually |
|
635 | 635 | managed = existing_group.group_data.get('extern_type') |
|
636 | 636 | if managed: |
|
637 | 637 | log.debug('Adding user %s to user group %s', user, gr) |
|
638 | 638 | UserGroupModel().add_user_to_group(existing_group, user) |
|
639 | 639 | else: |
|
640 | 640 | log.debug('Skipping addition to group %s since it is ' |
|
641 | 641 | 'not set to be automatically synchronized' % gr) |
|
642 | 642 | |
|
643 | 643 | def change_groups(self, user, groups): |
|
644 | 644 | """ |
|
645 | 645 | This method changes user group assignment |
|
646 | 646 | :param user: User |
|
647 | 647 | :param groups: array of UserGroupModel |
|
648 | 648 | """ |
|
649 | 649 | user = self._get_user(user) |
|
650 | 650 | log.debug('Changing user(%s) assignment to groups(%s)', user, groups) |
|
651 | 651 | current_groups = user.group_member |
|
652 | 652 | current_groups = [x.users_group for x in current_groups] |
|
653 | 653 | |
|
654 | 654 | # calculate from what groups user should be removed/add |
|
655 | 655 | groups = set(groups) |
|
656 | 656 | current_groups = set(current_groups) |
|
657 | 657 | |
|
658 | 658 | groups_to_remove = current_groups - groups |
|
659 | 659 | groups_to_add = groups - current_groups |
|
660 | 660 | |
|
661 | 661 | removed_from_groups = [] |
|
662 | 662 | added_to_groups = [] |
|
663 | 663 | for gr in groups_to_remove: |
|
664 | 664 | log.debug('Removing user %s from user group %s', |
|
665 | 665 | user.username, gr.users_group_name) |
|
666 | 666 | removed_from_groups.append(gr.users_group_id) |
|
667 | 667 | self.remove_user_from_group(gr.users_group_name, user.username) |
|
668 | 668 | for gr in groups_to_add: |
|
669 | 669 | log.debug('Adding user %s to user group %s', |
|
670 | 670 | user.username, gr.users_group_name) |
|
671 | 671 | added_to_groups.append(gr.users_group_id) |
|
672 | 672 | UserGroupModel().add_user_to_group( |
|
673 | 673 | gr.users_group_name, user.username) |
|
674 | 674 | |
|
675 | 675 | return added_to_groups, removed_from_groups |
|
676 | 676 | |
|
677 | 677 | def _serialize_user_group(self, user_group): |
|
678 | 678 | import rhodecode.lib.helpers as h |
|
679 | 679 | return { |
|
680 | 680 | 'id': user_group.users_group_id, |
|
681 | 681 | # TODO: marcink figure out a way to generate the url for the |
|
682 | 682 | # icon |
|
683 | 683 | 'icon_link': '', |
|
684 | 684 | 'value_display': 'Group: %s (%d members)' % ( |
|
685 | 685 | user_group.users_group_name, len(user_group.members),), |
|
686 | 686 | 'value': user_group.users_group_name, |
|
687 | 687 | 'description': user_group.user_group_description, |
|
688 | 688 | 'owner': user_group.user.username, |
|
689 | 689 | |
|
690 | 690 | 'owner_icon': h.gravatar_url(user_group.user.email, 30), |
|
691 | 691 | 'value_display_owner': h.person(user_group.user.email), |
|
692 | 692 | |
|
693 | 693 | 'value_type': 'user_group', |
|
694 | 694 | 'active': user_group.users_group_active, |
|
695 | 695 | } |
|
696 | 696 | |
|
697 | 697 | def get_user_groups(self, name_contains=None, limit=20, only_active=True, |
|
698 | 698 | expand_groups=False): |
|
699 | 699 | query = self.sa.query(UserGroup) |
|
700 | 700 | if only_active: |
|
701 | 701 | query = query.filter(UserGroup.users_group_active == true()) |
|
702 | 702 | |
|
703 | 703 | if name_contains: |
|
704 | 704 | ilike_expression = u'%{}%'.format(safe_unicode(name_contains)) |
|
705 | 705 | query = query.filter( |
|
706 | 706 | UserGroup.users_group_name.ilike(ilike_expression))\ |
|
707 | 707 | .order_by(func.length(UserGroup.users_group_name))\ |
|
708 | 708 | .order_by(UserGroup.users_group_name) |
|
709 | 709 | |
|
710 | 710 | query = query.limit(limit) |
|
711 | 711 | user_groups = query.all() |
|
712 | 712 | perm_set = ['usergroup.read', 'usergroup.write', 'usergroup.admin'] |
|
713 | 713 | user_groups = UserGroupList(user_groups, perm_set=perm_set) |
|
714 | 714 | |
|
715 | 715 | # store same serialize method to extract data from User |
|
716 | 716 | from rhodecode.model.user import UserModel |
|
717 | 717 | serialize_user = UserModel()._serialize_user |
|
718 | 718 | |
|
719 | 719 | _groups = [] |
|
720 | 720 | for group in user_groups: |
|
721 | 721 | entry = self._serialize_user_group(group) |
|
722 | 722 | if expand_groups: |
|
723 | 723 | expanded_members = [] |
|
724 | 724 | for member in group.members: |
|
725 | 725 | expanded_members.append(serialize_user(member.user)) |
|
726 | 726 | entry['members'] = expanded_members |
|
727 | 727 | _groups.append(entry) |
|
728 | 728 | return _groups |
|
729 | 729 | |
|
730 | 730 | @staticmethod |
|
731 | 731 | def get_user_groups_as_dict(user_group): |
|
732 | 732 | import rhodecode.lib.helpers as h |
|
733 | 733 | |
|
734 | 734 | data = { |
|
735 | 735 | 'users_group_id': user_group.users_group_id, |
|
736 | 'group_name': user_group.users_group_name, | |
|
736 | 'group_name': h.link_to_group(user_group.users_group_name), | |
|
737 | 737 | 'group_description': user_group.user_group_description, |
|
738 | 738 | 'active': user_group.users_group_active, |
|
739 | 739 | "owner": user_group.user.username, |
|
740 | 740 | 'owner_icon': h.gravatar_url(user_group.user.email, 30), |
|
741 | 741 | "owner_data": { |
|
742 | 742 | 'owner': user_group.user.username, |
|
743 | 743 | 'owner_icon': h.gravatar_url(user_group.user.email, 30)} |
|
744 | 744 | } |
|
745 | 745 | return data |
General Comments 0
You need to be logged in to leave comments.
Login now