Skip to content

Commit 84f1bf8

Browse files
committed
Update documentation for CakePHP 5 / PHP 8+ compatibility
- Add return types to all code examples (controller actions, methods) - Fix grammar: "lets" -> "let's", "unauthencticated" typo - Use strict comparison (===) instead of loose (==) - Fix syntax errors: missing semicolon, incorrect function call ($middlewareQueue()) - Remove unused imports (ResponseInterface) - Add missing return statements - Fix declare spacing: declare(strict_types=1) - Use plural table name convention ($this->Articles) - Remove extra ResponseInterface parameter from interface method
1 parent 98e6a29 commit 84f1bf8

File tree

6 files changed

+25
-28
lines changed

6 files changed

+25
-28
lines changed

docs/en/component.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ requires use of the Middleware, so make sure it is applied as well. To use the
88
component, first load it::
99

1010
// In your AppController
11-
public function initialize()
11+
public function initialize(): void
1212
{
1313
parent::initialize();
1414
$this->loadComponent('Authorization.Authorization');
@@ -42,7 +42,7 @@ In your controller actions or callback methods you can check authorization using
4242
the component::
4343

4444
// In the Articles Controller.
45-
public function edit($id)
45+
public function edit($id): \Cake\Http\Response
4646
{
4747
$article = $this->Articles->get($id);
4848
$this->Authorization->authorize($article);
@@ -106,7 +106,7 @@ Example::
106106

107107
//ArticlesController.php
108108

109-
public function index()
109+
public function index(): \Cake\Http\Response
110110
{
111111
$query = $this->Articles->find();
112112

@@ -115,7 +115,7 @@ Example::
115115
...
116116
}
117117

118-
public function delete($id)
118+
public function delete($id): \Cake\Http\Response
119119
{
120120
$article = $this->Articles->get($id);
121121

@@ -124,7 +124,7 @@ Example::
124124
...
125125
}
126126

127-
public function add()
127+
public function add(): \Cake\Http\Response
128128
{
129129
//this will authorize against `insert` model action while being called in `add` controller action.
130130
$this->Authorization->authorizeModel();
@@ -138,7 +138,7 @@ Authorization can also be skipped inside an action::
138138

139139
//ArticlesController.php
140140

141-
public function view($id)
141+
public function view($id): \Cake\Http\Response
142142
{
143143
$this->Authorization->skipAuthorization();
144144
...

docs/en/index.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Getting Started
2222
===============
2323

2424
The Authorization plugin integrates into your application as a middleware layer
25-
and optionally a component to make checking authorization easier. First, lets
25+
and optionally a component to make checking authorization easier. First, let's
2626
apply the middleware. In **src/Application.php** add the following to the class
2727
imports::
2828

@@ -31,7 +31,6 @@ imports::
3131
use Authorization\AuthorizationServiceProviderInterface;
3232
use Authorization\Middleware\AuthorizationMiddleware;
3333
use Authorization\Policy\OrmResolver;
34-
use Psr\Http\Message\ResponseInterface;
3534
use Psr\Http\Message\ServerRequestInterface;
3635

3736
Add the ``AuthorizationServiceProviderInterface`` to the implemented interfaces
@@ -56,7 +55,7 @@ Then make your application's ``middleware()`` method look like::
5655
// and authentication middleware.
5756
->add(new AuthorizationMiddleware($this));
5857

59-
return $middlewareQueue();
58+
return $middlewareQueue;
6059
}
6160

6261
The placement of the ``AuthorizationMiddleware`` is important and must be added
@@ -78,7 +77,7 @@ define the ``AuthorizationService`` it wants to use. Add the following method yo
7877
This configures basic :doc:`/policy-resolvers` that will match
7978
ORM entities with their policy classes.
8079

81-
Next, lets add the ``AuthorizationComponent`` to ``AppController``. In
80+
Next, let's add the ``AuthorizationComponent`` to ``AppController``. In
8281
**src/Controller/AppController.php** add the following to the ``initialize()``
8382
method::
8483

@@ -87,9 +86,9 @@ method::
8786
By loading the :doc:`/component` we'll be able to check
8887
authorization on a per-action basis more easily. For example, we can do::
8988

90-
public function edit($id = null)
89+
public function edit($id = null): \Cake\Http\Response
9190
{
92-
$article = $this->Article->get($id);
91+
$article = $this->Articles->get($id);
9392
$this->Authorization->authorize($article, 'update');
9493

9594
// Rest of action

docs/en/middleware.rst

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ A basic example would be::
2424

2525
class Application extends BaseApplication implements AuthorizationServiceProviderInterface
2626
{
27-
public function getAuthorizationService(ServerRequestInterface $request, ResponseInterface $response)
27+
public function getAuthorizationService(ServerRequestInterface $request): AuthorizationServiceInterface
2828
{
2929
$resolver = new OrmResolver();
3030

3131
return new AuthorizationService($resolver);
3232
}
3333

34-
public function middleware($middlewareQueue)
34+
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
3535
{
3636
// other middleware
3737
$middlewareQueue->add(new AuthorizationMiddleware($this));
@@ -114,7 +114,7 @@ implementing the ``Authorization\IdentityInterface`` and using the
114114
/**
115115
* Setter to be used by the middleware.
116116
*/
117-
public function setAuthorization(AuthorizationServiceInterface $service)
117+
public function setAuthorization(AuthorizationServiceInterface $service): static
118118
{
119119
$this->authorization = $service;
120120

@@ -150,10 +150,8 @@ If you also use the Authentication plugin make sure to implement both interfaces
150150

151151
/**
152152
* Authentication\IdentityInterface method
153-
*
154-
* @return string
155153
*/
156-
public function getIdentifier()
154+
public function getIdentifier(): int|string|null
157155
{
158156
return $this->id;
159157
}
@@ -254,7 +252,7 @@ How to create a custom UnauthorizedHandler
254252
#. Create this file ``src/Middleware/UnauthorizedHandler/CustomRedirectHandler.php``::
255253

256254
<?php
257-
declare( strict_types = 1 );
255+
declare(strict_types=1);
258256

259257
namespace App\Middleware\UnauthorizedHandler;
260258

docs/en/policies.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ You can generate empty policy classes for ORM objects using ``bake``:
4646
Writing Policy Methods
4747
======================
4848

49-
The policy class we just created doesn't do much right now. Lets define a method
49+
The policy class we just created doesn't do much right now. Let's define a method
5050
that allows us to check if a user can update an article::
5151

52-
public function canEdit(IdentityInterface $user, Article $article)
52+
public function canEdit(IdentityInterface $user, Article $article): bool
5353
{
54-
return $user->id == $article->user_id;
54+
return $user->id === $article->user_id;
5555
}
5656

5757
Policy methods must return ``true`` or a ``Result`` objects to indicate success.
5858
All other values will be interpreted as failure.
5959

6060
Policy methods will receive ``null`` for the ``$user`` parameter when handling
61-
unauthencticated users. If you want to automatically fail policy methods for
61+
unauthenticated users. If you want to automatically fail policy methods for
6262
anonymous users you can use the ``IdentityInterface`` typehint.
6363

6464
.. _policy-result-objects:
@@ -72,9 +72,9 @@ passed/failed::
7272

7373
use Authorization\Policy\Result;
7474

75-
public function canUpdate(IdentityInterface $user, Article $article)
75+
public function canUpdate(IdentityInterface $user, Article $article): Result
7676
{
77-
if ($user->id == $article->user_id) {
77+
if ($user->id === $article->user_id) {
7878
return new Result(true);
7979
}
8080
// Results let you define a 'reason' for the failure.

docs/en/policy-resolvers.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ The OrmResolver supports customization through its constructor::
7676
$overrides = [
7777
'Blog' => 'Cms',
7878
];
79-
$resolver = new OrmResolver($appNamespace, $overrides)
79+
$resolver = new OrmResolver($appNamespace, $overrides);
8080

8181
Using Multiple Resolvers
8282
========================

docs/en/request-authorization-middleware.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ Next, map the request class to the policy inside
5656
use Authorization\Middleware\AuthorizationMiddleware;
5757
use Authorization\Middleware\RequestAuthorizationMiddleware;
5858
use Authorization\Policy\MapResolver;
59-
use Authorization\Policy\OrmResolver;
60-
use Psr\Http\Message\ResponseInterface;
6159
use Cake\Http\ServerRequest;
6260

6361

@@ -77,6 +75,8 @@ AuthorizationMiddleware::
7775
// Add authorization (after authentication if you are using that plugin too).
7876
$middlewareQueue->add(new AuthorizationMiddleware($this));
7977
$middlewareQueue->add(new RequestAuthorizationMiddleware());
78+
79+
return $middlewareQueue;
8080
}
8181

8282
Controller Usage

0 commit comments

Comments
 (0)