Beispiel #1
0
def route_users_me_get(current_user: UserInDB = Depends(get_current_user)):
    """
    Get current user
    """
    if not check_if_user_is_active(current_user):
        raise HTTPException(status_code=400, detail="Inactive user")
    return current_user
Beispiel #2
0
def route_users_put(
    *,
    username,
    password=None,
    admin_channels=None,
    admin_roles=None,
    disabled=None,
    email=None,
    full_name=None,
):
    current_user = get_current_user()

    if not current_user:
        abort(400, "Could not authenticate user with provided token")
    elif not check_if_user_is_active(current_user):
        abort(400, "Inactive user")
    elif not check_if_user_is_superuser(current_user):
        abort(400, "The user doesn't have enough privileges")
    bucket = get_default_bucket()
    user = get_user(bucket, username)

    if not user:
        return abort(404, f"The user with this username does not exist in the system.")
    user_in = UserInUpdate(
        username=username,
        password=password,
        admin_channels=admin_channels,
        admin_roles=admin_roles,
        disabled=disabled,
        email=email,
        full_name=full_name,
    )
    user = update_user(bucket, user_in)
    return user
Beispiel #3
0
def test_check_if_user_is_active():
    email = random_lower_string()
    password = random_lower_string()
    user_in = UserInCreate(name=email, email=email, password=password)
    user = upsert_user(bucket, user_in)
    is_active = check_if_user_is_active(user)
    assert is_active is True
def route_users_post(
    *,
    name,
    password,
    admin_channels=[],
    admin_roles=[],
    disabled=False,
    email=None,
    human_name=None,
):
    current_user = get_current_user()

    if not current_user:
        abort(400, "Could not authenticate user with provided token")
    elif not check_if_user_is_active(current_user):
        abort(400, "Inactive user")
    elif not check_if_user_is_admin_or_superuser(current_user):
        abort(400, "The user doesn't have enough privileges")
    user = get_user(bucket, name)
    if user:
        return abort(
            400, f"The user with this username already exists in the system.")
    user_in = UserInCreate(
        name=name,
        password=password,
        admin_channels=admin_channels,
        admin_roles=admin_roles,
        disabled=disabled,
        email=email,
        human_name=human_name,
    )
    user = upsert_user(bucket, user_in)
    return user
Beispiel #5
0
def route_users_me_get():
    current_user = get_current_user()
    if not current_user:
        abort(400, "Could not authenticate user with provided token")
    elif not check_if_user_is_active(current_user):
        abort(400, "Inactive user")
    return current_user
Beispiel #6
0
def route_users_post(
    *,
    username,
    password,
    admin_channels=[],
    admin_roles=[],
    disabled=False,
    email=None,
    full_name=None,
):
    current_user = get_current_user()

    if not current_user:
        abort(400, "Could not authenticate user with provided token")
    elif not check_if_user_is_active(current_user):
        abort(400, "Inactive user")
    elif not check_if_user_is_superuser(current_user):
        abort(400, "The user doesn't have enough privileges")
    bucket = get_default_bucket()
    user = get_user(bucket, username)
    if user:
        return abort(400, f"The user with this username already exists in the system.")
    user_in = UserInCreate(
        username=username,
        password=password,
        admin_channels=admin_channels,
        admin_roles=admin_roles,
        disabled=disabled,
        email=email,
        full_name=full_name,
    )
    user = upsert_user(bucket, user_in)
    if config.EMAILS_ENABLED:
        send_new_account_email(email_to=email, username=username, password=password)
    return user
Beispiel #7
0
def route_users_put(
    *,
    username: str,
    user_in: UserInUpdate,
    current_user: UserInDB = Depends(get_current_user),
):
    """
    Update a user
    """
    if not check_if_user_is_active(current_user):
        raise HTTPException(status_code=400, detail="Inactive user")
    elif not check_if_user_is_superuser(current_user):
        raise HTTPException(
            status_code=400, detail="The user doesn't have enough privileges"
        )
    bucket = get_default_bucket()
    user = get_user(bucket, username)

    if not user:
        raise HTTPException(
            status_code=404,
            detail="The user with this username does not exist in the system",
        )
    user = update_user(bucket, user_in)
    return user
