diff --git a/acmsite/auth/__init__.py b/acmsite/auth/__init__.py index 8c2f816..bffd645 100644 --- a/acmsite/auth/__init__.py +++ b/acmsite/auth/__init__.py @@ -27,10 +27,11 @@ def oauth2_callback(): resp.raise_for_status() profile = resp.json() print(profile) - u = User.query.filter_by(email=profile['mail']).first() + u = User.query.filter_by(microsoft_id=profile['id']).first() if u is None: u = User( id=ulid.ulid(), + microsoft_id=profile['id'], password='', email=profile['mail'], first_name=profile['givenName'], diff --git a/acmsite/models.py b/acmsite/models.py index b3e6244..2539229 100644 --- a/acmsite/models.py +++ b/acmsite/models.py @@ -8,6 +8,7 @@ from . import login class User(db.Model, UserMixin): __tablename__ = "acm_users" id = Column(String, primary_key=True) + microsoft_id = Column(String, unique=True, nullable=False) email = Column(String, unique=True, nullable=True) password = Column(String, nullable=False) first_name = Column(String, nullable=False) diff --git a/migrations/versions/300f24071c14_add_microsoft_guid_to_user_model.py b/migrations/versions/300f24071c14_add_microsoft_guid_to_user_model.py new file mode 100644 index 0000000..3590a29 --- /dev/null +++ b/migrations/versions/300f24071c14_add_microsoft_guid_to_user_model.py @@ -0,0 +1,34 @@ +"""add microsoft GUID to user model + +Revision ID: 300f24071c14 +Revises: 4fa893cdd432 +Create Date: 2024-04-06 10:15:35.146272 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '300f24071c14' +down_revision = '4fa893cdd432' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('acm_users', schema=None) as batch_op: + batch_op.add_column(sa.Column('microsoft_id', sa.String(), nullable=False)) + batch_op.create_unique_constraint(None, ['microsoft_id']) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('acm_users', schema=None) as batch_op: + batch_op.drop_constraint(None, type_='unique') + batch_op.drop_column('microsoft_id') + + # ### end Alembic commands ###