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
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
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
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
def test_check_if_user_is_superuser_normal_user(): username = random_lower_string() password = random_lower_string() user_in = UserInCreate(name=username, email=username, password=password) user = upsert_user(bucket, user_in) is_superuser = check_if_user_is_superuser(user) assert is_superuser is False
def route_test_email(email_to): 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_superuser(current_user): abort(400, "Not a superuser") send_test_email(email_to=email_to) return ({"msg": "Test email sent"}, 201)
def route_test_celery(msg: Msg, current_user: UserInDB = Depends(get_current_user)): """ Test Celery worker """ if not check_if_user_is_superuser(current_user): raise HTTPException(status_code=400, detail="Not a superuser") celery_app.send_task("app.worker.test_celery", args=[msg.msg]) return {"msg": "Word received"}
def route_test_celery(word): 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_superuser(current_user): abort(400, "Not a superuser") celery_app.send_task("app.worker.test_celery", args=[word]) return ({"msg": "Word received"}, 201)
def route_test_email( email_to: EmailStr, current_user: UserInDB = Depends(get_current_user) ): """ Test emails """ if not check_if_user_is_superuser(current_user): raise HTTPException(status_code=400, detail="Not a superuser") send_test_email(email_to=email_to) return {"msg": "Test email sent"}
def test_check_if_user_is_superuser_normal_user(): username = random_lower_string() password = random_lower_string() user_in = UserInCreate(username=username, email=username, password=password) bucket = get_default_bucket() user = upsert_user(bucket, user_in, persist_to=1) is_superuser = check_if_user_is_superuser(user) assert is_superuser is False
def test_check_if_user_is_superuser(): email = random_lower_string() password = random_lower_string() user_in = UserInCreate(name=email, email=email, password=password, admin_roles=[RoleEnum.superuser]) user = upsert_user(bucket, user_in) is_superuser = check_if_user_is_superuser(user) assert is_superuser is True
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_admin(current_user) or 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}
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_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}
def route_users_id_get(username): 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") bucket = get_default_bucket() user = get_user(bucket, username) if user == current_user: return user if not check_if_user_is_superuser(current_user): abort(400, "The user doesn't have enough privileges") return user
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
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
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