Skip to content

Commit 019eb29

Browse files
chore: Refactor the link forms
1 parent c999ab3 commit 019eb29

File tree

20 files changed

+306
-452
lines changed

20 files changed

+306
-452
lines changed

src/controllers/Links.php

Lines changed: 83 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ class Links extends BaseController
2626
* @request_param string q
2727
* @request_param integer page
2828
*
29-
* @response 302 /login?redirect_to=/links
30-
* if the user is not connected
3129
* @response 200
30+
* On success.
31+
*
32+
* @throws auth\MissingCurrentUserError
33+
* If the user is not connected.
3234
*/
3335
public function index(Request $request): Response
3436
{
35-
$user = $this->requireCurrentUser(redirect_after_login: \Minz\Url::for('links'));
37+
$user = auth\CurrentUser::require();
3638

3739
$query = $request->parameters->getString('q');
3840
$pagination_page = $request->parameters->getInteger('page', 1);
@@ -99,30 +101,25 @@ public function index(Request $request): Response
99101
*
100102
* @request_param string id
101103
*
102-
* @response 302 /login?redirect_to=/links/:id
103-
* if user is not connected and the link is not public
104-
* @response 404
105-
* if the link doesn't exist or is inaccessible to current user
106104
* @response 200
105+
* On success.
106+
*
107+
* @throws auth\MissingCurrentUserError
108+
* If the link requires the users to be logged in while they are not.
109+
* @throws \Minz\Errors\MissingRecordError
110+
* If the link doesn't exist.
111+
* @throws auth\AccessDeniedError
112+
* If the authenticated user cannot view the link.
107113
*/
108114
public function show(Request $request): Response
109115
{
110116
$user = auth\CurrentUser::get();
111-
$link_id = $request->parameters->getString('id', '');
112-
$link = models\Link::find($link_id);
113-
114-
if (!$link) {
115-
return Response::notFound('not_found.phtml');
116-
}
117+
$link = models\Link::requireFromRequest($request);
117118

118-
$can_view = auth\LinksAccess::canView($user, $link);
119-
$can_update = auth\LinksAccess::canUpdate($user, $link);
120-
if (!$can_view && $user) {
121-
return Response::notFound('not_found.phtml');
122-
} elseif (!$can_view) {
123-
return Response::redirect('login', [
124-
'redirect_to' => \Minz\Url::for('link', ['id' => $link_id]),
125-
]);
119+
if ($user) {
120+
auth\Access::require($user, 'view', $link);
121+
} elseif (!auth\Access::can($user, 'view', $link)) {
122+
auth\CurrentUser::require();
126123
}
127124

128125
if ($user) {
@@ -135,7 +132,6 @@ public function show(Request $request): Response
135132

136133
return Response::ok('links/show.phtml', [
137134
'link' => $link,
138-
'can_update' => $can_update,
139135
'content' => '',
140136
'share_on_mastodon' => false,
141137
'mastodon_configured' => $mastodon_configured,
@@ -145,23 +141,24 @@ public function show(Request $request): Response
145141
/**
146142
* Show the page to add a link.
147143
*
148-
* @request_param string url The URL to prefill the URL input (default is '')
149-
* @request_param string collection_id Collection to check (default is bookmarks id)
144+
* @request_param string url
145+
* An optional URL to prefill the URL input.
146+
* @request_param string collection_id
147+
* An optional collection to select by default.
150148
*
151-
* @response 302 /login?redirect_to=/links/new if not connected
152149
* @response 200
150+
* On success.
151+
*
152+
* @throws auth\MissingCurrentUserError
153+
* If the user is not connected.
153154
*/
154155
public function new(Request $request): Response
155156
{
157+
$user = auth\CurrentUser::require();
158+
156159
$default_url = $request->parameters->getString('url', '');
157160
$default_collection_id = $request->parameters->getString('collection_id');
158161

159-
$from = \Minz\Url::for('new link', [
160-
'url' => $default_url,
161-
'collection_id' => $default_collection_id,
162-
]);
163-
$user = $this->requireCurrentUser(redirect_after_login: $from);
164-
165162
$default_collection_ids = [];
166163
if ($default_collection_id) {
167164
$default_collection_ids[] = $default_collection_id;
@@ -170,7 +167,9 @@ public function new(Request $request): Response
170167
$link = new models\Link($default_url, $user->id);
171168
$form = new forms\links\NewLink([
172169
'collection_ids' => $default_collection_ids,
173-
], $link);
170+
], $link, [
171+
'user' => $user,
172+
]);
174173

175174
return Response::ok('links/new.phtml', [
176175
'form' => $form,
@@ -183,27 +182,28 @@ public function new(Request $request): Response
183182
* @request_param string url
184183
* @request_param string[] collection_ids
185184
* @request_param string[] new_collection_names
185+
* @request_param boolean read_later
186186
* @request_param boolean is_hidden
187-
* @request_param string csrf
187+
* @request_param string csrf_token
188188
*
189-
* @response 302 /login?redirect_to=/links/new
190-
* If not connected.
191189
* @response 400
192-
* If CSRF or the url is invalid, if one collection id doesn't exist
193-
* or if both collection_ids and new_collection_names parameters are
194-
* missing/empty.
190+
* If at least one of the parameters is invalid.
195191
* @response 302 /links/:id
196192
* On success.
193+
*
194+
* @throws auth\MissingCurrentUserError
195+
* If the user is not connected.
197196
*/
198197
public function create(Request $request): Response
199198
{
200-
$url = $request->parameters->getString('url', '');
199+
$user = auth\CurrentUser::require();
201200

202-
$from = \Minz\Url::for('new link', ['url' => $url]);
203-
$user = $this->requireCurrentUser(redirect_after_login: $from);
201+
$url = $request->parameters->getString('url', '');
204202

205203
$link = $user->findOrBuildLink($url);
206-
$form = new forms\links\NewLink(model: $link);
204+
$form = new forms\links\NewLink(model: $link, options: [
205+
'user' => $user,
206+
]);
207207

208208
$form->handleRequest($request);
209209

@@ -244,59 +244,58 @@ public function create(Request $request): Response
244244
* Show the update link page.
245245
*
246246
* @request_param string id
247-
* @request_param string from (default is /links/:id)
248247
*
249-
* @response 302 /login?redirect_to=:from if not connected
250-
* @response 404 if the link doesn't exist or not associated to the current user
251248
* @response 200
249+
* On success.
250+
*
251+
* @throws auth\MissingCurrentUserError
252+
* If the user is not connected.
253+
* @throws \Minz\Errors\MissingRecordError
254+
* If the link doesn't exist.
255+
* @throws auth\AccessDeniedError
256+
* If the user cannot update the link.
252257
*/
253258
public function edit(Request $request): Response
254259
{
255-
$link_id = $request->parameters->getString('id', '');
256-
$from = $request->parameters->getString('from', \Minz\Url::for('link', ['id' => $link_id]));
260+
$user = auth\CurrentUser::require();
261+
$link = models\Link::requireFromRequest($request);
257262

258-
$user = $this->requireCurrentUser(redirect_after_login: $from);
259-
260-
$link = models\Link::find($link_id);
261-
262-
if (!$link || !auth\LinksAccess::canUpdate($user, $link)) {
263-
return Response::notFound('not_found.phtml');
264-
}
263+
auth\Access::require($user, 'update', $link);
265264

266265
$form = new forms\links\EditLink(model: $link);
267266

268267
return Response::ok('links/edit.phtml', [
269268
'link' => $link,
270269
'form' => $form,
271-
'from' => $from,
272270
]);
273271
}
274272

275273
/**
276274
* Update a link.
277275
*
278-
* @request_param string csrf
279276
* @request_param string id
280277
* @request_param string title
281278
* @request_param integer reading_time
282-
* @request_param string from (default is /links/:id)
279+
* @request_param string csrf_token
283280
*
284-
* @response 302 /login?redirect_to=/links/:id if not connected
285-
* @response 404 if the link doesn't exist or not associated to the current user
286-
* @response 400 :from if csrf token or title are invalid
281+
* @response 400
282+
* If at least one of the parameters is invalid.
287283
* @response 302 :from
284+
* On success.
285+
*
286+
* @throws auth\MissingCurrentUserError
287+
* If the user is not connected.
288+
* @throws \Minz\Errors\MissingRecordError
289+
* If the link doesn't exist.
290+
* @throws auth\AccessDeniedError
291+
* If the user cannot update the link.
288292
*/
289293
public function update(Request $request): Response
290294
{
291-
$link_id = $request->parameters->getString('id', '');
292-
$from = $request->parameters->getString('from', \Minz\Url::for('link', ['id' => $link_id]));
293-
294-
$user = $this->requireCurrentUser(redirect_after_login: $from);
295+
$user = auth\CurrentUser::require();
296+
$link = models\Link::requireFromRequest($request);
295297

296-
$link = models\Link::find($link_id);
297-
if (!$link || !auth\LinksAccess::canUpdate($user, $link)) {
298-
return Response::notFound('not_found.phtml');
299-
}
298+
auth\Access::require($user, 'update', $link);
300299

301300
$form = new forms\links\EditLink(model: $link);
302301
$form->handleRequest($request);
@@ -305,39 +304,41 @@ public function update(Request $request): Response
305304
return Response::badRequest('links/edit.phtml', [
306305
'link' => $link,
307306
'form' => $form,
308-
'from' => $from,
309307
]);
310308
}
311309

312310
$link = $form->model();
313311
$link->save();
314312

315-
return Response::found($from);
313+
return Response::found(utils\RequestHelper::from($request));
316314
}
317315

318316
/**
319317
* Delete a link.
320318
*
321319
* @request_param string id
322-
* @request_param string from default is /links/:id
323320
* @request_param string csrf_token
324321
*
325-
* @response 302 /login?redirect_to=:from if not connected
326-
* @response 404 if the link doesn’t exist or user hasn't access
327-
* @response 302 :from if csrf is invalid
328-
* @response 302 :from on success
322+
* @response 302 :from
323+
* If the CSRF token is invalid.
324+
* @response 302 :from
325+
* On success.
326+
*
327+
* @throws auth\MissingCurrentUserError
328+
* If the user is not connected.
329+
* @throws \Minz\Errors\MissingRecordError
330+
* If the link doesn't exist.
331+
* @throws auth\AccessDeniedError
332+
* If the user cannot delete the link.
329333
*/
330334
public function delete(Request $request): Response
331335
{
332-
$link_id = $request->parameters->getString('id', '');
333-
$from = $request->parameters->getString('from', \Minz\Url::for('link', ['id' => $link_id]));
336+
$user = auth\CurrentUser::require();
337+
$link = models\Link::requireFromRequest($request);
334338

335-
$user = $this->requireCurrentUser(redirect_after_login: $from);
339+
auth\Access::require($user, 'delete', $link);
336340

337-
$link = models\Link::find($link_id);
338-
if (!$link || !auth\LinksAccess::canDelete($user, $link)) {
339-
return Response::notFound('not_found.phtml');
340-
}
341+
$from = utils\RequestHelper::from($request);
341342

342343
$form = new forms\links\DeleteLink();
343344
$form->handleRequest($request);

0 commit comments

Comments
 (0)