Skip to content

Commit 328c05f

Browse files
committed
Fix documentation issues (3rd pass)
- Add return types to all controller action examples (login, logout, etc.) - Fix "Let's" grammar (was "Lets") - Fix `stopImpersonation()` typo -> `stopImpersonating()` - Replace deprecated `TableRegistry::getTableLocator()->get()` with `fetchTable()` - Fix indentation inconsistency in testing examples - Add missing redirect return in revertIdentity() example - Use `fetchTable()` instead of `$this->Users` property access - Use `saveOrFail()` instead of `save()` for better error handling - Use `'plugin' => false` instead of `'plugin' => null` for consistency - Add trailing commas in arrays
1 parent 87b5489 commit 328c05f

File tree

6 files changed

+59
-36
lines changed

6 files changed

+59
-36
lines changed

docs/en/authenticators.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ In your `UsersController`:
196196
``` php
197197
use Firebase\JWT\JWT;
198198

199-
public function login()
199+
public function login(): void
200200
{
201201
$result = $this->Authentication->getResult();
202202
if ($result->isValid()) {
@@ -585,7 +585,7 @@ Then in your controller's login method you can use `getLoginRedirect()` to get
585585
the redirect target safely from the query string parameter:
586586

587587
``` php
588-
public function login()
588+
public function login(): ?\Cake\Http\Response
589589
{
590590
$result = $this->Authentication->getResult();
591591

@@ -596,8 +596,11 @@ public function login()
596596
if (!$target) {
597597
$target = ['controller' => 'Pages', 'action' => 'display', 'home'];
598598
}
599+
599600
return $this->redirect($target);
600601
}
602+
603+
return null;
601604
}
602605
```
603606

docs/en/impersonation.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ user from your application's database:
1212

1313
``` php
1414
// In a controller
15-
public function impersonate()
15+
public function impersonate(): \Cake\Http\Response
1616
{
1717
$this->request->allowMethod(['POST']);
1818

@@ -25,9 +25,9 @@ public function impersonate()
2525
}
2626

2727
// Fetch the user we want to impersonate.
28-
$targetUser = $this->Users->findById(
29-
$this->request->getData('user_id')
30-
)->firstOrFail();
28+
$targetUser = $this->fetchTable('Users')
29+
->findById($this->request->getData('user_id'))
30+
->firstOrFail();
3131

3232
// Enable impersonation.
3333
$this->Authentication->impersonate($targetUser);
@@ -46,7 +46,7 @@ back to your previous identity using `AuthenticationComponent`:
4646

4747
``` php
4848
// In a controller
49-
public function revertIdentity()
49+
public function revertIdentity(): \Cake\Http\Response
5050
{
5151
$this->request->allowMethod(['POST']);
5252

@@ -55,6 +55,8 @@ public function revertIdentity()
5555
throw new NotFoundException();
5656
}
5757
$this->Authentication->stopImpersonating();
58+
59+
return $this->redirect($this->referer());
5860
}
5961
```
6062

@@ -64,4 +66,4 @@ There are a few limitations to impersonation.
6466

6567
1. Your application must be using the `Session` authenticator.
6668
2. You cannot impersonate another user while impersonation is active. Instead
67-
you must `stopImpersonation()` and then start it again.
69+
you must `stopImpersonating()` and then start it again.

docs/en/index.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function getAuthenticationService(ServerRequestInterface $request): Authe
109109
'fields' => $fields,
110110
'loginUrl' => Router::url([
111111
'prefix' => false,
112-
'plugin' => null,
112+
'plugin' => false,
113113
'controller' => 'Users',
114114
'action' => 'login',
115115
]),
@@ -168,17 +168,20 @@ like:
168168

169169
``` php
170170
// in src/Controller/UsersController.php
171-
public function login()
171+
public function login(): ?\Cake\Http\Response
172172
{
173173
$result = $this->Authentication->getResult();
174174
// If the user is logged in send them away.
175175
if ($result && $result->isValid()) {
176176
$target = $this->Authentication->getLoginRedirect() ?? '/home';
177+
177178
return $this->redirect($target);
178179
}
179180
if ($this->request->is('post')) {
180181
$this->Flash->error('Invalid username or password');
181182
}
183+
184+
return null;
182185
}
183186
```
184187

@@ -188,7 +191,7 @@ unauthenticated users are able to access it:
188191