Beispiel #8
0
def route_users_post(
    *, user_in: UserInCreate, current_user: UserInDB = Depends(get_current_user)
):
    """
    Create new user
    """
    if not check_if_user_is_active(current_user):
        raise HTTPException(status_code=400, detail="Inactive user")
    elif not check_if_user_is_superuser(current_user):
        raise HTTPException(
            status_code=400, detail="The user doesn't have enough privileges"
        )
    bucket = get_default_bucket()
    user = get_user(bucket, user_in.username)
    if user:
        raise HTTPException(
            status_code=400,
            detail="The user with this username already exists in the system.",
        )
    user = upsert_user(bucket, user_in, persist_to=1)
    if config.EMAILS_ENABLED and user_in.email:
        send_new_account_email(
            email_to=user_in.email, username=user_in.username, password=user_in.password
        )
    return user
Beispiel #9
0
def test_check_if_user_is_active():
    email = random_lower_string()
    password = random_lower_string()
    user_in = UserInCreate(username=email, email=email, password=password)
    bucket = get_default_bucket()
    user = upsert_user(bucket, user_in, persist_to=1)
    is_active = check_if_user_is_active(user)
    assert is_active is True
def route_users_get(skip=0, limit=100):
    current_user = get_current_user()
    if not current_user:
        abort(400, "Could not authenticate user with provided token")
    elif not check_if_user_is_active(current_user):
        abort(400, "Inactive user")
    elif not check_if_user_is_admin_or_superuser(current_user):
        abort(400, "The user doesn't have enough privileges")
    users = get_users(bucket, skip=skip, limit=limit)
    return users
Beispiel #11
0
def route_roles_get():
    current_user = get_current_user()
    if not current_user:
        abort(400, "Could not authenticate user with provided token")
    elif not check_if_user_is_active(current_user):
        abort(400, "Inactive user")
    elif not (check_if_user_is_superuser(current_user)):
        abort(400, "The current user does not have enogh privileges")
    roles = ensure_enums_to_strs(RoleEnum)
    return {"roles": roles}
Beispiel #12
0
def route_users_search_get(q, skip=0, limit=100):
    current_user = get_current_user()
    if not current_user:
        abort(400, "Could not authenticate user with provided token")
    elif not check_if_user_is_active(current_user):
        abort(400, "Inactive user")
    elif not check_if_user_is_superuser(current_user):
        abort(400, "The user doesn't have enough privileges")
    bucket = get_default_bucket()
    users = search_users(bucket=bucket, query_string=q, skip=skip, limit=limit)
    return users
def route_users_id_get(name):
    current_user = get_current_user()  # type: User
    if not current_user:
        abort(400, "Could not authenticate user with provided token")
    elif not check_if_user_is_active(current_user):
        abort(400, "Inactive user")
    user = get_user(bucket, name)
    if user == current_user:
        return user
    if not check_if_user_is_admin_or_superuser(current_user):
        abort(400, "The user doesn't have enough privileges")
    return user
Beispiel #14
0
def route_roles_get(current_user: UserInDB = Depends(get_current_user)):
    """
    Retrieve roles
    """
    if not check_if_user_is_active(current_user):
        raise HTTPException(status_code=400, detail="Inactive user")
    elif not (check_if_user_is_superuser(current_user)):
        raise HTTPException(
            status_code=400, detail="The current user does not have enogh privileges"
        )
    roles = ensure_enums_to_strs(RoleEnum)
    return {"roles": roles}
Beispiel #15
0
def route_reset_password(token, new_password):
    name = verify_password_reset_token(token)
    if not name:
        abort(400, "Invalid token")
    bucket = get_default_bucket()
    user = get_user(bucket, name)
    if not user:
        return abort(
            404, f"The user with this username does not exist in the system.")
    elif not check_if_user_is_active(user):
        abort(400, "Inactive user")
    user_in = UserInUpdate(name=name, password=new_password)
    user = update_user(bucket, user_in)
    return {"msg": "Password updated successfully"}
def route_login_access_token(username, password):
    user = authenticate_user(bucket, username, password)
    if not user:
        abort(400, "Incorrect email or password")
    elif not check_if_user_is_active(user):
        abort(400, "Inactive user")
    access_token_expires = timedelta(
        minutes=config.ACCESS_TOKEN_EXPIRE_MINUTES)
    return {
        "access_token":
        create_access_token(identity=username,
                            expires_delta=access_token_expires),
        "token_type":
        "bearer",
    }
Beispiel #17
0
def route_users_get(
    skip: int = 0, limit: int = 100, current_user: UserInDB = Depends(get_current_user)
):
    """
    Retrieve users
    """
    if not check_if_user_is_active(current_user):
        raise HTTPException(status_code=400, detail="Inactive user")
    elif not check_if_user_is_superuser(current_user):
        raise HTTPException(
            status_code=400, detail="The user doesn't have enough privileges"
        )
    bucket = get_default_bucket()
    users = get_users(bucket, skip=skip, limit=limit)
    return users
