Debian Codegraph @

adjutant_ui/content/token/forms.py

raw
1 # Copyright (c) 2016 Catalyst IT Ltd.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from django.conf import settings
16 from django.forms import ValidationError # noqa
17 from django import http
18 from django.utils.translation import gettext_lazy as _
19 from django.views.decorators.debug import sensitive_variables # noqa
20
21 from horizon import exceptions
22 from horizon import forms
23 from horizon import messages
24 from horizon.utils import functions as utils
25 from horizon.utils import validators
26
27 from openstack_dashboard import api
28
29
30 class PasswordForm(forms.SelfHandlingForm):
31 new_password = forms.RegexField(
32 label=_("New password"),
33 widget=forms.PasswordInput(render_value=False),
34 regex=validators.password_validator(),
35 error_messages={
36 'invalid': validators.password_validator_msg()
37 }
38 )
39 confirm_password = forms.CharField(
40 label=_("Confirm new password"),
41 widget=forms.PasswordInput(render_value=False)
42 )
43 no_autocomplete = True
44
45 def clean(self):
46 '''Check to make sure password fields match.'''
47 data = super(forms.Form, self).clean()
48 if 'new_password' in data:
49 if data['new_password'] != data.get('confirm_password', None):
50 raise ValidationError(_('Passwords do not match.'))
51 return data
52
53 # We have to protect the entire "data" dict because it contains
54 # newpassword strings.
55 @sensitive_variables('data')
56 def handle(self, request, data):
57 usable_data = self.cleaned_data
58 user_is_editable = api.keystone.keystone_can_edit_user()
59
60 if user_is_editable and usable_data:
61 try:
62 api.keystone.user_update_own_password(
63 request,
64 usable_data['current_password'],
65 usable_data['new_password']
66 )
67 response = http.HttpResponseRedirect(settings.LOGOUT_URL)
68 msg = _("Password changed. Please log in again to continue.")
69 utils.add_logout_reason(request, response, msg)
70 return response
71 except Exception:
72 exceptions.handle(request,
73 _('Unable to change password.'))
74 return False
75 else:
76 messages.error(request, _('Changing password is not supported.'))
77 return False
78
79
80 class ConfirmForm(forms.SelfHandlingForm):
81
82 def handle(self, request, data):
83 pass