Skip to content
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

10.next cake5 Add the ability to use a callback in bypassAuth in permissions #96

Open
wants to merge 13 commits into
base: 10.next-cake5
Choose a base branch
from
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ jobs:
prefer-lowest: ['']

steps:
- name: Setup MySQL latest
- name: Setup MySQL 8.0
if: matrix.db-type == 'mysql'
run: docker run --rm --name=mysqld -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=cakephp -p 3306:3306 -d mysql --default-authentication-plugin=mysql_native_password --disable-log-bin
run: |
sudo service mysql start
mysql -h 127.0.0.1 -u root -proot -e 'CREATE DATABASE cakephp;'

- name: Setup PostgreSQL latest
if: matrix.db-type == 'pgsql'
Expand Down
6 changes: 5 additions & 1 deletion src/Rbac/Rbac.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ protected function _matchPermission(array $permission, array|ArrayAccess $user,
'action' => $params['action'] ?? null,
'role' => $role,
];
if (!$user && ($permission['bypassAuth'] ?? false) !== true) {
$bypass = $permission['bypassAuth'] ?? false;
if (is_callable($bypass)) {
$bypass = $bypass($user, $role, $request);
}
if (!$user && $bypass !== true) {
return null;
}
foreach ($permission as $key => $value) {
Expand Down
1 change: 0 additions & 1 deletion tests/TestCase/Authenticator/CookieAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function testPersistIdentity($setCookie, $field, array $post, array $sess
'Authentication.Password',
]);
$uri = new Uri('/login');
$uri->base = null;
$request = new ServerRequest();
$request = $request->withUri($uri);

Expand Down
3 changes: 0 additions & 3 deletions tests/TestCase/Authenticator/TwoFactorAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class TwoFactorAuthenticatorTest extends TestCase
public function testAuthenticateFailedNoData()
{
$uri = new Uri('/testpath');
$uri->base = null;
$request = new ServerRequest();
$request = $request->withUri($uri);
$identifiers = new IdentifierCollection([
Expand All @@ -54,7 +53,6 @@ public function testAuthenticateFailedNoData()
public function testAuthenticateFailedInvalidUrl()
{
$uri = new Uri('/testpath');
$uri->base = null;
$request = new ServerRequest();
$request = $request->withUri($uri);
$request->getSession()->write(
Expand Down Expand Up @@ -85,7 +83,6 @@ public function testAuthenticateFailedInvalidUrl()
public function testAuthenticate()
{
$uri = new Uri('/testpath');
$uri->base = null;
$request = new ServerRequest();
$request = $request->withUri($uri);
$request->getSession()->write(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public function setUp(): void
$this->request->expects($this->any())->method('is')->will($this->returnValue(true));
$this->Controller = new Controller($this->request);
$this->Registry = $this->Controller->components();
$this->Controller->OneTimePasswordAuthenticator = new OneTimePasswordAuthenticatorComponent($this->Registry);
$this->Controller->components()->set('OneTimePasswordAuthenticator', new OneTimePasswordAuthenticatorComponent($this->Registry));
//$this->Controller->OneTimePasswordAuthenticator = new OneTimePasswordAuthenticatorComponent($this->Registry);
}

/**
Expand All @@ -92,7 +93,8 @@ public function tearDown(): void
*/
public function testInitialize()
{
$this->Controller->OneTimePasswordAuthenticator = new OneTimePasswordAuthenticatorComponent($this->Registry);
//$this->Controller->OneTimePasswordAuthenticator = new OneTimePasswordAuthenticatorComponent($this->Registry);
$this->Controller->components()->set('OneTimePasswordAuthenticator', new OneTimePasswordAuthenticatorComponent($this->Registry));
$this->assertInstanceOf(OneTimePasswordAuthenticatorComponent::class, $this->Controller->OneTimePasswordAuthenticator);
}

Expand Down
8 changes: 5 additions & 3 deletions tests/TestCase/Rbac/RbacTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use CakeDC\Auth\Rbac\Rules\Owner;
use CakeDC\Auth\Test\App\Auth\Rule\SampleRule;
use Exception;
use PHPUnit\Framework\Attributes\DataProvider;
use Psr\Log\LogLevel;
use ReflectionClass;
use RuntimeException;
Expand Down Expand Up @@ -183,12 +184,13 @@ public function testAuthorize($permissions, $user, $requestParams, $expected)
$this->assertSame($expected, $result);
}

public function providerAuthorize()
public static function providerAuthorize()
{
$trueRuleMock = $this->getMockBuilder(Owner::class)
$testCase = new static(RbacTest::class);
$trueRuleMock = $testCase->getMockBuilder(Owner::class)
->onlyMethods(['allowed'])
->getMock();
$trueRuleMock->expects($this->any())
$trueRuleMock->expects($testCase->any())
->method('allowed')
->willReturn(true);

Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
'default' => [
'engine' => 'File',
],
'_cake_core_' => [
'_cake_translations_' => [
'className' => 'File',
'prefix' => 'users_myapp_cake_core_',
'path' => CACHE . 'persistent/',
Expand Down
Loading