vincbeck commented on code in PR #77311:
URL: https://github.com/apache/airflow/pull/77311#discussion_r4630439654


##########
providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py:
##########
@@ -645,32 +645,36 @@ def get_authorized_dag_ids(
             # If user is authorized to access all DAGs, return all DAGs
             return {dag.dag_id for dag in 
session.execute(select(DagModel.dag_id))}
         if isinstance(user, AnonymousUser):

Review Comment:
   Why not use `_get_user_permissions(user)` for every user? `_is_authorized` 
just above already reads the cached `user.perms`, and so does 
`is_authorized_dag`. It would also remove the `AnonymousUser` branch and the DB 
query.



##########
providers/fab/tests/unit/fab/auth_manager/test_fab_auth_manager.py:
##########
@@ -1089,6 +1089,46 @@ def test_get_authorized_dag_ids(
 
         delete_user(flask_app, "username")
 
+    @pytest.mark.parametrize(
+        ("method", "role_permissions", "expected_results"),
+        [
+            pytest.param("GET", [(ACTION_CAN_READ, "DAG:test_dag1")], 
{"test_dag1"}, id="read-one-dag"),
+            pytest.param(
+                "PUT",
+                [(ACTION_CAN_EDIT, "DAG:test_dag1"), (ACTION_CAN_EDIT, 
"DAG:test_dag2")],
+                {"test_dag1", "test_dag2"},
+                id="edit-two-dags",
+            ),
+        ],
+    )
+    def test_get_authorized_dag_ids_for_public_user(
+        self, method, role_permissions, expected_results, 
auth_manager_with_appbuilder, flask_app, dag_maker
+    ):
+        for dag_id in ("test_dag1", "test_dag2", "test_dag3"):
+            with dag_maker(dag_id):
+                EmptyOperator(task_id="task")
+            if AIRFLOW_V_3_1_PLUS:
+                sync_dag_to_db(dag_maker.dag)
+        dag_maker.session.commit()
+        dag_maker.session.close()
+
+        security_manager = auth_manager_with_appbuilder.security_manager
+        security_manager.sync_perm_for_dag("test_dag1")
+        security_manager.sync_perm_for_dag("test_dag2")
+        delete_role(flask_app, "public_test")
+        create_role(flask_app, "public_test", role_permissions)
+
+        previous = flask_app.config.get("AUTH_ROLE_PUBLIC")
+        flask_app.config["AUTH_ROLE_PUBLIC"] = "public_test"
+        try:
+            user = auth_manager_with_appbuilder.build_public_user()
+            results = 
auth_manager_with_appbuilder.get_authorized_dag_ids(user=user, method=method)
+        finally:
+            flask_app.config["AUTH_ROLE_PUBLIC"] = previous
+            delete_role(flask_app, "public_test")

Review Comment:
   nit: you can use `conf_vars` with `[fab] auth_role_public` instead of 
changing the Flask config, that's the main way to set it
   
   ```suggestion
           try:
               with conf_vars({("fab", "auth_role_public"): "public_test"}):
                   user = auth_manager_with_appbuilder.build_public_user()
                   results = 
auth_manager_with_appbuilder.get_authorized_dag_ids(user=user, method=method)
           finally:
               delete_role(flask_app, "public_test")
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to