Beispiel #1
0
class User(MethodView):

    @resources.response(HTTPStatus.OK, schemas.User(many=True))
    @resources.paginate(CursorPage)
    @resources.odata(db.session)
    def get(self):
        return models.User
Beispiel #2
0
def ToResUser(db_user):
    return schemas.User(
        id=db_user.id,
        email=db_user.email,
        name=db_user.name,
        is_active=db_user.is_active,
    )
Beispiel #3
0
def get_current_user(token: str = Depends(oauth2_scheme),
                     db: Session = Depends(get_db)):
    credentials_exception = HTTPException(
        status_code=status.HTTP_401_UNAUTHORIZED,
        detail='Could not validate credentials',
        headers={'WWW-Authenticate': 'Bearer'},
    )

    try:
        payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
        username: str = payload.get('sub')

        if username is None:
            raise credentials_exception

        token_data = schemas.TokenData(username=username)
    except JWTError:
        raise credentials_exception

    user = db.query(
        models.User).filter(models.User.login == token_data.username).first()

    if user is None:
        raise credentials_exception

    return schemas.User(login=user.login,
                        password=user.password,
                        bots=str(user.bots))
async def read_users_me(current_user: User = Depends(deps.get_current_user)):
    db_user = current_user
    return schemas.User(
        username=db_user.username,
        email=db_user.email,
        full_name=db_user.full_name,
        disabled=db_user.disabled,
    )
def test_get_current_active_user():
    active_user = schemas.User(
        id=1,
        first_name='test',
        last_name='user',
        email='pass',
        team_id=1,
        disabled=False)

    user = get_current_active_user(active_user)
    assert user == active_user
def test_get_current_active_user_disabled():
    disabled_user = schemas.User(
        id=1,
        first_name='test',
        last_name='user',
        email='pass',
        team_id=1,
        disabled=True)

    with pytest.raises(HTTPException) as exception:
        get_current_active_user(disabled_user)

    assert exception.value.detail == 'Inactive user'
Beispiel #7
0
def create_user(
    *,
    user_in: schemas.UserCreate,
) -> Any:
    """
    Create new user.
    """
    next_user_id = users[-1].id + 1  # type: ignore
    user = schemas.User(
        id=next_user_id,
        email=user_in.email,
        is_active=user_in.is_active,
        is_superuser=user_in.is_superuser,
        full_name=user_in.full_name,
    )
    users.append(user)
    return user
Beispiel #8
0
def get_current_user(
    uow: user_uow.AbstractUnitOfWork = Depends(user_uow.get_sqlalchemy_uow),
    token: str = Depends(reusable_oauth2),
) -> schemas.User:
    try:
        payload = jwt.decode(
            token, settings.SECRET_KEY, algorithms=[security.ALGORITHM]
        )
        token_data = schemas.TokenPayload(**payload)
    except (jwt.JWTError, ValidationError):
        raise InvalidToken
    with uow:
        id = user_id(str(token_data.sub))
        user = uow.users.get_by_id(id)
        if not user:
            raise UserNotFound(id)
        return_user = schemas.User(**user.__dict__)
        uow.commit()
    return return_user
Beispiel #9
0
def read_user_me(
    db: Session = Depends(deps.get_db),
    current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
    """
    Get current user.
    """
    if not current_user.user_role:
        role = None
    else:
        role = current_user.user_role.role.name
    user_data = schemas.User(
        id=current_user.id,
        email=current_user.email,
        is_active=current_user.is_active,
        full_name=current_user.full_name,
        created_at=current_user.created_at,
        updated_at=current_user.updated_at,
        role=role,
    )
    return user_data
def create_user_signup(
        *,
        db: Session = Depends(deps.get_db),
        user_in: schemas.CreateUser,
) -> Any:
    """
    Create new user without the need to be logged in.
    """

    user = db.query(User).filter(User.email == user_in.email).first()
    if user:
        raise HTTPException(
            status_code=400,
            detail="The user with this email already exists in the system",
        )
    create_user_data = user_in.dict()
    create_user_data.pop("password")
    db_obj = User(**create_user_data)
    db_obj.hashed_password = get_password_hash(user_in.password)
    db.add(db_obj)
    db.commit()

    return schemas.User(**create_user_data)
Beispiel #11
0
"""
Users REST API
"""
from typing import Any, List

from fastapi import APIRouter, HTTPException, status

from app import schemas

router = APIRouter()

users = [
    schemas.User(
        id=1,
        email="*****@*****.**",
        is_active=True,
        is_superuser=True,
        full_name="Martin Fowler",
    ),
    schemas.User(
        id=2,
        email="*****@*****.**",
        is_active=False,
        is_superuser=True,
        full_name="Uncle Bob",
    ),
]


@router.get("/", response_model=List[schemas.User])
def read_users(