Exemplo n.º 1
0
def delete_user(uuid: UUID4, dba: Session = Depends(deps.get_db)):
    user = cruds.get_user_by_uuid(db=dba, uuid=uuid)
    if not user:
        raise HTTPException(status_code=404, detail='User not found')
    dba.query(models.User). \
        filter(models.User.uuid == uuid). \
        delete()
    dba.commit()
    return {'detail': 'User deleted successfully.'}
Exemplo n.º 2
0
def change_user_password(uuid: UUID4,
                         passwords: schemas.ChagePasswordFromDashboard,
                         dba: Session = Depends(deps.get_db)):
    user = cruds.get_user_by_uuid(db=dba, uuid=uuid)
    if not user:
        raise HTTPException(status_code=404, detail='User not found')
    if not verify_password(passwords.current_password, user.password_hash):
        raise HTTPException(status_code=403,
                            detail='Current password is incorrect')
    new_password_hash = get_password_hash(passwords.new_password)
    user.password_hash = new_password_hash
    dba.commit()
    dba.refresh(user)
    return {'detail': 'Password changed successfully.'}
Exemplo n.º 3
0
def update_user(uuid: UUID4,
                user_data: schemas.UserUpdate,
                dba: Session = Depends(deps.get_db)):
    user = cruds.get_user_by_uuid(uuid=uuid, db=dba)
    if not user:
        raise HTTPException(status_code=404, detail='User not found')
    user_update_dict = user_data.dict(exclude_unset=True)
    if len(user_update_dict) < 1:
        raise HTTPException(status_code=400, detail='Invalid request')

    for key, value in user_update_dict.items():
        setattr(user, key, value)
    dba.commit()
    dba.refresh(user)
    return user
Exemplo n.º 4
0
def remove_group_from_user(uuid: UUID4,
                           groups: schemas.UserGroup,
                           dba: Session = Depends(deps.get_db)):
    user = cruds.get_user_by_uuid(db=dba, uuid=uuid)
    group_list = groups.dict().pop('groups')
    if not user:
        raise HTTPException(status_code=404, detail='User not found')
    for group_name in group_list:
        group = get_group_by_name(name=group_name, db=dba)
        if not group:
            raise HTTPException(status_code=404,
                                detail=f'{group_name} is not found')
        user.groups.remove(group)
    dba.commit()
    dba.refresh(user)
    return user
Exemplo n.º 5
0
def user_detail(uuid: UUID4, dba: Session = Depends(deps.get_db)):
    user = cruds.get_user_by_uuid(uuid=uuid, db=dba)
    if not user:
        raise HTTPException(status_code=404, detail='User not found')
    return user