Show More
@@ -1,256 +1,256 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.model.scm import UserGroupList |
|
26 | from rhodecode.model.scm import UserGroupList | |
27 |
|
27 | |||
28 | from rhodecode.apps._base import BaseAppView, DataGridAppView |
|
28 | from rhodecode.apps._base import BaseAppView, DataGridAppView | |
29 | from rhodecode.lib.auth import ( |
|
29 | from rhodecode.lib.auth import ( | |
30 | LoginRequired, NotAnonymous, |
|
30 | LoginRequired, NotAnonymous, | |
31 | HasUserGroupPermissionAnyDecorator) |
|
31 | HasUserGroupPermissionAnyDecorator) | |
32 | from rhodecode.lib import helpers as h |
|
32 | from rhodecode.lib import helpers as h | |
33 | from rhodecode.lib.utils import PartialRenderer |
|
33 | from rhodecode.lib.utils import PartialRenderer | |
34 | from rhodecode.lib.utils2 import safe_unicode |
|
34 | from rhodecode.lib.utils2 import safe_unicode | |
35 | from rhodecode.model.db import ( |
|
35 | from rhodecode.model.db import ( | |
36 | joinedload, or_, count, User, UserGroup, UserGroupMember, |
|
36 | joinedload, or_, count, User, UserGroup, UserGroupMember, | |
37 | UserGroupRepoToPerm, UserGroupRepoGroupToPerm) |
|
37 | UserGroupRepoToPerm, UserGroupRepoGroupToPerm) | |
38 | from rhodecode.model.meta import Session |
|
38 | from rhodecode.model.meta import Session | |
39 |
|
39 | |||
40 | log = logging.getLogger(__name__) |
|
40 | log = logging.getLogger(__name__) | |
41 |
|
41 | |||
42 |
|
42 | |||
43 | class AdminUserGroupsView(BaseAppView, DataGridAppView): |
|
43 | class AdminUserGroupsView(BaseAppView, DataGridAppView): | |
44 |
|
44 | |||
45 | def load_default_context(self): |
|
45 | def load_default_context(self): | |
46 | c = self._get_local_tmpl_context() |
|
46 | c = self._get_local_tmpl_context() | |
47 | self._register_global_c(c) |
|
47 | self._register_global_c(c) | |
48 | return c |
|
48 | return c | |
49 |
|
49 | |||
50 | # permission check in data loading of |
|
50 | # permission check in data loading of | |
51 | # `user_groups_list_data` via UserGroupList |
|
51 | # `user_groups_list_data` via UserGroupList | |
52 | @LoginRequired() |
|
52 | @LoginRequired() | |
53 | @NotAnonymous() |
|
53 | @NotAnonymous() | |
54 | @view_config( |
|
54 | @view_config( | |
55 | route_name='user_groups', request_method='GET', |
|
55 | route_name='user_groups', request_method='GET', | |
56 | renderer='rhodecode:templates/admin/user_groups/user_groups.mako') |
|
56 | renderer='rhodecode:templates/admin/user_groups/user_groups.mako') | |
57 | def user_groups_list(self): |
|
57 | def user_groups_list(self): | |
58 | c = self.load_default_context() |
|
58 | c = self.load_default_context() | |
59 | return self._get_template_context(c) |
|
59 | return self._get_template_context(c) | |
60 |
|
60 | |||
61 | # permission check inside |
|
61 | # permission check inside | |
62 | @LoginRequired() |
|
62 | @LoginRequired() | |
63 | @NotAnonymous() |
|
63 | @NotAnonymous() | |
64 | @view_config( |
|
64 | @view_config( | |
65 | route_name='user_groups_data', request_method='GET', |
|
65 | route_name='user_groups_data', request_method='GET', | |
66 | renderer='json_ext', xhr=True) |
|
66 | renderer='json_ext', xhr=True) | |
67 | def user_groups_list_data(self): |
|
67 | def user_groups_list_data(self): | |
68 | column_map = { |
|
68 | column_map = { | |
69 | 'active': 'users_group_active', |
|
69 | 'active': 'users_group_active', | |
70 | 'description': 'user_group_description', |
|
70 | 'description': 'user_group_description', | |
71 | 'members': 'members_total', |
|
71 | 'members': 'members_total', | |
72 | 'owner': 'user_username', |
|
72 | 'owner': 'user_username', | |
73 | 'sync': 'group_data' |
|
73 | 'sync': 'group_data' | |
74 | } |
|
74 | } | |
75 | draw, start, limit = self._extract_chunk(self.request) |
|
75 | draw, start, limit = self._extract_chunk(self.request) | |
76 | search_q, order_by, order_dir = self._extract_ordering( |
|
76 | search_q, order_by, order_dir = self._extract_ordering( | |
77 | self.request, column_map=column_map) |
|
77 | self.request, column_map=column_map) | |
78 |
|
78 | |||
79 | _render = PartialRenderer('data_table/_dt_elements.mako') |
|
79 | _render = PartialRenderer('data_table/_dt_elements.mako') | |
80 |
|
80 | |||
81 | def user_group_name(user_group_id, user_group_name): |
|
81 | def user_group_name(user_group_id, user_group_name): | |
82 | return _render("user_group_name", user_group_id, user_group_name) |
|
82 | return _render("user_group_name", user_group_id, user_group_name) | |
83 |
|
83 | |||
84 | def user_group_actions(user_group_id, user_group_name): |
|
84 | def user_group_actions(user_group_id, user_group_name): | |
85 | return _render("user_group_actions", user_group_id, user_group_name) |
|
85 | return _render("user_group_actions", user_group_id, user_group_name) | |
86 |
|
86 | |||
87 | def user_profile(username): |
|
87 | def user_profile(username): | |
88 | return _render('user_profile', username) |
|
88 | return _render('user_profile', username) | |
89 |
|
89 | |||
90 | auth_user_group_list = UserGroupList( |
|
90 | auth_user_group_list = UserGroupList( | |
91 | UserGroup.query().all(), perm_set=['usergroup.admin']) |
|
91 | UserGroup.query().all(), perm_set=['usergroup.admin']) | |
92 |
|
92 | |||
93 | allowed_ids = [] |
|
93 | allowed_ids = [] | |
94 | for user_group in auth_user_group_list: |
|
94 | for user_group in auth_user_group_list: | |
95 | allowed_ids.append(user_group.users_group_id) |
|
95 | allowed_ids.append(user_group.users_group_id) | |
96 |
|
96 | |||
97 | user_groups_data_total_count = UserGroup.query()\ |
|
97 | user_groups_data_total_count = UserGroup.query()\ | |
98 | .filter(UserGroup.users_group_id.in_(allowed_ids))\ |
|
98 | .filter(UserGroup.users_group_id.in_(allowed_ids))\ | |
99 | .count() |
|
99 | .count() | |
100 |
|
100 | |||
101 | member_count = count(UserGroupMember.user_id) |
|
101 | member_count = count(UserGroupMember.user_id) | |
102 | base_q = Session.query( |
|
102 | base_q = Session.query( | |
103 | UserGroup.users_group_name, |
|
103 | UserGroup.users_group_name, | |
104 | UserGroup.user_group_description, |
|
104 | UserGroup.user_group_description, | |
105 | UserGroup.users_group_active, |
|
105 | UserGroup.users_group_active, | |
106 | UserGroup.users_group_id, |
|
106 | UserGroup.users_group_id, | |
107 | UserGroup.group_data, |
|
107 | UserGroup.group_data, | |
108 | User, |
|
108 | User, | |
109 | member_count.label('member_count') |
|
109 | member_count.label('member_count') | |
110 | ) \ |
|
110 | ) \ | |
111 | .filter(UserGroup.users_group_id.in_(allowed_ids)) \ |
|
111 | .filter(UserGroup.users_group_id.in_(allowed_ids)) \ | |
112 | .outerjoin(UserGroupMember) \ |
|
112 | .outerjoin(UserGroupMember) \ | |
113 | .join(User, User.user_id == UserGroup.user_id) \ |
|
113 | .join(User, User.user_id == UserGroup.user_id) \ | |
114 | .group_by(UserGroup, User) |
|
114 | .group_by(UserGroup, User) | |
115 |
|
115 | |||
116 | if search_q: |
|
116 | if search_q: | |
117 | like_expression = u'%{}%'.format(safe_unicode(search_q)) |
|
117 | like_expression = u'%{}%'.format(safe_unicode(search_q)) | |
118 | base_q = base_q.filter(or_( |
|
118 | base_q = base_q.filter(or_( | |
119 | UserGroup.users_group_name.ilike(like_expression), |
|
119 | UserGroup.users_group_name.ilike(like_expression), | |
120 | )) |
|
120 | )) | |
121 |
|
121 | |||
122 | user_groups_data_total_filtered_count = base_q.count() |
|
122 | user_groups_data_total_filtered_count = base_q.count() | |
123 |
|
123 | |||
124 | if order_by == 'members_total': |
|
124 | if order_by == 'members_total': | |
125 | sort_col = member_count |
|
125 | sort_col = member_count | |
126 | elif order_by == 'user_username': |
|
126 | elif order_by == 'user_username': | |
127 | sort_col = User.username |
|
127 | sort_col = User.username | |
128 | else: |
|
128 | else: | |
129 | sort_col = getattr(UserGroup, order_by, None) |
|
129 | sort_col = getattr(UserGroup, order_by, None) | |
130 |
|
130 | |||
131 | if isinstance(sort_col, count) or sort_col: |
|
131 | if isinstance(sort_col, count) or sort_col: | |
132 | if order_dir == 'asc': |
|
132 | if order_dir == 'asc': | |
133 | sort_col = sort_col.asc() |
|
133 | sort_col = sort_col.asc() | |
134 | else: |
|
134 | else: | |
135 | sort_col = sort_col.desc() |
|
135 | sort_col = sort_col.desc() | |
136 |
|
136 | |||
137 | base_q = base_q.order_by(sort_col) |
|
137 | base_q = base_q.order_by(sort_col) | |
138 | base_q = base_q.offset(start).limit(limit) |
|
138 | base_q = base_q.offset(start).limit(limit) | |
139 |
|
139 | |||
140 | # authenticated access to user groups |
|
140 | # authenticated access to user groups | |
141 | auth_user_group_list = base_q.all() |
|
141 | auth_user_group_list = base_q.all() | |
142 |
|
142 | |||
143 | user_groups_data = [] |
|
143 | user_groups_data = [] | |
144 | for user_gr in auth_user_group_list: |
|
144 | for user_gr in auth_user_group_list: | |
145 | user_groups_data.append({ |
|
145 | user_groups_data.append({ | |
146 | "users_group_name": user_group_name( |
|
146 | "users_group_name": user_group_name( | |
147 | user_gr.users_group_id, h.escape(user_gr.users_group_name)), |
|
147 | user_gr.users_group_id, h.escape(user_gr.users_group_name)), | |
148 | "name_raw": h.escape(user_gr.users_group_name), |
|
148 | "name_raw": h.escape(user_gr.users_group_name), | |
149 | "description": h.escape(user_gr.user_group_description), |
|
149 | "description": h.escape(user_gr.user_group_description), | |
150 | "members": user_gr.member_count, |
|
150 | "members": user_gr.member_count, | |
151 | # NOTE(marcink): because of advanced query we |
|
151 | # NOTE(marcink): because of advanced query we | |
152 | # need to load it like that |
|
152 | # need to load it like that | |
153 | "sync": UserGroup._load_group_data( |
|
153 | "sync": UserGroup._load_group_data( | |
154 | user_gr.group_data).get('extern_type'), |
|
154 | user_gr.group_data).get('extern_type'), | |
155 | "active": h.bool2icon(user_gr.users_group_active), |
|
155 | "active": h.bool2icon(user_gr.users_group_active), | |
156 | "owner": user_profile(user_gr.User.username), |
|
156 | "owner": user_profile(user_gr.User.username), | |
157 | "action": user_group_actions( |
|
157 | "action": user_group_actions( | |
158 | user_gr.users_group_id, user_gr.users_group_name) |
|
158 | user_gr.users_group_id, user_gr.users_group_name) | |
159 | }) |
|
159 | }) | |
160 |
|
160 | |||
161 | data = ({ |
|
161 | data = ({ | |
162 | 'draw': draw, |
|
162 | 'draw': draw, | |
163 | 'data': user_groups_data, |
|
163 | 'data': user_groups_data, | |
164 | 'recordsTotal': user_groups_data_total_count, |
|
164 | 'recordsTotal': user_groups_data_total_count, | |
165 | 'recordsFiltered': user_groups_data_total_filtered_count, |
|
165 | 'recordsFiltered': user_groups_data_total_filtered_count, | |
166 | }) |
|
166 | }) | |
167 |
|
167 | |||
168 | return data |
|
168 | return data | |
169 |
|
169 | |||
170 | @LoginRequired() |
|
170 | @LoginRequired() | |
171 | @HasUserGroupPermissionAnyDecorator('usergroup.admin') |
|
171 | @HasUserGroupPermissionAnyDecorator('usergroup.admin') | |
172 | @view_config( |
|
172 | @view_config( | |
173 | route_name='user_group_members_data', request_method='GET', |
|
173 | route_name='user_group_members_data', request_method='GET', | |
174 | renderer='json_ext', xhr=True) |
|
174 | renderer='json_ext', xhr=True) | |
175 | def user_group_members(self): |
|
175 | def user_group_members(self): | |
176 | """ |
|
176 | """ | |
177 | Return members of given user group |
|
177 | Return members of given user group | |
178 | """ |
|
178 | """ | |
179 | user_group_id = self.request.matchdict['user_group_id'] |
|
179 | user_group_id = self.request.matchdict['user_group_id'] | |
180 | user_group = UserGroup.get_or_404(user_group_id) |
|
180 | user_group = UserGroup.get_or_404(user_group_id) | |
181 | group_members_obj = sorted((x.user for x in user_group.members), |
|
181 | group_members_obj = sorted((x.user for x in user_group.members), | |
182 | key=lambda u: u.username.lower()) |
|
182 | key=lambda u: u.username.lower()) | |
183 |
|
183 | |||
184 | group_members = [ |
|
184 | group_members = [ | |
185 | { |
|
185 | { | |
186 | 'id': user.user_id, |
|
186 | 'id': user.user_id, | |
187 | 'first_name': user.first_name, |
|
187 | 'first_name': user.first_name, | |
188 | 'last_name': user.last_name, |
|
188 | 'last_name': user.last_name, | |
189 | 'username': user.username, |
|
189 | 'username': user.username, | |
190 | 'icon_link': h.gravatar_url(user.email, 30), |
|
190 | 'icon_link': h.gravatar_url(user.email, 30), | |
191 | 'value_display': h.person(user.email), |
|
191 | 'value_display': h.person(user.email), | |
192 | 'value': user.username, |
|
192 | 'value': user.username, | |
193 | 'value_type': 'user', |
|
193 | 'value_type': 'user', | |
194 | 'active': user.active, |
|
194 | 'active': user.active, | |
195 | } |
|
195 | } | |
196 | for user in group_members_obj |
|
196 | for user in group_members_obj | |
197 | ] |
|
197 | ] | |
198 |
|
198 | |||
199 | return { |
|
199 | return { | |
200 | 'members': group_members |
|
200 | 'members': group_members | |
201 | } |
|
201 | } | |
202 |
|
202 | |||
203 | def _get_perms_summary(self, user_group_id): |
|
203 | def _get_perms_summary(self, user_group_id): | |
204 | permissions = { |
|
204 | permissions = { | |
205 | 'repositories': {}, |
|
205 | 'repositories': {}, | |
206 | 'repositories_groups': {}, |
|
206 | 'repositories_groups': {}, | |
207 | } |
|
207 | } | |
208 | ugroup_repo_perms = UserGroupRepoToPerm.query()\ |
|
208 | ugroup_repo_perms = UserGroupRepoToPerm.query()\ | |
209 | .options(joinedload(UserGroupRepoToPerm.permission))\ |
|
209 | .options(joinedload(UserGroupRepoToPerm.permission))\ | |
210 | .options(joinedload(UserGroupRepoToPerm.repository))\ |
|
210 | .options(joinedload(UserGroupRepoToPerm.repository))\ | |
211 | .filter(UserGroupRepoToPerm.users_group_id == user_group_id)\ |
|
211 | .filter(UserGroupRepoToPerm.users_group_id == user_group_id)\ | |
212 | .all() |
|
212 | .all() | |
213 |
|
213 | |||
214 | for gr in ugroup_repo_perms: |
|
214 | for gr in ugroup_repo_perms: | |
215 | permissions['repositories'][gr.repository.repo_name] \ |
|
215 | permissions['repositories'][gr.repository.repo_name] \ | |
216 | = gr.permission.permission_name |
|
216 | = gr.permission.permission_name | |
217 |
|
217 | |||
218 | ugroup_group_perms = UserGroupRepoGroupToPerm.query()\ |
|
218 | ugroup_group_perms = UserGroupRepoGroupToPerm.query()\ | |
219 | .options(joinedload(UserGroupRepoGroupToPerm.permission))\ |
|
219 | .options(joinedload(UserGroupRepoGroupToPerm.permission))\ | |
220 | .options(joinedload(UserGroupRepoGroupToPerm.group))\ |
|
220 | .options(joinedload(UserGroupRepoGroupToPerm.group))\ | |
221 | .filter(UserGroupRepoGroupToPerm.users_group_id == user_group_id)\ |
|
221 | .filter(UserGroupRepoGroupToPerm.users_group_id == user_group_id)\ | |
222 | .all() |
|
222 | .all() | |
223 |
|
223 | |||
224 | for gr in ugroup_group_perms: |
|
224 | for gr in ugroup_group_perms: | |
225 | permissions['repositories_groups'][gr.group.group_name] \ |
|
225 | permissions['repositories_groups'][gr.group.group_name] \ | |
226 | = gr.permission.permission_name |
|
226 | = gr.permission.permission_name | |
227 | return permissions |
|
227 | return permissions | |
228 |
|
228 | |||
229 | @LoginRequired() |
|
229 | @LoginRequired() | |
230 | @HasUserGroupPermissionAnyDecorator('usergroup.admin') |
|
230 | @HasUserGroupPermissionAnyDecorator('usergroup.admin') | |
231 | @view_config( |
|
231 | @view_config( | |
232 | route_name='edit_user_group_perms_summary', request_method='GET', |
|
232 | route_name='edit_user_group_perms_summary', request_method='GET', | |
233 | renderer='rhodecode:templates/admin/user_groups/user_group_edit.mako') |
|
233 | renderer='rhodecode:templates/admin/user_groups/user_group_edit.mako') | |
234 | def user_group_perms_summary(self): |
|
234 | def user_group_perms_summary(self): | |
235 | c = self.load_default_context() |
|
235 | c = self.load_default_context() | |
236 |
|
236 | |||
237 | user_group_id = self.request.matchdict.get('user_group_id') |
|
237 | user_group_id = self.request.matchdict.get('user_group_id') | |
238 | c.user_group = UserGroup.get_or_404(user_group_id) |
|
238 | c.user_group = UserGroup.get_or_404(user_group_id) | |
239 |
|
239 | |||
240 | c.active = 'perms_summary' |
|
240 | c.active = 'perms_summary' | |
241 |
|
241 | |||
242 | c.permissions = self._get_perms_summary(c.user_group.users_group_id) |
|
242 | c.permissions = self._get_perms_summary(c.user_group.users_group_id) | |
243 | return self._get_template_context(c) |
|
243 | return self._get_template_context(c) | |
244 |
|
244 | |||
245 | @LoginRequired() |
|
245 | @LoginRequired() | |
246 | @HasUserGroupPermissionAnyDecorator('usergroup.admin') |
|
246 | @HasUserGroupPermissionAnyDecorator('usergroup.admin') | |
247 | @view_config( |
|
247 | @view_config( | |
248 | route_name='edit_user_group_perms_summary_json', request_method='GET', |
|
248 | route_name='edit_user_group_perms_summary_json', request_method='GET', | |
249 | renderer='json_ext') |
|
249 | renderer='json_ext') | |
250 | def user_group_perms_summary(self): |
|
250 | def user_group_perms_summary_json(self): | |
251 | self.load_default_context() |
|
251 | self.load_default_context() | |
252 |
|
252 | |||
253 | user_group_id = self.request.matchdict.get('user_group_id') |
|
253 | user_group_id = self.request.matchdict.get('user_group_id') | |
254 | user_group = UserGroup.get_or_404(user_group_id) |
|
254 | user_group = UserGroup.get_or_404(user_group_id) | |
255 |
|
255 | |||
256 | return self._get_perms_summary(user_group.users_group_id) |
|
256 | return self._get_perms_summary(user_group.users_group_id) |
General Comments 0
You need to be logged in to leave comments.
Login now