@@ -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 }
0 commit comments