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