コード例 #1
0
    def validate(self, db: Session, value: str,
                 current_user: User) -> Optional[Registration_Link]:

        query = db.query(Registration_Link)
        query = query.filter(Registration_Link.value == value)
        registration_link_in_db = query.first()

        if (registration_link_in_db is not None
                and registration_link_in_db.fk_user != current_user.id):
            return "you can't validate this link"
        if (registration_link_in_db is not None
                and registration_link_in_db.fk_user == current_user.id):
            logging.info("registration link ok")
            dateExpiration = registration_link_in_db.creation_date + timedelta(
                hours=settings.EMAIL_RESET_TOKEN_EXPIRE_HOURS)
            curDate = datetime.now()
            logging.info(f"date expirtaion: {dateExpiration}")
            logging.info(f"date current: {curDate}")
            if curDate < dateExpiration:
                logging.info("date ok we validate it")
                user.update(db=db,
                            db_obj=current_user,
                            obj_in={"is_verified": True})
        else:
            logging.info("bla bla bla bla not valid")
            # handle case when expired ?
            registration_link_in_db = None
        return registration_link_in_db
コード例 #2
0
def update_user(
        *,
        db: Session = Depends(get_db),
        user_id: str,
        user_in: UserUpdate,
        environment: str = Depends(get_environment_from_token),
        current_user: UserInDB = Depends(check_superuser_or_admin),
):
    """
    Update a user.
    """
    user = get(db, user_id=user_id)
    if not user:
        return Response(json.dumps({
            "messageCode": codes['db'],
            "title": "Erro no Banco de Dados",
            "message": ptBr['eUserNotFound']
        }),
            status_code=404)
    if not user.is_active:
        return Response(json.dumps({
            "messageCode": codes['db'],
            "title": "Erro no Banco de Dados",
            "message": ptBr['eUserNotActive']
        }),
            status_code=404)
    if user.is_active:
        user = update(db, user=user, user_in=user_in.user_data)
    return user
コード例 #3
0
def update_user(user_id: int,
                user_update: UserUpdate,
                user_auth: UserModel = Depends(verify_auth),
                db=Depends(get_db)):
    """Update a user by id. One user cannot update another"""
    if user_id != user_auth.id:
        raise HTTPException(status_code=403,
                            detail='Unauthorized to update other users')

    if not verify_password(user_update.oldPassword, user_auth.password):
        raise HTTPException(status_code=400,
                            detail='Invalid password. Try again.')

    return update(db, user_auth, user_update)
コード例 #4
0
def update_user(
        *,
        db: Session = Depends(get_db),
        user_id: int,
        user_in: UserUpdate,
        current_user: User = Depends(get_current_user_if_is_superuser),
) -> Any:
    """
    Update a user.
    """
    user_in_db = user.get(db, id=user_id)
    if not user_in_db:
        raise HTTPException(
            status_code=404,
            detail="The user with this username does not exist in the system",
        )
    user_in_db = user.update(db, db_obj=user_in_db, obj_in=user_in)
    return user_in_db
コード例 #5
0
ファイル: users.py プロジェクト: ukrittang/fastapi-starter
def update_user(
    *,
    db: Session = Depends(get_db),
    user_id: int,
    user_in: UserUpdate,
    # pylint: disable=unused-argument
    current_user: UserInDB = Depends(get_current_active_superuser),
):
    """
    Update a user.
    """
    user = crud_user.get(db, user_id=user_id)
    if not user:
        raise HTTPException(
            status_code=HTTP_404_NOT_FOUND,
            detail="The user with this username does not exist in the system",
        )
    user = crud_user.update(db, user=user, user_in=user_in)
    return user
コード例 #6
0
def update_user_me(
        *,
        db: Session = Depends(get_db),
        password: str = Body(None),
        full_name: str = Body(None),
        email: EmailStr = Body(None),
        current_user: User = Depends(get_current_user),
) -> Any:
    """
    Update own user.
    """
    current_user_data = jsonable_encoder(current_user)
    user_in = UserUpdate(**current_user_data)
    if password is not None:
        user_in.password = password
    if full_name is not None:
        user_in.full_name = full_name
    if email is not None:
        user_in.email = email
    user_in_db = user.update(db, db_obj=current_user, obj_in=user_in)
    return user_in_db