Skip to content

Add allowUnauthenticatedAll() convenience wrapper #706

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

Closed
wants to merge 2 commits into from
Closed
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
19 changes: 19 additions & 0 deletions src/Controller/Component/AuthenticationComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,25 @@ 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);
/** @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);

return $this;
}

/**
* Set the list of actions that don't require an authentication identity to be present.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Cake\ORM\Entity;
use InvalidArgumentException;
use TestApp\Authentication\InvalidAuthenticationService;
use TestApp\Controller\MyController;
use UnexpectedValueException;

/**
Expand Down Expand Up @@ -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
*
Expand Down
21 changes: 21 additions & 0 deletions tests/test_app/TestApp/Controller/MyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);

namespace TestApp\Controller;

use Cake\Controller\Controller;

class MyController extends Controller
{
public function index()
{
}

public function fooBar()
{
}

protected function notMe()
{
}
}