Show More
@@ -1,672 +1,673 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2016-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 | import datetime |
|
23 | 23 | import formencode |
|
24 | 24 | |
|
25 | 25 | from pyramid.httpexceptions import HTTPFound |
|
26 | 26 | from pyramid.view import view_config |
|
27 | 27 | from sqlalchemy.sql.functions import coalesce |
|
28 | 28 | from sqlalchemy.exc import IntegrityError |
|
29 | 29 | |
|
30 | 30 | from rhodecode.apps._base import BaseAppView, DataGridAppView |
|
31 | 31 | from rhodecode.apps.ssh_support import SshKeyFileChangeEvent |
|
32 | 32 | from rhodecode.events import trigger |
|
33 | 33 | |
|
34 | 34 | from rhodecode.lib import audit_logger |
|
35 | 35 | from rhodecode.lib.ext_json import json |
|
36 | 36 | from rhodecode.lib.auth import ( |
|
37 | 37 | LoginRequired, HasPermissionAllDecorator, CSRFRequired) |
|
38 | 38 | from rhodecode.lib import helpers as h |
|
39 | 39 | from rhodecode.lib.utils2 import safe_int, safe_unicode |
|
40 | 40 | from rhodecode.model.auth_token import AuthTokenModel |
|
41 | 41 | from rhodecode.model.ssh_key import SshKeyModel |
|
42 | 42 | from rhodecode.model.user import UserModel |
|
43 | 43 | from rhodecode.model.user_group import UserGroupModel |
|
44 | 44 | from rhodecode.model.db import ( |
|
45 | 45 | or_, User, UserIpMap, UserEmailMap, UserApiKeys, UserSshKeys) |
|
46 | 46 | from rhodecode.model.meta import Session |
|
47 | 47 | |
|
48 | 48 | log = logging.getLogger(__name__) |
|
49 | 49 | |
|
50 | 50 | |
|
51 | 51 | class AdminUsersView(BaseAppView, DataGridAppView): |
|
52 | 52 | ALLOW_SCOPED_TOKENS = False |
|
53 | 53 | """ |
|
54 | 54 | This view has alternative version inside EE, if modified please take a look |
|
55 | 55 | in there as well. |
|
56 | 56 | """ |
|
57 | 57 | |
|
58 | 58 | def load_default_context(self): |
|
59 | 59 | c = self._get_local_tmpl_context() |
|
60 | 60 | c.allow_scoped_tokens = self.ALLOW_SCOPED_TOKENS |
|
61 | 61 | self._register_global_c(c) |
|
62 | 62 | return c |
|
63 | 63 | |
|
64 | 64 | def _redirect_for_default_user(self, username): |
|
65 | 65 | _ = self.request.translate |
|
66 | 66 | if username == User.DEFAULT_USER: |
|
67 | 67 | h.flash(_("You can't edit this user"), category='warning') |
|
68 | 68 | # TODO(marcink): redirect to 'users' admin panel once this |
|
69 | 69 | # is a pyramid view |
|
70 | 70 | raise HTTPFound('/') |
|
71 | 71 | |
|
72 | 72 | @LoginRequired() |
|
73 | 73 | @HasPermissionAllDecorator('hg.admin') |
|
74 | 74 | @view_config( |
|
75 | 75 | route_name='users', request_method='GET', |
|
76 | 76 | renderer='rhodecode:templates/admin/users/users.mako') |
|
77 | 77 | def users_list(self): |
|
78 | 78 | c = self.load_default_context() |
|
79 | 79 | return self._get_template_context(c) |
|
80 | 80 | |
|
81 | 81 | @LoginRequired() |
|
82 | 82 | @HasPermissionAllDecorator('hg.admin') |
|
83 | 83 | @view_config( |
|
84 | 84 | # renderer defined below |
|
85 | 85 | route_name='users_data', request_method='GET', |
|
86 | 86 | renderer='json_ext', xhr=True) |
|
87 | 87 | def users_list_data(self): |
|
88 | 88 | column_map = { |
|
89 | 89 | 'first_name': 'name', |
|
90 | 90 | 'last_name': 'lastname', |
|
91 | 91 | } |
|
92 | 92 | draw, start, limit = self._extract_chunk(self.request) |
|
93 | 93 | search_q, order_by, order_dir = self._extract_ordering( |
|
94 | 94 | self.request, column_map=column_map) |
|
95 | 95 | |
|
96 | 96 | _render = self.request.get_partial_renderer( |
|
97 | 97 | 'data_table/_dt_elements.mako') |
|
98 | 98 | |
|
99 | 99 | def user_actions(user_id, username): |
|
100 | 100 | return _render("user_actions", user_id, username) |
|
101 | 101 | |
|
102 | 102 | users_data_total_count = User.query()\ |
|
103 | 103 | .filter(User.username != User.DEFAULT_USER) \ |
|
104 | 104 | .count() |
|
105 | 105 | |
|
106 | 106 | # json generate |
|
107 | 107 | base_q = User.query().filter(User.username != User.DEFAULT_USER) |
|
108 | 108 | |
|
109 | 109 | if search_q: |
|
110 | 110 | like_expression = u'%{}%'.format(safe_unicode(search_q)) |
|
111 | 111 | base_q = base_q.filter(or_( |
|
112 | 112 | User.username.ilike(like_expression), |
|
113 | 113 | User._email.ilike(like_expression), |
|
114 | 114 | User.name.ilike(like_expression), |
|
115 | 115 | User.lastname.ilike(like_expression), |
|
116 | 116 | )) |
|
117 | 117 | |
|
118 | 118 | users_data_total_filtered_count = base_q.count() |
|
119 | 119 | |
|
120 | 120 | sort_col = getattr(User, order_by, None) |
|
121 | 121 | if sort_col: |
|
122 | 122 | if order_dir == 'asc': |
|
123 | 123 | # handle null values properly to order by NULL last |
|
124 | 124 | if order_by in ['last_activity']: |
|
125 | 125 | sort_col = coalesce(sort_col, datetime.date.max) |
|
126 | 126 | sort_col = sort_col.asc() |
|
127 | 127 | else: |
|
128 | 128 | # handle null values properly to order by NULL last |
|
129 | 129 | if order_by in ['last_activity']: |
|
130 | 130 | sort_col = coalesce(sort_col, datetime.date.min) |
|
131 | 131 | sort_col = sort_col.desc() |
|
132 | 132 | |
|
133 | 133 | base_q = base_q.order_by(sort_col) |
|
134 | 134 | base_q = base_q.offset(start).limit(limit) |
|
135 | 135 | |
|
136 | 136 | users_list = base_q.all() |
|
137 | 137 | |
|
138 | 138 | users_data = [] |
|
139 | 139 | for user in users_list: |
|
140 | 140 | users_data.append({ |
|
141 | 141 | "username": h.gravatar_with_user(self.request, user.username), |
|
142 | 142 | "email": user.email, |
|
143 | 143 | "first_name": user.first_name, |
|
144 | 144 | "last_name": user.last_name, |
|
145 | 145 | "last_login": h.format_date(user.last_login), |
|
146 | 146 | "last_activity": h.format_date(user.last_activity), |
|
147 | 147 | "active": h.bool2icon(user.active), |
|
148 | 148 | "active_raw": user.active, |
|
149 | 149 | "admin": h.bool2icon(user.admin), |
|
150 | 150 | "extern_type": user.extern_type, |
|
151 | 151 | "extern_name": user.extern_name, |
|
152 | 152 | "action": user_actions(user.user_id, user.username), |
|
153 | 153 | }) |
|
154 | 154 | |
|
155 | 155 | data = ({ |
|
156 | 156 | 'draw': draw, |
|
157 | 157 | 'data': users_data, |
|
158 | 158 | 'recordsTotal': users_data_total_count, |
|
159 | 159 | 'recordsFiltered': users_data_total_filtered_count, |
|
160 | 160 | }) |
|
161 | 161 | |
|
162 | 162 | return data |
|
163 | 163 | |
|
164 | 164 | @LoginRequired() |
|
165 | 165 | @HasPermissionAllDecorator('hg.admin') |
|
166 | 166 | @view_config( |
|
167 | 167 | route_name='edit_user_auth_tokens', request_method='GET', |
|
168 | 168 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
169 | 169 | def auth_tokens(self): |
|
170 | 170 | _ = self.request.translate |
|
171 | 171 | c = self.load_default_context() |
|
172 | 172 | |
|
173 | 173 | user_id = self.request.matchdict.get('user_id') |
|
174 | 174 | c.user = User.get_or_404(user_id) |
|
175 | 175 | self._redirect_for_default_user(c.user.username) |
|
176 | 176 | |
|
177 | 177 | c.active = 'auth_tokens' |
|
178 | 178 | |
|
179 | 179 | c.lifetime_values = [ |
|
180 | 180 | (str(-1), _('forever')), |
|
181 | 181 | (str(5), _('5 minutes')), |
|
182 | 182 | (str(60), _('1 hour')), |
|
183 | 183 | (str(60 * 24), _('1 day')), |
|
184 | 184 | (str(60 * 24 * 30), _('1 month')), |
|
185 | 185 | ] |
|
186 | 186 | c.lifetime_options = [(c.lifetime_values, _("Lifetime"))] |
|
187 | 187 | c.role_values = [ |
|
188 | 188 | (x, AuthTokenModel.cls._get_role_name(x)) |
|
189 | 189 | for x in AuthTokenModel.cls.ROLES] |
|
190 | 190 | c.role_options = [(c.role_values, _("Role"))] |
|
191 | 191 | c.user_auth_tokens = AuthTokenModel().get_auth_tokens( |
|
192 | 192 | c.user.user_id, show_expired=True) |
|
193 | 193 | return self._get_template_context(c) |
|
194 | 194 | |
|
195 | 195 | def maybe_attach_token_scope(self, token): |
|
196 | 196 | # implemented in EE edition |
|
197 | 197 | pass |
|
198 | 198 | |
|
199 | 199 | @LoginRequired() |
|
200 | 200 | @HasPermissionAllDecorator('hg.admin') |
|
201 | 201 | @CSRFRequired() |
|
202 | 202 | @view_config( |
|
203 | 203 | route_name='edit_user_auth_tokens_add', request_method='POST') |
|
204 | 204 | def auth_tokens_add(self): |
|
205 | 205 | _ = self.request.translate |
|
206 | 206 | c = self.load_default_context() |
|
207 | 207 | |
|
208 | 208 | user_id = self.request.matchdict.get('user_id') |
|
209 | 209 | c.user = User.get_or_404(user_id) |
|
210 | 210 | |
|
211 | 211 | self._redirect_for_default_user(c.user.username) |
|
212 | 212 | |
|
213 | 213 | user_data = c.user.get_api_data() |
|
214 | 214 | lifetime = safe_int(self.request.POST.get('lifetime'), -1) |
|
215 | 215 | description = self.request.POST.get('description') |
|
216 | 216 | role = self.request.POST.get('role') |
|
217 | 217 | |
|
218 | 218 | token = AuthTokenModel().create( |
|
219 | 219 | c.user.user_id, description, lifetime, role) |
|
220 | 220 | token_data = token.get_api_data() |
|
221 | 221 | |
|
222 | 222 | self.maybe_attach_token_scope(token) |
|
223 | 223 | audit_logger.store_web( |
|
224 | 224 | 'user.edit.token.add', action_data={ |
|
225 | 225 | 'data': {'token': token_data, 'user': user_data}}, |
|
226 | 226 | user=self._rhodecode_user, ) |
|
227 | 227 | Session().commit() |
|
228 | 228 | |
|
229 | 229 | h.flash(_("Auth token successfully created"), category='success') |
|
230 | 230 | return HTTPFound(h.route_path('edit_user_auth_tokens', user_id=user_id)) |
|
231 | 231 | |
|
232 | 232 | @LoginRequired() |
|
233 | 233 | @HasPermissionAllDecorator('hg.admin') |
|
234 | 234 | @CSRFRequired() |
|
235 | 235 | @view_config( |
|
236 | 236 | route_name='edit_user_auth_tokens_delete', request_method='POST') |
|
237 | 237 | def auth_tokens_delete(self): |
|
238 | 238 | _ = self.request.translate |
|
239 | 239 | c = self.load_default_context() |
|
240 | 240 | |
|
241 | 241 | user_id = self.request.matchdict.get('user_id') |
|
242 | 242 | c.user = User.get_or_404(user_id) |
|
243 | 243 | self._redirect_for_default_user(c.user.username) |
|
244 | 244 | user_data = c.user.get_api_data() |
|
245 | 245 | |
|
246 | 246 | del_auth_token = self.request.POST.get('del_auth_token') |
|
247 | 247 | |
|
248 | 248 | if del_auth_token: |
|
249 | 249 | token = UserApiKeys.get_or_404(del_auth_token) |
|
250 | 250 | token_data = token.get_api_data() |
|
251 | 251 | |
|
252 | 252 | AuthTokenModel().delete(del_auth_token, c.user.user_id) |
|
253 | 253 | audit_logger.store_web( |
|
254 | 254 | 'user.edit.token.delete', action_data={ |
|
255 | 255 | 'data': {'token': token_data, 'user': user_data}}, |
|
256 | 256 | user=self._rhodecode_user,) |
|
257 | 257 | Session().commit() |
|
258 | 258 | h.flash(_("Auth token successfully deleted"), category='success') |
|
259 | 259 | |
|
260 | 260 | return HTTPFound(h.route_path('edit_user_auth_tokens', user_id=user_id)) |
|
261 | 261 | |
|
262 | 262 | @LoginRequired() |
|
263 | 263 | @HasPermissionAllDecorator('hg.admin') |
|
264 | 264 | @view_config( |
|
265 | 265 | route_name='edit_user_ssh_keys', request_method='GET', |
|
266 | 266 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
267 | 267 | def ssh_keys(self): |
|
268 | 268 | _ = self.request.translate |
|
269 | 269 | c = self.load_default_context() |
|
270 | 270 | |
|
271 | 271 | user_id = self.request.matchdict.get('user_id') |
|
272 | 272 | c.user = User.get_or_404(user_id) |
|
273 | 273 | self._redirect_for_default_user(c.user.username) |
|
274 | 274 | |
|
275 | 275 | c.active = 'ssh_keys' |
|
276 | 276 | c.default_key = self.request.GET.get('default_key') |
|
277 | 277 | c.user_ssh_keys = SshKeyModel().get_ssh_keys(c.user.user_id) |
|
278 | 278 | return self._get_template_context(c) |
|
279 | 279 | |
|
280 | 280 | @LoginRequired() |
|
281 | 281 | @HasPermissionAllDecorator('hg.admin') |
|
282 | 282 | @view_config( |
|
283 | 283 | route_name='edit_user_ssh_keys_generate_keypair', request_method='GET', |
|
284 | 284 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
285 | 285 | def ssh_keys_generate_keypair(self): |
|
286 | 286 | _ = self.request.translate |
|
287 | 287 | c = self.load_default_context() |
|
288 | 288 | |
|
289 | 289 | user_id = self.request.matchdict.get('user_id') |
|
290 | 290 | c.user = User.get_or_404(user_id) |
|
291 | 291 | self._redirect_for_default_user(c.user.username) |
|
292 | 292 | |
|
293 | 293 | c.active = 'ssh_keys_generate' |
|
294 | 294 | comment = 'RhodeCode-SSH {}'.format(c.user.email or '') |
|
295 | 295 | c.private, c.public = SshKeyModel().generate_keypair(comment=comment) |
|
296 | 296 | |
|
297 | 297 | return self._get_template_context(c) |
|
298 | 298 | |
|
299 | 299 | @LoginRequired() |
|
300 | 300 | @HasPermissionAllDecorator('hg.admin') |
|
301 | 301 | @CSRFRequired() |
|
302 | 302 | @view_config( |
|
303 | 303 | route_name='edit_user_ssh_keys_add', request_method='POST') |
|
304 | 304 | def ssh_keys_add(self): |
|
305 | 305 | _ = self.request.translate |
|
306 | 306 | c = self.load_default_context() |
|
307 | 307 | |
|
308 | 308 | user_id = self.request.matchdict.get('user_id') |
|
309 | 309 | c.user = User.get_or_404(user_id) |
|
310 | 310 | |
|
311 | 311 | self._redirect_for_default_user(c.user.username) |
|
312 | 312 | |
|
313 | 313 | user_data = c.user.get_api_data() |
|
314 | 314 | key_data = self.request.POST.get('key_data') |
|
315 | 315 | description = self.request.POST.get('description') |
|
316 | 316 | |
|
317 | 317 | try: |
|
318 | 318 | if not key_data: |
|
319 | 319 | raise ValueError('Please add a valid public key') |
|
320 | 320 | |
|
321 | 321 | key = SshKeyModel().parse_key(key_data.strip()) |
|
322 | 322 | fingerprint = key.hash_md5() |
|
323 | 323 | |
|
324 | 324 | ssh_key = SshKeyModel().create( |
|
325 | 325 | c.user.user_id, fingerprint, key_data, description) |
|
326 | 326 | ssh_key_data = ssh_key.get_api_data() |
|
327 | 327 | |
|
328 | 328 | audit_logger.store_web( |
|
329 | 329 | 'user.edit.ssh_key.add', action_data={ |
|
330 | 330 | 'data': {'ssh_key': ssh_key_data, 'user': user_data}}, |
|
331 | 331 | user=self._rhodecode_user, ) |
|
332 | 332 | Session().commit() |
|
333 | 333 | |
|
334 | 334 | # Trigger an event on change of keys. |
|
335 | 335 | trigger(SshKeyFileChangeEvent(), self.request.registry) |
|
336 | 336 | |
|
337 | 337 | h.flash(_("Ssh Key successfully created"), category='success') |
|
338 | 338 | |
|
339 | 339 | except IntegrityError: |
|
340 | 340 | log.exception("Exception during ssh key saving") |
|
341 | 341 | h.flash(_('An error occurred during ssh key saving: {}').format( |
|
342 | 342 | 'Such key already exists, please use a different one'), |
|
343 | 343 | category='error') |
|
344 | 344 | except Exception as e: |
|
345 | 345 | log.exception("Exception during ssh key saving") |
|
346 | 346 | h.flash(_('An error occurred during ssh key saving: {}').format(e), |
|
347 | 347 | category='error') |
|
348 | 348 | |
|
349 | 349 | return HTTPFound( |
|
350 | 350 | h.route_path('edit_user_ssh_keys', user_id=user_id)) |
|
351 | 351 | |
|
352 | 352 | @LoginRequired() |
|
353 | 353 | @HasPermissionAllDecorator('hg.admin') |
|
354 | 354 | @CSRFRequired() |
|
355 | 355 | @view_config( |
|
356 | 356 | route_name='edit_user_ssh_keys_delete', request_method='POST') |
|
357 | 357 | def ssh_keys_delete(self): |
|
358 | 358 | _ = self.request.translate |
|
359 | 359 | c = self.load_default_context() |
|
360 | 360 | |
|
361 | 361 | user_id = self.request.matchdict.get('user_id') |
|
362 | 362 | c.user = User.get_or_404(user_id) |
|
363 | 363 | self._redirect_for_default_user(c.user.username) |
|
364 | 364 | user_data = c.user.get_api_data() |
|
365 | 365 | |
|
366 | 366 | del_ssh_key = self.request.POST.get('del_ssh_key') |
|
367 | 367 | |
|
368 | 368 | if del_ssh_key: |
|
369 | 369 | ssh_key = UserSshKeys.get_or_404(del_ssh_key) |
|
370 | 370 | ssh_key_data = ssh_key.get_api_data() |
|
371 | 371 | |
|
372 | 372 | SshKeyModel().delete(del_ssh_key, c.user.user_id) |
|
373 | 373 | audit_logger.store_web( |
|
374 | 374 | 'user.edit.ssh_key.delete', action_data={ |
|
375 | 375 | 'data': {'ssh_key': ssh_key_data, 'user': user_data}}, |
|
376 | 376 | user=self._rhodecode_user,) |
|
377 | 377 | Session().commit() |
|
378 | 378 | # Trigger an event on change of keys. |
|
379 | 379 | trigger(SshKeyFileChangeEvent(), self.request.registry) |
|
380 | 380 | h.flash(_("Ssh key successfully deleted"), category='success') |
|
381 | 381 | |
|
382 | 382 | return HTTPFound(h.route_path('edit_user_ssh_keys', user_id=user_id)) |
|
383 | 383 | |
|
384 | 384 | @LoginRequired() |
|
385 | 385 | @HasPermissionAllDecorator('hg.admin') |
|
386 | 386 | @view_config( |
|
387 | 387 | route_name='edit_user_emails', request_method='GET', |
|
388 | 388 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
389 | 389 | def emails(self): |
|
390 | 390 | _ = self.request.translate |
|
391 | 391 | c = self.load_default_context() |
|
392 | 392 | |
|
393 | 393 | user_id = self.request.matchdict.get('user_id') |
|
394 | 394 | c.user = User.get_or_404(user_id) |
|
395 | 395 | self._redirect_for_default_user(c.user.username) |
|
396 | 396 | |
|
397 | 397 | c.active = 'emails' |
|
398 | 398 | c.user_email_map = UserEmailMap.query() \ |
|
399 | 399 | .filter(UserEmailMap.user == c.user).all() |
|
400 | 400 | |
|
401 | 401 | return self._get_template_context(c) |
|
402 | 402 | |
|
403 | 403 | @LoginRequired() |
|
404 | 404 | @HasPermissionAllDecorator('hg.admin') |
|
405 | 405 | @CSRFRequired() |
|
406 | 406 | @view_config( |
|
407 | 407 | route_name='edit_user_emails_add', request_method='POST') |
|
408 | 408 | def emails_add(self): |
|
409 | 409 | _ = self.request.translate |
|
410 | 410 | c = self.load_default_context() |
|
411 | 411 | |
|
412 | 412 | user_id = self.request.matchdict.get('user_id') |
|
413 | 413 | c.user = User.get_or_404(user_id) |
|
414 | 414 | self._redirect_for_default_user(c.user.username) |
|
415 | 415 | |
|
416 | 416 | email = self.request.POST.get('new_email') |
|
417 | 417 | user_data = c.user.get_api_data() |
|
418 | 418 | try: |
|
419 | 419 | UserModel().add_extra_email(c.user.user_id, email) |
|
420 | 420 | audit_logger.store_web( |
|
421 | 421 | 'user.edit.email.add', action_data={'email': email, 'user': user_data}, |
|
422 | 422 | user=self._rhodecode_user) |
|
423 | 423 | Session().commit() |
|
424 | 424 | h.flash(_("Added new email address `%s` for user account") % email, |
|
425 | 425 | category='success') |
|
426 | 426 | except formencode.Invalid as error: |
|
427 | 427 | h.flash(h.escape(error.error_dict['email']), category='error') |
|
428 | 428 | except Exception: |
|
429 | 429 | log.exception("Exception during email saving") |
|
430 | 430 | h.flash(_('An error occurred during email saving'), |
|
431 | 431 | category='error') |
|
432 | 432 | raise HTTPFound(h.route_path('edit_user_emails', user_id=user_id)) |
|
433 | 433 | |
|
434 | 434 | @LoginRequired() |
|
435 | 435 | @HasPermissionAllDecorator('hg.admin') |
|
436 | 436 | @CSRFRequired() |
|
437 | 437 | @view_config( |
|
438 | 438 | route_name='edit_user_emails_delete', request_method='POST') |
|
439 | 439 | def emails_delete(self): |
|
440 | 440 | _ = self.request.translate |
|
441 | 441 | c = self.load_default_context() |
|
442 | 442 | |
|
443 | 443 | user_id = self.request.matchdict.get('user_id') |
|
444 | 444 | c.user = User.get_or_404(user_id) |
|
445 | 445 | self._redirect_for_default_user(c.user.username) |
|
446 | 446 | |
|
447 | 447 | email_id = self.request.POST.get('del_email_id') |
|
448 | 448 | user_model = UserModel() |
|
449 | 449 | |
|
450 | 450 | email = UserEmailMap.query().get(email_id).email |
|
451 | 451 | user_data = c.user.get_api_data() |
|
452 | 452 | user_model.delete_extra_email(c.user.user_id, email_id) |
|
453 | 453 | audit_logger.store_web( |
|
454 | 454 | 'user.edit.email.delete', action_data={'email': email, 'user': user_data}, |
|
455 | 455 | user=self._rhodecode_user) |
|
456 | 456 | Session().commit() |
|
457 | 457 | h.flash(_("Removed email address from user account"), |
|
458 | 458 | category='success') |
|
459 | 459 | raise HTTPFound(h.route_path('edit_user_emails', user_id=user_id)) |
|
460 | 460 | |
|
461 | 461 | @LoginRequired() |
|
462 | 462 | @HasPermissionAllDecorator('hg.admin') |
|
463 | 463 | @view_config( |
|
464 | 464 | route_name='edit_user_ips', request_method='GET', |
|
465 | 465 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
466 | 466 | def ips(self): |
|
467 | 467 | _ = self.request.translate |
|
468 | 468 | c = self.load_default_context() |
|
469 | 469 | |
|
470 | 470 | user_id = self.request.matchdict.get('user_id') |
|
471 | 471 | c.user = User.get_or_404(user_id) |
|
472 | 472 | self._redirect_for_default_user(c.user.username) |
|
473 | 473 | |
|
474 | 474 | c.active = 'ips' |
|
475 | 475 | c.user_ip_map = UserIpMap.query() \ |
|
476 | 476 | .filter(UserIpMap.user == c.user).all() |
|
477 | 477 | |
|
478 | 478 | c.inherit_default_ips = c.user.inherit_default_permissions |
|
479 | 479 | c.default_user_ip_map = UserIpMap.query() \ |
|
480 | 480 | .filter(UserIpMap.user == User.get_default_user()).all() |
|
481 | 481 | |
|
482 | 482 | return self._get_template_context(c) |
|
483 | 483 | |
|
484 | 484 | @LoginRequired() |
|
485 | 485 | @HasPermissionAllDecorator('hg.admin') |
|
486 | 486 | @CSRFRequired() |
|
487 | 487 | @view_config( |
|
488 | 488 | route_name='edit_user_ips_add', request_method='POST') |
|
489 | 489 | def ips_add(self): |
|
490 | 490 | _ = self.request.translate |
|
491 | 491 | c = self.load_default_context() |
|
492 | 492 | |
|
493 | 493 | user_id = self.request.matchdict.get('user_id') |
|
494 | 494 | c.user = User.get_or_404(user_id) |
|
495 | 495 | # NOTE(marcink): this view is allowed for default users, as we can |
|
496 | 496 | # edit their IP white list |
|
497 | 497 | |
|
498 | 498 | user_model = UserModel() |
|
499 | 499 | desc = self.request.POST.get('description') |
|
500 | 500 | try: |
|
501 | 501 | ip_list = user_model.parse_ip_range( |
|
502 | 502 | self.request.POST.get('new_ip')) |
|
503 | 503 | except Exception as e: |
|
504 | 504 | ip_list = [] |
|
505 | 505 | log.exception("Exception during ip saving") |
|
506 | 506 | h.flash(_('An error occurred during ip saving:%s' % (e,)), |
|
507 | 507 | category='error') |
|
508 | 508 | added = [] |
|
509 | 509 | user_data = c.user.get_api_data() |
|
510 | 510 | for ip in ip_list: |
|
511 | 511 | try: |
|
512 | 512 | user_model.add_extra_ip(c.user.user_id, ip, desc) |
|
513 | 513 | audit_logger.store_web( |
|
514 | 514 | 'user.edit.ip.add', action_data={'ip': ip, 'user': user_data}, |
|
515 | 515 | user=self._rhodecode_user) |
|
516 | 516 | Session().commit() |
|
517 | 517 | added.append(ip) |
|
518 | 518 | except formencode.Invalid as error: |
|
519 | 519 | msg = error.error_dict['ip'] |
|
520 | 520 | h.flash(msg, category='error') |
|
521 | 521 | except Exception: |
|
522 | 522 | log.exception("Exception during ip saving") |
|
523 | 523 | h.flash(_('An error occurred during ip saving'), |
|
524 | 524 | category='error') |
|
525 | 525 | if added: |
|
526 | 526 | h.flash( |
|
527 | 527 | _("Added ips %s to user whitelist") % (', '.join(ip_list), ), |
|
528 | 528 | category='success') |
|
529 | 529 | if 'default_user' in self.request.POST: |
|
530 | 530 | # case for editing global IP list we do it for 'DEFAULT' user |
|
531 | 531 | raise HTTPFound(h.route_path('admin_permissions_ips')) |
|
532 | 532 | raise HTTPFound(h.route_path('edit_user_ips', user_id=user_id)) |
|
533 | 533 | |
|
534 | 534 | @LoginRequired() |
|
535 | 535 | @HasPermissionAllDecorator('hg.admin') |
|
536 | 536 | @CSRFRequired() |
|
537 | 537 | @view_config( |
|
538 | 538 | route_name='edit_user_ips_delete', request_method='POST') |
|
539 | 539 | def ips_delete(self): |
|
540 | 540 | _ = self.request.translate |
|
541 | 541 | c = self.load_default_context() |
|
542 | 542 | |
|
543 | 543 | user_id = self.request.matchdict.get('user_id') |
|
544 | 544 | c.user = User.get_or_404(user_id) |
|
545 | 545 | # NOTE(marcink): this view is allowed for default users, as we can |
|
546 | 546 | # edit their IP white list |
|
547 | 547 | |
|
548 | 548 | ip_id = self.request.POST.get('del_ip_id') |
|
549 | 549 | user_model = UserModel() |
|
550 | 550 | user_data = c.user.get_api_data() |
|
551 | 551 | ip = UserIpMap.query().get(ip_id).ip_addr |
|
552 | 552 | user_model.delete_extra_ip(c.user.user_id, ip_id) |
|
553 | 553 | audit_logger.store_web( |
|
554 | 554 | 'user.edit.ip.delete', action_data={'ip': ip, 'user': user_data}, |
|
555 | 555 | user=self._rhodecode_user) |
|
556 | 556 | Session().commit() |
|
557 | 557 | h.flash(_("Removed ip address from user whitelist"), category='success') |
|
558 | 558 | |
|
559 | 559 | if 'default_user' in self.request.POST: |
|
560 | 560 | # case for editing global IP list we do it for 'DEFAULT' user |
|
561 | 561 | raise HTTPFound(h.route_path('admin_permissions_ips')) |
|
562 | 562 | raise HTTPFound(h.route_path('edit_user_ips', user_id=user_id)) |
|
563 | 563 | |
|
564 | 564 | @LoginRequired() |
|
565 | 565 | @HasPermissionAllDecorator('hg.admin') |
|
566 | 566 | @view_config( |
|
567 | 567 | route_name='edit_user_groups_management', request_method='GET', |
|
568 | 568 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
569 | 569 | def groups_management(self): |
|
570 | 570 | c = self.load_default_context() |
|
571 | 571 | |
|
572 | 572 | user_id = self.request.matchdict.get('user_id') |
|
573 | 573 | c.user = User.get_or_404(user_id) |
|
574 | 574 | c.data = c.user.group_member |
|
575 | 575 | self._redirect_for_default_user(c.user.username) |
|
576 | 576 | groups = [UserGroupModel.get_user_groups_as_dict(group.users_group) |
|
577 | 577 | for group in c.user.group_member] |
|
578 | 578 | c.groups = json.dumps(groups) |
|
579 | 579 | c.active = 'groups' |
|
580 | 580 | |
|
581 | 581 | return self._get_template_context(c) |
|
582 | 582 | |
|
583 | 583 | @LoginRequired() |
|
584 | 584 | @HasPermissionAllDecorator('hg.admin') |
|
585 | 585 | @CSRFRequired() |
|
586 | 586 | @view_config( |
|
587 | 587 | route_name='edit_user_groups_management_updates', request_method='POST') |
|
588 | 588 | def groups_management_updates(self): |
|
589 | 589 | _ = self.request.translate |
|
590 | 590 | c = self.load_default_context() |
|
591 | 591 | |
|
592 | 592 | user_id = self.request.matchdict.get('user_id') |
|
593 | 593 | c.user = User.get_or_404(user_id) |
|
594 | 594 | self._redirect_for_default_user(c.user.username) |
|
595 | 595 | |
|
596 |
user |
|
|
597 |
user |
|
|
596 | user_groups = set(self.request.POST.getall('users_group_id')) | |
|
597 | user_groups_objects = [] | |
|
598 | 598 | |
|
599 |
for ugid in user |
|
|
600 | users_groups_model.append(UserGroupModel().get_group(safe_int(ugid))) | |
|
599 | for ugid in user_groups: | |
|
600 | user_groups_objects.append( | |
|
601 | UserGroupModel().get_group(safe_int(ugid))) | |
|
601 | 602 | user_group_model = UserGroupModel() |
|
602 |
user_group_model.change_groups(c.user, user |
|
|
603 | user_group_model.change_groups(c.user, user_groups_objects) | |
|
603 | 604 | |
|
604 | 605 | Session().commit() |
|
605 | 606 | c.active = 'user_groups_management' |
|
606 | 607 | h.flash(_("Groups successfully changed"), category='success') |
|
607 | 608 | |
|
608 | 609 | return HTTPFound(h.route_path( |
|
609 | 610 | 'edit_user_groups_management', user_id=user_id)) |
|
610 | 611 | |
|
611 | 612 | @LoginRequired() |
|
612 | 613 | @HasPermissionAllDecorator('hg.admin') |
|
613 | 614 | @view_config( |
|
614 | 615 | route_name='edit_user_audit_logs', request_method='GET', |
|
615 | 616 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
616 | 617 | def user_audit_logs(self): |
|
617 | 618 | _ = self.request.translate |
|
618 | 619 | c = self.load_default_context() |
|
619 | 620 | |
|
620 | 621 | user_id = self.request.matchdict.get('user_id') |
|
621 | 622 | c.user = User.get_or_404(user_id) |
|
622 | 623 | self._redirect_for_default_user(c.user.username) |
|
623 | 624 | c.active = 'audit' |
|
624 | 625 | |
|
625 | 626 | p = safe_int(self.request.GET.get('page', 1), 1) |
|
626 | 627 | |
|
627 | 628 | filter_term = self.request.GET.get('filter') |
|
628 | 629 | user_log = UserModel().get_user_log(c.user, filter_term) |
|
629 | 630 | |
|
630 | 631 | def url_generator(**kw): |
|
631 | 632 | if filter_term: |
|
632 | 633 | kw['filter'] = filter_term |
|
633 | 634 | return self.request.current_route_path(_query=kw) |
|
634 | 635 | |
|
635 | 636 | c.audit_logs = h.Page( |
|
636 | 637 | user_log, page=p, items_per_page=10, url=url_generator) |
|
637 | 638 | c.filter_term = filter_term |
|
638 | 639 | return self._get_template_context(c) |
|
639 | 640 | |
|
640 | 641 | @LoginRequired() |
|
641 | 642 | @HasPermissionAllDecorator('hg.admin') |
|
642 | 643 | @view_config( |
|
643 | 644 | route_name='edit_user_perms_summary', request_method='GET', |
|
644 | 645 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
645 | 646 | def user_perms_summary(self): |
|
646 | 647 | _ = self.request.translate |
|
647 | 648 | c = self.load_default_context() |
|
648 | 649 | |
|
649 | 650 | user_id = self.request.matchdict.get('user_id') |
|
650 | 651 | c.user = User.get_or_404(user_id) |
|
651 | 652 | self._redirect_for_default_user(c.user.username) |
|
652 | 653 | |
|
653 | 654 | c.active = 'perms_summary' |
|
654 | 655 | c.perm_user = c.user.AuthUser(ip_addr=self.request.remote_addr) |
|
655 | 656 | |
|
656 | 657 | return self._get_template_context(c) |
|
657 | 658 | |
|
658 | 659 | @LoginRequired() |
|
659 | 660 | @HasPermissionAllDecorator('hg.admin') |
|
660 | 661 | @view_config( |
|
661 | 662 | route_name='edit_user_perms_summary_json', request_method='GET', |
|
662 | 663 | renderer='json_ext') |
|
663 | 664 | def user_perms_summary_json(self): |
|
664 | 665 | self.load_default_context() |
|
665 | 666 | |
|
666 | 667 | user_id = self.request.matchdict.get('user_id') |
|
667 | 668 | user = User.get_or_404(user_id) |
|
668 | 669 | self._redirect_for_default_user(user.username) |
|
669 | 670 | |
|
670 | 671 | perm_user = user.AuthUser(ip_addr=self.request.remote_addr) |
|
671 | 672 | |
|
672 | 673 | return perm_user.permissions |
General Comments 0
You need to be logged in to leave comments.
Login now