Skip to content

Commit 88da164

Browse files
authored
Merge pull request #708 from cakephp/allow-unauthenticated-All
Allow unauthenticated all
2 parents 75ad8e3 + 7bbc909 commit 88da164

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

docs/en/authentication-component.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,6 @@ applied during the ``Controller.initialize`` event instead::
103103
]);
104104

105105
You can also disable identity checks entirely with the ``requireIdentity``
106-
option.
106+
option or by calling ``disableIdentityCheck`` from the controller's ``beforeFilter()`` method itself::
107+
108+
$this->Authentication->disableIdentityCheck();

src/Controller/Component/AuthenticationComponent.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,17 @@ protected function doIdentityCheck(): void
190190
}
191191
}
192192

193+
/**
194+
* Disables the identity check for this controller and as all its actions.
195+
* They then don't require an authentication identity to be present.
196+
*
197+
* @return void
198+
*/
199+
public function disableIdentityCheck(): void
200+
{
201+
$this->setConfig('requireIdentity', false);
202+
}
203+
193204
/**
194205
* Set the list of actions that don't require an authentication identity to be present.
195206
*

tests/TestCase/Controller/Component/AuthenticationComponentTest.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ public function testUnauthenticatedActionsNoActionsFails()
490490
}
491491

492492
/**
493-
* test disabling requireidentity via settings
493+
* test disabling requireIdentity via settings
494494
*
495495
* @return void
496496
*/
@@ -511,6 +511,27 @@ public function testUnauthenticatedActionsDisabledOptions()
511511
$this->assertTrue(true, 'No exception should be raised as require identity is off.');
512512
}
513513

514+
/**
515+
* test disabling requireIdentity via convenience method
516+
*
517+
* @return void
518+
*/
519+
public function testUnauthenticatedActionsDisabledOptionsCall()
520+
{
521+
$request = $this->request
522+
->withParam('action', 'view')
523+
->withAttribute('authentication', $this->service);
524+
525+
$controller = new Controller($request);
526+
$controller->loadComponent('Authentication.Authentication');
527+
$controller->Authentication->disableIdentityCheck();
528+
529+
// Mismatched actions would normally cause an error.
530+
$controller->Authentication->allowUnauthenticated(['index', 'add']);
531+
$controller->startupProcess();
532+
$this->assertTrue(true, 'No exception should be raised as require identity is off.');
533+
}
534+
514535
/**
515536
* Test that the identity check can be run from callback for Controller.initialize
516537
*

0 commit comments

Comments
 (0)