From 6bb7ab5ce9378827c02bbb388199943b08fe19f0 Mon Sep 17 00:00:00 2001 From: DmitryGantimurov Date: Thu, 27 Jul 2023 18:55:30 +0300 Subject: [PATCH] Auth is working. Disabled field added (models) --- back/main.py | 1 - back/models.py | 3 +- back/schema.py | 1 + back/utils.py | 4 +-- ...new_colomn_disabled_added_to_user_table.py | 28 +++++++++++++++++++ 5 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 migrations/versions/daffcb4729af_new_colomn_disabled_added_to_user_table.py diff --git a/back/main.py b/back/main.py index e61031c..831d364 100644 --- a/back/main.py +++ b/back/main.py @@ -167,7 +167,6 @@ def create_user(data = Body()): async def login_for_access_token( form_data: Annotated[OAuth2PasswordRequestForm, Depends()] ): - # разобраться с первым параметром user = authenticate_user(database, form_data.username, form_data.password) if not user: raise HTTPException( diff --git a/back/models.py b/back/models.py index f1c0039..357e77a 100644 --- a/back/models.py +++ b/back/models.py @@ -1,4 +1,4 @@ -from sqlalchemy import Column, Integer, String +from sqlalchemy import Column, Integer, String, Boolean from fastapi_users.db import SQLAlchemyBaseUserTableUUID, SQLAlchemyUserDatabase from fastapi import Depends from .db import Base, engine @@ -19,6 +19,7 @@ class UserDatabase(Base):#класс пользователя hashed_password = Column(String) name = Column(String, nullable=True)#имя пользователя surname = Column(String)#фамилия пользователя + disabled = Column(Boolean, default=True) class Announcement(Base): #класс объявления diff --git a/back/schema.py b/back/schema.py index a563691..56e839f 100644 --- a/back/schema.py +++ b/back/schema.py @@ -37,6 +37,7 @@ class User(BaseModel): email: str name: Union[str, None] = None surname: str + disabled: Union[bool, None] = None class Config: orm_mode = True diff --git a/back/utils.py b/back/utils.py index 3b419cd..3f3af8d 100644 --- a/back/utils.py +++ b/back/utils.py @@ -73,11 +73,11 @@ async def get_current_user(db: Session, token: Annotated[str, Depends(oauth2_sch user = get_user(db=db, email=token_data.email) if user is None: raise credentials_exception - return user + return UserInDB(user) async def get_current_active_user( - current_user: Annotated[User, Depends(get_current_user)] + current_user: Annotated[UserInDB, Depends(get_current_user)] ): if current_user.disabled: raise HTTPException(status_code=400, detail="Inactive user") diff --git a/migrations/versions/daffcb4729af_new_colomn_disabled_added_to_user_table.py b/migrations/versions/daffcb4729af_new_colomn_disabled_added_to_user_table.py new file mode 100644 index 0000000..b6abe04 --- /dev/null +++ b/migrations/versions/daffcb4729af_new_colomn_disabled_added_to_user_table.py @@ -0,0 +1,28 @@ +"""new colomn (disabled) added to user table + +Revision ID: daffcb4729af +Revises: 18001c2231e3 +Create Date: 2023-07-26 23:54:22.640750 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'daffcb4729af' +down_revision = '18001c2231e3' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('users', sa.Column('disabled', sa.Boolean(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('users', 'disabled') + # ### end Alembic commands ###