Add support for USERNAME_IS_EMAIL

* Updated all the user forms to show 'name' field
  by default, and remove it if setting is set.
* Rewrite the login form based on the setting.

Change-Id: Ifea7695554860dadeb43ceb4bddf1961300f3adc
This commit is contained in:
adrian-turjak 2017-04-26 14:17:09 +12:00
parent 93f658d988
commit 4feb401f9b
7 changed files with 73 additions and 13 deletions

View File

@ -221,15 +221,8 @@ def token_submit(request, token, data):
data=json.dumps(data), headers=headers)
def forgotpassword_submit(request, email, username=None):
# Username is optional only if the registration API considers it so
# In this case the backend assumes email==username
def forgotpassword_submit(request, data):
headers = {"Content-Type": "application/json"}
data = {
'email': email
}
if username:
data['username'] = username
try:
return post(request, 'openstack/users/password-reset',
data=json.dumps(data),

View File

@ -27,21 +27,26 @@ USERNAME_IS_EMAIL = True
class ForgotPasswordForm(hforms.SelfHandlingForm):
username = forms.CharField(max_length=255, label=_("User Name"))
email = forms.EmailField(
label=_("Email"),
widget=forms.TextInput(attrs={"autofocus": "autofocus"})
)
def __init__(self, *args, **kwargs):
super(ForgotPasswordForm, self).__init__(*args, **kwargs)
if (hasattr(settings, 'USERNAME_IS_EMAIL') and
getattr(settings, 'USERNAME_IS_EMAIL')):
self.fields.pop('username')
def clean(self, *args, **kwargs):
# validate username and email?
return super(ForgotPasswordForm, self).clean(*args, **kwargs)
def handle(self, *args, **kwargs):
email = self.cleaned_data['email']
def handle(self, request, data):
try:
submit_response = stacktask.forgotpassword_submit(self.request,
email)
submit_response = stacktask.forgotpassword_submit(
request, data)
if submit_response.ok:
return True
except Exception:

View File

@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from django.conf import settings
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
@ -36,6 +37,7 @@ def get_role_choices(request):
class InviteUserForm(forms.SelfHandlingForm):
username = forms.CharField(max_length=255, label=_("User Name"))
email = forms.EmailField()
roles = forms.MultipleChoiceField(label=_("Roles"),
required=True,
@ -46,6 +48,9 @@ class InviteUserForm(forms.SelfHandlingForm):
def __init__(self, *args, **kwargs):
super(InviteUserForm, self).__init__(*args, **kwargs)
if (hasattr(settings, 'USERNAME_IS_EMAIL') and
getattr(settings, 'USERNAME_IS_EMAIL')):
self.fields.pop('username')
self.fields['roles'].choices = get_role_choices(self.request)
self.fields['roles'].initial = ['_member_']

View File

@ -0,0 +1,8 @@
# Making use of this to make a generic enabled file
# to load base stacktask-ui content.
FEATURE = "stacktask-ui-base"
# A list of applications to be added to INSTALLED_APPS.
ADD_INSTALLED_APPS = [
'stacktask_ui'
]

View File

@ -0,0 +1,9 @@
{% overextends 'auth/_login_page.html' %}
{% load i18n %}
{% load relabel_username_field %}
{% block login_body %}
{% relabel_username_field %}
{{ block.super }}
{% endblock %}

View File

View File

@ -0,0 +1,40 @@
#
# Copyright 2016 Catalyst IT Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from django.conf import settings
from django import template
from django.utils.translation import ugettext_lazy
register = template.Library()
@register.simple_tag(takes_context=True)
def relabel_username_field(context):
"""Takes the username field and relabels it to 'email'.
Note(dalees):
This function modifies context inside a template renderer,
which is really bad practice MVC. In this case we prefer
not to modify the openstack_auth form module, so changing
form label directly is our best option. Avoid if you can!
"""
if (hasattr(settings, 'USERNAME_IS_EMAIL') and
getattr(settings, 'USERNAME_IS_EMAIL')):
try:
context['form'].fields['username'].label = ugettext_lazy('Email')
except Exception:
pass
return u""