Beispiel #1
0
def get_user(user_id: int,
             response: Response,
             cache_control: bool = Depends(cache_control),
             cached_response_json: dict = Depends(cached_response_json),
             cache_fingerprint: str = Depends(cache_fingerprint),
             is_localhost: bool = Depends(is_localhost),
             current_user: models.User = Depends(current_user),
             db: Session = Depends(get_db)):
    try:
        if not is_localhost:
            if current_user.id != user_id:
                raise HTTPException(
                    status_code=403,
                    detail={"error": "nein nein nein, this id not for you!"})

        user = crud.get_user(db, user_id)
    except crud.UserNotFoundError:
        raise HTTPException(status_code=400,
                            detail={"error": "failed to create user"})

    user_schema = schemas.User.from_orm(user)
    user_schema_dict = user_schema.dict()

    install_cache_response(cache_fingerprint, user_schema_dict)

    return user_schema
async def get_me(
    current_user: User = Depends(get_current_active_user),
    db: Session = Depends(get_db),
):
    db_user = crud.get_user(db, user_id=current_user.id)
    if db_user is None:
        raise HTTPException(status_code=404, detail="User not found")
    return db_user
Beispiel #3
0
def authenticate_user(signin: SignIn, db: Session):
    user = crud.get_user(signin, db)
    print("authenticate_user", user)
    if not user:
        return False
    if not verify_password(signin.passwd, user.passwd):
        return False
    return user
def read_user(
    user_id: int,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_admin_user),
):
    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
def register_user(user: schemas.UserCreate,
                  db: Session = Depends(deps.get_db)) -> JSONResponse:
    data = crud.get_user(db, email=user.email)
    if data is not None and not data:
        return JSONResponse(status_code=400,
                            content={"message": "email already registered"})
        # raise HTTPException(status_code=400,
        #                     detail="email already registered")
    data = crud.create_user(db=db, user=user)
    if data is None:
        return JSONResponse(status_code=500,
                            content={"message": "Internal Server Error"})
    return JSONResponse(status_code=200,
                        content={"message": "success"})
Beispiel #6
0
def recover_password(
    email: str, db: Session = Depends(deps.get_db)) -> JSONResponse:
    """
    Password Recovery
    """
    db_user = crud.get_user(db, email=email)

    if db_user is None:
        return JSONResponse(status_code=404,
                            content={
                                "message":
                                "The user with this email "
                                "does not exist in the system."
                            })

    password_reset_token = access_token.generate_password_reset_token(
        email=email)
    send_reset_password_email(email=email,
                              password_reset_token=password_reset_token)
    return JSONResponse(status_code=200, content={"message": "success"})
Beispiel #7
0
def authenticate_user(
    user: schemas.UserAuthenticate, db: Session = Depends(deps.get_db)
) -> JSONResponse:
    db_user = crud.get_user(db, email=user.email)
    if db_user is None:
        return JSONResponse(status_code=400,
                            content={"message": "Invalid Credentials"})
    else:
        is_password_correct = crud.check_username_password(db, user)
        if is_password_correct is False:
            return JSONResponse(status_code=400,
                                content={"message": "Invalid Credentials"})
        else:
            access_token_expires = timedelta(
                minutes=ProjectSettings.ACCESS_TOKEN_EXPIRE_MINUTES)
            token = access_token.create_access_token(
                data={"sub": user.email}, expires_delta=access_token_expires)
            return JSONResponse(status_code=200,
                                content={
                                    "access_token": token,
                                    "token_type": "Bearer"
                                })
Beispiel #8
0
def read_user(user_id: int, email: str, db: Session = Depends(get_db)):
    db_user = crud.get_user(db, email, user_id=user_id)
    if db_user is None:
        raise HTTPException(status_code=404, detail="User not found")
    return db_user