forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 0
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 apache#11 from ASUS-AICS/aics_privacy_control_tabl…
…e_perm_autorevoke Aics privacy control table perm autorevoke
- Loading branch information
Showing
9 changed files
with
474 additions
and
51 deletions.
There are no files selected for viewing
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
67 changes: 67 additions & 0 deletions
67
superset/migrations/versions/9847d62ead51_add_aics_table_permission_control_model.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,67 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
"""Add AICS table permission control model | ||
Revision ID: 9847d62ead51 | ||
Revises: 5ad4d4f30245 | ||
Create Date: 2020-05-06 15:23:35.200160 | ||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '9847d62ead51' | ||
down_revision = '5ad4d4f30245' | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('aics_table_permission', | ||
sa.Column('created_on', sa.DateTime(), nullable=True), | ||
sa.Column('changed_on', sa.DateTime(), nullable=True), | ||
sa.Column('id', sa.Integer(), nullable=False), | ||
sa.Column('user_id', sa.Integer(), nullable=True), | ||
sa.Column('apply_date', sa.Date(), nullable=False), | ||
sa.Column('expire_date', sa.Date(), nullable=False), | ||
sa.Column('force_terminate_date', sa.DateTime(), nullable=True), | ||
sa.Column('is_active', sa.Boolean()), | ||
sa.Column('created_by_fk', sa.Integer(), nullable=True), | ||
sa.Column('changed_by_fk', sa.Integer(), nullable=True), | ||
sa.ForeignKeyConstraint(['changed_by_fk'], ['ab_user.id'], ), | ||
sa.ForeignKeyConstraint(['created_by_fk'], ['ab_user.id'], ), | ||
sa.ForeignKeyConstraint(['user_id'], ['ab_user.id'], ), | ||
sa.PrimaryKeyConstraint('id') | ||
) | ||
op.create_table('aics_tableperm_permissionview', | ||
sa.Column('id', sa.Integer(), nullable=False), | ||
sa.Column('tableperm_id', sa.Integer(), nullable=True), | ||
sa.Column('permissionview_id', sa.Integer(), nullable=True), | ||
sa.ForeignKeyConstraint(['permissionview_id'], ['ab_permission_view.id'], ), | ||
sa.ForeignKeyConstraint(['tableperm_id'], ['aics_table_permission.id'], ), | ||
sa.PrimaryKeyConstraint('id'), | ||
sa.UniqueConstraint('tableperm_id', 'permissionview_id') | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table('aics_tableperm_permissionview') | ||
op.drop_table('aics_table_permission') | ||
# ### 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
import re | ||
from datetime import datetime, timedelta | ||
|
||
from flask_appbuilder import Model | ||
from sqlalchemy import ( | ||
Table, | ||
Column, | ||
ForeignKey, | ||
Integer, | ||
Date, | ||
DateTime, | ||
Boolean, | ||
Sequence, | ||
UniqueConstraint, | ||
) | ||
from sqlalchemy.orm import relationship | ||
|
||
from superset import security_manager | ||
from superset.models.helpers import AuditMixinNullable | ||
|
||
# Table for many-to-many relation between users and tables | ||
# Ref: https://docs.sqlalchemy.org/en/13/orm/basic_relationships.html#relationships-many-to-many | ||
assoc_tableperm_permissionview = Table( | ||
"aics_tableperm_permissionview", | ||
Model.metadata, | ||
Column('id', Integer, Sequence("aics_tableperm_permissionview_id_seq"), primary_key=True) , | ||
Column('tableperm_id', Integer, ForeignKey("aics_table_permission.id")), | ||
Column('permissionview_id', Integer, ForeignKey("ab_permission_view.id")), | ||
UniqueConstraint("tableperm_id", "permissionview_id"), | ||
) | ||
|
||
class TablePermission(Model, AuditMixinNullable): | ||
""" | ||
Customized table permission control table | ||
Superset 0.35 leverage Flask AppBuilder Role/Permission for permission control | ||
However, it makes permission auto-revocation impossible. | ||
So we build this permission control table and with auto-revocation mechanism | ||
""" | ||
|
||
__tablename__ = "aics_table_permission" | ||
id = Column(Integer, Sequence("aics_table_permission_id_seq"), primary_key=True) # pylint: disable=invalid-name | ||
user_id = Column(Integer, ForeignKey("ab_user.id")) | ||
user = relationship( | ||
security_manager.user_model, backref="table_permissions", foreign_keys=[user_id] | ||
) | ||
|
||
apply_date = Column(Date, nullable=False, default=datetime.now().date()) | ||
expire_date = Column(Date, nullable=False, default=datetime.now().date()+timedelta(days=6*365/12)) | ||
force_terminate_date = Column(DateTime) | ||
is_active = Column(Boolean, default=True) | ||
|
||
table_permissions = relationship( | ||
security_manager.permissionview_model, secondary=assoc_tableperm_permissionview, backref="table_perm" | ||
) | ||
|
||
# Following properties are used for display fields that are not expected to be changed | ||
@property | ||
def username_detail(self): | ||
return f'{self.user.get_full_name()} ({self.user.username})' | ||
|
||
@username_detail.setter | ||
def username_detail(self, value): | ||
pass | ||
|
||
@property | ||
def exp_or_terminate_date(self): | ||
if self.force_terminate_date != None: | ||
return f'{str(self.force_terminate_date.date())}(T)' | ||
return self.expire_date | ||
|
||
@exp_or_terminate_date.setter | ||
def exp_or_terminate_date(self, value): | ||
pass | ||
|
||
@property | ||
def table_permission_list(self): | ||
table_perm = [str(perm.view_menu) for perm in self.table_permissions] | ||
return ', '.join(table_perm) | ||
|
||
@table_permission_list.setter | ||
def table_permission_list(self, value): | ||
pass | ||
|
||
@property | ||
def status(self): | ||
if self.is_active: | ||
return 'Active' | ||
elif self.force_terminate_date != None: | ||
return 'Force Revoked' | ||
else: | ||
return 'Expired' | ||
|
||
@status.setter | ||
def status(self, value): | ||
pass |
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.