Beispiel #1
0
def signup_save(data: SignupIn, request: Request) -> UserType:
    user_data = data.user
    with transaction.atomic():
        try:
            user_queryset = get_user_queryset(User.objects.all(), CallbackContext(request.path_params))
            instance = user_queryset.get(**{User.USERNAME_FIELD: user_data.username.lower()})
        except User.DoesNotExist:
            # Create the user and save the casing the user chose as the first name
            try:
                instance = create_user(
                    CallbackContext(request.path_params),
                    **user_data.dict(),
                    password=None,
                    first_name=user_data.username,
                )
                instance.full_clean()
            except HttpError as e:
                raise e
            except django_exceptions.ValidationError as e:
                transform_validation_error("user", e)
            except Exception as e:
                raise HttpError("generic", str(e))

        if hasattr(instance, "userinfo"):
            raise HttpError("user_exists", "User already exists", status_code=status.HTTP_409_CONFLICT)

        models.UserInfo.objects.create(**data.dict(exclude={"user"}), owner=instance)
    return instance
Beispiel #2
0
def outgoing_create(
        data: CollectionInvitationIn,
        request: Request,
        user: UserType = Depends(get_authenticated_user),
):
    collection = get_object_or_404(models.Collection.objects,
                                   uid=data.collection)
    to_user = get_object_or_404(get_user_queryset(
        User.objects.all(), CallbackContext(request.path_params)),
                                username=data.username)

    context = Context(user, None)
    data.validate_db(context)

    if not is_collection_admin(collection, user):
        raise PermissionDenied("admin_access_required",
                               "User is not an admin of this collection")

    member = collection.members.get(user=user)

    with transaction.atomic():
        try:
            models.CollectionInvitation.objects.create(**data.dict(
                exclude={"collection", "username"}),
                                                       user=to_user,
                                                       fromMember=member)
        except IntegrityError:
            raise HttpError("invitation_exists", "Invitation already exists")
Beispiel #3
0
def get_websocket_user(
    websocket: WebSocket,
    ticket_model: t.Optional[TicketInner] = Depends(load_websocket_ticket)):
    if ticket_model is None:
        return None
    user_queryset = get_user_queryset(User.objects.all(),
                                      CallbackContext(websocket.path_params))
    return user_queryset.get(id=ticket_model.user)
Beispiel #4
0
def dashboard_url(request: Request, user: UserType = Depends(get_authenticated_user)):
    get_dashboard_url = app_settings.DASHBOARD_URL_FUNC
    if get_dashboard_url is None:
        raise HttpError("not_supported", "This server doesn't have a user dashboard.")

    ret = {
        "url": get_dashboard_url(CallbackContext(request.path_params, user=user)),
    }
    return ret
Beispiel #5
0
def outgoing_fetch_user_profile(
        username: str,
        request: Request,
        user: UserType = Depends(get_authenticated_user),
):
    kwargs = {User.USERNAME_FIELD: username.lower()}
    user = get_object_or_404(
        get_user_queryset(User.objects.all(),
                          CallbackContext(request.path_params)), **kwargs)
    user_info = get_object_or_404(models.UserInfo.objects.all(), owner=user)
    return UserInfoOut.from_orm(user_info)
Beispiel #6
0
def get_login_user(request: Request, challenge: LoginChallengeIn) -> UserType:
    username = challenge.username

    kwargs = get_user_username_email_kwargs(username)
    try:
        user_queryset = get_user_queryset(User.objects.all(), CallbackContext(request.path_params))
        user = user_queryset.get(**kwargs)
        if not hasattr(user, "userinfo"):
            raise AuthenticationFailed(code="user_not_init", detail="User not properly init")
        return user
    except User.DoesNotExist:
        raise AuthenticationFailed(code="user_not_found", detail="User not found")
Beispiel #7
0
def reset(data: SignupIn, request: Request):
    # Only run when in DEBUG mode! It's only used for tests
    if not settings.DEBUG:
        raise HttpError(code="generic", detail="Only allowed in debug mode.")

    with transaction.atomic():
        user_queryset = get_user_queryset(User.objects.all(),
                                          CallbackContext(request.path_params))
        user = get_object_or_404(user_queryset, username=data.user.username)
        # Only allow test users for extra safety
        if not getattr(user, User.USERNAME_FIELD).startswith("test_user"):
            raise HttpError(code="generic",
                            detail="Endpoint not allowed for user.")

        if hasattr(user, "userinfo"):
            user.userinfo.delete()

        signup_save(data, request)
        # Delete all of the journal data for this user for a clear test env
        user.collection_set.all().delete()
        user.collectionmember_set.all().delete()
        user.incoming_invitations.all().delete()