From c0a7687c46d4948b873e00bc552c375206321c60 Mon Sep 17 00:00:00 2001 From: mscherer Date: Fri, 18 Apr 2025 15:48:02 +0200 Subject: [PATCH 1/2] Add allowUnauthenticatedAll() convenience wrapper --- .../Component/AuthenticationComponent.php | 17 +++++++++++++++ .../Component/AuthenticationComponentTest.php | 19 +++++++++++++++++ .../TestApp/Controller/MyController.php | 21 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 tests/test_app/TestApp/Controller/MyController.php diff --git a/src/Controller/Component/AuthenticationComponent.php b/src/Controller/Component/AuthenticationComponent.php index d9ab5114..bb0e4785 100644 --- a/src/Controller/Component/AuthenticationComponent.php +++ b/src/Controller/Component/AuthenticationComponent.php @@ -190,6 +190,23 @@ protected function doIdentityCheck(): void } } + /** + * Set the list of actions that don't require an authentication identity to be present. + * + * @return $this + */ + public function allowUnauthenticatedAll() + { + $controller = $this->getController(); + $methods = get_class_methods($controller); + $baseMethods = get_class_methods(get_parent_class($controller)); + + $actions = array_diff($methods, $baseMethods); + $this->unauthenticatedActions = array_values($actions); + + return $this; + } + /** * Set the list of actions that don't require an authentication identity to be present. * diff --git a/tests/TestCase/Controller/Component/AuthenticationComponentTest.php b/tests/TestCase/Controller/Component/AuthenticationComponentTest.php index 51cf7573..b3a52fb8 100644 --- a/tests/TestCase/Controller/Component/AuthenticationComponentTest.php +++ b/tests/TestCase/Controller/Component/AuthenticationComponentTest.php @@ -33,6 +33,7 @@ use Cake\ORM\Entity; use InvalidArgumentException; use TestApp\Authentication\InvalidAuthenticationService; +use TestApp\Controller\MyController; use UnexpectedValueException; /** @@ -436,6 +437,24 @@ public function testUnauthenticatedActions() ); } + /** + * test unauthenticated actions methods + * + * @return void + */ + public function testUnauthenticatedAll() + { + $request = $this->request + ->withParam('action', 'view') + ->withAttribute('authentication', $this->service); + + $controller = new MyController($request); + $controller->loadComponent('Authentication.Authentication'); + + $controller->Authentication->allowUnauthenticatedAll(); + $this->assertSame(['index', 'fooBar'], $controller->Authentication->getUnauthenticatedActions()); + } + /** * test unauthenticated actions ok * diff --git a/tests/test_app/TestApp/Controller/MyController.php b/tests/test_app/TestApp/Controller/MyController.php new file mode 100644 index 00000000..23528b21 --- /dev/null +++ b/tests/test_app/TestApp/Controller/MyController.php @@ -0,0 +1,21 @@ + Date: Fri, 18 Apr 2025 15:51:45 +0200 Subject: [PATCH 2/2] Fix up PHPStan. --- src/Controller/Component/AuthenticationComponent.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Controller/Component/AuthenticationComponent.php b/src/Controller/Component/AuthenticationComponent.php index bb0e4785..cf7c1931 100644 --- a/src/Controller/Component/AuthenticationComponent.php +++ b/src/Controller/Component/AuthenticationComponent.php @@ -199,7 +199,9 @@ public function allowUnauthenticatedAll() { $controller = $this->getController(); $methods = get_class_methods($controller); - $baseMethods = get_class_methods(get_parent_class($controller)); + /** @var class-string<\Cake\Controller\Controller> $parentClass */ + $parentClass = get_parent_class($controller); + $baseMethods = get_class_methods($parentClass); $actions = array_diff($methods, $baseMethods); $this->unauthenticatedActions = array_values($actions);