Skip to content

Commit 37bb500

Browse files
committed
feat: update authorizer contracts
1 parent 9d27d32 commit 37bb500

File tree

37 files changed

+426
-281
lines changed

37 files changed

+426
-281
lines changed

src/Contracts/Auth/Authorizer.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
interface Authorizer
2323
{
2424
/**
25-
* Authorize the index controller action.
25+
* Authorize a JSON:API index query.
2626
*
2727
* @param Request|null $request
2828
* @param string $modelClass
@@ -49,72 +49,72 @@ public function store(?Request $request, string $modelClass): bool|Response;
4949
public function show(?Request $request, object $model): bool|Response;
5050

5151
/**
52-
* Authorize the update controller action.
52+
* Authorize a JSON:API update command.
5353
*
5454
* @param object $model
55-
* @param Request $request
55+
* @param Request|null $request
5656
* @return bool|Response
5757
*/
58-
public function update(Request $request, object $model): bool|Response;
58+
public function update(?Request $request, object $model): bool|Response;
5959

6060
/**
61-
* Authorize the destroy controller action.
61+
* Authorize a JSON:API destroy command.
6262
*
63-
* @param Request $request
63+
* @param Request|null $request
6464
* @param object $model
6565
* @return bool|Response
6666
*/
67-
public function destroy(Request $request, object $model): bool|Response;
67+
public function destroy(?Request $request, object $model): bool|Response;
6868

6969
/**
70-
* Authorize the show-related controller action.
70+
* Authorize a JSON:API show related query.
7171
*
72-
* @param Request $request
72+
* @param Request|null $request
7373
* @param object $model
7474
* @param string $fieldName
7575
* @return bool|Response
7676
*/
77-
public function showRelated(Request $request, object $model, string $fieldName): bool|Response;
77+
public function showRelated(?Request $request, object $model, string $fieldName): bool|Response;
7878

7979
/**
80-
* Authorize the show-relationship controller action.
80+
* Authorize a JSON:API show relationship query.
8181
*
82-
* @param Request $request
82+
* @param Request|null $request
8383
* @param object $model
8484
* @param string $fieldName
8585
* @return bool|Response
8686
*/
87-
public function showRelationship(Request $request, object $model, string $fieldName): bool|Response;
87+
public function showRelationship(?Request $request, object $model, string $fieldName): bool|Response;
8888

8989
/**
90-
* Authorize the update-relationship controller action.
90+
* Authorize a JSON:API update relationship command.
9191
*
92-
* @param Request $request
92+
* @param Request|null $request
9393
* @param object $model
9494
* @param string $fieldName
9595
* @return bool|Response
9696
*/
97-
public function updateRelationship(Request $request, object $model, string $fieldName): bool|Response;
97+
public function updateRelationship(?Request $request, object $model, string $fieldName): bool|Response;
9898

9999
/**
100-
* Authorize the attach-relationship controller action.
100+
* Authorize a JSON:API attach relationship command.
101101
*
102-
* @param Request $request
102+
* @param Request|null $request
103103
* @param object $model
104104
* @param string $fieldName
105105
* @return bool|Response
106106
*/
107-
public function attachRelationship(Request $request, object $model, string $fieldName): bool|Response;
107+
public function attachRelationship(?Request $request, object $model, string $fieldName): bool|Response;
108108

109109
/**
110-
* Authorize the detach-relationship controller action.
110+
* Authorize a JSON:API detach relationship command.
111111
*
112-
* @param Request $request
112+
* @param Request|null $request
113113
* @param object $model
114114
* @param string $fieldName
115115
* @return bool|Response
116116
*/
117-
public function detachRelationship(Request $request, object $model, string $fieldName): bool|Response;
117+
public function detachRelationship(?Request $request, object $model, string $fieldName): bool|Response;
118118

119119
/**
120120
* Get JSON:API errors describing the failure, or throw an appropriate exception.
Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
<?php
2+
/*
3+
* Copyright 2024 Cloud Creativity Limited
4+
*
5+
* Use of this source code is governed by an MIT-style
6+
* license that can be found in the LICENSE file or at
7+
* https://opensource.org/licenses/MIT.
8+
*/
9+
10+
namespace LaravelJsonApi\Contracts\Auth;
11+
12+
use Illuminate\Auth\Access\AuthorizationException;
13+
use Illuminate\Auth\AuthenticationException;
14+
use Illuminate\Http\Request;
15+
use LaravelJsonApi\Core\Document\ErrorList;
16+
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
17+
18+
interface ResourceAuthorizer
19+
{
20+
/**
21+
* Authorize a JSON:API index query.
22+
*
23+
* @param Request|null $request
24+
* @return ErrorList|null
25+
* @throws AuthenticationException
26+
* @throws AuthorizationException
27+
* @throws HttpExceptionInterface
28+
*/
29+
public function index(?Request $request): ?ErrorList;
30+
31+
/**
32+
* Authorize a JSON:API index query or fail.
33+
*
34+
* @param Request|null $request
35+
* @return void
36+
* @throws AuthenticationException
37+
* @throws AuthorizationException
38+
* @throws HttpExceptionInterface
39+
*/
40+
public function indexOrFail(?Request $request): void;
41+
42+
/**
43+
* Authorize a JSON:API store operation.
44+
*
45+
* @param Request|null $request
46+
* @return ErrorList|null
47+
* @throws AuthorizationException
48+
* @throws AuthenticationException
49+
* @throws HttpExceptionInterface
50+
*/
51+
public function store(?Request $request): ?ErrorList;
52+
53+
/**
54+
* Authorize a JSON:API store operation or fail.
55+
*
56+
* @param Request|null $request
57+
* @return void
58+
* @throws AuthorizationException
59+
* @throws AuthenticationException
60+
* @throws HttpExceptionInterface
61+
*/
62+
public function storeOrFail(?Request $request): void;
63+
64+
/**
65+
* Authorize a JSON:API show query.
66+
*
67+
* @param Request|null $request
68+
* @param object $model
69+
* @return ErrorList|null
70+
* @throws AuthorizationException
71+
* @throws AuthenticationException
72+
* @throws HttpExceptionInterface
73+
*/
74+
public function show(?Request $request, object $model): ?ErrorList;
75+
76+
/**
77+
* Authorize a JSON:API show query, or fail.
78+
*
79+
* @param Request|null $request
80+
* @param object $model
81+
* @return void
82+
* @throws AuthorizationException
83+
* @throws AuthenticationException
84+
* @throws HttpExceptionInterface
85+
*/
86+
public function showOrFail(?Request $request, object $model): void;
87+
88+
/**
89+
* Authorize a JSON:API update command.
90+
*
91+
* @param Request|null $request
92+
* @param object $model
93+
* @return ErrorList|null
94+
* @throws AuthorizationException
95+
* @throws AuthenticationException
96+
* @throws HttpExceptionInterface
97+
*/
98+
public function update(?Request $request, object $model): ?ErrorList;
99+
100+
/**
101+
* Authorize a JSON:API update command, or fail.
102+
*
103+
* @param Request|null $request
104+
* @param object $model
105+
* @return void
106+
* @throws AuthorizationException
107+
* @throws AuthenticationException
108+
* @throws HttpExceptionInterface
109+
*/
110+
public function updateOrFail(?Request $request, object $model): void;
111+
112+
/**
113+
* Authorize a JSON:API destroy command.
114+
*
115+
* @param Request|null $request
116+
* @param object $model
117+
* @return ErrorList|null
118+
* @throws AuthorizationException
119+
* @throws AuthenticationException
120+
* @throws HttpExceptionInterface
121+
*/
122+
public function destroy(?Request $request, object $model): ?ErrorList;
123+
124+
/**
125+
* Authorize a JSON:API destroy command, or fail.
126+
*
127+
* @param Request|null $request
128+
* @param object $model
129+
* @return void
130+
* @throws AuthorizationException
131+
* @throws AuthenticationException
132+
* @throws HttpExceptionInterface
133+
*/
134+
public function destroyOrFail(?Request $request, object $model): void;
135+
136+
/**
137+
* Authorize a JSON:API show related query.
138+
*
139+
* @param Request|null $request
140+
* @param object $model
141+
* @param string $fieldName
142+
* @return ErrorList|null
143+
* @throws AuthorizationException
144+
* @throws AuthenticationException
145+
* @throws HttpExceptionInterface
146+
*/
147+
public function showRelated(?Request $request, object $model, string $fieldName): ?ErrorList;
148+
149+
/**
150+
* Authorize a JSON:API show related query, or fail.
151+
*
152+
* @param Request|null $request
153+
* @param object $model
154+
* @param string $fieldName
155+
* @return void
156+
* @throws AuthorizationException
157+
* @throws AuthenticationException
158+
* @throws HttpExceptionInterface
159+
*/
160+
public function showRelatedOrFail(?Request $request, object $model, string $fieldName): void;
161+
162+
/**
163+
* Authorize a JSON:API show relationship query.
164+
*
165+
* @param Request|null $request
166+
* @param object $model
167+
* @param string $fieldName
168+
* @return ErrorList|null
169+
* @throws AuthorizationException
170+
* @throws AuthenticationException
171+
* @throws HttpExceptionInterface
172+
*/
173+
public function showRelationship(?Request $request, object $model, string $fieldName): ?ErrorList;
174+
175+
/**
176+
* Authorize a JSON:API show relationship query, or fail.
177+
*
178+
* @param Request|null $request
179+
* @param object $model
180+
* @param string $fieldName
181+
* @return void
182+
* @throws AuthorizationException
183+
* @throws AuthenticationException
184+
* @throws HttpExceptionInterface
185+
*/
186+
public function showRelationshipOrFail(?Request $request, object $model, string $fieldName): void;
187+
188+
/**
189+
* Authorize a JSON:API update relationship command.
190+
*
191+
* @param Request|null $request
192+
* @param object $model
193+
* @param string $fieldName
194+
* @return ErrorList|null
195+
* @throws AuthorizationException
196+
* @throws AuthenticationException
197+
* @throws HttpExceptionInterface
198+
*/
199+
public function updateRelationship(?Request $request, object $model, string $fieldName): ?ErrorList;
200+
201+
/**
202+
* Authorize a JSON:API update relationship command, or fail.
203+
*
204+
* @param Request|null $request
205+
* @param object $model
206+
* @param string $fieldName
207+
* @return void
208+
* @throws AuthorizationException
209+
* @throws AuthenticationException
210+
* @throws HttpExceptionInterface
211+
*/
212+
public function updateRelationshipOrFail(?Request $request, object $model, string $fieldName): void;
213+
214+
/**
215+
* Authorize a JSON:API attach relationship command.
216+
*
217+
* @param Request|null $request
218+
* @param object $model
219+
* @param string $fieldName
220+
* @return ErrorList|null
221+
* @throws AuthorizationException
222+
* @throws AuthenticationException
223+
* @throws HttpExceptionInterface
224+
*/
225+
public function attachRelationship(?Request $request, object $model, string $fieldName): ?ErrorList;
226+
227+
/**
228+
* Authorize a JSON:API attach relationship command, or fail.
229+
*
230+
* @param Request|null $request
231+
* @param object $model
232+
* @param string $fieldName
233+
* @return void
234+
* @throws AuthorizationException
235+
* @throws AuthenticationException
236+
* @throws HttpExceptionInterface
237+
*/
238+
public function attachRelationshipOrFail(?Request $request, object $model, string $fieldName): void;
239+
240+
/**
241+
* Authorize a JSON:API detach relationship command.
242+
*
243+
* @param Request|null $request
244+
* @param object $model
245+
* @param string $fieldName
246+
* @return ErrorList|null
247+
* @throws AuthorizationException
248+
* @throws AuthenticationException
249+
* @throws HttpExceptionInterface
250+
*/
251+
public function detachRelationship(?Request $request, object $model, string $fieldName): ?ErrorList;
252+
253+
/**
254+
* Authorize a JSON:API detach relationship command, or fail.
255+
*
256+
* @param Request|null $request
257+
* @param object $model
258+
* @param string $fieldName
259+
* @return void
260+
* @throws AuthorizationException
261+
* @throws AuthenticationException
262+
* @throws HttpExceptionInterface
263+
*/
264+
public function detachRelationshipOrFail(?Request $request, object $model, string $fieldName): void;
265+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/*
3+
* Copyright 2024 Cloud Creativity Limited
4+
*
5+
* Use of this source code is governed by an MIT-style
6+
* license that can be found in the LICENSE file or at
7+
* https://opensource.org/licenses/MIT.
8+
*/
9+
10+
namespace LaravelJsonApi\Contracts\Auth;
11+
12+
use LaravelJsonApi\Core\Values\ResourceType;
13+
14+
interface ResourceAuthorizerFactory
15+
{
16+
/**
17+
* Return a new resource authorizer instance.
18+
*
19+
* @param ResourceType|string $type
20+
* @return ResourceAuthorizer
21+
*/
22+
public function make(ResourceType|string $type): ResourceAuthorizer;
23+
}

0 commit comments

Comments
 (0)