189192
``` php
190193
// in src/Controller/UsersController.php
191-
public function beforeFilter(\Cake\Event\EventInterface $event)
194+
public function beforeFilter(\Cake\Event\EventInterface $event): void
192195
{
193196
parent::beforeFilter($event);
194197

@@ -216,9 +219,10 @@ Then add a simple logout action:
216219

217220
``` php
218221
// in src/Controller/UsersController.php
219-
public function logout()
222+
public function logout(): \Cake\Http\Response
220223
{
221224
$this->Authentication->logout();
225+
222226
return $this->redirect(['controller' => 'Users', 'action' => 'login']);
223227
}
224228
```

docs/en/migration-from-the-authcomponent.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,20 +184,23 @@ upon a successful login, change your login action to check the new
184184
identity results:
185185

186186
``` php
187-
public function login()
187+
public function login(): ?\Cake\Http\Response
188188
{
189189
$result = $this->Authentication->getResult();
190190

191-
// regardless of POST or GET, redirect if user is logged in
191+
// Regardless of POST or GET, redirect if user is logged in
192192
if ($result->isValid()) {
193193
$target = $this->Authentication->getLoginRedirect();
194+
194195
return $this->redirect($target);
195196
}
196197

197-
// display error if user submitted and authentication failed
198+
// Display error if user submitted and authentication failed
198199
if ($this->request->is('post')) {
199200
$this->Flash->error('Invalid username or password');
200201
}
202+
203+
return null;
201204
}
202205
```
203206

@@ -289,7 +292,7 @@ Then in your controller's login method you can use `getLoginRedirect()` to get
289292
the redirect target safely from the query string parameter:
290293

291294
``` php
292-
public function login()
295+
public function login(): ?\Cake\Http\Response
293296
{
294297
$result = $this->Authentication->getResult();
295298

@@ -300,8 +303,11 @@ public function login()
300303
if (!$target) {
301304
$target = ['controller' => 'Pages', 'action' => 'display', 'home'];
302305
}
306+
303307
return $this->redirect($target);
304308
}
309+
310+
return null;
305311
}
306312
```
307313

@@ -312,29 +318,33 @@ functionality. You can replicate that logic with this plugin by
312318
leveraging the `AuthenticationService`:
313319

314320
``` php
315-
public function login()
321+
public function login(): ?\Cake\Http\Response
316322
{
317323
$result = $this->Authentication->getResult();
318324

319-
// regardless of POST or GET, redirect if user is logged in
325+
// Regardless of POST or GET, redirect if user is logged in
320326
if ($result->isValid()) {
321327
$authService = $this->Authentication->getAuthenticationService();
322328

323329
// Get the identifier that was used for authentication.
324330
$identifier = $authService->getIdentificationProvider();
325331
if ($identifier !== null && $identifier->needsPasswordRehash()) {
326332
// Rehash happens on save.
327-
$user = $this->Users->get($this->Authentication->getIdentityData('id'));
333+
$user = $this->fetchTable('Users')->get(
334+
$this->Authentication->getIdentityData('id')
335+
);
328336
$user->password = $this->request->getData('password');
329-
$this->Users->save($user);
337+
$this->fetchTable('Users')->saveOrFail($user);
330338
}
331339

332340
// Redirect to a logged in page
333341
return $this->redirect([
334342
'controller' => 'Pages',
335343
'action' => 'display',
336-
'home'
344+
'home',
337345
]);
338346
}
347+
348+
return null;
339349
}
340350
```

docs/en/password-hashers.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,26 @@ access the `Password` identifier and check if the current user’s
6060
password needs to be upgraded:
6161

6262
``` php
63-
public function login()
63+
public function login(): ?\Cake\Http\Response
6464
{
6565
$authentication = $this->request->getAttribute('authentication');
6666
$result = $authentication->getResult();
6767

68-
// regardless of POST or GET, redirect if user is logged in
68+
// Regardless of POST or GET, redirect if user is logged in
6969
if ($result->isValid()) {
7070
if ($authentication->getIdentificationProvider()->needsPasswordRehash()) {
7171
// Rehash happens on save.
72-
$user = $this->Users->get($authentication->getIdentity()->getIdentifier());
72+
$user = $this->fetchTable('Users')->get(
73+
$authentication->getIdentity()->getIdentifier()
74+
);
7375
$user->password = $this->request->getData('password');
74-
$this->Users->saveOrFail($user);
76+
$this->fetchTable('Users')->saveOrFail($user);
7577
}
7678

7779
// Redirect or display a template.
80+
return $this->redirect('/');
7881
}
82+
83+
return null;
7984
}
8085
```

docs/en/testing.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ArticlesControllerTest extends TestCase
1919
```
2020

2121
Based on the type of authentication you're using you will need to
22-
simulate credentials differently. Lets review a few more common types of
22+
simulate credentials differently. Let's review a few more common types of
2323
authentication.
2424

2525
## Session based authentication
@@ -29,10 +29,9 @@ normally would be found in the session. In your test cases you can
2929
define a helper method that lets you 'login':
3030

3131
``` php
32-
protected function login($userId = 1)
32+
protected function login(int $userId = 1): void
3333
{
34-
$users = TableRegistry::getTableLocator()->get('Users');
35-
$user = $users->get($userId);
34+
$user = $this->fetchTable('Users')->get($userId);
3635
$this->session(['Auth' => $user]);
3736
}
3837
```
@@ -81,14 +80,14 @@ variables that [PHP creates](https://php.net/manual/en/features.http-auth.php)
8180
automatically:
8281

8382
``` php
84-
public function testGet()
83+
public function testGet(): void
8584
{
86-
$this->configRequest([
87-
'environment' => [
88-
'PHP_AUTH_USER' => 'username',
89-
'PHP_AUTH_PW' => 'password',
90-
]
91-
]);
85+
$this->configRequest([
86+
'environment' => [
87+
'PHP_AUTH_USER' => 'username',
88+
'PHP_AUTH_PW' => 'password',
89+
],
90+
]);
9291
$this->get('/api/bookmarks');
9392
$this->assertResponseOk();
9493
}

0 commit comments

Comments
 (0)