|
|
@@ -20,9 +20,8 @@ import logging
|
|
|
|
|
|
from django.conf import settings
|
|
|
from django.contrib.auth import authenticate, get_backends
|
|
|
-from django.contrib.auth.models import User
|
|
|
-from django.contrib.auth.forms import AuthenticationForm as AuthAuthenticationForm, UserCreationForm as AuthUserCreationForm
|
|
|
-from django.forms import CharField, TextInput, PasswordInput, ChoiceField, ValidationError
|
|
|
+from django.contrib.auth.forms import AuthenticationForm as DjangoAuthenticationForm, UserCreationForm as DjangoUserCreationForm
|
|
|
+from django.forms import CharField, TextInput, PasswordInput, ChoiceField, ValidationError, Form
|
|
|
from django.utils.safestring import mark_safe
|
|
|
from django.utils.encoding import smart_str
|
|
|
from django.utils.translation import ugettext_lazy as _t, ugettext as _
|
|
|
@@ -32,6 +31,12 @@ from useradmin.hue_password_policy import hue_get_password_validators
|
|
|
|
|
|
from desktop.auth.backend import is_admin
|
|
|
|
|
|
+if conf.ENABLE_ORGANIZATIONS.get():
|
|
|
+ from useradmin.models import OrganizationUser as User
|
|
|
+else:
|
|
|
+ from django.contrib.auth.models import User
|
|
|
+
|
|
|
+
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
@@ -57,7 +62,7 @@ def get_server_choices():
|
|
|
return auth_choices
|
|
|
|
|
|
|
|
|
-class AuthenticationForm(AuthAuthenticationForm):
|
|
|
+class AuthenticationForm(DjangoAuthenticationForm):
|
|
|
"""
|
|
|
Adds appropriate classes to authentication form
|
|
|
"""
|
|
|
@@ -103,6 +108,77 @@ class AuthenticationForm(AuthAuthenticationForm):
|
|
|
return self.authenticate()
|
|
|
|
|
|
|
|
|
+class OrganizationAuthenticationForm(Form):
|
|
|
+ """
|
|
|
+ Adds appropriate classes to authentication form
|
|
|
+ """
|
|
|
+ error_messages = {
|
|
|
+ 'invalid_login': _t("Invalid email or password"),
|
|
|
+ 'inactive': _t("Account deactivated. Please contact an administrator."),
|
|
|
+ }
|
|
|
+
|
|
|
+ # username = None
|
|
|
+ email = CharField(label=_t("Email"), widget=TextInput(attrs={'maxlength': 150, 'placeholder': _t("Email"), 'autocomplete': 'off', 'autofocus': 'autofocus'}))
|
|
|
+ password = CharField(label=_t("Password"), widget=PasswordInput(attrs={'placeholder': _t("Password"), 'autocomplete': 'off'}))
|
|
|
+
|
|
|
+ def __init__(self, request=None, *args, **kwargs):
|
|
|
+ """
|
|
|
+ The 'request' parameter is set for custom auth use by subclasses.
|
|
|
+ The form data comes in via the standard 'data' kwarg.
|
|
|
+ """
|
|
|
+ self.request = request
|
|
|
+ self.user_cache = None
|
|
|
+ super(OrganizationAuthenticationForm, self).__init__(*args, **kwargs)
|
|
|
+
|
|
|
+ def clean(self):
|
|
|
+ email = self.cleaned_data.get('email')
|
|
|
+ password = self.cleaned_data.get('password')
|
|
|
+
|
|
|
+ if email is not None and password:
|
|
|
+ self.user_cache = authenticate(self.request, email=email, password=password)
|
|
|
+ if self.user_cache is None:
|
|
|
+ raise self.get_invalid_login_error()
|
|
|
+ else:
|
|
|
+ self.confirm_login_allowed(self.user_cache)
|
|
|
+
|
|
|
+ return self.cleaned_data
|
|
|
+
|
|
|
+ # def authenticate(self):
|
|
|
+ # return super(OrganizationAuthenticationForm, self).clean()
|
|
|
+
|
|
|
+ def confirm_login_allowed(self, user):
|
|
|
+ """
|
|
|
+ Controls whether the given User may log in. This is a policy setting,
|
|
|
+ independent of end-user authentication. This default behavior is to
|
|
|
+ allow login by active users, and reject login by inactive users.
|
|
|
+
|
|
|
+ If the given user cannot log in, this method should raise a
|
|
|
+ ``forms.ValidationError``.
|
|
|
+
|
|
|
+ If the given user may log in, this method should return None.
|
|
|
+ """
|
|
|
+ if not user.is_active:
|
|
|
+ raise ValidationError(
|
|
|
+ self.error_messages['inactive'],
|
|
|
+ code='inactive',
|
|
|
+ )
|
|
|
+
|
|
|
+ def get_user(self):
|
|
|
+ return self.user_cache
|
|
|
+
|
|
|
+ def get_invalid_login_error(self):
|
|
|
+ return ValidationError(
|
|
|
+ self.error_messages['invalid_login'],
|
|
|
+ code='invalid_login',
|
|
|
+ params={'email': 'Email'},
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+ # def clean(self):
|
|
|
+ # # TODO: checks for inactivity
|
|
|
+ # return self.authenticate()
|
|
|
+
|
|
|
+
|
|
|
class ImpersonationAuthenticationForm(AuthenticationForm):
|
|
|
login_as = CharField(label=_t("Login as"), max_length=30, widget=TextInput(attrs={'placeholder': _t("Login as username"), 'autocomplete': 'off'}))
|
|
|
|
|
|
@@ -168,15 +244,13 @@ class LdapAuthenticationForm(AuthenticationForm):
|
|
|
return self.cleaned_data
|
|
|
|
|
|
|
|
|
-class UserCreationForm(AuthUserCreationForm):
|
|
|
+class UserCreationForm(DjangoUserCreationForm):
|
|
|
"""
|
|
|
Accepts one password field and populates the others.
|
|
|
password fields with the value of that password field
|
|
|
Adds appropriate classes to authentication form.
|
|
|
"""
|
|
|
- password = CharField(label=_t("Password"),
|
|
|
- widget=PasswordInput(attrs={'class': 'input-large'}),
|
|
|
- validators=hue_get_password_validators())
|
|
|
+ password = CharField(label=_t("Password"), widget=PasswordInput(attrs={'class': 'input-large'}), validators=hue_get_password_validators())
|
|
|
|
|
|
def __init__(self, data=None, *args, **kwargs):
|
|
|
if data and 'password' in data:
|
|
|
@@ -186,6 +260,21 @@ class UserCreationForm(AuthUserCreationForm):
|
|
|
super(UserCreationForm, self).__init__(data=data, *args, **kwargs)
|
|
|
|
|
|
|
|
|
+class OrganizationUserCreationForm(DjangoUserCreationForm):
|
|
|
+ password = CharField(label=_t("Password"), widget=PasswordInput(attrs={'class': 'input-large'}), validators=hue_get_password_validators())
|
|
|
+
|
|
|
+ def __init__(self, data=None, *args, **kwargs):
|
|
|
+ if data and 'password' in data:
|
|
|
+ data = data.copy()
|
|
|
+ data['password1'] = data['password']
|
|
|
+ data['password2'] = data['password']
|
|
|
+ super(OrganizationUserCreationForm, self).__init__(data=data, *args, **kwargs)
|
|
|
+
|
|
|
+ class Meta(DjangoUserCreationForm.Meta):
|
|
|
+ model = User
|
|
|
+ fields = ('email',)
|
|
|
+
|
|
|
+
|
|
|
class LdapUserCreationForm(UserCreationForm):
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
super(LdapUserCreationForm, self).__init__(*args, **kwargs)
|