Skip to content

Commit 0cfe7af

Browse files
WIP
1 parent 93d88fe commit 0cfe7af

File tree

17 files changed

+246
-196
lines changed

17 files changed

+246
-196
lines changed

src/controllers/BaseController.php

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\auth;
66
use App\models;
7+
use App\utils;
78
use Minz\Controller;
89
use Minz\Request;
910
use Minz\Response;
@@ -25,7 +26,7 @@ class BaseController
2526
* @throws errors\MissingCurrentUserError
2627
* If the user is not logged in.
2728
*/
28-
public function requireCurrentUser(string $redirect_after_login): models\User
29+
public function requireCurrentUser(string $redirect_after_login = ''): models\User
2930
{
3031
$current_user = auth\CurrentUser::get();
3132

@@ -46,28 +47,15 @@ public function redirectOnMissingCurrentUser(
4647
): Response {
4748
$redirect_to = $error->redirect_after_login;
4849

49-
if (!$redirect_to) {
50-
$redirect_to = $request->selfUri();
50+
if ($redirect_to === '') {
51+
$redirect_to = utils\RequestHelper::from($request);
5152
}
5253

53-
return Response::redirect('login', [
54-
'redirect_to' => $redirect_to,
55-
]);
56-
}
57-
58-
public function isPathRedirectable(string $path): bool
59-
{
60-
$router = \Minz\Engine::router();
61-
62-
if ($router === null) {
63-
return false;
54+
$login_parameters = [];
55+
if ($redirect_to) {
56+
$login_parameters['redirect_to'] = $redirect_to;
6457
}
6558

66-
try {
67-
$router->match('GET', $path);
68-
return true;
69-
} catch (\Minz\Errors\RouteNotFoundError $e) {
70-
return false;
71-
}
59+
return Response::redirect('login', $login_parameters);
7260
}
7361
}

src/controllers/Links.php

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ 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+
*
31+
* @throws errors\MissingCurrentUserError
32+
* If the user is not connected.
3233
*/
3334
public function index(Request $request): Response
3435
{
35-
$user = $this->requireCurrentUser(redirect_after_login: \Minz\Url::for('links'));
36+
$user = $this->requireCurrentUser();
3637

3738
$query = $request->parameters->getString('q');
3839
$pagination_page = $request->parameters->getInteger('page', 1);
@@ -99,11 +100,13 @@ public function index(Request $request): Response
99100
*
100101
* @request_param string id
101102
*
102-
* @response 302 /login?redirect_to=/links/:id
103-
* if user is not connected and the link is not public
104103
* @response 404
105104
* if the link doesn't exist or is inaccessible to current user
106105
* @response 200
106+
*
107+
* @throws errors\MissingCurrentUserError
108+
* If the link exists but require the users to be logged in while
109+
* they are not.
107110
*/
108111
public function show(Request $request): Response
109112
{
@@ -120,9 +123,7 @@ public function show(Request $request): Response
120123
if (!$can_view && $user) {
121124
return Response::notFound('not_found.phtml');
122125
} elseif (!$can_view) {
123-
return Response::redirect('login', [
124-
'redirect_to' => \Minz\Url::for('link', ['id' => $link_id]),
125-
]);
126+
throw new errors\MissingCurrentUserError();
126127
}
127128

128129
if ($user) {
@@ -148,20 +149,18 @@ public function show(Request $request): Response
148149
* @request_param string url The URL to prefill the URL input (default is '')
149150
* @request_param string collection_id Collection to check (default is bookmarks id)
150151
*
151-
* @response 302 /login?redirect_to=/links/new if not connected
152152
* @response 200
153+
*
154+
* @throws errors\MissingCurrentUserError
155+
* If the user is not connected.
153156
*/
154157
public function new(Request $request): Response
155158
{
159+
$user = $this->requireCurrentUser();
160+
156161
$default_url = $request->parameters->getString('url', '');
157162
$default_collection_id = $request->parameters->getString('collection_id');
158163

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-
165164
$default_collection_ids = [];
166165
if ($default_collection_id) {
167166
$default_collection_ids[] = $default_collection_id;
@@ -183,24 +182,25 @@ 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
192190
* If CSRF or the url is invalid, if one collection id doesn't exist
193191
* or if both collection_ids and new_collection_names parameters are
194192
* missing/empty.
195193
* @response 302 /links/:id
196194
* On success.
195+
*
196+
* @throws errors\MissingCurrentUserError
197+
* If the user is not connected.
197198
*/
198199
public function create(Request $request): Response
199200
{
200201
$url = $request->parameters->getString('url', '');
201202

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

205205
$link = $user->findOrBuildLink($url);
206206
$form = new forms\links\NewLink(model: $link);
@@ -244,19 +244,18 @@ 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
250248
* @response 404 if the link doesn't exist or not associated to the current user
251249
* @response 200
250+
*
251+
* @throws errors\MissingCurrentUserError
252+
* If the user is not connected.
252253
*/
253254
public function edit(Request $request): Response
254255
{
255-
$link_id = $request->parameters->getString('id', '');
256-
$from = $request->parameters->getString('from', \Minz\Url::for('link', ['id' => $link_id]));
257-
258-
$user = $this->requireCurrentUser(redirect_after_login: $from);
256+
$user = $this->requireCurrentUser();
259257

258+
$link_id = $request->parameters->getString('id', '');
260259
$link = models\Link::find($link_id);
261260

262261
if (!$link || !auth\LinksAccess::canUpdate($user, $link)) {
@@ -268,32 +267,31 @@ public function edit(Request $request): Response
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
285281
* @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
282+
* @response 400 if csrf token or title are invalid
287283
* @response 302 :from
284+
*
285+
* @throws errors\MissingCurrentUserError
286+
* If the user is not connected.
288287
*/
289288
public function update(Request $request): Response
290289
{
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);
290+
$user = $this->requireCurrentUser();
295291

292+
$link_id = $request->parameters->getString('id', '');
296293
$link = models\Link::find($link_id);
294+
297295
if (!$link || !auth\LinksAccess::canUpdate($user, $link)) {
298296
return Response::notFound('not_found.phtml');
299297
}
@@ -305,36 +303,37 @@ public function update(Request $request): Response
305303
return Response::badRequest('links/edit.phtml', [
306304
'link' => $link,
307305
'form' => $form,
308-
'from' => $from,
309306
]);
310307
}
311308

312309
$link = $form->model();
313310
$link->save();
314311

315-
return Response::found($from);
312+
return Response::found(utils\RequestHelper::from($request));
316313
}
317314

318315
/**
319316
* Delete a link.
320317
*
321318
* @request_param string id
322-
* @request_param string from default is /links/:id
323319
* @request_param string csrf_token
324320
*
325-
* @response 302 /login?redirect_to=:from if not connected
326321
* @response 404 if the link doesn’t exist or user hasn't access
327-
* @response 302 :from if csrf is invalid
322+
* @response 302 :from if csrf token is invalid
328323
* @response 302 :from on success
324+
*
325+
* @throws errors\MissingCurrentUserError
326+
* If the user is not connected.
329327
*/
330328
public function delete(Request $request): Response
331329
{
332-
$link_id = $request->parameters->getString('id', '');
333-
$from = $request->parameters->getString('from', \Minz\Url::for('link', ['id' => $link_id]));
330+
$user = $this->requireCurrentUser();
334331

335-
$user = $this->requireCurrentUser(redirect_after_login: $from);
332+
$from = utils\RequestHelper::from($request);
336333

334+
$link_id = $request->parameters->getString('id', '');
337335
$link = models\Link::find($link_id);
336+
338337
if (!$link || !auth\LinksAccess::canDelete($user, $link)) {
339338
return Response::notFound('not_found.phtml');
340339
}

src/controllers/Sessions.php

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,22 @@ class Sessions extends BaseController
2929
public function new(Request $request): Response
3030
{
3131
$redirect_to = $request->parameters->getString('redirect_to', \Minz\Url::for('home'));
32+
33+
if (!utils\RequestHelper::isPathRedirectable($redirect_to)) {
34+
$redirect_to = \Minz\Url::for('home');
35+
}
36+
3237
if (auth\CurrentUser::get()) {
3338
return Response::found($redirect_to);
3439
}
3540

36-
return Response::ok('sessions/new.phtml', [
37-
'form' => new forms\Login(),
41+
$form = new forms\Login([
3842
'redirect_to' => $redirect_to,
3943
]);
44+
45+
return Response::ok('sessions/new.phtml', [
46+
'form' => $form,
47+
]);
4048
}
4149

4250
/**
@@ -56,36 +64,29 @@ public function new(Request $request): Response
5664
*/
5765
public function create(Request $request): Response
5866
{
59-
$redirect_to = $request->parameters->getString('redirect_to', \Minz\Url::for('home'));
60-
61-
if (!$this->isPathRedirectable($redirect_to)) {
62-
$redirect_to = \Minz\Url::for('home');
63-
}
64-
65-
if (auth\CurrentUser::get()) {
66-
return Response::found($redirect_to);
67-
}
68-
6967
$form = new forms\Login();
7068
$form->handleRequest($request);
7169

7270
if (!$form->validate()) {
7371
return Response::badRequest('sessions/new.phtml', [
7472
'form' => $form,
75-
'redirect_to' => $redirect_to,
7673
]);
7774
}
7875

79-
$user = $form->user();
76+
$response = Response::found($form->redirect_to);
8077

81-
$session = auth\CurrentUser::createBrowserSession($user, $request);
82-
$session_token = $session->token();
78+
if (!auth\CurrentUser::get()) {
79+
$user = $form->user();
80+
81+
$session = auth\CurrentUser::createBrowserSession($user, $request);
82+
$session_token = $session->token();
83+
84+
$response->setCookie('session_token', $session_token->token, [
85+
'expires' => $session_token->expired_at->getTimestamp(),
86+
'samesite' => 'Lax',
87+
]);
88+
}
8389

84-
$response = Response::found($redirect_to);
85-
$response->setCookie('session_token', $session_token->token, [
86-
'expires' => $session_token->expired_at->getTimestamp(),
87-
'samesite' => 'Lax',
88-
]);
8990
return $response;
9091
}
9192

src/controllers/errors/MissingCurrentUserError.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class MissingCurrentUserError extends \RuntimeException
1313
{
1414
public function __construct(
15-
public string $redirect_after_login
15+
public string $redirect_after_login = '',
1616
) {
1717
parent::__construct();
1818
}

0 commit comments

Comments
 (0)