Skip to content

Allow unauthenticated all #708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/en/authentication-component.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,6 @@ applied during the ``Controller.initialize`` event instead::
]);

You can also disable identity checks entirely with the ``requireIdentity``
option.
option or by calling ``disableIdentityCheck`` from the controller's ``beforeFilter()`` method itself::

$this->Authentication->disableIdentityCheck();
11 changes: 11 additions & 0 deletions src/Controller/Component/AuthenticationComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ protected function doIdentityCheck(): void
}
}

/**
* Disables the identity check for this controller and as all its actions.
* They then don't require an authentication identity to be present.
Comment on lines +194 to +195
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Disables the identity check for this controller and as all its actions.
* They then don't require an authentication identity to be present.
* Disables identity checks for the current action.

*
* @return void
*/
public function disableIdentityCheck(): void
{
$this->setConfig('requireIdentity', false);
}

/**
* Set the list of actions that don't require an authentication identity to be present.
*
Expand Down
46 changes: 31 additions & 15 deletions tests/TestCase/Controller/Component/AuthenticationComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ class AuthenticationComponentTest extends TestCase
*/
protected $request;

/**
* @var \Cake\Http\Response
*/
protected $response;

/**
* @var \Authentication\AuthenticationService
*/
Expand Down Expand Up @@ -495,7 +490,7 @@ public function testUnauthenticatedActionsNoActionsFails()
}

/**
* test disabling requireidentity via settings
* test disabling requireIdentity via settings
*
* @return void
*/
Expand All @@ -516,6 +511,27 @@ public function testUnauthenticatedActionsDisabledOptions()
$this->assertTrue(true, 'No exception should be raised as require identity is off.');
}

/**
* test disabling requireIdentity via convenience method
*
* @return void
*/
public function testUnauthenticatedActionsDisabledOptionsCall()
{
$request = $this->request
->withParam('action', 'view')
->withAttribute('authentication', $this->service);

$controller = new Controller($request);
$controller->loadComponent('Authentication.Authentication');
$controller->Authentication->disableIdentityCheck();

// Mismatched actions would normally cause an error.
$controller->Authentication->allowUnauthenticated(['index', 'add']);
$controller->startupProcess();
$this->assertTrue(true, 'No exception should be raised as require identity is off.');
}

/**
* Test that the identity check can be run from callback for Controller.initialize
*
Expand Down Expand Up @@ -574,7 +590,7 @@ public function testImpersonate()
$request = $this->request
->withAttribute('identity', $identity)
->withAttribute('authentication', $this->service);
$controller = new Controller($request, $this->response);
$controller = new Controller($request);
$registry = new ComponentRegistry($controller);
$component = new AuthenticationComponent($registry);

Expand Down Expand Up @@ -605,7 +621,7 @@ public function testImpersonateDecoratorIgnored()
$request = $this->request
->withAttribute('identity', $identity)
->withAttribute('authentication', $this->service);
$controller = new Controller($request, $this->response);
$controller = new Controller($request);
$registry = new ComponentRegistry($controller);
$component = new AuthenticationComponent($registry);

Expand All @@ -630,7 +646,7 @@ public function testImpersonateNoIdentity()
$impersonated = new ArrayObject(['username' => 'larry']);
$request = $this->request
->withAttribute('authentication', $this->service);
$controller = new Controller($request, $this->response);
$controller = new Controller($request);
$registry = new ComponentRegistry($controller);
$component = new AuthenticationComponent($registry);
$this->expectException(UnauthenticatedException::class);
Expand Down Expand Up @@ -659,7 +675,7 @@ public function testImpersonateFailure()
$request = $this->request
->withAttribute('identity', $identity)
->withAttribute('authentication', $service);
$controller = new Controller($request, $this->response);
$controller = new Controller($request);
$registry = new ComponentRegistry($controller);
$component = new AuthenticationComponent($registry);
$this->expectException(UnexpectedValueException::class);
Expand All @@ -680,7 +696,7 @@ public function testStopImpersonating()
$this->request->getSession()->write('AuthImpersonate', $impersonator);
$this->service->authenticate($this->request);
$request = $this->request->withAttribute('authentication', $this->service);
$controller = new Controller($request, $this->response);
$controller = new Controller($request);
$registry = new ComponentRegistry($controller);
$component = new AuthenticationComponent($registry);
$this->assertEquals($impersonator, $controller->getRequest()->getSession()->read('AuthImpersonate'));
Expand Down Expand Up @@ -710,7 +726,7 @@ public function testStopImpersonatingFailure()
$request = $this->request
->withAttribute('identity', $identity)
->withAttribute('authentication', $service);
$controller = new Controller($request, $this->response);
$controller = new Controller($request);
$registry = new ComponentRegistry($controller);
$component = new AuthenticationComponent($registry);
$this->expectException(UnexpectedValueException::class);
Expand All @@ -733,7 +749,7 @@ public function testIsImpersonating()
$request = $this->request
->withAttribute('authentication', $this->service)
->withAttribute('identity', new Identity($impersonated));
$controller = new Controller($request, $this->response);
$controller = new Controller($request);
$registry = new ComponentRegistry($controller);
$component = new AuthenticationComponent($registry);

Expand All @@ -757,7 +773,7 @@ public function testGetImpersonationAuthenticationServiceFailure()
$request = $this->request
->withAttribute('authentication', $service)
->withAttribute('identity', new Identity($user));
$controller = new Controller($request, $this->response);
$controller = new Controller($request);
$registry = new ComponentRegistry($controller);
$component = new AuthenticationComponent($registry);

Expand All @@ -778,7 +794,7 @@ public function testIsImpersonatingNotImpersonating()
$this->request->getSession()->write('Auth', $user);
$this->service->authenticate($this->request);
$request = $this->request->withAttribute('authentication', $this->service);
$controller = new Controller($request, $this->response);
$controller = new Controller($request);
$registry = new ComponentRegistry($controller);
$component = new AuthenticationComponent($registry);

Expand Down