示例#1
0
async def create_team_membership(
        team_membership: schema.TeamMembershipCreate,
        db: Session = Depends(get_db),
        auth_user: User = Depends(auth_required),
):
    user = get_user(db, team_membership.user_id)
    if not user:
        raise NoResourceWithIdError("user", team_membership.user_id)

    team = get_team(db=db, team_id=team_membership.team_id)
    if not team:
        raise NoResourceWithIdError("team", team_membership.team_id)

    role = get_role(db=db, role_id=team_membership.role_id)
    if not role:
        raise NoResourceWithIdError("role", team_membership.role_id)

    found = crud.membership_already_exists(db=db,
                                           user=user,
                                           team=team,
                                           role=role)
    if found:
        raise HTTPException(
            HTTP_409_CONFLICT,
            detail=
            "The user already has membership of that role within the team",
        )

    return crud.create_team_membership(db=db, user=user, team=team, role=role)
def authenticate_user(db, username: str, password: str):
    user = users_crud.get_user(db, username=username)
    if not user:
        return False
    if not verify_password(password, user.hashed_password):
        return False
    return user
示例#3
0
async def login_required(token: str = Depends(oauth2_scheme),
                         db: Session = Depends(get_db)):
    try:
        payload = jwt.decode(token, SECRET_KEY, algorithms=[HASH_ALGORITHM])
        user_id: str = payload.get("sub")
        if user_id is None:
            raise UnauthorisedTokenException()
    except jwt.PyJWTError:
        raise UnauthorisedTokenException()
    user = get_user(db, user_id)
    if user is None:
        raise UnauthorisedTokenException()
    return user
async def get_current_user(token: str = Depends(oauth2_scheme),
                           db: Session = Depends(get_db)):
    try:
        payload = jwt.decode(token,
                             config.SECRET_KEY,
                             algorithms=[config.ALGORITHM])
        username: str = payload.get('sub')
        if username is None:
            raise CREDENTIALS_EXCEPTION
    except JWTError:
        raise CREDENTIALS_EXCEPTION
    user = users_crud.get_user(db, username)
    if user is None:
        raise CREDENTIALS_EXCEPTION
    return user
示例#5
0
def delete_user(test_instance, deleted_user, authenticated_user):
    """
    Utility func to delete user
    :param test_instance: FeatureTest Instance
    :param deleted_user: User to be deleted
    :param authenticated_user: User authenticated
    """
    response = test_instance.client.delete(
        "/auth/user/%d/" % deleted_user.id,
        headers=test_instance.set_auth(
            test_instance.get_token(authenticated_user)))

    assert response.status_code == 204
    assert get_user(test_instance.database_conn, deleted_user.id) is None
    response = test_instance.client.get("/auth/user/%d/" % deleted_user.id)
    assert response.status_code == 404
示例#6
0
def update_event(test_instance, event, user_id, success):
    """
    Utility fn to update an event
    """
    user = get_user(database=test_instance.database_conn, user_id=user_id)
    new_name = test_instance.random_string()
    response = test_instance.client.patch(
        "/content/events/%d/" % event.id,
        headers=test_instance.set_auth_from_user(user),
        json={"name": new_name})
    if success:
        assert response.status_code == 200
        # modified_event = get_event_by_id(database=test_instance.database_conn, event_id=event.id)
        # assert modified_event.name == new_name
    else:
        assert response.status_code == 403
示例#7
0
def update_user(test_instance, updated_user, authenticated_user):
    """
    Utility func to update user
    :param test_instance: FeatureTest instance
    :param updated_user: User to be updated
    :param authenticated_user: User authenticated
    """
    new_name = test_instance.random_string()
    response = test_instance.client.patch(
        "/auth/user/%d/" % updated_user.id,
        headers=test_instance.set_auth(
            test_instance.get_token(authenticated_user)),
        json={"name": new_name},
    )
    assert response.status_code == 200
    assert get_user(test_instance.database_conn,
                    updated_user.id).name == new_name
示例#8
0
def read_user(user_id: int, db: Session = Depends(get_db)):
    db_user = crud.get_user(db, user_id=user_id)
    if db_user is None:
        raise HTTPException(status_code=404, detail="User not found")
    return db_user
示例#9
0
async def get_user(user_id: int,
                   db: Session = Depends(get_db),
                   user: User = Depends(login_required)):
    return crud.get_user(db, user_id)
示例#10
0
def get_user(id: int, db: Session = Depends(get_db)):
    user = crud.get_user(db=db, user_id=id)
    data = schemas.User(**user.to_json())

    return success(data)
示例#11
0
async def get_user(user_id: str,
                   db: Session = Depends(get_db),
                   user: User = Depends(auth_required)):
    return crud.get_user(db, user_id)