diff --git a/forms-flow-api/src/formsflow_api/models/filter.py b/forms-flow-api/src/formsflow_api/models/filter.py index f54876c383..01406ea4f5 100644 --- a/forms-flow-api/src/formsflow_api/models/filter.py +++ b/forms-flow-api/src/formsflow_api/models/filter.py @@ -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.""" diff --git a/forms-flow-api/src/formsflow_api/services/filter.py b/forms-flow-api/src/formsflow_api/services/filter.py index fc7e140778..9cbca647b0 100644 --- a/forms-flow-api/src/formsflow_api/services/filter.py +++ b/forms-flow-api/src/formsflow_api/services/filter.py @@ -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 @@ -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) @@ -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: @@ -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)