From 5b1d41eb58ea0d0322fb103b48a9935b935febd1 Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Sat, 6 Apr 2024 10:17:45 +1100 Subject: [PATCH] Don't key off of email for login Let's not emulate the rest of WPI's shitty login system that doesn't deal with name changes This also opens us to up to making it significantly easier to update names and emails, if they don't match on login but ID matches. Will file bug to implement this, though it'll be hard to test Note that this is a BREAKING CHANGE, the database will need to be cleared before applying migrations will succeed --- acmsite/auth/__init__.py | 3 +- acmsite/models.py | 1 + ...071c14_add_microsoft_guid_to_user_model.py | 34 +++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 migrations/versions/300f24071c14_add_microsoft_guid_to_user_model.py 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 ###