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
def test_check_if_user_is_active_inactive(): email = random_lower_string() password = random_lower_string() user = create_or_get_user(email, password) user["roles"].remove("active") user.save() is_active = check_if_user_is_active(user) assert is_active is False
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_superuser(current_user): abort(400, "Not a superuser") db_users = get_db_users() result = db_users.get_query_result(selector={"type": "user"}) return result[skip:skip + limit]
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") user = get_user(username) if user == current_user: return user if not check_if_user_is_superuser(current_user): abort(400, "Not a superuser") return user
def route_roles_get(): 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") if check_if_user_is_superuser(current_user): return get_roles(db_session) else: return get_user_roles(current_user)
def route_users_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") if check_if_user_is_superuser(current_user): return get_users(db_session) else: # return the current user's data, but in a list return [current_user]
def route_users_username_dbid_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") user = get_user(username) user_db_id = get_database_id_for_user(username) response = {"msg": user_db_id} if user == current_user: return response if not check_if_user_is_superuser(current_user): abort(400, "Not a superuser") return response
def route_roles_post(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, "Not a superuser") role = get_role_by_name(name, db_session) if role: return abort(400, f"The role: {name} already exists in the system") role = create_role(name, db_session) return role
def route_users_post(username=None, password=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, "Not a superuser") user = get_user(username) if user: return abort( 400, f"The user with this username already exists in the system.") user = create_user_with_default_db(username, password) return user
def route_login_access_token(username, password): user = authenticate_user(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", }
def route_users_id_get(user_id): 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_by_id(user_id, db_session) if not user: return abort(400, f"The user with id: {user_id} does not exists") if not check_if_user_is_superuser(current_user): return abort(400, "Not authorized") return user
def route_users_post(email=None, password=None, first_name=None, last_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, "Only a superuser can execute this action") user = get_user_by_username(email, db_session) if user: return abort( 400, f"The user with this email already exists in the system: {email}" ) user = create_user(db_session, email, password, first_name, last_name) return user
def route_users_assign_role_post(user_id, role_id): 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") elif not check_if_user_is_superuser(current_user): abort(404, "Not authorized") user = get_user_by_id(user_id, db_session) if not user: return abort(400, f"The user with id: {user_id} does not exists") role = get_role_by_id(role_id, db_session) if not role: return abort(400, f"The role does not exist") updated_user = assign_role_to_user(role, user, db_session) return updated_user
def test_check_if_user_is_active(): email = random_lower_string() password = random_lower_string() user = create_or_get_user(email, password) # noqa is_active = check_if_user_is_active(user) assert is_active is True