Commit 39cda970 authored by David Seaward's avatar David Seaward
Browse files

* create LDAP users on registration


* adjust format of WooCommerce cart URL
Signed-off-by: David Seaward's avatarDavid Seaward <david.seaward@puri.sm>
parent 28fc608f
Pipeline #4647 failed with stage
from django import forms
from django.contrib.auth import get_user_model
from captcha.fields import CaptchaField
from django.conf import settings
from django.contrib.auth import authenticate, login
from django.contrib.auth import get_user_model
from registration import signals
from registration.backends.simple.views import RegistrationView
from ldapregister.forms import RegistrationForm
from captcha.fields import CaptchaField
from cart.models import ChosenReward
from ldapregister.forms import RegistrationForm
User = get_user_model()
......@@ -25,7 +25,7 @@ class CartRegistrationView(RegistrationView):
return super().post(*args, **kwargs)
def get_success_url(self, user):
return '{}/{}/{}'.format(
return '{}/{}/{}/'.format(
getattr(settings, 'WOO_URL'),
getattr(settings, 'WOO_CART_PATH'),
str(self.reward)
......@@ -40,7 +40,8 @@ class CartRegistrationView(RegistrationView):
new_user = authenticate(
username=getattr(new_user, User.USERNAME_FIELD),
password=form.cleaned_data['password1']
password=form.cleaned_data['password1'],
registration=True,
)
login(self.request, new_user)
signals.user_registered.send(sender=self.__class__,
......
......@@ -54,6 +54,39 @@ class AuthenticationBackend(BaseBackend):
return is_valid
def authenticate(self, request=None, username=None, password=None, **kwargs):
is_registration = kwargs.get("registration", False)
user_model = User
normalized_username = user_model.normalize_username(username)
# first, validate username (even if it exists, username must be valid)
if not settings.DEBUG_SKIP_VALIDATE_ON_AUTHENTICATION:
validator = UsernameValidator()
validator(normalized_username)
# second, attempt LDAP authentication
# (if registering and the user doesn't exist, create it)
user = super(AuthenticationBackend, self).authenticate(request, normalized_username, password, **kwargs)
is_creation = is_registration and user is not None
if is_creation:
try:
user = User.objects.get(username=normalized_username)
except User.DoesNotExist:
user = User(username=normalized_username, email=None)
# update/set user details
user.email = user.get_identity()
user.set_password(password)
user.save()
return super(AuthenticationBackend, self).authenticate(request, normalized_username, password, **kwargs)
else:
return user
def old_authenticate(self, request=None, username=None, password=None, **kwargs):
user_model = User
normalized_username = user_model.normalize_username(username)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment