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