Skip to content

Commit 7a43d21

Browse files
authored
Auth plugins support (#151)
* Support AuthUser for new plugins. * Docs * Update AuthUserComponent.php * Support AuthUser for new plugins.
1 parent 9fb87fd commit 7a43d21

File tree

5 files changed

+56
-24
lines changed

5 files changed

+56
-24
lines changed

docs/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ If you only need the basic request policy provided by this plugin, and no furthe
9191
then it is best to stick to the Auth component as simple wrapper.
9292
It is then limited to controller scope (no middleware/routing support) as it always has been so far.
9393

94+
You can seamlessly upgrade to the new plugins while keeping your INI files.
95+
They are also compatible with AuthUser component and helper as well as the Auth panel.
96+
9497
## Upgrade notes
9598
Coming from CakePHP 4.x the following major changes will affect your app:
9699
- Cake\Auth namespace has been removed and is now migrated to TinyAuth\Auth, that includes the

phpstan.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ parameters:
44
- src/
55
bootstrapFiles:
66
- tests/bootstrap.php
7-
checkMissingIterableValueType: false
8-
checkGenericClassInNonGenericObjectType: false
97
ignoreErrors:
8+
- identifier: missingType.iterableValue
9+
- identifier: missingType.generics
1010
- '#Constructor of class .+SessionStorage has an unused parameter \$response#'
1111
- '#PHPDoc tag @mixin contains invalid type .+InstanceConfigTrait.#'

src/Controller/Component/AuthUserComponent.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,13 @@
1212

1313
/**
1414
* Easy access to the current logged-in user and the corresponding auth data.
15-
*
16-
* @property \TinyAuth\Controller\Component\AuthComponent $Auth
1715
*/
1816
class AuthUserComponent extends Component {
1917

2018
use AclTrait;
2119
use AllowTrait;
2220
use AuthUserTrait;
2321

24-
/**
25-
* @var array
26-
*/
27-
protected array $components = ['TinyAuth.Auth'];
28-
2922
/**
3023
* @param \Cake\Controller\ComponentRegistry $registry
3124
* @param array $config
@@ -63,14 +56,37 @@ public function hasAccess(array $url): bool {
6356
'action' => 'index',
6457
];
6558

66-
return $this->_checkUser((array)$this->Auth->user(), $url);
59+
return $this->_checkUser($this->_getUser(), $url);
6760
}
6861

6962
/**
7063
* @return array
7164
*/
7265
protected function _getUser() {
73-
return (array)$this->Auth->user();
66+
/** @var \Authentication\Identity|null $identity */
67+
$identity = $this->getController()->getRequest()->getAttribute('identity');
68+
if ($identity) {
69+
/** @var \Cake\Datasource\EntityInterface|array $data */
70+
$data = $identity->getOriginalData();
71+
if (!is_array($data)) {
72+
return $data->toArray();
73+
}
74+
75+
return $data;
76+
}
77+
78+
// We skip for new plugin(s)
79+
if ($this->getController()->components()->has('Authentication')) {
80+
return [];
81+
}
82+
83+
// Fallback to old Auth style
84+
if (!$this->getController()->components()->has('Auth')) {
85+
$this->getController()->loadComponent('TinyAuth.Auth');
86+
}
87+
88+
/** @phpstan-ignore property.notFound */
89+
return (array)$this->getController()->Auth->user();
7490
}
7591

7692
}

tests/TestCase/Controller/Component/AuthUserComponentTest.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Cake\Controller\ComponentRegistry;
66
use Cake\Controller\Controller;
77
use Cake\Core\Configure;
8+
use Cake\Core\Plugin;
89
use Cake\Event\Event;
910
use Cake\Http\ServerRequest;
1011
use Cake\TestSuite\TestCase;
@@ -26,6 +27,11 @@ class AuthUserComponentTest extends TestCase {
2627
*/
2728
protected $AuthUser;
2829

30+
/**
31+
* @var \Cake\Controller\Controller
32+
*/
33+
protected $controller;
34+
2935
/**
3036
* @return void
3137
*/
@@ -34,10 +40,13 @@ public function setUp(): void {
3440
'allowFilePath' => ROOT . DS . 'tests' . DS . 'test_files' . DS,
3541
];
3642

37-
$controller = new Controller(new ServerRequest());
38-
$componentRegistry = new ComponentRegistry($controller);
43+
$this->controller = new Controller(new ServerRequest());
44+
$componentRegistry = new ComponentRegistry($this->controller);
3945
$this->AuthUser = new TestAuthUserComponent($componentRegistry);
40-
$this->AuthUser->Auth = $this->getMockBuilder(AuthComponent::class)->onlyMethods(['user'])->setConstructorArgs([$componentRegistry, $config])->getMock();
46+
$this->controller->loadComponent('TinyAuth.Auth', [
47+
'allowFilePath' => Plugin::path('TinyAuth') . 'tests' . DS . 'test_files' . DS,
48+
]);
49+
$this->controller->Auth = $this->getMockBuilder(AuthComponent::class)->onlyMethods(['user'])->setConstructorArgs([$componentRegistry, $config])->getMock();
4150

4251
Configure::write('Roles', [
4352
'user' => 1,
@@ -55,7 +64,7 @@ public function testIsAuthorizedValid() {
5564
'id' => 1,
5665
'role_id' => 1,
5766
];
58-
$this->AuthUser->Auth->expects($this->once())
67+
$this->controller->Auth->expects($this->once())
5968
->method('user')
6069
->with(null)
6170
->will($this->returnValue($user));
@@ -76,7 +85,7 @@ public function testIsAuthorizedInvalid() {
7685
'id' => 1,
7786
'role_id' => 1,
7887
];
79-
$this->AuthUser->Auth->expects($this->once())
88+
$this->controller->Auth->expects($this->once())
8089
->method('user')
8190
->with(null)
8291
->will($this->returnValue($user));
@@ -95,7 +104,7 @@ public function testIsAuthorizedInvalid() {
95104
public function testIsAuthorizedNotLoggedIn() {
96105
$user = [
97106
];
98-
$this->AuthUser->Auth->expects($this->once())
107+
$this->controller->Auth->expects($this->once())
99108
->method('user')
100109
->with(null)
101110
->will($this->returnValue($user));
@@ -180,7 +189,7 @@ public function testEmptyAuthSession() {
180189
* @return void
181190
*/
182191
public function testId() {
183-
$this->AuthUser->Auth->expects($this->once())
192+
$this->controller->Auth->expects($this->once())
184193
->method('user')
185194
->with(null)
186195
->will($this->returnValue(['id' => '1']));
@@ -192,7 +201,7 @@ public function testId() {
192201
* @return void
193202
*/
194203
public function testIsMe() {
195-
$this->AuthUser->Auth->expects($this->any())
204+
$this->controller->Auth->expects($this->any())
196205
->method('user')
197206
->with(null)
198207
->will($this->returnValue(['id' => '1']));
@@ -209,7 +218,7 @@ public function testIsMe() {
209218
* @return void
210219
*/
211220
public function testUser() {
212-
$this->AuthUser->Auth->expects($this->any())
221+
$this->controller->Auth->expects($this->any())
213222
->method('user')
214223
->with(null)
215224
->will($this->returnValue(['id' => '1', 'username' => 'foo']));
@@ -225,7 +234,7 @@ public function testUser() {
225234
public function testRoles() {
226235
$this->AuthUser->setConfig('multiRole', true);
227236

228-
$this->AuthUser->Auth->expects($this->once())
237+
$this->controller->Auth->expects($this->once())
229238
->method('user')
230239
->will($this->returnValue(['id' => '1', 'Roles' => ['1', '2']]));
231240

@@ -238,7 +247,7 @@ public function testRoles() {
238247
public function testRolesDeep() {
239248
$this->AuthUser->setConfig('multiRole', true);
240249

241-
$this->AuthUser->Auth->expects($this->once())
250+
$this->controller->Auth->expects($this->once())
242251
->method('user')
243252
->with(null)
244253
->will($this->returnValue(['id' => '1', 'Roles' => [['id' => '1'], ['id' => '2']]]));
@@ -252,7 +261,7 @@ public function testRolesDeep() {
252261
public function testHasRole() {
253262
$this->AuthUser->setConfig('multiRole', true);
254263

255-
$this->AuthUser->Auth->expects($this->exactly(3))
264+
$this->controller->Auth->expects($this->exactly(3))
256265
->method('user')
257266
->with(null)
258267
->will($this->returnValue(['id' => '1', 'Roles' => [['id' => '1'], ['id' => '2']]]));
@@ -271,7 +280,7 @@ public function testHasRole() {
271280
public function testHasRoles() {
272281
$this->AuthUser->setConfig('multiRole', true);
273282

274-
$this->AuthUser->Auth->expects($this->exactly(6))
283+
$this->controller->Auth->expects($this->exactly(6))
275284
->method('user')
276285
->with(null)
277286
->will($this->returnValue(['id' => '1', 'Roles' => [['id' => '1'], ['id' => '2']]]));

tests/TestCase/Panel/AuthPanelTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function tearDown(): void {
6161
*/
6262
public function testPanelRestrictedAction() {
6363
$controller = new Controller(new ServerRequest());
64+
$controller->loadComponent('TinyAuth.Auth');
6465
$event = new Event('event', $controller);
6566

6667
$this->panel->shutdown($event);
@@ -86,6 +87,7 @@ public function testPanelPublicAction() {
8687
$request = new ServerRequest(['url' => '/users']);
8788
$request = $request->withAttribute('params', $url);
8889
$controller = new Controller($request);
90+
$controller->loadComponent('TinyAuth.Auth');
8991
$event = new Event('event', $controller);
9092

9193
$this->panel->shutdown($event);
@@ -115,6 +117,7 @@ public function testPanelAclRestricted() {
115117
$request = new ServerRequest(['url' => '/tags']);
116118
$request = $request->withAttribute('params', $url);
117119
$controller = new Controller($request);
120+
$controller->loadComponent('TinyAuth.Auth');
118121
$event = new Event('event', $controller);
119122

120123
$this->panel->shutdown($event);
@@ -147,6 +150,7 @@ public function testPanelAclAllowed() {
147150
'role_id' => 1,
148151
]);
149152
$controller = new Controller($request);
153+
$controller->loadComponent('TinyAuth.Auth');
150154
$event = new Event('event', $controller);
151155

152156
$this->panel->shutdown($event);

0 commit comments

Comments
 (0)