##// END OF EJS Templates
permissions: move refresh operation as a flag to the get_default_user...
marcink -
r1764:f0bc9388 default
parent child Browse files
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -1,249 +1,244 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-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
22 22 """
23 23 permissions controller for RhodeCode Enterprise
24 24 """
25 25
26 26
27 27 import logging
28 28
29 29 import formencode
30 30 from formencode import htmlfill
31 31 from pylons import request, tmpl_context as c, url
32 32 from pylons.controllers.util import redirect
33 33 from pylons.i18n.translation import _
34 34
35 35 from rhodecode.lib import helpers as h
36 36 from rhodecode.lib import auth
37 37 from rhodecode.lib.auth import (LoginRequired, HasPermissionAllDecorator)
38 38 from rhodecode.lib.base import BaseController, render
39 39 from rhodecode.model.db import User, UserIpMap
40 40 from rhodecode.model.forms import (
41 41 ApplicationPermissionsForm, ObjectPermissionsForm, UserPermissionsForm)
42 42 from rhodecode.model.meta import Session
43 43 from rhodecode.model.permission import PermissionModel
44 44 from rhodecode.model.settings import SettingsModel
45 45
46 46 log = logging.getLogger(__name__)
47 47
48 48
49 49 class PermissionsController(BaseController):
50 50 """REST Controller styled on the Atom Publishing Protocol"""
51 51 # To properly map this controller, ensure your config/routing.py
52 52 # file has a resource setup:
53 53 # map.resource('permission', 'permissions')
54 54
55 55 @LoginRequired()
56 56 def __before__(self):
57 57 super(PermissionsController, self).__before__()
58 58
59 59 def __load_data(self):
60 60 PermissionModel().set_global_permission_choices(c, gettext_translator=_)
61 61
62 62 @HasPermissionAllDecorator('hg.admin')
63 63 def permission_application(self):
64 64 c.active = 'application'
65 65 self.__load_data()
66 66
67 c.user = User.get_default_user()
68
69 # TODO: johbo: The default user might be based on outdated state which
70 # has been loaded from the cache. A call to refresh() ensures that the
71 # latest state from the database is used.
72 Session().refresh(c.user)
67 c.user = User.get_default_user(refresh=True)
73 68
74 69 app_settings = SettingsModel().get_all_settings()
75 70 defaults = {
76 71 'anonymous': c.user.active,
77 72 'default_register_message': app_settings.get(
78 73 'rhodecode_register_message')
79 74 }
80 75 defaults.update(c.user.get_default_perms())
81 76
82 77 return htmlfill.render(
83 78 render('admin/permissions/permissions.mako'),
84 79 defaults=defaults,
85 80 encoding="UTF-8",
86 81 force_defaults=False)
87 82
88 83 @HasPermissionAllDecorator('hg.admin')
89 84 @auth.CSRFRequired()
90 85 def permission_application_update(self):
91 86 c.active = 'application'
92 87 self.__load_data()
93 88 _form = ApplicationPermissionsForm(
94 89 [x[0] for x in c.register_choices],
95 90 [x[0] for x in c.password_reset_choices],
96 91 [x[0] for x in c.extern_activate_choices])()
97 92
98 93 try:
99 94 form_result = _form.to_python(dict(request.POST))
100 95 form_result.update({'perm_user_name': User.DEFAULT_USER})
101 96 PermissionModel().update_application_permissions(form_result)
102 97
103 98 settings = [
104 99 ('register_message', 'default_register_message'),
105 100 ]
106 101 for setting, form_key in settings:
107 102 sett = SettingsModel().create_or_update_setting(
108 103 setting, form_result[form_key])
109 104 Session().add(sett)
110 105
111 106 Session().commit()
112 107 h.flash(_('Application permissions updated successfully'),
113 108 category='success')
114 109
115 110 except formencode.Invalid as errors:
116 111 defaults = errors.value
117 112
118 113 return htmlfill.render(
119 114 render('admin/permissions/permissions.mako'),
120 115 defaults=defaults,
121 116 errors=errors.error_dict or {},
122 117 prefix_error=False,
123 118 encoding="UTF-8",
124 119 force_defaults=False)
125 120 except Exception:
126 121 log.exception("Exception during update of permissions")
127 122 h.flash(_('Error occurred during update of permissions'),
128 123 category='error')
129 124
130 125 return redirect(url('admin_permissions_application'))
131 126
132 127 @HasPermissionAllDecorator('hg.admin')
133 128 def permission_objects(self):
134 129 c.active = 'objects'
135 130 self.__load_data()
136 131 c.user = User.get_default_user()
137 132 defaults = {}
138 133 defaults.update(c.user.get_default_perms())
139 134 return htmlfill.render(
140 135 render('admin/permissions/permissions.mako'),
141 136 defaults=defaults,
142 137 encoding="UTF-8",
143 138 force_defaults=False)
144 139
145 140 @HasPermissionAllDecorator('hg.admin')
146 141 @auth.CSRFRequired()
147 142 def permission_objects_update(self):
148 143 c.active = 'objects'
149 144 self.__load_data()
150 145 _form = ObjectPermissionsForm(
151 146 [x[0] for x in c.repo_perms_choices],
152 147 [x[0] for x in c.group_perms_choices],
153 148 [x[0] for x in c.user_group_perms_choices])()
154 149
155 150 try:
156 151 form_result = _form.to_python(dict(request.POST))
157 152 form_result.update({'perm_user_name': User.DEFAULT_USER})
158 153 PermissionModel().update_object_permissions(form_result)
159 154
160 155 Session().commit()
161 156 h.flash(_('Object permissions updated successfully'),
162 157 category='success')
163 158
164 159 except formencode.Invalid as errors:
165 160 defaults = errors.value
166 161
167 162 return htmlfill.render(
168 163 render('admin/permissions/permissions.mako'),
169 164 defaults=defaults,
170 165 errors=errors.error_dict or {},
171 166 prefix_error=False,
172 167 encoding="UTF-8",
173 168 force_defaults=False)
174 169 except Exception:
175 170 log.exception("Exception during update of permissions")
176 171 h.flash(_('Error occurred during update of permissions'),
177 172 category='error')
178 173
179 174 return redirect(url('admin_permissions_object'))
180 175
181 176 @HasPermissionAllDecorator('hg.admin')
182 177 def permission_global(self):
183 178 c.active = 'global'
184 179 self.__load_data()
185 180
186 181 c.user = User.get_default_user()
187 182 defaults = {}
188 183 defaults.update(c.user.get_default_perms())
189 184
190 185 return htmlfill.render(
191 186 render('admin/permissions/permissions.mako'),
192 187 defaults=defaults,
193 188 encoding="UTF-8",
194 189 force_defaults=False)
195 190
196 191 @HasPermissionAllDecorator('hg.admin')
197 192 @auth.CSRFRequired()
198 193 def permission_global_update(self):
199 194 c.active = 'global'
200 195 self.__load_data()
201 196 _form = UserPermissionsForm(
202 197 [x[0] for x in c.repo_create_choices],
203 198 [x[0] for x in c.repo_create_on_write_choices],
204 199 [x[0] for x in c.repo_group_create_choices],
205 200 [x[0] for x in c.user_group_create_choices],
206 201 [x[0] for x in c.fork_choices],
207 202 [x[0] for x in c.inherit_default_permission_choices])()
208 203
209 204 try:
210 205 form_result = _form.to_python(dict(request.POST))
211 206 form_result.update({'perm_user_name': User.DEFAULT_USER})
212 207 PermissionModel().update_user_permissions(form_result)
213 208
214 209 Session().commit()
215 210 h.flash(_('Global permissions updated successfully'),
216 211 category='success')
217 212
218 213 except formencode.Invalid as errors:
219 214 defaults = errors.value
220 215
221 216 return htmlfill.render(
222 217 render('admin/permissions/permissions.mako'),
223 218 defaults=defaults,
224 219 errors=errors.error_dict or {},
225 220 prefix_error=False,
226 221 encoding="UTF-8",
227 222 force_defaults=False)
228 223 except Exception:
229 224 log.exception("Exception during update of permissions")
230 225 h.flash(_('Error occurred during update of permissions'),
231 226 category='error')
232 227
233 228 return redirect(url('admin_permissions_global'))
234 229
235 230 @HasPermissionAllDecorator('hg.admin')
236 231 def permission_ips(self):
237 232 c.active = 'ips'
238 233 c.user = User.get_default_user()
239 234 c.user_ip_map = (
240 235 UserIpMap.query().filter(UserIpMap.user == c.user).all())
241 236
242 237 return render('admin/permissions/permissions.mako')
243 238
244 239 @HasPermissionAllDecorator('hg.admin')
245 240 def permission_perms(self):
246 241 c.active = 'perms'
247 242 c.user = User.get_default_user()
248 243 c.perm_user = c.user.AuthUser
249 244 return render('admin/permissions/permissions.mako')
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now