Beispiel #18
0
def route_users_me_put(*, password=None, full_name=None, email=None):
    current_user = get_current_user()

    if not current_user:
        abort(400, "Could not authenticate user with provided token")
    elif not check_if_user_is_active(current_user):
        abort(400, "Inactive user")
    user_in = UserInUpdate(jsonable_encoder(current_user))
    if password is not None:
        user_in.password = password
    if full_name is not None:
        user_in.full_name = full_name
    if email is not None:
        user_in.email = email
    bucket = get_default_bucket()
    user = update_user(bucket, user_in)
    return user
Beispiel #19
0
def route_users_id_get(
    username: str, current_user: UserInDB = Depends(get_current_user)
):
    """
    Get a specific user by username (email)
    """
    if not check_if_user_is_active(current_user):
        raise HTTPException(status_code=400, detail="Inactive user")
    bucket = get_default_bucket()
    user = get_user(bucket, username)
    if user == current_user:
        return user
    if not check_if_user_is_superuser(current_user):
        raise HTTPException(
            status_code=400, detail="The user doesn't have enough privileges"
        )
    return user
Beispiel #20
0
def route_reset_password(token: str, new_password: str):
    """
    Reset password
    """
    username = verify_password_reset_token(token)
    if not username:
        raise HTTPException(status_code=400, detail="Invalid token")
    bucket = get_default_bucket()
    user = get_user(bucket, username)
    if not user:
        raise HTTPException(
            status_code=404,
            detail="The user with this username does not exist in the system.",
        )
    elif not check_if_user_is_active(user):
        raise HTTPException(status_code=400, detail="Inactive user")
    user_in = UserInUpdate(name=username, password=new_password)
    user = update_user(bucket, user_in)
    return {"msg": "Password updated successfully"}
Beispiel #21
0
def route_login_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
    """
    OAuth2 compatible token login, get an access token for future requests
    """
    bucket = get_default_bucket()
    user = authenticate_user(bucket, form_data.username, form_data.password)
    if not user:
        raise HTTPException(status_code=400,
                            detail="Incorrect email or password")
    elif not check_if_user_is_active(user):
        raise HTTPException(status_code=400, detail="Inactive user")
    access_token_expires = timedelta(
        minutes=config.ACCESS_TOKEN_EXPIRE_MINUTES)
    return {
        "access_token":
        create_access_token(data={"username": form_data.username},
                            expires_delta=access_token_expires),
        "token_type":
        "bearer",
    }
Beispiel #22
0
def route_search_users(
    q: str,
    skip: int = 0,
    limit: int = 100,
    current_user: UserInDB = Depends(get_current_user),
):
    """
    Search users, use Bleve Query String syntax: http://blevesearch.com/docs/Query-String-Query/

    For typeahead sufix with `*`. For example, a query with: `email:johnd*` will match users with
    email `[email protected]`, `[email protected]`, etc.
    """
    if not check_if_user_is_active(current_user):
        raise HTTPException(status_code=400, detail="Inactive user")
    elif not check_if_user_is_superuser(current_user):
        raise HTTPException(
            status_code=400, detail="The user doesn't have enough privileges"
        )
    bucket = get_default_bucket()
    users = search_users(bucket=bucket, query_string=q, skip=skip, limit=limit)
    return users
Beispiel #23
0
def route_users_me_put(
    *,
    password: str = Body(None),
    full_name: str = Body(None),
    email: EmailStr = Body(None),
    current_user: UserInDB = Depends(get_current_user),
):
    """
    Update own user
    """
    if not check_if_user_is_active(current_user):
        raise HTTPException(status_code=400, detail="Inactive user")
    user_in = UserInUpdate(**current_user.dict())
    if password is not None:
        user_in.password = password
    if full_name is not None:
        user_in.full_name = full_name
    if email is not None:
        user_in.email = email
    bucket = get_default_bucket()
    user = update_user(bucket, user_in)
    return user
def route_users_me_put(
    *,
    password=None,
    human_name=None,
    email=None,
):
    current_user: UserStored = get_current_user()

    if not current_user:
        abort(400, "Could not authenticate user with provided token")
    elif not check_if_user_is_active(current_user):
        abort(400, "Inactive user")
    user_in = UserInUpdate(**current_user.json_dict())
    if password is not None:
        user_in.password = password
    if human_name is not None:
        user_in.human_name = human_name
    if email is not None:
        user_in.email = email

    user = update_user(bucket, user_in)
    return user