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