Beispiel #1
0
def init_db(db_session: Session) -> None:
    user = get_user_by_email(db_session, user_email=FIRST_SUPERUSER_EMAIL)

    if not user:
        user_in = UserCreate(
            email=FIRST_SUPERUSER_EMAIL,
            password=FIRST_SUPERUSER_PASSWORD,
            is_superuser=True,
        )
        create_user(db_session, user_in=user_in)
Beispiel #2
0
async def register_user(*,
                        db: Session = Depends(get_db),
                        data: UserCreate,
                        background_tasks: BackgroundTasks):
    """registering new users."""
    otp = generate_random_otp()
    with expected_integrity_error(
            db, detail="There was a conflict with an existing user.",
            debug=False):
        if user := create_user(db, user_in=data, otp=otp):
            background_tasks.add_task(send_email_verify_otp, user.email,
                                      user.otp)
def add_user(data: UserCreate = Body(...),
             db: Session = Depends(get_db),
             user: User = Depends(get_current_user_authorizer())):
    if user.is_admin is False:
        raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail='权限不足')
    db_user = get_user_by_name(db, name=data.name)
    if db_user:
        raise HTTPException(status_code=HTTP_400_BAD_REQUEST,
                            detail="Name already registered")
    return create_user(db=db, user_item=data)
Beispiel #4
0
def create_user(
    user: UserCreate,
    db: Session = Depends(deps.get_db)
) -> Any:
    db_user = crud.get_user(db, user.name)
    if db_user:
        raise HTTPException(status_code=400, detail="Name already registered")
    created_user = crud.create_user(db, user)
    access_token = create_access_token(created_user.name)
    return {"access_token": access_token, "token_type": "bearer"}
def add_admin(data: UserCreate = Body(...),
              api_key: str = Body(...),
              db: Session = Depends(get_db)):
    if api_key != secret_key:
        raise HTTPException(HTTP_400_BAD_REQUEST, 'apikey错误')
    db_user = get_user_by_name(db, name=data.name)
    if db_user:
        raise HTTPException(status_code=HTTP_400_BAD_REQUEST,
                            detail="Name already registered")
    data.is_admin = True
    return create_user(db=db, user_item=data)
def test_login_access_token(client: TestClient, db: Session) -> None:
    user = UserCreate(name="test", password="******")
    user_db = user_crud.create_user(db, user)
    r = client.post(f"{settings.API_STR}/login/access-token",
                    data={
                        "username": "******",
                        "password": "******"
                    })
    result = r.json()
    assert r.status_code == 200
    assert 'access_token' in result
    assert result['access_token']
def test_face_authorize_access_token(client: TestClient, db: Session,
                                     img_file: IO) -> None:
    encodings = encode_face(img_file)
    user_create = UserCreate(name="test", password="******")
    user_db = user_crud.create_user(db, user_create)
    user_db.encodings = encodings
    db.commit()
    db.refresh(user_db)
    img_file.seek(0)
    r = client.post(f"{settings.API_STR}/login/face-access-token",
                    files={'file': img_file})
    result = r.json()
    assert r.status_code == 200
    assert 'access_token' in result
    assert result['access_token']
Beispiel #8
0
def register():
    try:
        user = create_user()
    except AssertionError as exception_message:
        return make_response(
            jsonify(msg="Error: {}. ".format(exception_message)), 400)
    data = {
        "msg": "User created sucessfully",
        "user": user.dict(),
        "current_user": user.get_id(),
        "is_anonymous": user.is_anonymous,
        "is_active": user.is_active,
        "is_authenticated": user.is_authenticated,
    }
    login_user(user)
    return make_response(jsonify(data), 201)
def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
    db_user = crud.get_user_by_email(db, email=user.email)
    if db_user:
        raise HTTPException(status_code=400, detail="Email already registered")
    return crud.create_user(db=db, user=user)
Beispiel #10
0
def create_user(user: UserInDB):
    return db_user.create_user(user)