:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat: add back feature flag check for tasks access checks (#57957)
Jonathan Mieloofdc820d70a70
diff --git a/products/tasks/backend/access.py b/products/tasks/backend/access.pynew file mode 100644index 000000000000..cdfe0a3c4e7f--- /dev/null+++ b/products/tasks/backend/access.py@@ -0,0 +1,34 @@+import posthoganalytics++from posthog.models.user import User++from .models import CodeInviteRedemption+++def _is_tasks_flag_enabled(user: User) -> bool:+ if not user.distinct_id:+ return False+ org = getattr(user, "organization", None)+ kwargs: dict = {+ "only_evaluate_locally": False,+ "send_feature_flag_events": False,+ }+ if org is not None:+ # The `tasks` flag's release conditions are mostly person-level (email allowlist + domain),+ # but maybe we want org level conditions later on.+ org_id = str(org.id)+ kwargs["groups"] = {"organization": org_id}+ kwargs["group_properties"] = {"organization": {"id": org_id}}+ return bool(posthoganalytics.feature_enabled("tasks", user.distinct_id, **kwargs))+++def has_tasks_access(user: User) -> bool:+ """+ User has access to PostHog Code if the `tasks` feature flag is enabled for them+ OR they have redeemed an invite code.+ """+ if not user or not user.is_authenticated:+ return False+ if _is_tasks_flag_enabled(user):+ return True+ return CodeInviteRedemption.objects.filter(user=user).exists()diff --git a/products/tasks/backend/api.py b/products/tasks/backend/api.pyindex bfdca2a447c0..e013be4555e5 100644--- a/products/tasks/backend/api.py+++ b/products/tasks/backend/api.py@@ -41,6 +41,7 @@ from posthog.storage import object_storagefrom ee.hogai.utils.aio import async_to_sync+from .access import has_tasks_accessfrom .automation_service import (delete_automation_schedule,run_task_automation,@@ -216,25 +217,7 @@ class TasksAccessPermission(BasePermission):message = "You need a valid invite code to access this feature."def has_permission(self, request, view) -> bool:- user = request.user- if not user or not user.is_authenticated:- return False-- # Check 1: feature flag (covers existing enrolled users, staff overrides)- org_id = str(view.organization.id)- flag_enabled = posthoganalytics.feature_enabled(- "tasks",- user.distinct_id,- groups={"organization": org_id},- group_properties={"organization": {"id": org_id}},- only_evaluate_locally=False,- send_feature_flag_events=False,- )- if flag_enabled:- return True-- # Check 2: invite code redemption (covers new invited users)- return CodeInviteRedemption.objects.filter(user=user).exists()+ return has_tasks_access(request.user)@extend_schema(tags=["tasks"])@@ -897,22 +880,6 @@ class TaskRunViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):queryset = TaskRun.objects.select_related("task", "task__created_by", "task__github_integration", "task__github_user_integration").all()- posthog_feature_flag = {- "tasks": [- "list",- "retrieve",- "create",- "update",- "partial_update",- "set_output",- "append_log",- "relay_message",- "session_logs",- "command",- "stream",- "resume_in_cloud",- ]- }http_method_names = ["get", "post", "patch", "head", "options"]filter_rewrite_rules = {"team_id": "team_id"}@@ -2544,8 +2511,7 @@ class CodeInviteViewSet(viewsets.ViewSet):)@action(detail=False, methods=["get"], url_path="check-access")def check_access(self, request, **kwargs):- has_redeemed = CodeInviteRedemption.objects.filter(user=request.user).exists()- return Response({"has_access": has_redeemed})+ return Response({"has_access": has_tasks_access(request.user)})@extend_schema(tags=["sandbox-environments"])diff --git a/products/tasks/backend/tests/test_api.py b/products/tasks/backend/tests/test_api.pyindex 6ce16e36693e..e0f62c8e7dd3 100644--- a/products/tasks/backend/tests/test_api.py+++ b/products/tasks/backend/tests/test_api.py@@ -4842,6 +4842,29 @@ class TestTasksAPIPermissions(BaseTaskAPITest):response = self.client.get("/api/projects/@current/tasks/")self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)+ def test_check_access_flag_on_no_redemption(self):+ self.set_tasks_feature_flag(True)++ response = self.client.get("/api/code/invites/check-access/")+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertTrue(response.json()["has_access"])++ def test_check_access_flag_off_no_redemption(self):+ self.set_tasks_feature_flag(False)++ response = self.client.get("/api/code/invites/check-access/")+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertFalse(response.json()["has_access"])++ def test_check_access_flag_off_with_redemption(self):+ self.set_tasks_feature_flag(False)+ invite = CodeInvite.objects.create(code="ACCESSCODE", max_redemptions=0, is_active=True)+ CodeInviteRedemption.objects.create(invite_code=invite, user=self.user)++ response = self.client.get("/api/code/invites/check-access/")+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertTrue(response.json()["has_access"])+def test_authentication_required(self):task = self.create_task()automation = self.create_automation(name="Daily PRs", prompt="Check my PRs")