Skip to content
Snippets Groups Projects
Commit 70f5f4ff authored by kaiyou's avatar kaiyou
Browse files

Support alternative claim names

parent 9fec2a18
No related branches found
No related tags found
No related merge requests found
...@@ -205,6 +205,20 @@ class Profile(db.Model): ...@@ -205,6 +205,20 @@ class Profile(db.Model):
).filter(cls.status.in_((cls.ACTIVE, cls.BLOCKED, cls.REQUEST))) ).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): class History(db.Model):
""" Records an even in an account's or profile's lifetime. """ Records an even in an account's or profile's lifetime.
""" """
......
...@@ -8,7 +8,8 @@ import click ...@@ -8,7 +8,8 @@ import click
@click.argument("service_uuid") @click.argument("service_uuid")
@click.argument("username") @click.argument("username")
@click.argument("password_hash") @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 models.Service.query.get(service_uuid)
assert not models.Profile.query.filter_by(service_uuid=service_uuid, username=username).first() assert not models.Profile.query.filter_by(service_uuid=service_uuid, username=username).first()
profile = models.Profile( profile = models.Profile(
...@@ -18,4 +19,11 @@ def unclaimed(service_uuid, username, password_hash): ...@@ -18,4 +19,11 @@ def unclaimed(service_uuid, username, password_hash):
extra={"password": password_hash} extra={"password": password_hash}
) )
models.db.session.add(profile) 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() models.db.session.commit()
...@@ -84,9 +84,18 @@ def claim(service_uuid): ...@@ -84,9 +84,18 @@ def claim(service_uuid):
service = models.Service.query.get(service_uuid) or flask.abort(404) service = models.Service.query.get(service_uuid) or flask.abort(404)
form = forms.ClaimForm() form = forms.ClaimForm()
if form.validate_on_submit(): if form.validate_on_submit():
profile = models.Profile.query.filter_by( claim_names = models.ClaimName.query.filter_by(
service_uuid=service_uuid, username=form.username.data, service_uuid=service_uuid,
status = models.Profile.UNCLAIMED 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() ).first()
check = context.CryptContext([ check = context.CryptContext([
scheme for scheme in dir(hash) if not scheme.startswith('__') scheme for scheme in dir(hash) if not scheme.startswith('__')
...@@ -95,6 +104,8 @@ def claim(service_uuid): ...@@ -95,6 +104,8 @@ def claim(service_uuid):
profile.user = flask_login.current_user profile.user = flask_login.current_user
profile.status = models.Profile.ACTIVE profile.status = models.Profile.ACTIVE
del profile.extra["password"] del profile.extra["password"]
for claim_name in profile.claimnames:
models.db.session.delete(claim_name)
models.db.session.add(profile) models.db.session.add(profile)
models.log( models.log(
category=models.History.STATUS, category=models.History.STATUS,
......
""" 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')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment