Skip to content

Commit 1732da8

Browse files
authored
Merge pull request #125 from arodu/feature/43963-12x
Load extensions by normalized name and cast RBAC role to string
2 parents 7f33f44 + f08b148 commit 1732da8

7 files changed

Lines changed: 219 additions & 4 deletions

File tree

‎composer.json‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@
6969
},
7070
"config": {
7171
"sort-packages": true,
72+
"policy": {
73+
"advisories": {
74+
"ignore": [
75+
"league/flysystem"
76+
]
77+
}
78+
},
7279
"allow-plugins": {
7380
"cweagans/composer-patches": true,
7481
"dealerdirect/phpcodesniffer-composer-installer": true

‎src/Rbac/ApiRbac.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ public function checkPermissions(array|ArrayAccess $user, ServerRequestInterface
132132
{
133133
$roleField = $this->getConfig('role_field');
134134
$defaultRole = $this->getConfig('default_role');
135-
$role = Hash::get($user, $roleField, $defaultRole);
135+
// The role is matched against the permission 'role' key and passed to the rules,
136+
// whose signature (CakeDC\Auth\Rbac\Rules\Rule) types it as string. Apps commonly
137+
// store the role as an integer group id, so cast it here to avoid a TypeError.
138+
$role = (string)Hash::get($user, $roleField, $defaultRole);
136139

137140
if ($this->permissions === null) {
138141
return false;

‎src/Service/Action/Action.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ protected function _loadExtensions(): void
394394

395395
$extensions = $registry->normalizeArray($this->extensions);
396396
foreach ($extensions as $name => $properties) {
397-
$instance = $registry->load($properties['className'], $properties);
397+
$instance = $registry->load($name, $properties);
398398
$this->_eventManager->on($instance);
399399
if ($instance->attachable()) {
400400
[, $prop] = pluginSplit($name);

‎src/Service/Service.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -921,8 +921,8 @@ protected function _loadExtensions(): void
921921
}
922922
$registry = $this->getExtensions();
923923
$extensions = $registry->normalizeArray($this->extensions);
924-
foreach ($extensions as $properties) {
925-
$instance = $registry->load($properties['class'], $properties['config']);
924+
foreach ($extensions as $name => $config) {
925+
$instance = $registry->load($name, $config);
926926
$this->_eventManager->on($instance);
927927
}
928928
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* Copyright 2016 - 2026, Cake Development Corporation (http://cakedc.com)
6+
*
7+
* Licensed under The MIT License
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright 2016 - 2026, Cake Development Corporation (http://cakedc.com)
11+
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
12+
*/
13+
14+
namespace CakeDC\Api\Test\TestCase\Rbac;
15+
16+
use CakeDC\Api\Rbac\ApiRbac;
17+
use CakeDC\Api\Service\ServiceRegistry;
18+
use CakeDC\Api\Test\ConfigTrait;
19+
use CakeDC\Api\TestSuite\TestCase;
20+
21+
/**
22+
* Unit tests for the RBAC permission matching logic.
23+
*/
24+
class ApiRbacTest extends TestCase
25+
{
26+
use ConfigTrait;
27+
28+
/**
29+
* tearDown
30+
*
31+
* @return void
32+
*/
33+
public function tearDown(): void
34+
{
35+
ServiceRegistry::getServiceLocator()->clear();
36+
parent::tearDown();
37+
}
38+
39+
/**
40+
* Build a service with a resolved action and attach it to the request.
41+
*
42+
* @param array $permissions RBAC permissions.
43+
* @param array $user User data.
44+
* @return array{rbac: \CakeDC\Api\Rbac\ApiRbac, request: \Cake\Http\ServerRequest, user: array}
45+
*/
46+
protected function buildRequest(array $permissions, array $user = ['role' => 1, 'id' => 1]): array
47+
{
48+
ServiceRegistry::getServiceLocator()->clear();
49+
$this->_initializeRequest([
50+
'params' => ['service' => 'articles'],
51+
], 'GET');
52+
$service = ServiceRegistry::getServiceLocator()->get('articles', [
53+
'version' => null,
54+
'service' => 'articles',
55+
'request' => $this->request,
56+
'response' => $this->response,
57+
'baseUrl' => '/articles',
58+
]);
59+
$service->dispatchPrepareAction();
60+
61+
$this->request = $this->request->withAttribute('service', $service);
62+
63+
$rbac = new ApiRbac([
64+
'api_permissions' => $permissions,
65+
'role_field' => 'role',
66+
'default_role' => 'user',
67+
]);
68+
69+
return [$rbac, $this->request, $user];
70+
}
71+
72+
/**
73+
* Apps commonly store the role as an integer group id. The role is passed to
74+
* the rules (typed `string`), so an integer role must be cast to string and
75+
* must not raise a TypeError.
76+
*/
77+
public function testIntegerRoleIsPassedAsStringToRules(): void
78+
{
79+
$rule = new RoleCapturingRule();
80+
[$rbac, $request, $user] = $this->buildRequest([
81+
['role' => 1, 'service' => 'articles', 'action' => 'index', 'rule' => $rule],
82+
]);
83+
84+
$this->assertTrue($rbac->checkPermissions($user, $request));
85+
$this->assertTrue($rule->called);
86+
$this->assertSame('1', $rule->receivedRole);
87+
}
88+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* Copyright 2016 - 2026, Cake Development Corporation (http://cakedc.com)
6+
*
7+
* Licensed under The MIT License
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright 2016 - 2026, Cake Development Corporation (http://cakedc.com)
11+
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
12+
*/
13+
14+
namespace CakeDC\Api\Test\TestCase\Rbac;
15+
16+
use ArrayAccess;
17+
use CakeDC\Auth\Rbac\Rules\AbstractRule;
18+
use Psr\Http\Message\ServerRequestInterface;
19+
20+
/**
21+
* Rule that records the role it receives, to assert the role is passed as a string.
22+
*/
23+
class RoleCapturingRule extends AbstractRule
24+
{
25+
/**
26+
* @var bool Whether the rule was invoked.
27+
*/
28+
public bool $called = false;
29+
30+
/**
31+
* @var string|null The role received by the rule.
32+
*/
33+
public ?string $receivedRole = null;
34+
35+
/**
36+
* @inheritDoc
37+
*/
38+
public function allowed(array|ArrayAccess $user, string $role, ServerRequestInterface $request): bool
39+
{
40+
$this->called = true;
41+
$this->receivedRole = $role;
42+
43+
return true;
44+
}
45+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* Copyright 2016 - 2026, Cake Development Corporation (http://cakedc.com)
6+
*
7+
* Licensed under The MIT License
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright 2016 - 2026, Cake Development Corporation (http://cakedc.com)
11+
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
12+
*/
13+
14+
namespace CakeDC\Api\Test\TestCase\Service;
15+
16+
use Cake\Http\Response;
17+
use Cake\Http\ServerRequest;
18+
use CakeDC\Api\Service\Action\CrudIndexAction;
19+
use CakeDC\Api\Service\FallbackService;
20+
use CakeDC\Api\TestSuite\TestCase;
21+
22+
/**
23+
* Regression tests for the extension loading logic.
24+
*
25+
* CakePHP 5 ObjectRegistry::normalizeArray() returns a `name => config` map (the
26+
* old CakePHP 4 `class`/`config` shape is gone), so extensions must be loaded by
27+
* their normalized name. A plugin-prefixed extension (e.g. `CakeDC/Api.Log`) only
28+
* carries a `className` key, and an app extension without a plugin prefix carries
29+
* neither `class` nor `className`.
30+
*/
31+
class ExtensionLoadingTest extends TestCase
32+
{
33+
/**
34+
* A service configured with extensions must load them without a TypeError.
35+
*
36+
* @return void
37+
*/
38+
public function testServiceLoadsConfiguredExtension(): void
39+
{
40+
$service = new FallbackService([
41+
'request' => new ServerRequest(),
42+
'response' => new Response(),
43+
'Extension' => ['CakeDC/Api.Log'],
44+
]);
45+
46+
$this->assertTrue($service->getExtensions()->has('Log'));
47+
}
48+
49+
/**
50+
* An action configured with an app extension (no plugin prefix) must load it.
51+
*
52+
* @return void
53+
*/
54+
public function testActionLoadsAppExtensionWithoutPluginPrefix(): void
55+
{
56+
$request = new ServerRequest();
57+
$response = new Response();
58+
$service = new FallbackService([
59+
'request' => $request,
60+
'response' => $response,
61+
]);
62+
$action = new CrudIndexAction([
63+
'service' => $service,
64+
'request' => $request,
65+
'response' => $response,
66+
'Extension' => ['Sort'],
67+
]);
68+
69+
$this->assertNotNull($action->getExtensions());
70+
$this->assertTrue($action->getExtensions()->has('Sort'));
71+
}
72+
}

0 commit comments

Comments
 (0)