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