Show More
@@ -1,217 +1,199 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 | |
|
48 | 48 | # TODO: Think about replacing the htmlfill stuff. |
|
49 | 49 | def _render_and_fill(self, template, template_context, request, |
|
50 | 50 | form_defaults, validation_errors): |
|
51 | 51 | """ |
|
52 | 52 | Helper to render a template and fill the HTML form fields with |
|
53 | 53 | defaults. Also displays the form errors. |
|
54 | 54 | """ |
|
55 | 55 | # Render template to string. |
|
56 | 56 | html = render(template, template_context, request=request) |
|
57 | 57 | |
|
58 | 58 | # Fill the HTML form fields with default values and add error messages. |
|
59 | 59 | html = formencode.htmlfill.render( |
|
60 | 60 | html, |
|
61 | 61 | defaults=form_defaults, |
|
62 | 62 | errors=validation_errors, |
|
63 | 63 | prefix_error=False, |
|
64 | 64 | encoding="UTF-8", |
|
65 | 65 | force_defaults=False) |
|
66 | 66 | |
|
67 | 67 | return html |
|
68 | 68 | |
|
69 | def settings_get(self): | |
|
69 | def settings_get(self, errors={}): | |
|
70 | 70 | """ |
|
71 | 71 | View that displays the plugin settings as a form. |
|
72 | 72 | """ |
|
73 | form_defaults = {} | |
|
74 | validation_errors = None | |
|
75 | 73 | schema = self.plugin.get_settings_schema() |
|
76 | 74 | |
|
77 | 75 | # Get default values for the form. |
|
78 | 76 | for node in schema.children: |
|
79 |
value = self.plugin.get_setting_by_name(node.name) |
|
|
80 | form_defaults[node.name] = value | |
|
77 | value = self.plugin.get_setting_by_name(node.name) | |
|
78 | if value: | |
|
79 | node.default = value | |
|
81 | 80 | |
|
82 | 81 | template_context = { |
|
82 | 'errors': errors, | |
|
83 | 'plugin': self.context.plugin, | |
|
83 | 84 | 'resource': self.context, |
|
84 | 'plugin': self.context.plugin | |
|
85 | 85 | } |
|
86 | 86 | |
|
87 | return Response(self._render_and_fill( | |
|
88 | 'rhodecode:templates/admin/auth/plugin_settings.html', | |
|
89 | template_context, | |
|
90 | self.request, | |
|
91 | form_defaults, | |
|
92 | validation_errors)) | |
|
87 | return template_context | |
|
93 | 88 | |
|
94 | 89 | def settings_post(self): |
|
95 | 90 | """ |
|
96 | 91 | View that validates and stores the plugin settings. |
|
97 | 92 | """ |
|
98 | 93 | schema = self.plugin.get_settings_schema() |
|
99 | 94 | try: |
|
100 | 95 | valid_data = schema.deserialize(self.request.params) |
|
101 | 96 | except colander.Invalid, e: |
|
102 | 97 | # Display error message and display form again. |
|
103 | form_defaults = self.request.params | |
|
104 | validation_errors = e.asdict() | |
|
105 | 98 | self.request.session.flash( |
|
106 | 99 | _('Errors exist when saving plugin settings. ' |
|
107 | 100 |
|
|
108 | 101 | queue='error') |
|
109 | ||
|
110 | template_context = { | |
|
111 | 'resource': self.context, | |
|
112 | 'plugin': self.context.plugin | |
|
113 | } | |
|
114 | ||
|
115 | return Response(self._render_and_fill( | |
|
116 | 'rhodecode:templates/admin/auth/plugin_settings.html', | |
|
117 | template_context, | |
|
118 | self.request, | |
|
119 | form_defaults, | |
|
120 | validation_errors)) | |
|
102 | return self.settings_get(errors=e.asdict()) | |
|
121 | 103 | |
|
122 | 104 | # Store validated data. |
|
123 | 105 | for name, value in valid_data.items(): |
|
124 | 106 | self.plugin.create_or_update_setting(name, value) |
|
125 | 107 | Session.commit() |
|
126 | 108 | |
|
127 | 109 | # Display success message and redirect. |
|
128 | 110 | self.request.session.flash( |
|
129 | 111 | _('Auth settings updated successfully.'), |
|
130 | 112 | queue='success') |
|
131 | 113 | redirect_to = self.request.resource_path( |
|
132 | 114 | self.context, route_name='auth_home') |
|
133 | 115 | return HTTPFound(redirect_to) |
|
134 | 116 | |
|
135 | 117 | |
|
136 | 118 | # TODO: Ongoing migration in these views. |
|
137 | 119 | # - Maybe we should also use a colander schema for these views. |
|
138 | 120 | class AuthSettingsView(object): |
|
139 | 121 | def __init__(self, context, request): |
|
140 | 122 | self.context = context |
|
141 | 123 | self.request = request |
|
142 | 124 | |
|
143 | 125 | # TODO: Move this into a utility function. It is needed in all view |
|
144 | 126 | # classes during migration. Maybe a mixin? |
|
145 | 127 | |
|
146 | 128 | # Some of the decorators rely on this attribute to be present on the |
|
147 | 129 | # class of the decorated method. |
|
148 | 130 | self._rhodecode_user = request.user |
|
149 | 131 | |
|
150 | 132 | @LoginRequired() |
|
151 | 133 | @HasPermissionAllDecorator('hg.admin') |
|
152 | 134 | def index(self, defaults={}, errors=None, prefix_error=False): |
|
153 | 135 | authn_registry = self.request.registry.getUtility(IAuthnPluginRegistry) |
|
154 | 136 | enabled_plugins = SettingsModel().get_auth_plugins() |
|
155 | 137 | |
|
156 | 138 | # Create template context and render it. |
|
157 | 139 | template_context = { |
|
158 | 140 | 'resource': self.context, |
|
159 | 141 | 'available_plugins': authn_registry.get_plugins(), |
|
160 | 142 | 'enabled_plugins': enabled_plugins, |
|
161 | 143 | } |
|
162 | 144 | html = render('rhodecode:templates/admin/auth/auth_settings.html', |
|
163 | 145 | template_context, |
|
164 | 146 | request=self.request) |
|
165 | 147 | |
|
166 | 148 | # Create form default values and fill the form. |
|
167 | 149 | form_defaults = { |
|
168 | 150 | 'auth_plugins': ','.join(enabled_plugins) |
|
169 | 151 | } |
|
170 | 152 | form_defaults.update(defaults) |
|
171 | 153 | html = formencode.htmlfill.render( |
|
172 | 154 | html, |
|
173 | 155 | defaults=form_defaults, |
|
174 | 156 | errors=errors, |
|
175 | 157 | prefix_error=prefix_error, |
|
176 | 158 | encoding="UTF-8", |
|
177 | 159 | force_defaults=False) |
|
178 | 160 | |
|
179 | 161 | return Response(html) |
|
180 | 162 | |
|
181 | 163 | @LoginRequired() |
|
182 | 164 | @HasPermissionAllDecorator('hg.admin') |
|
183 | 165 | @auth.CSRFRequired() |
|
184 | 166 | def auth_settings(self): |
|
185 | 167 | try: |
|
186 | 168 | form = AuthSettingsForm()() |
|
187 | 169 | form_result = form.to_python(self.request.params) |
|
188 | 170 | plugins = ','.join(form_result['auth_plugins']) |
|
189 | 171 | setting = SettingsModel().create_or_update_setting( |
|
190 | 172 | 'auth_plugins', plugins) |
|
191 | 173 | Session().add(setting) |
|
192 | 174 | Session().commit() |
|
193 | 175 | |
|
194 | 176 | cache_manager = get_auth_cache_manager() |
|
195 | 177 | cache_manager.clear() |
|
196 | 178 | self.request.session.flash( |
|
197 | 179 | _('Auth settings updated successfully.'), |
|
198 | 180 | queue='success') |
|
199 | 181 | except formencode.Invalid as errors: |
|
200 | 182 | e = errors.error_dict or {} |
|
201 | 183 | self.request.session.flash( |
|
202 | 184 | _('Errors exist when saving plugin setting. ' |
|
203 | 185 | 'Please check the form inputs.'), |
|
204 | 186 | queue='error') |
|
205 | 187 | return self.index( |
|
206 | 188 | defaults=errors.value, |
|
207 | 189 | errors=e, |
|
208 | 190 | prefix_error=False) |
|
209 | 191 | except Exception: |
|
210 | 192 | log.exception('Exception in auth_settings') |
|
211 | 193 | self.request.session.flash( |
|
212 | 194 | _('Error occurred during update of auth settings.'), |
|
213 | 195 | queue='error') |
|
214 | 196 | |
|
215 | 197 | redirect_to = self.request.resource_path( |
|
216 | 198 | self.context, route_name='auth_home') |
|
217 | 199 | return HTTPFound(redirect_to) |
General Comments 0
You need to be logged in to leave comments.
Login now