Auth is working. Disabled field added (models)

This commit is contained in:
DmitryGantimurov 2023-07-27 18:55:30 +03:00 committed by Dmitry Gantimurov
parent dd719a20ec
commit 6bb7ab5ce9
5 changed files with 33 additions and 4 deletions

View File

@ -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(

View File

@ -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): #класс объявления

View File

@ -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

View File

@ -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")

View File

@ -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 ###