def create(*, obj_in: UserCreate) -> User: passwordhash = get_password_hash(obj_in.password) user = UserInDB(**obj_in.dict(by_alias=True), hashed_password=passwordhash) doc_data = jsonable_encoder(user) db = get_default_bucket() collection = db["users"] res = collection.insert_one(doc_data) user_db = collection.find_one({"_id": ObjectId(res.inserted_id)}) user = UserInDB(**user_db) return user
def update(*, db_obj: User, obj_in: Union[UserUpdate, Dict[str, Any]]) -> User: if isinstance(obj_in, dict): update_data = obj_in else: update_data = obj_in.dict(exclude_unset=True) if update_data["password"]: hashed_password = get_password_hash(update_data["password"]) del update_data["password"] update_data["hashed_password"] = hashed_password obj_data = jsonable_encoder(update_data) for field in obj_data: if field in update_data: setattr(db_obj, field, update_data[field]) query_str = {"email": f"{db_obj.email}"} newvalues = {"$set": obj_data} db = get_default_bucket() collection = db["users"] user_db = collection.find_one_and_update(query_str, newvalues) user = UserInDB(**user_db) return user
def get_multi(*, skip: int = 0, limit: int = 100): db = get_default_bucket() collection = db["users"] users = [] for user_db in collection.find(): user = UserInDB(**user_db) users.append(user) return users
def update(self, db_session: Session, *, db_obj: User, obj_in: UserUpdate) -> User: if obj_in.password: update_data = obj_in.dict(exclude_unset=True) hashed_password = get_password_hash(obj_in.password) del update_data["password"] update_data["hashed_password"] = hashed_password use_obj_in = UserInDB.parse_obj(update_data) return super().update(db_session, db_obj=db_obj, obj_in=use_obj_in)
def get(*, id: ObjectId): db = get_default_bucket() collection = db["users"] query_str = {"id": str(id)} user_db = collection.find_one(query_str) if not user_db: return None user = UserInDB(**user_db) return user
def get_by_email(*, email: str) -> Optional[User]: query_str = {"email": f"{email}"} db = get_default_bucket() collection = db["users"] user_db = collection.find_one(query_str) if not user_db: return None user = UserInDB(**user_db) return user
async def get_current_user(payload: UserPayload = Depends( JWTBearerPayload())) -> UserInDB: """ Extract the user based on HTTP bearer jwt token """ user_email = payload.get('user_email') if not user_email: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid authentication credentials", headers={"WWW-Authenticate": "Bearer"}, ) user_in_db = await UserTableHandler.find_by_email(user_email) if user_in_db: return UserInDB(**user_in_db) raise HTTPException(status_code=401, detail="Invalid authorization code.")