-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2101 from auslin-aot/sync/7-6-2024-develop-to-per…
…mission-matrix 🔗[Sync] Develop to permission matrix
- Loading branch information
Showing
24 changed files
with
344 additions
and
31 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
forms-flow-api/migrations/versions/77d8b68e6c1f_users_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
"""Users table | ||
Revision ID: 77d8b68e6c1f | ||
Revises: f1599a5bd658 | ||
Create Date: 2024-05-30 16:01:37.273907 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '77d8b68e6c1f' | ||
down_revision = 'f1599a5bd658' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('user', | ||
sa.Column('id', sa.Integer(), nullable=False), | ||
sa.Column('user_name', sa.String(length=50), nullable=False), | ||
sa.Column('default_filter', sa.Integer(), nullable=True), | ||
sa.Column('locale', sa.String(), nullable=True, comment='language code'), | ||
sa.Column('tenant', sa.String(), nullable=True, comment='tenant key'), | ||
sa.Column('created', sa.DateTime(), nullable=False), | ||
sa.Column('modified', sa.DateTime(), nullable=True), | ||
sa.Column('created_by', sa.String(), nullable=False), | ||
sa.Column('modified_by', sa.String(), nullable=True), | ||
sa.ForeignKeyConstraint(['default_filter'], ['filter.id'], ondelete='SET NULL'), | ||
sa.PrimaryKeyConstraint('id'), | ||
sa.UniqueConstraint('user_name', 'tenant', name='uq_tenant_user_name') | ||
) | ||
op.alter_column('themes', 'logo_type', | ||
existing_type=sa.VARCHAR(length=100), | ||
type_=sa.String(length=50), | ||
existing_nullable=False) | ||
op.alter_column('themes', 'logo_data', | ||
existing_type=sa.VARCHAR(), | ||
comment='logo_data contain a base64 or a URL.', | ||
existing_nullable=False) | ||
op.alter_column('themes', 'theme', | ||
existing_type=postgresql.JSON(astext_type=sa.Text()), | ||
comment='Json data', | ||
existing_nullable=False) | ||
op.create_unique_constraint('uq_tenant', 'themes', ['tenant']) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_constraint('uq_tenant', 'themes', type_='unique') | ||
op.alter_column('themes', 'theme', | ||
existing_type=postgresql.JSON(astext_type=sa.Text()), | ||
comment=None, | ||
existing_comment='Json data', | ||
existing_nullable=False) | ||
op.alter_column('themes', 'logo_data', | ||
existing_type=sa.VARCHAR(), | ||
comment=None, | ||
existing_comment='logo_data contain a base64 or a URL.', | ||
existing_nullable=False) | ||
op.alter_column('themes', 'logo_type', | ||
existing_type=sa.String(length=50), | ||
type_=sa.VARCHAR(length=100), | ||
existing_nullable=False) | ||
op.drop_table('user') | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"""This manages User Database Models.""" | ||
|
||
from flask_sqlalchemy.query import Query | ||
from formsflow_api_utils.utils.user_context import UserContext, user_context | ||
from sqlalchemy import UniqueConstraint | ||
|
||
from .audit_mixin import AuditDateTimeMixin, AuditUserMixin | ||
from .base_model import BaseModel | ||
from .db import db | ||
|
||
|
||
class User(AuditDateTimeMixin, AuditUserMixin, BaseModel, db.Model): | ||
"""This class manages user information.""" | ||
|
||
id = db.Column(db.Integer, primary_key=True) | ||
user_name = db.Column(db.String(50), nullable=False) | ||
default_filter = db.Column( | ||
db.Integer, db.ForeignKey("filter.id", ondelete="SET NULL"), nullable=True | ||
) | ||
locale = db.Column(db.String(), nullable=True, comment="language code") | ||
tenant = db.Column(db.String(), nullable=True, comment="tenant key") | ||
__table_args__ = ( | ||
UniqueConstraint("user_name", "tenant", name="uq_tenant_user_name"), | ||
) | ||
|
||
@classmethod | ||
def create_user(cls, user_data: dict): | ||
"""Create new user.""" | ||
assert user_data is not None | ||
user = cls() | ||
user.created_by = user_data.get("created_by") | ||
user.user_name = user_data.get("user_name") | ||
user.locale = user_data.get("locale") | ||
user.tenant = user_data.get("tenant") | ||
user.default_filter = user_data.get("default_filter") | ||
user.save() | ||
return user | ||
|
||
def update(self, user_data: dict): | ||
"""Update user data.""" | ||
self.update_from_dict( | ||
[ | ||
"locale", | ||
"tenant", | ||
"default_filter", | ||
], | ||
user_data, | ||
) | ||
self.commit() | ||
|
||
@classmethod | ||
@user_context | ||
def tenant_authorization(cls, query: Query, **kwargs): | ||
"""Modifies the query to include tenant check if needed.""" | ||
tenant_auth_query: Query = query | ||
user: UserContext = kwargs["user"] | ||
tenant_key: str = user.tenant_key | ||
if not isinstance(query, Query): | ||
raise TypeError("Query object must be of type Query") | ||
if tenant_key is not None: | ||
tenant_auth_query = tenant_auth_query.filter(cls.tenant == tenant_key) | ||
return tenant_auth_query | ||
|
||
@classmethod | ||
def get_user_by_user_name(cls, user_name: str = None): | ||
"""Find user data by username.""" | ||
assert user_name is not None | ||
query = cls.query.filter(cls.user_name == user_name) | ||
query = cls.tenant_authorization(query) | ||
return query.one_or_none() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.