##// END OF EJS Templates
admin-users: replace the previous nullslast() with db independent solution....
marcink -
r1636:917dc475 stable
parent child Browse files
Show More
@@ -1,317 +1,328 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 import datetime
22 23
23 24 from pyramid.httpexceptions import HTTPFound
24 25 from pyramid.view import view_config
26 from sqlalchemy.sql.functions import coalesce
25 27
26 28 from rhodecode.lib.helpers import Page
27 29 from rhodecode_tools.lib.ext_json import json
28 30
29 31 from rhodecode.apps._base import BaseAppView
30 32 from rhodecode.lib.auth import (
31 33 LoginRequired, HasPermissionAllDecorator, CSRFRequired)
32 34 from rhodecode.lib import helpers as h
33 35 from rhodecode.lib.utils import PartialRenderer
34 36 from rhodecode.lib.utils2 import safe_int, safe_unicode
35 37 from rhodecode.model.auth_token import AuthTokenModel
36 38 from rhodecode.model.user import UserModel
37 39 from rhodecode.model.user_group import UserGroupModel
38 40 from rhodecode.model.db import User, or_
39 41 from rhodecode.model.meta import Session
40 42
41 43 log = logging.getLogger(__name__)
42 44
43 45
44 46 class AdminUsersView(BaseAppView):
45 47 ALLOW_SCOPED_TOKENS = False
46 48 """
47 49 This view has alternative version inside EE, if modified please take a look
48 50 in there as well.
49 51 """
50 52
51 53 def load_default_context(self):
52 54 c = self._get_local_tmpl_context()
53 55 c.allow_scoped_tokens = self.ALLOW_SCOPED_TOKENS
54 56 self._register_global_c(c)
55 57 return c
56 58
57 59 def _redirect_for_default_user(self, username):
58 60 _ = self.request.translate
59 61 if username == User.DEFAULT_USER:
60 62 h.flash(_("You can't edit this user"), category='warning')
61 63 # TODO(marcink): redirect to 'users' admin panel once this
62 64 # is a pyramid view
63 65 raise HTTPFound('/')
64 66
65 67 def _extract_ordering(self, request):
66 68 column_index = safe_int(request.GET.get('order[0][column]'))
67 69 order_dir = request.GET.get(
68 70 'order[0][dir]', 'desc')
69 71 order_by = request.GET.get(
70 72 'columns[%s][data][sort]' % column_index, 'name_raw')
71 73
72 74 # translate datatable to DB columns
73 75 order_by = {
74 76 'first_name': 'name',
75 77 'last_name': 'lastname',
76 78 }.get(order_by) or order_by
77 79
78 80 search_q = request.GET.get('search[value]')
79 81 return search_q, order_by, order_dir
80 82
81 83 def _extract_chunk(self, request):
82 84 start = safe_int(request.GET.get('start'), 0)
83 85 length = safe_int(request.GET.get('length'), 25)
84 86 draw = safe_int(request.GET.get('draw'))
85 87 return draw, start, length
86 88
87 89 @HasPermissionAllDecorator('hg.admin')
88 90 @view_config(
89 91 route_name='users', request_method='GET',
90 92 renderer='rhodecode:templates/admin/users/users.mako')
91 93 def users_list(self):
92 94 c = self.load_default_context()
93 95 return self._get_template_context(c)
94 96
95 97 @HasPermissionAllDecorator('hg.admin')
96 98 @view_config(
97 99 # renderer defined below
98 100 route_name='users_data', request_method='GET', renderer='json',
99 101 xhr=True)
100 102 def users_list_data(self):
101 103 draw, start, limit = self._extract_chunk(self.request)
102 104 search_q, order_by, order_dir = self._extract_ordering(self.request)
103 105
104 106 _render = PartialRenderer('data_table/_dt_elements.mako')
105 107
106 108 def user_actions(user_id, username):
107 109 return _render("user_actions", user_id, username)
108 110
109 111 users_data_total_count = User.query()\
110 112 .filter(User.username != User.DEFAULT_USER) \
111 113 .count()
112 114
113 115 # json generate
114 116 base_q = User.query().filter(User.username != User.DEFAULT_USER)
115 117
116 118 if search_q:
117 119 like_expression = u'%{}%'.format(safe_unicode(search_q))
118 120 base_q = base_q.filter(or_(
119 121 User.username.ilike(like_expression),
120 122 User._email.ilike(like_expression),
121 123 User.name.ilike(like_expression),
122 124 User.lastname.ilike(like_expression),
123 125 ))
124 126
125 127 users_data_total_filtered_count = base_q.count()
126 128
127 129 sort_col = getattr(User, order_by, None)
128 if sort_col and order_dir == 'asc':
129 base_q = base_q.order_by(sort_col.asc())
130 elif sort_col:
131 base_q = base_q.order_by(sort_col.desc())
130 if sort_col:
131 if order_dir == 'asc':
132 # handle null values properly to order by NULL last
133 if order_by in ['last_activity']:
134 sort_col = coalesce(sort_col, datetime.date.max)
135 sort_col = sort_col.asc()
136 else:
137 # handle null values properly to order by NULL last
138 if order_by in ['last_activity']:
139 sort_col = coalesce(sort_col, datetime.date.min)
140 sort_col = sort_col.desc()
132 141
142 base_q = base_q.order_by(sort_col)
133 143 base_q = base_q.offset(start).limit(limit)
144
134 145 users_list = base_q.all()
135 146
136 147 users_data = []
137 148 for user in users_list:
138 149 users_data.append({
139 150 "username": h.gravatar_with_user(user.username),
140 151 "email": user.email,
141 152 "first_name": h.escape(user.name),
142 153 "last_name": h.escape(user.lastname),
143 154 "last_login": h.format_date(user.last_login),
144 155 "last_activity": h.format_date(user.last_activity),
145 156 "active": h.bool2icon(user.active),
146 157 "active_raw": user.active,
147 158 "admin": h.bool2icon(user.admin),
148 159 "extern_type": user.extern_type,
149 160 "extern_name": user.extern_name,
150 161 "action": user_actions(user.user_id, user.username),
151 162 })
152 163
153 164 data = ({
154 165 'draw': draw,
155 166 'data': users_data,
156 167 'recordsTotal': users_data_total_count,
157 168 'recordsFiltered': users_data_total_filtered_count,
158 169 })
159 170
160 171 return data
161 172
162 173 @LoginRequired()
163 174 @HasPermissionAllDecorator('hg.admin')
164 175 @view_config(
165 176 route_name='edit_user_auth_tokens', request_method='GET',
166 177 renderer='rhodecode:templates/admin/users/user_edit.mako')
167 178 def auth_tokens(self):
168 179 _ = self.request.translate
169 180 c = self.load_default_context()
170 181
171 182 user_id = self.request.matchdict.get('user_id')
172 183 c.user = User.get_or_404(user_id, pyramid_exc=True)
173 184 self._redirect_for_default_user(c.user.username)
174 185
175 186 c.active = 'auth_tokens'
176 187
177 188 c.lifetime_values = [
178 189 (str(-1), _('forever')),
179 190 (str(5), _('5 minutes')),
180 191 (str(60), _('1 hour')),
181 192 (str(60 * 24), _('1 day')),
182 193 (str(60 * 24 * 30), _('1 month')),
183 194 ]
184 195 c.lifetime_options = [(c.lifetime_values, _("Lifetime"))]
185 196 c.role_values = [
186 197 (x, AuthTokenModel.cls._get_role_name(x))
187 198 for x in AuthTokenModel.cls.ROLES]
188 199 c.role_options = [(c.role_values, _("Role"))]
189 200 c.user_auth_tokens = AuthTokenModel().get_auth_tokens(
190 201 c.user.user_id, show_expired=True)
191 202 return self._get_template_context(c)
192 203
193 204 def maybe_attach_token_scope(self, token):
194 205 # implemented in EE edition
195 206 pass
196 207
197 208 @LoginRequired()
198 209 @HasPermissionAllDecorator('hg.admin')
199 210 @CSRFRequired()
200 211 @view_config(
201 212 route_name='edit_user_auth_tokens_add', request_method='POST')
202 213 def auth_tokens_add(self):
203 214 _ = self.request.translate
204 215 c = self.load_default_context()
205 216
206 217 user_id = self.request.matchdict.get('user_id')
207 218 c.user = User.get_or_404(user_id, pyramid_exc=True)
208 219 self._redirect_for_default_user(c.user.username)
209 220
210 221 lifetime = safe_int(self.request.POST.get('lifetime'), -1)
211 222 description = self.request.POST.get('description')
212 223 role = self.request.POST.get('role')
213 224
214 225 token = AuthTokenModel().create(
215 226 c.user.user_id, description, lifetime, role)
216 227 self.maybe_attach_token_scope(token)
217 228 Session().commit()
218 229
219 230 h.flash(_("Auth token successfully created"), category='success')
220 231 return HTTPFound(h.route_path('edit_user_auth_tokens', user_id=user_id))
221 232
222 233 @LoginRequired()
223 234 @HasPermissionAllDecorator('hg.admin')
224 235 @CSRFRequired()
225 236 @view_config(
226 237 route_name='edit_user_auth_tokens_delete', request_method='POST')
227 238 def auth_tokens_delete(self):
228 239 _ = self.request.translate
229 240 c = self.load_default_context()
230 241
231 242 user_id = self.request.matchdict.get('user_id')
232 243 c.user = User.get_or_404(user_id, pyramid_exc=True)
233 244 self._redirect_for_default_user(c.user.username)
234 245
235 246 del_auth_token = self.request.POST.get('del_auth_token')
236 247
237 248 if del_auth_token:
238 249 AuthTokenModel().delete(del_auth_token, c.user.user_id)
239 250 Session().commit()
240 251 h.flash(_("Auth token successfully deleted"), category='success')
241 252
242 253 return HTTPFound(h.route_path('edit_user_auth_tokens', user_id=user_id))
243 254
244 255 @LoginRequired()
245 256 @HasPermissionAllDecorator('hg.admin')
246 257 @view_config(
247 258 route_name='edit_user_groups_management', request_method='GET',
248 259 renderer='rhodecode:templates/admin/users/user_edit.mako')
249 260 def groups_management(self):
250 261 c = self.load_default_context()
251 262
252 263 user_id = self.request.matchdict.get('user_id')
253 264 c.user = User.get_or_404(user_id, pyramid_exc=True)
254 265 c.data = c.user.group_member
255 266 self._redirect_for_default_user(c.user.username)
256 267 groups = [UserGroupModel.get_user_groups_as_dict(group.users_group) for group in c.user.group_member]
257 268 c.groups = json.dumps(groups)
258 269 c.active = 'groups'
259 270
260 271 return self._get_template_context(c)
261 272
262 273 @LoginRequired()
263 274 @HasPermissionAllDecorator('hg.admin')
264 275 @view_config(
265 276 route_name='edit_user_groups_management_updates', request_method='POST')
266 277 def groups_management_updates(self):
267 278 _ = self.request.translate
268 279 c = self.load_default_context()
269 280
270 281 user_id = self.request.matchdict.get('user_id')
271 282 c.user = User.get_or_404(user_id, pyramid_exc=True)
272 283 self._redirect_for_default_user(c.user.username)
273 284
274 285 users_groups = set(self.request.POST.getall('users_group_id'))
275 286 users_groups_model = []
276 287
277 288 for ugid in users_groups:
278 289 users_groups_model.append(UserGroupModel().get_group(safe_int(ugid)))
279 290 user_group_model = UserGroupModel()
280 291 user_group_model.change_groups(c.user, users_groups_model)
281 292
282 293 Session().commit()
283 294 c.active = 'user_groups_management'
284 295 h.flash(_("Groups successfully changed"), category='success')
285 296
286 297 return HTTPFound(h.route_path(
287 298 'edit_user_groups_management', user_id=user_id))
288 299
289 300 @LoginRequired()
290 301 @HasPermissionAllDecorator('hg.admin')
291 302 @view_config(
292 303 route_name='edit_user_audit_logs', request_method='GET',
293 304 renderer='rhodecode:templates/admin/users/user_edit.mako')
294 305 def user_audit_logs(self):
295 306 _ = self.request.translate
296 307 c = self.load_default_context()
297 308
298 309 user_id = self.request.matchdict.get('user_id')
299 310 c.user = User.get_or_404(user_id, pyramid_exc=True)
300 311 self._redirect_for_default_user(c.user.username)
301 312 c.active = 'audit'
302 313
303 314 p = safe_int(self.request.GET.get('page', 1), 1)
304 315
305 316 filter_term = self.request.GET.get('filter')
306 317 c.user_log = UserModel().get_user_log(c.user, filter_term)
307 318
308 319 def url_generator(**kw):
309 320 if filter_term:
310 321 kw['filter'] = filter_term
311 322 return self.request.current_route_path(_query=kw)
312 323
313 324 c.user_log = Page(c.user_log, page=p, items_per_page=10,
314 325 url=url_generator)
315 326 c.filter_term = filter_term
316 327 return self._get_template_context(c)
317 328
General Comments 0
You need to be logged in to leave comments. Login now