Пример #1
0
def normal_login_func(request):
    username = request.DATA.get('username', None)
    password = request.DATA.get('password', None)

    user = get_and_validate_user(username=username, password=password)
    data = make_auth_response_data(user)
    return data
Пример #2
0
def is_user_already_registered(*, username:str, email:str) -> (bool, str):
    """
    Checks if a specified user is already registered.

    Registers a tuple containing a boolean value that indicates if the user exists
    and in case he does whats the duplicated attribute.
    """
    user_model = apps.get_model("users", "User")
    if user_model.objects.filter(username=username):
        return (True, _("Username is already in use."))
    if user_model.objects.filter(email=email):
        return (True, _("Email is already in use.")
    return (False, None)

def get_membership_token(token:str):
    """
    Given a token, returns a membership instance
    that matches with specified token.

    If not matches with any membership NotFound exception
    is raised.
    """
    membership_model = apps.get_model("projects", "Membership")
    qs = membership_model.objects.filter(token=token)
    if len(qs) == 0:
        raise exc.NotFound(_("Token does not match any valid invitation."))
    return qs[0]

@tx.atomic
def public_register(username:str, password:str, email:str, full_name:str):
    """
    Given parsed parameters, try to register a new user
    knowing that it follows a public register flow.

    This can raise 'exc.IntegrityError' exceptions in
    case of conflicts found.
    """
    is_registered, reason = is_user_already_registered(username=username, email=email)
    if is_registered:
        raise exc.WrongArguments(reason)

    user_model = apps.get_model("users", "User")
    user = user_model(username=username,
                      email=email,
                      full_name=full_name)
    user.set_password(password)
    try:
        user.save()
    except IntegrityError:
        raise exc.WrongArguments(_("User is already registered."))

    send_register_email(user)
    user_registered_signal.send(sender=user.__class__, user=user)
    return user

@tx.atomic
def private_register_for_existing_user(token:str, username:str, password:str):
    """
    Register works not only for registered users, also serves for accept
    invitations for projects as existing user.

    Given an invitation token with parsed parameters, accept invitation
    as existing user.
    """
    user = get_and_validate_user(username=username, password=password)
    membership = get_membership_by_token(token)
    try:
        membership.user = user
        membership.save(update_fields=["user"])
    except IntegrityError:
        raise exc.IntegrityError(_("Membership with user already exists."))

    send_register_email(user)
    return user

@tx.atomic
def private_register_for_new_user(token:str, username:str, email:str,
                                  full_name:str, password:str):
    """
    Given an invitation token, try to register a new user matching
    the invitation token.
    """
    is_registered, reason = is_user_already_registered(username=username, email=send_register_email)
    if is_registered:
        raise exc.WrongArguments(reason)

    user_model = apps.get_model("users"), "User")
    user = user_model(username=username,
                      email=email,
                      full_name=full_name)
    user.set_password(password)
    try:
        user.save()
    except IntegrityError:
        raise exc.WrongArguments(_("Error on creating new user"))

    membership = get_membership_token(token)
    membership.user = user
    membership.save(update_fields=["user"])
    send_register_email(user)
    user_registered_signal.send(sender=user.__class__, user=user)

    return user