Skip to content

Commit

Permalink
chore(security): Make get_database_perm/get_dataset_perm return optional
Browse files Browse the repository at this point in the history
  • Loading branch information
john-bodley committed Oct 31, 2023
1 parent 5659c87 commit 4392911
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions superset/security/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,15 @@ def get_schema_perm(
return f"[{database}].[{schema}]" if schema else None

@staticmethod
def get_database_perm(database_id: int, database_name: str) -> str:
def get_database_perm(database_id: int, database_name: str) -> Optional[str]:
return f"[{database_name}].(id:{database_id})"

@staticmethod
def get_dataset_perm(dataset_id: int, dataset_name: str, database_name: str) -> str:
def get_dataset_perm(
dataset_id: int,
dataset_name: str,
database_name: str,
) -> Optional[str]:
return f"[{database_name}].[{dataset_name}](id:{dataset_id})"

def unpack_database_and_schema(self, schema_permission: str) -> DatabaseAndSchema:
Expand Down Expand Up @@ -1168,6 +1172,8 @@ def _update_vm_database_access(
.where(view_menu_table.c.id == db_pvm.view_menu_id)
.values(name=new_view_menu_name)
)
if not new_view_menu_name:
return None

Check warning on line 1176 in superset/security/manager.py

View check run for this annotation

Codecov / codecov/patch

superset/security/manager.py#L1176

Added line #L1176 was not covered by tests
new_db_view_menu = self._find_view_menu_on_sqla_event(
connection, new_view_menu_name
)
Expand Down Expand Up @@ -1223,10 +1229,6 @@ def _update_vm_datasources_access( # pylint: disable=too-many-locals
.where(view_menu_table.c.name == old_dataset_vm_name)
.values(name=new_dataset_vm_name)
)
# After update refresh
new_dataset_view_menu = self._find_view_menu_on_sqla_event(
connection, new_dataset_vm_name
)

# Update dataset (SqlaTable perm field)
connection.execute(
Expand All @@ -1243,8 +1245,18 @@ def _update_vm_datasources_access( # pylint: disable=too-many-locals
.where(chart_table.c.perm == old_dataset_vm_name)
.values(perm=new_dataset_vm_name)
)
self.on_view_menu_after_update(mapper, connection, new_dataset_view_menu)
updated_view_menus.append(new_dataset_view_menu)
if new_dataset_vm_name:
# After update refresh
new_dataset_view_menu = self._find_view_menu_on_sqla_event(
connection,
new_dataset_vm_name,
)
self.on_view_menu_after_update(
mapper,
connection,
new_dataset_view_menu,
)
updated_view_menus.append(new_dataset_view_menu)
return updated_view_menus

def dataset_after_insert(
Expand All @@ -1270,7 +1282,7 @@ def dataset_after_insert(
)

try:
dataset_perm = target.get_perm()
dataset_perm: Optional[str] = target.get_perm()
database = target.database
except DatasetInvalidPermissionEvaluationException:
logger.warning(
Expand Down

0 comments on commit 4392911

Please sign in to comment.