def register(request): """ User registration page """ user = request.user if user.is_authenticated: return redirect('home') if request.method == 'POST': form = forms.RegistrationForm(request=request, data=request.POST) if form.is_valid(): # Create the new user try: user = form.save() except IntegrityError: form.full_clean() if form.is_valid(): raise user = User.objects.get(username=form.data['username']) else: uidb64 = force_str(urlsafe_base64_encode(force_bytes(user.pk))) token = default_token_generator.make_token(user) notify_account_registration(request, user, uidb64, token) return render(request, 'user/register_done.html', {'email': user.email}) else: form = forms.RegistrationForm(request=request) response = render(request, 'user/register.html', {'form': form}) form.set_response_cookies(response) return response
def sso_register(request): """SSO Registration view This view should be protected by an appropriate Shibboleth proxy. SSO_REMOTE_USER_HEADER should be set by the proxy. GET does two things: - if the user did not fill it renders the registration form. - if the user filled the form but didn't click the confirmation url it renders a message that tells the user to click this link. POST submits the registration form. """ user = request.user if user.is_authenticated: return redirect('project_home') remote_sso_id = request.META.get(settings.SSO_REMOTE_USER_HEADER) # This should not happen as the SSO_REMOTE_USER_HEADER header should be always set by Nginx if not remote_sso_id: return redirect('login') if request.method == 'POST': form = forms.SSORegistrationForm(request.POST, sso_id=remote_sso_id) if form.is_valid(): user = form.save() uidb64 = force_str(urlsafe_base64_encode(force_bytes(user.pk))) token = default_token_generator.make_token(user) notify_account_registration(request, user, uidb64, token, sso=True) return render(request, 'user/register_done.html', { 'email': user.email, 'sso': True }) else: try: remote_user = User.objects.get(sso_id=remote_sso_id) if remote_user.is_active: return redirect('sso_login') return render(request, 'user/register_done.html', { 'email': remote_user.email, 'sso': True }) except User.DoesNotExist: form = forms.SSORegistrationForm() return render(request, 'sso/register.html', {'form': form})