From 70f5f4ffeab2dae909899b070c0b9dbf94876998 Mon Sep 17 00:00:00 2001 From: kaiyou <pierre@jaury.eu> Date: Mon, 30 Mar 2020 18:00:44 +0200 Subject: [PATCH] Support alternative claim names --- hiboo/models.py | 14 ++++++++ hiboo/profile/cli.py | 10 +++++- hiboo/profile/login.py | 17 +++++++-- .../versions/c5109b93fc0f_add_claim_names.py | 35 +++++++++++++++++++ 4 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 migrations/versions/c5109b93fc0f_add_claim_names.py diff --git a/hiboo/models.py b/hiboo/models.py index 0ccce53..10e90f8 100644 --- a/hiboo/models.py +++ b/hiboo/models.py @@ -205,6 +205,20 @@ class Profile(db.Model): ).filter(cls.status.in_((cls.ACTIVE, cls.BLOCKED, cls.REQUEST))) +class ClaimName(db.Model): + """ A profile might have multiple claimable names. + """ + __tablename__ = "claimname" + + profile_uuid = db.Column(db.String(36), db.ForeignKey(Profile.uuid)) + service_uuid = db.Column(db.String(36), db.ForeignKey(Service.uuid)) + profile = db.relationship(Profile, + backref=db.backref('claimnames', cascade='all, delete-orphan', lazy='dynamic')) + + username = db.Column(db.String(255), nullable=False) + + + class History(db.Model): """ Records an even in an account's or profile's lifetime. """ diff --git a/hiboo/profile/cli.py b/hiboo/profile/cli.py index 5383b34..a23e051 100644 --- a/hiboo/profile/cli.py +++ b/hiboo/profile/cli.py @@ -8,7 +8,8 @@ import click @click.argument("service_uuid") @click.argument("username") @click.argument("password_hash") -def unclaimed(service_uuid, username, password_hash): +@click.argument("claim_names", nargs=-1) +def unclaimed(service_uuid, username, password_hash, claim_names): assert models.Service.query.get(service_uuid) assert not models.Profile.query.filter_by(service_uuid=service_uuid, username=username).first() profile = models.Profile( @@ -18,4 +19,11 @@ def unclaimed(service_uuid, username, password_hash): extra={"password": password_hash} ) models.db.session.add(profile) + for username in claim_names: + claim_name = models.ClaimName( + service_uuid=service_uuid, + profile=profile, + username=username + ) + models.db.session.add(claim_name) models.db.session.commit() diff --git a/hiboo/profile/login.py b/hiboo/profile/login.py index 48ea2e8..c4bc7da 100644 --- a/hiboo/profile/login.py +++ b/hiboo/profile/login.py @@ -84,9 +84,18 @@ def claim(service_uuid): service = models.Service.query.get(service_uuid) or flask.abort(404) form = forms.ClaimForm() if form.validate_on_submit(): - profile = models.Profile.query.filter_by( - service_uuid=service_uuid, username=form.username.data, - status = models.Profile.UNCLAIMED + claim_names = models.ClaimName.query.filter_by( + service_uuid=service_uuid, + username=form.username.data + ).options(models.sqlalchemy.orm.load_only("profile_uuid")).all() + claim_names_uuid = [claim_name.profile_uuid for claim_name in claim_names] + profile = models.Profile.query.filter( + models.Profile.service_uuid==service.uuid, + models.Profile.status==models.Profile.UNCLAIMED, + models.sqlalchemy.or_( + models.Profile.username==form.username.data, + models.Profile.uuid.in_(claim_names_uuid) + ) ).first() check = context.CryptContext([ scheme for scheme in dir(hash) if not scheme.startswith('__') @@ -95,6 +104,8 @@ def claim(service_uuid): profile.user = flask_login.current_user profile.status = models.Profile.ACTIVE del profile.extra["password"] + for claim_name in profile.claimnames: + models.db.session.delete(claim_name) models.db.session.add(profile) models.log( category=models.History.STATUS, diff --git a/migrations/versions/c5109b93fc0f_add_claim_names.py b/migrations/versions/c5109b93fc0f_add_claim_names.py new file mode 100644 index 0000000..f1820ae --- /dev/null +++ b/migrations/versions/c5109b93fc0f_add_claim_names.py @@ -0,0 +1,35 @@ +""" Add additional claim names + +Revision ID: c5109b93fc0f +Revises: 24626feb96c7 +Create Date: 2020-03-30 17:28:26.222118 +""" + +from alembic import op +import sqlalchemy as sa +import hiboo + + +revision = 'c5109b93fc0f' +down_revision = '24626feb96c7' +branch_labels = None +depends_on = None + + +def upgrade(): + op.create_table('claimname', + sa.Column('profile_uuid', sa.String(length=36), nullable=True), + sa.Column('service_uuid', sa.String(length=36), nullable=True), + sa.Column('username', sa.String(length=255), nullable=False), + sa.Column('uuid', sa.String(length=36), nullable=False), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.Column('updated_at', sa.DateTime(), nullable=True), + sa.Column('comment', sa.String(length=255), nullable=True), + sa.ForeignKeyConstraint(['profile_uuid'], ['profile.uuid'], ), + sa.ForeignKeyConstraint(['service_uuid'], ['service.uuid'], ), + sa.PrimaryKeyConstraint('uuid') + ) + + +def downgrade(): + op.drop_table('claimname') -- GitLab