forms: add ou selector to login form (#30252)

This commit is contained in:
Benjamin Dauvergne 2019-02-14 11:24:05 +01:00
parent f44735179f
commit fdc2959104
3 changed files with 29 additions and 0 deletions

View File

@ -137,6 +137,8 @@ default_settings = dict(
' form'),
A2_USERNAME_HELP_TEXT=Setting(default=None, definition='Help text to explain validation rules of usernames'),
A2_USERNAME_IS_UNIQUE=Setting(default=True, definition='Check username uniqueness'),
A2_LOGIN_FORM_OU_SELECTOR=Setting(default=False, definition='Whether to add an OU selector to the login form'),
A2_LOGIN_FORM_OU_SELECTOR_LABEL=Setting(default=None, definition='Label of OU field on login page'),
A2_REGISTRATION_USERNAME_IS_UNIQUE=Setting(default=True, definition='Check username uniqueness on registration'),
IDP_BACKENDS=(),
AUTH_FRONTENDS=(),

View File

@ -22,12 +22,17 @@ from django.utils.translation import ugettext_lazy as _
from django.contrib.auth import REDIRECT_FIELD_NAME, forms as auth_forms
from django.utils import html
from django_rbac.utils import get_ou_model
from authentic2.utils import lazy_label
from authentic2.compat import get_user_model
from authentic2.forms.fields import PasswordField
from .. import app_settings
from ..exponential_retry_timeout import ExponentialRetryTimeout
OU = get_ou_model()
class EmailChangeFormNoPassword(forms.Form):
email = forms.EmailField(label=_('New email'))
@ -181,6 +186,10 @@ class AuthenticationForm(auth_forms.AuthenticationForm):
required=False,
label=_('Remember me'),
help_text=_('Do not ask for authentication next time'))
ou = forms.ModelChoiceField(
label=lazy_label(_('Organizational unit'), lambda: app_settings.A2_LOGIN_FORM_OU_SELECTOR_LABEL),
required=True,
queryset=OU.objects.all())
def __init__(self, *args, **kwargs):
super(AuthenticationForm, self).__init__(*args, **kwargs)
@ -192,6 +201,9 @@ class AuthenticationForm(auth_forms.AuthenticationForm):
if not app_settings.A2_USER_REMEMBER_ME:
del self.fields['remember_me']
if not app_settings.A2_LOGIN_FORM_OU_SELECTOR:
del self.fields['ou']
if self.request:
self.remote_addr = self.request.META['REMOTE_ADDR']
else:

View File

@ -133,3 +133,18 @@ def test_session_remember_me_nok(app, settings, simple_user, freezer):
freezer.move_to('2018-01-31')
response = app.get('/')
assert simple_user.first_name not in response
def test_ou_selector(app, settings, simple_user):
settings.A2_LOGIN_FORM_OU_SELECTOR = True
response = app.get('/login/')
# Check selector is here and there are no errors
assert not response.pyquery('.errorlist')
assert response.pyquery.find('select#id_ou')
assert (set([elt.text for elt in response.pyquery.find('select#id_ou option')])
== set([u'Default organizational unit', u'OU1', u'---------']))
# Check selector is required
response.form.set('username', simple_user.username)
response.form.set('password', simple_user.username)
response = response.form.submit(name='login-password-submit')
assert response.pyquery('.errorlist')