##// END OF EJS Templates
authn: Fix priority of default values if some values are missing during POST...
johbo -
r237:840f51fa default
parent child Browse files
Show More
@@ -1,188 +1,191 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2012-2016 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 colander
22 22 import formencode.htmlfill
23 23 import logging
24 24
25 25 from pyramid.httpexceptions import HTTPFound
26 26 from pyramid.renderers import render
27 27 from pyramid.response import Response
28 28
29 29 from rhodecode.authentication.base import get_auth_cache_manager
30 30 from rhodecode.authentication.interface import IAuthnPluginRegistry
31 31 from rhodecode.lib import auth
32 32 from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator
33 33 from rhodecode.model.forms import AuthSettingsForm
34 34 from rhodecode.model.meta import Session
35 35 from rhodecode.model.settings import SettingsModel
36 36 from rhodecode.translation import _
37 37
38 38 log = logging.getLogger(__name__)
39 39
40 40
41 41 class AuthnPluginViewBase(object):
42 42
43 43 def __init__(self, context, request):
44 44 self.request = request
45 45 self.context = context
46 46 self.plugin = context.plugin
47 47 self._rhodecode_user = request.user
48 48
49 49 @LoginRequired()
50 50 @HasPermissionAllDecorator('hg.admin')
51 51 def settings_get(self, defaults=None, errors=None):
52 52 """
53 53 View that displays the plugin settings as a form.
54 54 """
55 55 defaults = defaults or {}
56 56 errors = errors or {}
57 57 schema = self.plugin.get_settings_schema()
58 58
59 # Get default values for the form.
59 # Compute default values for the form. Priority is:
60 # 1. Passed to this method 2. DB value 3. Schema default
60 61 for node in schema:
61 db_value = self.plugin.get_setting_by_name(node.name)
62 db_value = self.plugin.get_setting_by_name(
63 node.name, colander.null)
62 64 defaults.setdefault(node.name, db_value)
65 defaults = schema.serialize(defaults)
63 66
64 67 template_context = {
65 68 'defaults': defaults,
66 69 'errors': errors,
67 70 'plugin': self.context.plugin,
68 71 'resource': self.context,
69 72 }
70 73
71 74 return template_context
72 75
73 76 @LoginRequired()
74 77 @HasPermissionAllDecorator('hg.admin')
75 78 @auth.CSRFRequired()
76 79 def settings_post(self):
77 80 """
78 81 View that validates and stores the plugin settings.
79 82 """
80 83 schema = self.plugin.get_settings_schema()
81 84 try:
82 85 valid_data = schema.deserialize(self.request.params)
83 86 except colander.Invalid, e:
84 87 # Display error message and display form again.
85 88 self.request.session.flash(
86 89 _('Errors exist when saving plugin settings. '
87 90 'Please check the form inputs.'),
88 91 queue='error')
89 92 defaults = schema.flatten(self.request.params)
90 93 return self.settings_get(errors=e.asdict(), defaults=defaults)
91 94
92 95 # Store validated data.
93 96 for name, value in valid_data.items():
94 97 self.plugin.create_or_update_setting(name, value)
95 98 Session.commit()
96 99
97 100 # Display success message and redirect.
98 101 self.request.session.flash(
99 102 _('Auth settings updated successfully.'),
100 103 queue='success')
101 104 redirect_to = self.request.resource_path(
102 105 self.context, route_name='auth_home')
103 106 return HTTPFound(redirect_to)
104 107
105 108
106 109 # TODO: Ongoing migration in these views.
107 110 # - Maybe we should also use a colander schema for these views.
108 111 class AuthSettingsView(object):
109 112 def __init__(self, context, request):
110 113 self.context = context
111 114 self.request = request
112 115
113 116 # TODO: Move this into a utility function. It is needed in all view
114 117 # classes during migration. Maybe a mixin?
115 118
116 119 # Some of the decorators rely on this attribute to be present on the
117 120 # class of the decorated method.
118 121 self._rhodecode_user = request.user
119 122
120 123 @LoginRequired()
121 124 @HasPermissionAllDecorator('hg.admin')
122 125 def index(self, defaults=None, errors=None, prefix_error=False):
123 126 defaults = defaults or {}
124 127 authn_registry = self.request.registry.getUtility(IAuthnPluginRegistry)
125 128 enabled_plugins = SettingsModel().get_auth_plugins()
126 129
127 130 # Create template context and render it.
128 131 template_context = {
129 132 'resource': self.context,
130 133 'available_plugins': authn_registry.get_plugins(),
131 134 'enabled_plugins': enabled_plugins,
132 135 }
133 136 html = render('rhodecode:templates/admin/auth/auth_settings.html',
134 137 template_context,
135 138 request=self.request)
136 139
137 140 # Create form default values and fill the form.
138 141 form_defaults = {
139 142 'auth_plugins': ','.join(enabled_plugins)
140 143 }
141 144 form_defaults.update(defaults)
142 145 html = formencode.htmlfill.render(
143 146 html,
144 147 defaults=form_defaults,
145 148 errors=errors,
146 149 prefix_error=prefix_error,
147 150 encoding="UTF-8",
148 151 force_defaults=False)
149 152
150 153 return Response(html)
151 154
152 155 @LoginRequired()
153 156 @HasPermissionAllDecorator('hg.admin')
154 157 @auth.CSRFRequired()
155 158 def auth_settings(self):
156 159 try:
157 160 form = AuthSettingsForm()()
158 161 form_result = form.to_python(self.request.params)
159 162 plugins = ','.join(form_result['auth_plugins'])
160 163 setting = SettingsModel().create_or_update_setting(
161 164 'auth_plugins', plugins)
162 165 Session().add(setting)
163 166 Session().commit()
164 167
165 168 cache_manager = get_auth_cache_manager()
166 169 cache_manager.clear()
167 170 self.request.session.flash(
168 171 _('Auth settings updated successfully.'),
169 172 queue='success')
170 173 except formencode.Invalid as errors:
171 174 e = errors.error_dict or {}
172 175 self.request.session.flash(
173 176 _('Errors exist when saving plugin setting. '
174 177 'Please check the form inputs.'),
175 178 queue='error')
176 179 return self.index(
177 180 defaults=errors.value,
178 181 errors=e,
179 182 prefix_error=False)
180 183 except Exception:
181 184 log.exception('Exception in auth_settings')
182 185 self.request.session.flash(
183 186 _('Error occurred during update of auth settings.'),
184 187 queue='error')
185 188
186 189 redirect_to = self.request.resource_path(
187 190 self.context, route_name='auth_home')
188 191 return HTTPFound(redirect_to)
General Comments 0
You need to be logged in to leave comments. Login now