示例#1
0
def signup(request, token):
    with scopes_disabled():
        try:
            token = UUID(token, version=4)
        except ValueError:
            messages.add_message(request, messages.ERROR,
                                 _('Malformed Invite Link supplied!'))
            return HttpResponseRedirect(reverse('index'))

        if link := InviteLink.objects.filter(valid_until__gte=datetime.today(),
                                             used_by=None,
                                             uuid=token).first():
            if request.method == 'POST':
                updated_request = request.POST.copy()
                if link.username != '':
                    updated_request.update({'name': link.username})

                form = UserCreateForm(updated_request)

                if form.is_valid():
                    if form.cleaned_data['password'] != form.cleaned_data[
                            'password_confirm']:  # noqa: E501
                        form.add_error('password', _('Passwords dont match!'))
                    else:
                        user = User(username=form.cleaned_data['name'], )
                        try:
                            validate_password(form.cleaned_data['password'],
                                              user=user)
                            user.set_password(form.cleaned_data['password'])
                            user.save()
                            messages.add_message(
                                request, messages.SUCCESS,
                                _('User has been created, please login!'))

                            link.used_by = user
                            link.save()
                            user.groups.add(link.group)

                            user.userpreference.space = link.space
                            user.userpreference.save()
                            return HttpResponseRedirect(
                                reverse('account_login'))
                        except ValidationError as e:
                            for m in e:
                                form.add_error('password', m)
            else:
                form = UserCreateForm()

            if link.username != '':
                form.fields['name'].initial = link.username
                form.fields['name'].disabled = True
            return render(request, 'account/signup.html', {
                'form': form,
                'link': link
            })

        messages.add_message(request, messages.ERROR,
                             _('Invite Link not valid or already used!'))
        return HttpResponseRedirect(reverse('index'))
示例#2
0
def setup(request):
    with scopes_disabled():
        if User.objects.count(
        ) > 0 or 'django.contrib.auth.backends.RemoteUserBackend' in settings.AUTHENTICATION_BACKENDS:
            messages.add_message(
                request, messages.ERROR,
                _('The setup page can only be used to create the first user! If you have forgotten your superuser credentials please consult the django documentation on how to reset passwords.'
                  ))
            return HttpResponseRedirect(reverse('account_login'))

        if request.method == 'POST':
            form = UserCreateForm(request.POST)
            if form.is_valid():
                if form.cleaned_data['password'] != form.cleaned_data[
                        'password_confirm']:
                    form.add_error('password', _('Passwords dont match!'))
                else:
                    user = User(username=form.cleaned_data['name'],
                                is_superuser=True,
                                is_staff=True)
                    try:
                        validate_password(form.cleaned_data['password'],
                                          user=user)
                        user.set_password(form.cleaned_data['password'])
                        user.save()

                        user.groups.add(Group.objects.get(name='admin'))

                        user.userpreference.space = Space.objects.first()
                        user.userpreference.save()

                        for x in Space.objects.all():
                            x.created_by = user
                            x.save()
                        messages.add_message(
                            request, messages.SUCCESS,
                            _('User has been created, please login!'))
                        return HttpResponseRedirect(reverse('account_login'))
                    except ValidationError as e:
                        for m in e:
                            form.add_error('password', m)
        else:
            form = UserCreateForm()

        return render(request, 'setup.html', {'form': form})