##// END OF EJS Templates
permissions: flush members of user groups permissions to clear caches....
marcink -
r3153:37902585 default
parent child Browse files
Show More
@@ -1,109 +1,116 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
23 23 from pyramid.view import view_config
24 24 from pyramid.httpexceptions import HTTPFound
25 25
26 26 from rhodecode import events
27 27 from rhodecode.apps._base import RepoGroupAppView
28 28 from rhodecode.lib import helpers as h
29 29 from rhodecode.lib import audit_logger
30 30 from rhodecode.lib.auth import (
31 31 LoginRequired, HasRepoGroupPermissionAnyDecorator, CSRFRequired)
32 from rhodecode.lib.utils2 import safe_int
33 from rhodecode.model.db import UserGroup
32 34 from rhodecode.model.repo_group import RepoGroupModel
33 35 from rhodecode.model.forms import RepoGroupPermsForm
34 36 from rhodecode.model.meta import Session
35 37
36 38 log = logging.getLogger(__name__)
37 39
38 40
39 41 class RepoGroupPermissionsView(RepoGroupAppView):
40 42 def load_default_context(self):
41 43 c = self._get_local_tmpl_context()
42 44
43 45 return c
44 46
45 47 @LoginRequired()
46 48 @HasRepoGroupPermissionAnyDecorator('group.admin')
47 49 @view_config(
48 50 route_name='edit_repo_group_perms', request_method='GET',
49 51 renderer='rhodecode:templates/admin/repo_groups/repo_group_edit.mako')
50 52 def edit_repo_group_permissions(self):
51 53 c = self.load_default_context()
52 54 c.active = 'permissions'
53 55 c.repo_group = self.db_repo_group
54 56 return self._get_template_context(c)
55 57
56 58 @LoginRequired()
57 59 @HasRepoGroupPermissionAnyDecorator('group.admin')
58 60 @CSRFRequired()
59 61 @view_config(
60 62 route_name='edit_repo_group_perms_update', request_method='POST',
61 63 renderer='rhodecode:templates/admin/repo_groups/repo_group_edit.mako')
62 64 def edit_repo_groups_permissions_update(self):
63 65 _ = self.request.translate
64 66 c = self.load_default_context()
65 67 c.active = 'perms'
66 68 c.repo_group = self.db_repo_group
67 69
68 70 valid_recursive_choices = ['none', 'repos', 'groups', 'all']
69 71 form = RepoGroupPermsForm(self.request.translate, valid_recursive_choices)()\
70 72 .to_python(self.request.POST)
71 73
72 74 if not c.rhodecode_user.is_admin:
73 75 if self._revoke_perms_on_yourself(form):
74 76 msg = _('Cannot change permission for yourself as admin')
75 77 h.flash(msg, category='warning')
76 78 raise HTTPFound(
77 79 h.route_path('edit_repo_group_perms',
78 80 repo_group_name=self.db_repo_group_name))
79 81
80 82 # iterate over all members(if in recursive mode) of this groups and
81 83 # set the permissions !
82 84 # this can be potentially heavy operation
83 85 changes = RepoGroupModel().update_permissions(
84 86 c.repo_group,
85 87 form['perm_additions'], form['perm_updates'], form['perm_deletions'],
86 88 form['recursive'])
87 89
88 90 action_data = {
89 91 'added': changes['added'],
90 92 'updated': changes['updated'],
91 93 'deleted': changes['deleted'],
92 94 }
93 95 audit_logger.store_web(
94 96 'repo_group.edit.permissions', action_data=action_data,
95 97 user=c.rhodecode_user)
96 98
97 99 Session().commit()
98 100 h.flash(_('Repository Group permissions updated'), category='success')
99 101
100 102 affected_user_ids = []
101 103 for change in changes['added'] + changes['updated'] + changes['deleted']:
102 104 if change['type'] == 'user':
103 105 affected_user_ids.append(change['id'])
106 if change['type'] == 'user_group':
107 user_group = UserGroup.get(safe_int(change['id']))
108 if user_group:
109 group_members_ids = [x.user_id for x in user_group.members]
110 affected_user_ids.extend(group_members_ids)
104 111
105 112 events.trigger(events.UserPermissionsChange(affected_user_ids))
106 113
107 114 raise HTTPFound(
108 115 h.route_path('edit_repo_group_perms',
109 116 repo_group_name=self.db_repo_group_name))
@@ -1,95 +1,102 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
23 23 from pyramid.httpexceptions import HTTPFound
24 24 from pyramid.view import view_config
25 25
26 26 from rhodecode import events
27 27 from rhodecode.apps._base import RepoAppView
28 28 from rhodecode.lib import helpers as h
29 29 from rhodecode.lib import audit_logger
30 30 from rhodecode.lib.auth import (
31 31 LoginRequired, HasRepoPermissionAnyDecorator, CSRFRequired)
32 from rhodecode.lib.utils2 import safe_int
33 from rhodecode.model.db import UserGroup
32 34 from rhodecode.model.forms import RepoPermsForm
33 35 from rhodecode.model.meta import Session
34 36 from rhodecode.model.repo import RepoModel
35 37
36 38 log = logging.getLogger(__name__)
37 39
38 40
39 41 class RepoSettingsPermissionsView(RepoAppView):
40 42
41 43 def load_default_context(self):
42 44 c = self._get_local_tmpl_context()
43 45 return c
44 46
45 47 @LoginRequired()
46 48 @HasRepoPermissionAnyDecorator('repository.admin')
47 49 @view_config(
48 50 route_name='edit_repo_perms', request_method='GET',
49 51 renderer='rhodecode:templates/admin/repos/repo_edit.mako')
50 52 def edit_permissions(self):
51 53 c = self.load_default_context()
52 54 c.active = 'permissions'
53 55 return self._get_template_context(c)
54 56
55 57 @LoginRequired()
56 58 @HasRepoPermissionAnyDecorator('repository.admin')
57 59 @CSRFRequired()
58 60 @view_config(
59 61 route_name='edit_repo_perms', request_method='POST',
60 62 renderer='rhodecode:templates/admin/repos/repo_edit.mako')
61 63 def edit_permissions_update(self):
62 64 _ = self.request.translate
63 65 c = self.load_default_context()
64 66 c.active = 'permissions'
65 67 data = self.request.POST
66 68 # store private flag outside of HTML to verify if we can modify
67 69 # default user permissions, prevents submission of FAKE post data
68 70 # into the form for private repos
69 71 data['repo_private'] = self.db_repo.private
70 72 form = RepoPermsForm(self.request.translate)().to_python(data)
71 73 changes = RepoModel().update_permissions(
72 74 self.db_repo_name, form['perm_additions'], form['perm_updates'],
73 75 form['perm_deletions'])
74 76
75 77 action_data = {
76 78 'added': changes['added'],
77 79 'updated': changes['updated'],
78 80 'deleted': changes['deleted'],
79 81 }
80 82 audit_logger.store_web(
81 83 'repo.edit.permissions', action_data=action_data,
82 84 user=self._rhodecode_user, repo=self.db_repo)
83 85
84 86 Session().commit()
85 87 h.flash(_('Repository permissions updated'), category='success')
86 88
87 89 affected_user_ids = []
88 90 for change in changes['added'] + changes['updated'] + changes['deleted']:
89 91 if change['type'] == 'user':
90 92 affected_user_ids.append(change['id'])
93 if change['type'] == 'user_group':
94 user_group = UserGroup.get(safe_int(change['id']))
95 if user_group:
96 group_members_ids = [x.user_id for x in user_group.members]
97 affected_user_ids.extend(group_members_ids)
91 98
92 99 events.trigger(events.UserPermissionsChange(affected_user_ids))
93 100
94 101 raise HTTPFound(
95 102 h.route_path('edit_repo_perms', repo_name=self.db_repo_name))
@@ -1,545 +1,550 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2016-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
23 23 import peppercorn
24 24 import formencode
25 25 import formencode.htmlfill
26 26 from pyramid.httpexceptions import HTTPFound
27 27 from pyramid.view import view_config
28 28 from pyramid.response import Response
29 29 from pyramid.renderers import render
30 30
31 31 from rhodecode import events
32 32 from rhodecode.lib.exceptions import (
33 33 RepoGroupAssignmentError, UserGroupAssignedException)
34 34 from rhodecode.model.forms import (
35 35 UserGroupPermsForm, UserGroupForm, UserIndividualPermissionsForm,
36 36 UserPermissionsForm)
37 37 from rhodecode.model.permission import PermissionModel
38 38
39 39 from rhodecode.apps._base import UserGroupAppView
40 40 from rhodecode.lib.auth import (
41 41 LoginRequired, HasUserGroupPermissionAnyDecorator, CSRFRequired)
42 42 from rhodecode.lib import helpers as h, audit_logger
43 from rhodecode.lib.utils2 import str2bool
44 from rhodecode.model.db import User
43 from rhodecode.lib.utils2 import str2bool, safe_int
44 from rhodecode.model.db import User, UserGroup
45 45 from rhodecode.model.meta import Session
46 46 from rhodecode.model.user_group import UserGroupModel
47 47
48 48 log = logging.getLogger(__name__)
49 49
50 50
51 51 class UserGroupsView(UserGroupAppView):
52 52
53 53 def load_default_context(self):
54 54 c = self._get_local_tmpl_context()
55 55
56 56 PermissionModel().set_global_permission_choices(
57 57 c, gettext_translator=self.request.translate)
58 58
59 59 return c
60 60
61 61 @LoginRequired()
62 62 @HasUserGroupPermissionAnyDecorator('usergroup.admin')
63 63 @view_config(
64 64 route_name='user_group_members_data', request_method='GET',
65 65 renderer='json_ext', xhr=True)
66 66 def user_group_members(self):
67 67 """
68 68 Return members of given user group
69 69 """
70 70 self.load_default_context()
71 71 user_group = self.db_user_group
72 72 group_members_obj = sorted((x.user for x in user_group.members),
73 73 key=lambda u: u.username.lower())
74 74
75 75 group_members = [
76 76 {
77 77 'id': user.user_id,
78 78 'first_name': user.first_name,
79 79 'last_name': user.last_name,
80 80 'username': user.username,
81 81 'icon_link': h.gravatar_url(user.email, 30),
82 82 'value_display': h.person(user.email),
83 83 'value': user.username,
84 84 'value_type': 'user',
85 85 'active': user.active,
86 86 }
87 87 for user in group_members_obj
88 88 ]
89 89
90 90 return {
91 91 'members': group_members
92 92 }
93 93
94 94 @LoginRequired()
95 95 @HasUserGroupPermissionAnyDecorator('usergroup.admin')
96 96 @view_config(
97 97 route_name='edit_user_group_perms_summary', request_method='GET',
98 98 renderer='rhodecode:templates/admin/user_groups/user_group_edit.mako')
99 99 def user_group_perms_summary(self):
100 100 c = self.load_default_context()
101 101 c.user_group = self.db_user_group
102 102 c.active = 'perms_summary'
103 103 c.permissions = UserGroupModel().get_perms_summary(
104 104 c.user_group.users_group_id)
105 105 return self._get_template_context(c)
106 106
107 107 @LoginRequired()
108 108 @HasUserGroupPermissionAnyDecorator('usergroup.admin')
109 109 @view_config(
110 110 route_name='edit_user_group_perms_summary_json', request_method='GET',
111 111 renderer='json_ext')
112 112 def user_group_perms_summary_json(self):
113 113 self.load_default_context()
114 114 user_group = self.db_user_group
115 115 return UserGroupModel().get_perms_summary(user_group.users_group_id)
116 116
117 117 def _revoke_perms_on_yourself(self, form_result):
118 118 _updates = filter(lambda u: self._rhodecode_user.user_id == int(u[0]),
119 119 form_result['perm_updates'])
120 120 _additions = filter(lambda u: self._rhodecode_user.user_id == int(u[0]),
121 121 form_result['perm_additions'])
122 122 _deletions = filter(lambda u: self._rhodecode_user.user_id == int(u[0]),
123 123 form_result['perm_deletions'])
124 124 admin_perm = 'usergroup.admin'
125 125 if _updates and _updates[0][1] != admin_perm or \
126 126 _additions and _additions[0][1] != admin_perm or \
127 127 _deletions and _deletions[0][1] != admin_perm:
128 128 return True
129 129 return False
130 130
131 131 @LoginRequired()
132 132 @HasUserGroupPermissionAnyDecorator('usergroup.admin')
133 133 @CSRFRequired()
134 134 @view_config(
135 135 route_name='user_groups_update', request_method='POST',
136 136 renderer='rhodecode:templates/admin/user_groups/user_group_edit.mako')
137 137 def user_group_update(self):
138 138 _ = self.request.translate
139 139
140 140 user_group = self.db_user_group
141 141 user_group_id = user_group.users_group_id
142 142
143 143 old_user_group_name = self.db_user_group_name
144 144 new_user_group_name = old_user_group_name
145 145
146 146 c = self.load_default_context()
147 147 c.user_group = user_group
148 148 c.group_members_obj = [x.user for x in c.user_group.members]
149 149 c.group_members_obj.sort(key=lambda u: u.username.lower())
150 150 c.group_members = [(x.user_id, x.username) for x in c.group_members_obj]
151 151 c.active = 'settings'
152 152
153 153 users_group_form = UserGroupForm(
154 154 self.request.translate, edit=True,
155 155 old_data=c.user_group.get_dict(), allow_disabled=True)()
156 156
157 157 old_values = c.user_group.get_api_data()
158 158
159 159 try:
160 160 form_result = users_group_form.to_python(self.request.POST)
161 161 pstruct = peppercorn.parse(self.request.POST.items())
162 162 form_result['users_group_members'] = pstruct['user_group_members']
163 163
164 164 user_group, added_members, removed_members = \
165 165 UserGroupModel().update(c.user_group, form_result)
166 166 new_user_group_name = form_result['users_group_name']
167 167
168 168 for user_id in added_members:
169 169 user = User.get(user_id)
170 170 user_data = user.get_api_data()
171 171 audit_logger.store_web(
172 172 'user_group.edit.member.add',
173 173 action_data={'user': user_data, 'old_data': old_values},
174 174 user=self._rhodecode_user)
175 175
176 176 for user_id in removed_members:
177 177 user = User.get(user_id)
178 178 user_data = user.get_api_data()
179 179 audit_logger.store_web(
180 180 'user_group.edit.member.delete',
181 181 action_data={'user': user_data, 'old_data': old_values},
182 182 user=self._rhodecode_user)
183 183
184 184 audit_logger.store_web(
185 185 'user_group.edit', action_data={'old_data': old_values},
186 186 user=self._rhodecode_user)
187 187
188 188 h.flash(_('Updated user group %s') % new_user_group_name,
189 189 category='success')
190 190
191 191 affected_user_ids = []
192 192 for user_id in added_members + removed_members:
193 193 affected_user_ids.append(user_id)
194 194
195 195 name_changed = old_user_group_name != new_user_group_name
196 196 if name_changed:
197 197 owner = User.get_by_username(form_result['user'])
198 198 owner_id = owner.user_id if owner else self._rhodecode_user.user_id
199 199 affected_user_ids.append(self._rhodecode_user.user_id)
200 200 affected_user_ids.append(owner_id)
201 201
202 202 events.trigger(events.UserPermissionsChange(affected_user_ids))
203 203
204 204 Session().commit()
205 205 except formencode.Invalid as errors:
206 206 defaults = errors.value
207 207 e = errors.error_dict or {}
208 208
209 209 data = render(
210 210 'rhodecode:templates/admin/user_groups/user_group_edit.mako',
211 211 self._get_template_context(c), self.request)
212 212 html = formencode.htmlfill.render(
213 213 data,
214 214 defaults=defaults,
215 215 errors=e,
216 216 prefix_error=False,
217 217 encoding="UTF-8",
218 218 force_defaults=False
219 219 )
220 220 return Response(html)
221 221
222 222 except Exception:
223 223 log.exception("Exception during update of user group")
224 224 h.flash(_('Error occurred during update of user group %s')
225 225 % new_user_group_name, category='error')
226 226
227 227 raise HTTPFound(
228 228 h.route_path('edit_user_group', user_group_id=user_group_id))
229 229
230 230 @LoginRequired()
231 231 @HasUserGroupPermissionAnyDecorator('usergroup.admin')
232 232 @CSRFRequired()
233 233 @view_config(
234 234 route_name='user_groups_delete', request_method='POST',
235 235 renderer='rhodecode:templates/admin/user_groups/user_group_edit.mako')
236 236 def user_group_delete(self):
237 237 _ = self.request.translate
238 238 user_group = self.db_user_group
239 239
240 240 self.load_default_context()
241 241 force = str2bool(self.request.POST.get('force'))
242 242
243 243 old_values = user_group.get_api_data()
244 244 try:
245 245 UserGroupModel().delete(user_group, force=force)
246 246 audit_logger.store_web(
247 247 'user.delete', action_data={'old_data': old_values},
248 248 user=self._rhodecode_user)
249 249 Session().commit()
250 250 h.flash(_('Successfully deleted user group'), category='success')
251 251 except UserGroupAssignedException as e:
252 252 h.flash(str(e), category='error')
253 253 except Exception:
254 254 log.exception("Exception during deletion of user group")
255 255 h.flash(_('An error occurred during deletion of user group'),
256 256 category='error')
257 257 raise HTTPFound(h.route_path('user_groups'))
258 258
259 259 @LoginRequired()
260 260 @HasUserGroupPermissionAnyDecorator('usergroup.admin')
261 261 @view_config(
262 262 route_name='edit_user_group', request_method='GET',
263 263 renderer='rhodecode:templates/admin/user_groups/user_group_edit.mako')
264 264 def user_group_edit(self):
265 265 user_group = self.db_user_group
266 266
267 267 c = self.load_default_context()
268 268 c.user_group = user_group
269 269 c.group_members_obj = [x.user for x in c.user_group.members]
270 270 c.group_members_obj.sort(key=lambda u: u.username.lower())
271 271 c.group_members = [(x.user_id, x.username) for x in c.group_members_obj]
272 272
273 273 c.active = 'settings'
274 274
275 275 defaults = user_group.get_dict()
276 276 # fill owner
277 277 if user_group.user:
278 278 defaults.update({'user': user_group.user.username})
279 279 else:
280 280 replacement_user = User.get_first_super_admin().username
281 281 defaults.update({'user': replacement_user})
282 282
283 283 data = render(
284 284 'rhodecode:templates/admin/user_groups/user_group_edit.mako',
285 285 self._get_template_context(c), self.request)
286 286 html = formencode.htmlfill.render(
287 287 data,
288 288 defaults=defaults,
289 289 encoding="UTF-8",
290 290 force_defaults=False
291 291 )
292 292 return Response(html)
293 293
294 294 @LoginRequired()
295 295 @HasUserGroupPermissionAnyDecorator('usergroup.admin')
296 296 @view_config(
297 297 route_name='edit_user_group_perms', request_method='GET',
298 298 renderer='rhodecode:templates/admin/user_groups/user_group_edit.mako')
299 299 def user_group_edit_perms(self):
300 300 user_group = self.db_user_group
301 301 c = self.load_default_context()
302 302 c.user_group = user_group
303 303 c.active = 'perms'
304 304
305 305 defaults = {}
306 306 # fill user group users
307 307 for p in c.user_group.user_user_group_to_perm:
308 308 defaults.update({'u_perm_%s' % p.user.user_id:
309 309 p.permission.permission_name})
310 310
311 311 for p in c.user_group.user_group_user_group_to_perm:
312 312 defaults.update({'g_perm_%s' % p.user_group.users_group_id:
313 313 p.permission.permission_name})
314 314
315 315 data = render(
316 316 'rhodecode:templates/admin/user_groups/user_group_edit.mako',
317 317 self._get_template_context(c), self.request)
318 318 html = formencode.htmlfill.render(
319 319 data,
320 320 defaults=defaults,
321 321 encoding="UTF-8",
322 322 force_defaults=False
323 323 )
324 324 return Response(html)
325 325
326 326 @LoginRequired()
327 327 @HasUserGroupPermissionAnyDecorator('usergroup.admin')
328 328 @CSRFRequired()
329 329 @view_config(
330 330 route_name='edit_user_group_perms_update', request_method='POST',
331 331 renderer='rhodecode:templates/admin/user_groups/user_group_edit.mako')
332 332 def user_group_update_perms(self):
333 333 """
334 334 grant permission for given user group
335 335 """
336 336 _ = self.request.translate
337 337
338 338 user_group = self.db_user_group
339 339 user_group_id = user_group.users_group_id
340 340 c = self.load_default_context()
341 341 c.user_group = user_group
342 342 form = UserGroupPermsForm(self.request.translate)().to_python(self.request.POST)
343 343
344 344 if not self._rhodecode_user.is_admin:
345 345 if self._revoke_perms_on_yourself(form):
346 346 msg = _('Cannot change permission for yourself as admin')
347 347 h.flash(msg, category='warning')
348 348 raise HTTPFound(
349 349 h.route_path('edit_user_group_perms',
350 350 user_group_id=user_group_id))
351 351
352 352 try:
353 353 changes = UserGroupModel().update_permissions(
354 354 user_group,
355 355 form['perm_additions'], form['perm_updates'],
356 356 form['perm_deletions'])
357 357
358 358 except RepoGroupAssignmentError:
359 359 h.flash(_('Target group cannot be the same'), category='error')
360 360 raise HTTPFound(
361 361 h.route_path('edit_user_group_perms',
362 362 user_group_id=user_group_id))
363 363
364 364 action_data = {
365 365 'added': changes['added'],
366 366 'updated': changes['updated'],
367 367 'deleted': changes['deleted'],
368 368 }
369 369 audit_logger.store_web(
370 370 'user_group.edit.permissions', action_data=action_data,
371 371 user=self._rhodecode_user)
372 372
373 373 Session().commit()
374 374 h.flash(_('User Group permissions updated'), category='success')
375 375
376 376 affected_user_ids = []
377 377 for change in changes['added'] + changes['updated'] + changes['deleted']:
378 378 if change['type'] == 'user':
379 379 affected_user_ids.append(change['id'])
380 if change['type'] == 'user_group':
381 user_group = UserGroup.get(safe_int(change['id']))
382 if user_group:
383 group_members_ids = [x.user_id for x in user_group.members]
384 affected_user_ids.extend(group_members_ids)
380 385
381 386 events.trigger(events.UserPermissionsChange(affected_user_ids))
382 387
383 388 raise HTTPFound(
384 389 h.route_path('edit_user_group_perms', user_group_id=user_group_id))
385 390
386 391 @LoginRequired()
387 392 @HasUserGroupPermissionAnyDecorator('usergroup.admin')
388 393 @view_config(
389 394 route_name='edit_user_group_global_perms', request_method='GET',
390 395 renderer='rhodecode:templates/admin/user_groups/user_group_edit.mako')
391 396 def user_group_global_perms_edit(self):
392 397 user_group = self.db_user_group
393 398 c = self.load_default_context()
394 399 c.user_group = user_group
395 400 c.active = 'global_perms'
396 401
397 402 c.default_user = User.get_default_user()
398 403 defaults = c.user_group.get_dict()
399 404 defaults.update(c.default_user.get_default_perms(suffix='_inherited'))
400 405 defaults.update(c.user_group.get_default_perms())
401 406
402 407 data = render(
403 408 'rhodecode:templates/admin/user_groups/user_group_edit.mako',
404 409 self._get_template_context(c), self.request)
405 410 html = formencode.htmlfill.render(
406 411 data,
407 412 defaults=defaults,
408 413 encoding="UTF-8",
409 414 force_defaults=False
410 415 )
411 416 return Response(html)
412 417
413 418 @LoginRequired()
414 419 @HasUserGroupPermissionAnyDecorator('usergroup.admin')
415 420 @CSRFRequired()
416 421 @view_config(
417 422 route_name='edit_user_group_global_perms_update', request_method='POST',
418 423 renderer='rhodecode:templates/admin/user_groups/user_group_edit.mako')
419 424 def user_group_global_perms_update(self):
420 425 _ = self.request.translate
421 426 user_group = self.db_user_group
422 427 user_group_id = self.db_user_group.users_group_id
423 428
424 429 c = self.load_default_context()
425 430 c.user_group = user_group
426 431 c.active = 'global_perms'
427 432
428 433 try:
429 434 # first stage that verifies the checkbox
430 435 _form = UserIndividualPermissionsForm(self.request.translate)
431 436 form_result = _form.to_python(dict(self.request.POST))
432 437 inherit_perms = form_result['inherit_default_permissions']
433 438 user_group.inherit_default_permissions = inherit_perms
434 439 Session().add(user_group)
435 440
436 441 if not inherit_perms:
437 442 # only update the individual ones if we un check the flag
438 443 _form = UserPermissionsForm(
439 444 self.request.translate,
440 445 [x[0] for x in c.repo_create_choices],
441 446 [x[0] for x in c.repo_create_on_write_choices],
442 447 [x[0] for x in c.repo_group_create_choices],
443 448 [x[0] for x in c.user_group_create_choices],
444 449 [x[0] for x in c.fork_choices],
445 450 [x[0] for x in c.inherit_default_permission_choices])()
446 451
447 452 form_result = _form.to_python(dict(self.request.POST))
448 453 form_result.update(
449 454 {'perm_user_group_id': user_group.users_group_id})
450 455
451 456 PermissionModel().update_user_group_permissions(form_result)
452 457
453 458 Session().commit()
454 459 h.flash(_('User Group global permissions updated successfully'),
455 460 category='success')
456 461
457 462 except formencode.Invalid as errors:
458 463 defaults = errors.value
459 464
460 465 data = render(
461 466 'rhodecode:templates/admin/user_groups/user_group_edit.mako',
462 467 self._get_template_context(c), self.request)
463 468 html = formencode.htmlfill.render(
464 469 data,
465 470 defaults=defaults,
466 471 errors=errors.error_dict or {},
467 472 prefix_error=False,
468 473 encoding="UTF-8",
469 474 force_defaults=False
470 475 )
471 476 return Response(html)
472 477 except Exception:
473 478 log.exception("Exception during permissions saving")
474 479 h.flash(_('An error occurred during permissions saving'),
475 480 category='error')
476 481
477 482 raise HTTPFound(
478 483 h.route_path('edit_user_group_global_perms',
479 484 user_group_id=user_group_id))
480 485
481 486 @LoginRequired()
482 487 @HasUserGroupPermissionAnyDecorator('usergroup.admin')
483 488 @view_config(
484 489 route_name='edit_user_group_advanced', request_method='GET',
485 490 renderer='rhodecode:templates/admin/user_groups/user_group_edit.mako')
486 491 def user_group_edit_advanced(self):
487 492 user_group = self.db_user_group
488 493
489 494 c = self.load_default_context()
490 495 c.user_group = user_group
491 496 c.active = 'advanced'
492 497 c.group_members_obj = sorted(
493 498 (x.user for x in c.user_group.members),
494 499 key=lambda u: u.username.lower())
495 500
496 501 c.group_to_repos = sorted(
497 502 (x.repository for x in c.user_group.users_group_repo_to_perm),
498 503 key=lambda u: u.repo_name.lower())
499 504
500 505 c.group_to_repo_groups = sorted(
501 506 (x.group for x in c.user_group.users_group_repo_group_to_perm),
502 507 key=lambda u: u.group_name.lower())
503 508
504 509 c.group_to_review_rules = sorted(
505 510 (x.users_group for x in c.user_group.user_group_review_rules),
506 511 key=lambda u: u.users_group_name.lower())
507 512
508 513 return self._get_template_context(c)
509 514
510 515 @LoginRequired()
511 516 @HasUserGroupPermissionAnyDecorator('usergroup.admin')
512 517 @CSRFRequired()
513 518 @view_config(
514 519 route_name='edit_user_group_advanced_sync', request_method='POST',
515 520 renderer='rhodecode:templates/admin/user_groups/user_group_edit.mako')
516 521 def user_group_edit_advanced_set_synchronization(self):
517 522 _ = self.request.translate
518 523 user_group = self.db_user_group
519 524 user_group_id = user_group.users_group_id
520 525
521 526 existing = user_group.group_data.get('extern_type')
522 527
523 528 if existing:
524 529 new_state = user_group.group_data
525 530 new_state['extern_type'] = None
526 531 else:
527 532 new_state = user_group.group_data
528 533 new_state['extern_type'] = 'manual'
529 534 new_state['extern_type_set_by'] = self._rhodecode_user.username
530 535
531 536 try:
532 537 user_group.group_data = new_state
533 538 Session().add(user_group)
534 539 Session().commit()
535 540
536 541 h.flash(_('User Group synchronization updated successfully'),
537 542 category='success')
538 543 except Exception:
539 544 log.exception("Exception during sync settings saving")
540 545 h.flash(_('An error occurred during synchronization update'),
541 546 category='error')
542 547
543 548 raise HTTPFound(
544 549 h.route_path('edit_user_group_advanced',
545 550 user_group_id=user_group_id))
General Comments 0
You need to be logged in to leave comments. Login now