Skip to content

Commit

Permalink
multienant changes to All Tasks filter (AOT-Technologies#1751)
Browse files Browse the repository at this point in the history
In a multi-tenant setup, check for the 'All Tasks' filter. If it's not present for the specific tenant, create it, ensuring the tenant-specific 'All Tasks' filter remains undeleted by the admin.
  • Loading branch information
auslin-aot authored Nov 21, 2023
1 parent 34e65c0 commit 16dd18d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
8 changes: 8 additions & 0 deletions forms-flow-api/src/formsflow_api/models/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ def find_all_active_filters(cls, tenant: str = None) -> List[Filter]:
query = query.filter(Filter.tenant == tenant)
return query.all()

@classmethod
def find_all_filters(cls, tenant: str = None) -> List[Filter]:
"""Find all filters."""
query = cls.query
if tenant:
query = query.filter(Filter.tenant == tenant)
return query.all()

@classmethod
def create_filter_from_dict(cls, filter_data: dict) -> Filter:
"""Create Filter."""
Expand Down
56 changes: 53 additions & 3 deletions forms-flow-api/src/formsflow_api/services/filter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""This exposes filter service."""

from flask import current_app
from formsflow_api_utils.exceptions import BusinessException
from formsflow_api_utils.utils import ADMIN_GROUP
from formsflow_api_utils.utils.user_context import UserContext, user_context
Expand Down Expand Up @@ -49,10 +50,51 @@ def create_filter(filter_payload, **kwargs):
def get_user_filters(**kwargs):
"""Get filters for the user."""
user: UserContext = kwargs["user"]
tenant_key = user.tenant_key
# The migration script creates a default 'All Tasks' filter that is not specific to any tenant.
# In a multi-tenant environment, verify the existence of the 'All Tasks' filter.

# If the 'All Tasks' filter does not exist for the specific tenant, create it,
# ensuring that the tenant-specific 'All Tasks' filter is not deleted by the tenant admin.
if current_app.config.get("MULTI_TENANCY_ENABLED"):
all_filters = Filter.find_all_filters()
all_tasks_filter = any(
(item.name.lower() == "all tasks" and item.status == "active")
or (item.name.lower() == "all tasks" and item.tenant == tenant_key)
for item in all_filters
)

if not all_tasks_filter:
filter_obj = Filter(
name="All Tasks",
variables=[
{"name": "applicationId", "label": "Application Id"},
{"name": "formName", "label": "Form Name"},
],
status="active",
created_by="system",
created="now()",
criteria={},
users={},
roles={},
tenant=tenant_key,
task_visible_attributes={
"applicationId": True,
"dueDate": True,
"priority": True,
"assignee": True,
"taskTitle": True,
"createdDate": True,
"groups": True,
"followupDate": True,
},
)
filter_obj.save()

filters = Filter.find_user_filters(
roles=user.group_or_roles,
user=user.user_name,
tenant=user.tenant_key,
tenant=tenant_key,
admin=ADMIN_GROUP in user.roles,
)
filter_data = filter_schema.dump(filters, many=True)
Expand Down Expand Up @@ -101,7 +143,11 @@ def mark_inactive(filter_id, **kwargs):
admin=ADMIN_GROUP in user.roles,
)
if filter_result:
if tenant_key is not None and filter_result.tenant != tenant_key:
if (
tenant_key is not None
and filter_result.tenant != tenant_key
and filter_result.tenant is not None
):
raise PermissionError("Tenant authentication failed.")
filter_result.mark_inactive()
else:
Expand All @@ -121,7 +167,11 @@ def update_filter(filter_id, filter_data, **kwargs):
)

if filter_result:
if tenant_key is not None and filter_result.tenant != tenant_key:
if (
tenant_key is not None
and filter_result.tenant != tenant_key
and filter_result.tenant is not None
):
raise PermissionError("Tenant authentication failed.")
filter_data = FilterService.update_payload(filter_data)
filter_result.update(filter_data)
Expand Down

0 comments on commit 16dd18d

Please sign in to comment.