Show More
@@ -1,240 +1,240 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2016-2017 RhodeCode GmbH |
|
3 | # Copyright (C) 2016-2017 RhodeCode GmbH | |
4 | # |
|
4 | # | |
5 | # This program is free software: you can redistribute it and/or modify |
|
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |
7 | # (only), as published by the Free Software Foundation. |
|
7 | # (only), as published by the Free Software Foundation. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License |
|
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | # |
|
16 | # | |
17 | # This program is dual-licensed. If you wish to learn more about the |
|
17 | # This program is dual-licensed. If you wish to learn more about the | |
18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
20 |
|
20 | |||
21 | import logging |
|
21 | import logging | |
22 |
|
22 | |||
23 | from pyramid.httpexceptions import HTTPFound |
|
23 | from pyramid.httpexceptions import HTTPFound | |
24 | from pyramid.view import view_config |
|
24 | from pyramid.view import view_config | |
25 |
|
25 | |||
26 | from rhodecode.apps._base import BaseAppView |
|
26 | from rhodecode.apps._base import BaseAppView | |
27 | from rhodecode.lib.auth import ( |
|
27 | from rhodecode.lib.auth import ( | |
28 | LoginRequired, HasPermissionAllDecorator, CSRFRequired) |
|
28 | LoginRequired, HasPermissionAllDecorator, CSRFRequired) | |
29 | from rhodecode.lib import helpers as h |
|
29 | from rhodecode.lib import helpers as h | |
30 | from rhodecode.lib.utils import PartialRenderer |
|
30 | from rhodecode.lib.utils import PartialRenderer | |
31 | from rhodecode.lib.utils2 import safe_int, safe_unicode |
|
31 | from rhodecode.lib.utils2 import safe_int, safe_unicode | |
32 | from rhodecode.model.auth_token import AuthTokenModel |
|
32 | from rhodecode.model.auth_token import AuthTokenModel | |
33 | from rhodecode.model.db import User, or_ |
|
33 | from rhodecode.model.db import User, or_ | |
34 | from rhodecode.model.meta import Session |
|
34 | from rhodecode.model.meta import Session | |
35 |
|
35 | |||
36 | log = logging.getLogger(__name__) |
|
36 | log = logging.getLogger(__name__) | |
37 |
|
37 | |||
38 |
|
38 | |||
39 | class AdminUsersView(BaseAppView): |
|
39 | class AdminUsersView(BaseAppView): | |
40 | ALLOW_SCOPED_TOKENS = False |
|
40 | ALLOW_SCOPED_TOKENS = False | |
41 | """ |
|
41 | """ | |
42 | This view has alternative version inside EE, if modified please take a look |
|
42 | This view has alternative version inside EE, if modified please take a look | |
43 | in there as well. |
|
43 | in there as well. | |
44 | """ |
|
44 | """ | |
45 |
|
45 | |||
46 | def load_default_context(self): |
|
46 | def load_default_context(self): | |
47 | c = self._get_local_tmpl_context() |
|
47 | c = self._get_local_tmpl_context() | |
48 | c.auth_user = self.request.user |
|
48 | c.auth_user = self.request.user | |
49 | c.allow_scoped_tokens = self.ALLOW_SCOPED_TOKENS |
|
49 | c.allow_scoped_tokens = self.ALLOW_SCOPED_TOKENS | |
50 | self._register_global_c(c) |
|
50 | self._register_global_c(c) | |
51 | return c |
|
51 | return c | |
52 |
|
52 | |||
53 | def _redirect_for_default_user(self, username): |
|
53 | def _redirect_for_default_user(self, username): | |
54 | _ = self.request.translate |
|
54 | _ = self.request.translate | |
55 | if username == User.DEFAULT_USER: |
|
55 | if username == User.DEFAULT_USER: | |
56 | h.flash(_("You can't edit this user"), category='warning') |
|
56 | h.flash(_("You can't edit this user"), category='warning') | |
57 | # TODO(marcink): redirect to 'users' admin panel once this |
|
57 | # TODO(marcink): redirect to 'users' admin panel once this | |
58 | # is a pyramid view |
|
58 | # is a pyramid view | |
59 | raise HTTPFound('/') |
|
59 | raise HTTPFound('/') | |
60 |
|
60 | |||
61 | def _extract_ordering(self, request): |
|
61 | def _extract_ordering(self, request): | |
62 | column_index = safe_int(request.GET.get('order[0][column]')) |
|
62 | column_index = safe_int(request.GET.get('order[0][column]')) | |
63 | order_dir = request.GET.get( |
|
63 | order_dir = request.GET.get( | |
64 | 'order[0][dir]', 'desc') |
|
64 | 'order[0][dir]', 'desc') | |
65 | order_by = request.GET.get( |
|
65 | order_by = request.GET.get( | |
66 | 'columns[%s][data][sort]' % column_index, 'name_raw') |
|
66 | 'columns[%s][data][sort]' % column_index, 'name_raw') | |
67 |
|
67 | |||
68 | # translate datatable to DB columns |
|
68 | # translate datatable to DB columns | |
69 | order_by = { |
|
69 | order_by = { | |
70 | 'first_name': 'name', |
|
70 | 'first_name': 'name', | |
71 | 'last_name': 'lastname', |
|
71 | 'last_name': 'lastname', | |
72 | 'last_activity': '' |
|
72 | 'last_activity': '' | |
73 | }.get(order_by) or order_by |
|
73 | }.get(order_by) or order_by | |
74 |
|
74 | |||
75 | search_q = request.GET.get('search[value]') |
|
75 | search_q = request.GET.get('search[value]') | |
76 | return search_q, order_by, order_dir |
|
76 | return search_q, order_by, order_dir | |
77 |
|
77 | |||
78 | def _extract_chunk(self, request): |
|
78 | def _extract_chunk(self, request): | |
79 | start = safe_int(request.GET.get('start'), 0) |
|
79 | start = safe_int(request.GET.get('start'), 0) | |
80 | length = safe_int(request.GET.get('length'), 25) |
|
80 | length = safe_int(request.GET.get('length'), 25) | |
81 | draw = safe_int(request.GET.get('draw')) |
|
81 | draw = safe_int(request.GET.get('draw')) | |
82 | return draw, start, length |
|
82 | return draw, start, length | |
83 |
|
83 | |||
84 | @HasPermissionAllDecorator('hg.admin') |
|
84 | @HasPermissionAllDecorator('hg.admin') | |
85 | @view_config( |
|
85 | @view_config( | |
86 | route_name='users', request_method='GET', |
|
86 | route_name='users', request_method='GET', | |
87 | renderer='rhodecode:templates/admin/users/users.mako') |
|
87 | renderer='rhodecode:templates/admin/users/users.mako') | |
88 | def users_list(self): |
|
88 | def users_list(self): | |
89 | c = self.load_default_context() |
|
89 | c = self.load_default_context() | |
90 | return self._get_template_context(c) |
|
90 | return self._get_template_context(c) | |
91 |
|
91 | |||
92 | @HasPermissionAllDecorator('hg.admin') |
|
92 | @HasPermissionAllDecorator('hg.admin') | |
93 | @view_config( |
|
93 | @view_config( | |
94 | # renderer defined below |
|
94 | # renderer defined below | |
95 | route_name='users_data', request_method='GET', renderer='json', |
|
95 | route_name='users_data', request_method='GET', renderer='json', | |
96 | xhr=True) |
|
96 | xhr=True) | |
97 | def users_list_data(self): |
|
97 | def users_list_data(self): | |
98 | draw, start, limit = self._extract_chunk(self.request) |
|
98 | draw, start, limit = self._extract_chunk(self.request) | |
99 | search_q, order_by, order_dir = self._extract_ordering(self.request) |
|
99 | search_q, order_by, order_dir = self._extract_ordering(self.request) | |
100 |
|
100 | |||
101 | _render = PartialRenderer('data_table/_dt_elements.mako') |
|
101 | _render = PartialRenderer('data_table/_dt_elements.mako') | |
102 |
|
102 | |||
103 | def user_actions(user_id, username): |
|
103 | def user_actions(user_id, username): | |
104 | return _render("user_actions", user_id, username) |
|
104 | return _render("user_actions", user_id, username) | |
105 |
|
105 | |||
106 | users_data_total_count = User.query()\ |
|
106 | users_data_total_count = User.query()\ | |
107 | .filter(User.username != User.DEFAULT_USER) \ |
|
107 | .filter(User.username != User.DEFAULT_USER) \ | |
108 | .count() |
|
108 | .count() | |
109 |
|
109 | |||
110 | # json generate |
|
110 | # json generate | |
111 | base_q = User.query().filter(User.username != User.DEFAULT_USER) |
|
111 | base_q = User.query().filter(User.username != User.DEFAULT_USER) | |
112 |
|
112 | |||
113 | if search_q: |
|
113 | if search_q: | |
114 | like_expression = u'{}%'.format(safe_unicode(search_q)) |
|
114 | like_expression = u'%{}%'.format(safe_unicode(search_q)) | |
115 | base_q = base_q.filter(or_( |
|
115 | base_q = base_q.filter(or_( | |
116 | User.username.ilike(like_expression), |
|
116 | User.username.ilike(like_expression), | |
117 | User._email.ilike(like_expression), |
|
117 | User._email.ilike(like_expression), | |
118 | User.name.ilike(like_expression), |
|
118 | User.name.ilike(like_expression), | |
119 | User.lastname.ilike(like_expression), |
|
119 | User.lastname.ilike(like_expression), | |
120 | )) |
|
120 | )) | |
121 |
|
121 | |||
122 | users_data_total_filtered_count = base_q.count() |
|
122 | users_data_total_filtered_count = base_q.count() | |
123 |
|
123 | |||
124 | sort_col = getattr(User, order_by, None) |
|
124 | sort_col = getattr(User, order_by, None) | |
125 | if sort_col and order_dir == 'asc': |
|
125 | if sort_col and order_dir == 'asc': | |
126 | base_q = base_q.order_by(sort_col.asc()) |
|
126 | base_q = base_q.order_by(sort_col.asc()) | |
127 | elif sort_col: |
|
127 | elif sort_col: | |
128 | base_q = base_q.order_by(sort_col.desc()) |
|
128 | base_q = base_q.order_by(sort_col.desc()) | |
129 |
|
129 | |||
130 | base_q = base_q.offset(start).limit(limit) |
|
130 | base_q = base_q.offset(start).limit(limit) | |
131 | users_list = base_q.all() |
|
131 | users_list = base_q.all() | |
132 |
|
132 | |||
133 | users_data = [] |
|
133 | users_data = [] | |
134 | for user in users_list: |
|
134 | for user in users_list: | |
135 | users_data.append({ |
|
135 | users_data.append({ | |
136 | "username": h.gravatar_with_user(user.username), |
|
136 | "username": h.gravatar_with_user(user.username), | |
137 | "email": user.email, |
|
137 | "email": user.email, | |
138 | "first_name": h.escape(user.name), |
|
138 | "first_name": h.escape(user.name), | |
139 | "last_name": h.escape(user.lastname), |
|
139 | "last_name": h.escape(user.lastname), | |
140 | "last_login": h.format_date(user.last_login), |
|
140 | "last_login": h.format_date(user.last_login), | |
141 | "last_activity": h.format_date( |
|
141 | "last_activity": h.format_date( | |
142 | h.time_to_datetime(user.user_data.get('last_activity', 0))), |
|
142 | h.time_to_datetime(user.user_data.get('last_activity', 0))), | |
143 | "active": h.bool2icon(user.active), |
|
143 | "active": h.bool2icon(user.active), | |
144 | "active_raw": user.active, |
|
144 | "active_raw": user.active, | |
145 | "admin": h.bool2icon(user.admin), |
|
145 | "admin": h.bool2icon(user.admin), | |
146 | "extern_type": user.extern_type, |
|
146 | "extern_type": user.extern_type, | |
147 | "extern_name": user.extern_name, |
|
147 | "extern_name": user.extern_name, | |
148 | "action": user_actions(user.user_id, user.username), |
|
148 | "action": user_actions(user.user_id, user.username), | |
149 | }) |
|
149 | }) | |
150 |
|
150 | |||
151 | data = ({ |
|
151 | data = ({ | |
152 | 'draw': draw, |
|
152 | 'draw': draw, | |
153 | 'data': users_data, |
|
153 | 'data': users_data, | |
154 | 'recordsTotal': users_data_total_count, |
|
154 | 'recordsTotal': users_data_total_count, | |
155 | 'recordsFiltered': users_data_total_filtered_count, |
|
155 | 'recordsFiltered': users_data_total_filtered_count, | |
156 | }) |
|
156 | }) | |
157 |
|
157 | |||
158 | return data |
|
158 | return data | |
159 |
|
159 | |||
160 | @LoginRequired() |
|
160 | @LoginRequired() | |
161 | @HasPermissionAllDecorator('hg.admin') |
|
161 | @HasPermissionAllDecorator('hg.admin') | |
162 | @view_config( |
|
162 | @view_config( | |
163 | route_name='edit_user_auth_tokens', request_method='GET', |
|
163 | route_name='edit_user_auth_tokens', request_method='GET', | |
164 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
164 | renderer='rhodecode:templates/admin/users/user_edit.mako') | |
165 | def auth_tokens(self): |
|
165 | def auth_tokens(self): | |
166 | _ = self.request.translate |
|
166 | _ = self.request.translate | |
167 | c = self.load_default_context() |
|
167 | c = self.load_default_context() | |
168 |
|
168 | |||
169 | user_id = self.request.matchdict.get('user_id') |
|
169 | user_id = self.request.matchdict.get('user_id') | |
170 | c.user = User.get_or_404(user_id, pyramid_exc=True) |
|
170 | c.user = User.get_or_404(user_id, pyramid_exc=True) | |
171 | self._redirect_for_default_user(c.user.username) |
|
171 | self._redirect_for_default_user(c.user.username) | |
172 |
|
172 | |||
173 | c.active = 'auth_tokens' |
|
173 | c.active = 'auth_tokens' | |
174 |
|
174 | |||
175 | c.lifetime_values = [ |
|
175 | c.lifetime_values = [ | |
176 | (str(-1), _('forever')), |
|
176 | (str(-1), _('forever')), | |
177 | (str(5), _('5 minutes')), |
|
177 | (str(5), _('5 minutes')), | |
178 | (str(60), _('1 hour')), |
|
178 | (str(60), _('1 hour')), | |
179 | (str(60 * 24), _('1 day')), |
|
179 | (str(60 * 24), _('1 day')), | |
180 | (str(60 * 24 * 30), _('1 month')), |
|
180 | (str(60 * 24 * 30), _('1 month')), | |
181 | ] |
|
181 | ] | |
182 | c.lifetime_options = [(c.lifetime_values, _("Lifetime"))] |
|
182 | c.lifetime_options = [(c.lifetime_values, _("Lifetime"))] | |
183 | c.role_values = [ |
|
183 | c.role_values = [ | |
184 | (x, AuthTokenModel.cls._get_role_name(x)) |
|
184 | (x, AuthTokenModel.cls._get_role_name(x)) | |
185 | for x in AuthTokenModel.cls.ROLES] |
|
185 | for x in AuthTokenModel.cls.ROLES] | |
186 | c.role_options = [(c.role_values, _("Role"))] |
|
186 | c.role_options = [(c.role_values, _("Role"))] | |
187 | c.user_auth_tokens = AuthTokenModel().get_auth_tokens( |
|
187 | c.user_auth_tokens = AuthTokenModel().get_auth_tokens( | |
188 | c.user.user_id, show_expired=True) |
|
188 | c.user.user_id, show_expired=True) | |
189 | return self._get_template_context(c) |
|
189 | return self._get_template_context(c) | |
190 |
|
190 | |||
191 | def maybe_attach_token_scope(self, token): |
|
191 | def maybe_attach_token_scope(self, token): | |
192 | # implemented in EE edition |
|
192 | # implemented in EE edition | |
193 | pass |
|
193 | pass | |
194 |
|
194 | |||
195 | @LoginRequired() |
|
195 | @LoginRequired() | |
196 | @HasPermissionAllDecorator('hg.admin') |
|
196 | @HasPermissionAllDecorator('hg.admin') | |
197 | @CSRFRequired() |
|
197 | @CSRFRequired() | |
198 | @view_config( |
|
198 | @view_config( | |
199 | route_name='edit_user_auth_tokens_add', request_method='POST') |
|
199 | route_name='edit_user_auth_tokens_add', request_method='POST') | |
200 | def auth_tokens_add(self): |
|
200 | def auth_tokens_add(self): | |
201 | _ = self.request.translate |
|
201 | _ = self.request.translate | |
202 | c = self.load_default_context() |
|
202 | c = self.load_default_context() | |
203 |
|
203 | |||
204 | user_id = self.request.matchdict.get('user_id') |
|
204 | user_id = self.request.matchdict.get('user_id') | |
205 | c.user = User.get_or_404(user_id, pyramid_exc=True) |
|
205 | c.user = User.get_or_404(user_id, pyramid_exc=True) | |
206 | self._redirect_for_default_user(c.user.username) |
|
206 | self._redirect_for_default_user(c.user.username) | |
207 |
|
207 | |||
208 | lifetime = safe_int(self.request.POST.get('lifetime'), -1) |
|
208 | lifetime = safe_int(self.request.POST.get('lifetime'), -1) | |
209 | description = self.request.POST.get('description') |
|
209 | description = self.request.POST.get('description') | |
210 | role = self.request.POST.get('role') |
|
210 | role = self.request.POST.get('role') | |
211 |
|
211 | |||
212 | token = AuthTokenModel().create( |
|
212 | token = AuthTokenModel().create( | |
213 | c.user.user_id, description, lifetime, role) |
|
213 | c.user.user_id, description, lifetime, role) | |
214 | self.maybe_attach_token_scope(token) |
|
214 | self.maybe_attach_token_scope(token) | |
215 | Session().commit() |
|
215 | Session().commit() | |
216 |
|
216 | |||
217 | h.flash(_("Auth token successfully created"), category='success') |
|
217 | h.flash(_("Auth token successfully created"), category='success') | |
218 | return HTTPFound(h.route_path('edit_user_auth_tokens', user_id=user_id)) |
|
218 | return HTTPFound(h.route_path('edit_user_auth_tokens', user_id=user_id)) | |
219 |
|
219 | |||
220 | @LoginRequired() |
|
220 | @LoginRequired() | |
221 | @HasPermissionAllDecorator('hg.admin') |
|
221 | @HasPermissionAllDecorator('hg.admin') | |
222 | @CSRFRequired() |
|
222 | @CSRFRequired() | |
223 | @view_config( |
|
223 | @view_config( | |
224 | route_name='edit_user_auth_tokens_delete', request_method='POST') |
|
224 | route_name='edit_user_auth_tokens_delete', request_method='POST') | |
225 | def auth_tokens_delete(self): |
|
225 | def auth_tokens_delete(self): | |
226 | _ = self.request.translate |
|
226 | _ = self.request.translate | |
227 | c = self.load_default_context() |
|
227 | c = self.load_default_context() | |
228 |
|
228 | |||
229 | user_id = self.request.matchdict.get('user_id') |
|
229 | user_id = self.request.matchdict.get('user_id') | |
230 | c.user = User.get_or_404(user_id, pyramid_exc=True) |
|
230 | c.user = User.get_or_404(user_id, pyramid_exc=True) | |
231 | self._redirect_for_default_user(c.user.username) |
|
231 | self._redirect_for_default_user(c.user.username) | |
232 |
|
232 | |||
233 | del_auth_token = self.request.POST.get('del_auth_token') |
|
233 | del_auth_token = self.request.POST.get('del_auth_token') | |
234 |
|
234 | |||
235 | if del_auth_token: |
|
235 | if del_auth_token: | |
236 | AuthTokenModel().delete(del_auth_token, c.user.user_id) |
|
236 | AuthTokenModel().delete(del_auth_token, c.user.user_id) | |
237 | Session().commit() |
|
237 | Session().commit() | |
238 | h.flash(_("Auth token successfully deleted"), category='success') |
|
238 | h.flash(_("Auth token successfully deleted"), category='success') | |
239 |
|
239 | |||
240 | return HTTPFound(h.route_path('edit_user_auth_tokens', user_id=user_id)) |
|
240 | return HTTPFound(h.route_path('edit_user_auth_tokens', user_id=user_id)) |
General Comments 0
You need to be logged in to leave comments.
Login now