示例#1
0
def create_user(strategy, details, backend, user=None, *args, **kwargs):
    """Aggressively attempt to register and sign in new user"""
    if user:
        return None

    request = strategy.request
    email = details.get('email')
    username = kwargs.get('clean_username')

    if not email or not username:
        return None

    try:
        validate_email(email)
        validate_new_registration(request, {
            'email': email,
            'username': username,
        })
    except ValidationError:
        return None

    activation_kwargs = {}
    if settings.account_activation == 'admin':
        activation_kwargs = {'requires_activation': UserModel.ACTIVATION_ADMIN}

    new_user = UserModel.objects.create_user(username,
                                             email,
                                             create_audit_trail=True,
                                             joined_from_ip=request.user_ip,
                                             set_default_avatar=True,
                                             **activation_kwargs)

    send_welcome_email(request, new_user)

    return {'user': new_user, 'is_new': True}
示例#2
0
def create_user_with_form(strategy, details, backend, user=None, *args, **kwargs):
    """Alternatively to create_user lets user confirm account creation before authenticating"""
    if user:
        return None

    request = strategy.request
    backend_name = get_social_auth_backend_name(backend.name)

    if request.method == 'POST':
        try:
            request_data = json.loads(request.body)
        except (TypeError, ValueError):
            request_data = request.POST.copy()
            
        form = SocialAuthRegisterForm(
            request_data,
            request=request,    
            agreements=Agreement.objects.get_agreements(),
        )
        
        if not form.is_valid():
            return JsonResponse(form.errors, status=400)

        email_verified = form.cleaned_data['email'] == details.get('email')

        activation_kwargs = {}
        if settings.account_activation == 'admin':
            activation_kwargs = {'requires_activation': UserModel.ACTIVATION_ADMIN}
        elif settings.account_activation == 'user' and not email_verified:
            activation_kwargs = {'requires_activation': UserModel.ACTIVATION_USER}

        try:
            new_user = UserModel.objects.create_user(
                form.cleaned_data['username'],
                form.cleaned_data['email'],
                create_audit_trail=True,
                joined_from_ip=request.user_ip,
                set_default_avatar=True,
                **activation_kwargs
            )
        except IntegrityError:
            return JsonResponse({'__all__': _("Please try resubmitting the form.")}, status=400)

        save_user_agreements(new_user, form)
        send_welcome_email(request, new_user)

        return {'user': new_user, 'is_new': True}

    request.frontend_context['SOCIAL_AUTH'] = {
        'backend_name': backend_name,
        'step': 'register',
        'email': details.get('email'),
        'username': kwargs.get('clean_username'),
        'url': reverse('social:complete', kwargs={'backend': backend.name}),
    }

    return render(request, 'misago/socialauth.html', {
        'backend_name': backend_name,
    })
示例#3
0
def create_endpoint(request):
    if settings.account_activation == 'closed':
        raise PermissionDenied(
            _("New users registrations are currently closed."))

    form = RegisterForm(
        request.data,
        request=request,
        agreements=Agreement.objects.get_agreements(),
    )

    try:
        if form.is_valid():
            captcha.test_request(request)
    except ValidationError as e:
        form.add_error('captcha', e)

    if not form.is_valid():
        return Response(form.errors, status=status.HTTP_400_BAD_REQUEST)

    activation_kwargs = {}
    if settings.account_activation == 'user':
        activation_kwargs = {'requires_activation': UserModel.ACTIVATION_USER}
    elif settings.account_activation == 'admin':
        activation_kwargs = {'requires_activation': UserModel.ACTIVATION_ADMIN}

    try:
        new_user = UserModel.objects.create_user(
            form.cleaned_data['username'],
            form.cleaned_data['email'],
            form.cleaned_data['password'],
            create_audit_trail=True,
            joined_from_ip=request.user_ip,
            set_default_avatar=True,
            **activation_kwargs)
    except IntegrityError:
        return Response(
            {'__all__': _("Please try resubmitting the form.")},
            status=status.HTTP_400_BAD_REQUEST,
        )

    save_user_agreements(new_user, form)
    send_welcome_email(request, new_user)

    if new_user.requires_activation == UserModel.ACTIVATION_NONE:
        authenticated_user = authenticate(
            username=new_user.email, password=form.cleaned_data['password'])
        login(request, authenticated_user)

    return Response(get_registration_result_json(new_user))
示例#4
0
def create_endpoint(request):
    if settings.account_activation == 'closed':
        raise PermissionDenied(_("New users registrations are currently closed."))

    ban = get_ip_ban(request.user_ip, registration_only=True)
    if ban:
        raise Banned(ban)

    serializer = RegisterUserSerializer(
        data=request.data,
        context={'request': request},
    )

    serializer.is_valid(raise_exception=True)

    activation_kwargs = {}
    if settings.account_activation == 'user':
        activation_kwargs = {'requires_activation': UserModel.ACTIVATION_USER}
    elif settings.account_activation == 'admin':
        activation_kwargs = {'requires_activation': UserModel.ACTIVATION_ADMIN}

    try:
        new_user = UserModel.objects.create_user(
            serializer.validated_data['username'],
            serializer.validated_data['email'],
            serializer.validated_data['password'],
            joined_from_ip=request.user_ip,
            set_default_avatar=True,
            **activation_kwargs
        )
    except IntegrityError:
        return Response(
            {
                'detail': _("Please try resubmitting the form."),
            },
            status=400,
        )

    send_welcome_email(request, new_user)

    if not new_user.requires_activation == 'none':
        authenticated_user = authenticate(
            username=new_user.email,
            password=serializer.validated_data['password'],
        )
        login(request, authenticated_user)

    return Response(get_registration_result_json(new_user))