22
33namespace App \controllers \my ;
44
5- use Minz \Request ;
6- use Minz \Response ;
75use App \auth ;
86use App \controllers \BaseController ;
7+ use App \forms ;
98use App \models ;
9+ use Minz \Request ;
10+ use Minz \Response ;
1011
1112/**
1213 * @author Marien Fressinaud <dev@marienfressinaud.fr>
@@ -17,27 +18,23 @@ class Sessions extends BaseController
1718 /**
1819 * List the sessions of the current user
1920 *
20- * @response 302 /login?redirect_to=/my/sessions
21- * If the user is not connected.
22- * @response 302 /my/security/confirmation?from=/my/sessions
23- * If the password is not confirmed.
2421 * @response 200
2522 * On success.
23+ *
24+ * @throws auth\MissingCurrentUserError
25+ * If the user is not connected.
26+ * @throws auth\PasswordNotConfirmedError
27+ * If the password is not confirmed.
2628 */
2729 public function index (Request $ request ): Response
2830 {
29- $ user = $ this -> requireCurrentUser (redirect_after_login: \ Minz \Url:: for ( ' sessions ' ) );
31+ $ user = auth \CurrentUser:: require ( );
3032
31- $ session = auth \CurrentUser::session ();
33+ auth \CurrentUser::requireConfirmedPassword ();
3234
35+ $ session = auth \CurrentUser::session ();
3336 assert ($ session !== null );
3437
35- if (!$ session ->isPasswordConfirmed ()) {
36- return Response::redirect ('password confirmation ' , [
37- 'from ' => \Minz \Url::for ('sessions ' ),
38- ]);
39- }
40-
4138 $ sessions = models \Session::listBy ([
4239 'user_id ' => $ user ->id ,
4340 ], 'created_at DESC ' );
@@ -53,53 +50,50 @@ public function index(Request $request): Response
5350 *
5451 * @request_param string id
5552 *
56- * @response 302 /login?redirect_to=/my/sessions
57- * If the user is not connected.
58- * @response 302 /my/security/confirmation?from=/my/sessions
59- * If the password is not confirmed.
60- * @response 404
61- * If the session doesn't exist.
6253 * @response 302 /my/sessions
54+ * @flash error
6355 * If the CSRF token is invalid.
6456 * @response 302 /my/sessions
6557 * On success.
58+ *
59+ * @throws auth\MissingCurrentUserError
60+ * If the user is not connected.
61+ * @throws auth\PasswordNotConfirmedError
62+ * If the password is not confirmed.
63+ * @throws \Minz\Errors\MissingRecordError
64+ * If the session doesn't exist.
65+ * @throws auth\AccessDeniedError
66+ * If the user cannot delete the session.
6667 */
6768 public function delete (Request $ request ): Response
6869 {
69- $ user = $ this ->requireCurrentUser (redirect_after_login: \Minz \Url::for ('sessions ' ));
70-
71- $ current_session = auth \CurrentUser::session ();
70+ $ user = auth \CurrentUser::require ();
7271
73- assert ( $ current_session !== null );
72+ auth \CurrentUser:: requireConfirmedPassword ( );
7473
75- if (!$ current_session ->isPasswordConfirmed ()) {
76- return Response::redirect ('password confirmation ' , [
77- 'from ' => \Minz \Url::for ('sessions ' ),
78- ]);
79- }
74+ $ session = models \Session::requireFromRequest ($ request );
8075
81- $ session_id = $ request ->parameters ->getString ('id ' , '' );
82- $ csrf = $ request ->parameters ->getString ('csrf ' , '' );
76+ auth \Access::require ($ user , 'delete ' , $ session );
8377
84- $ session = models \Session::findBy ([
85- 'id ' => $ session_id ,
86- 'user_id ' => $ user ->id ,
87- ]);
78+ $ form = new forms \security \DeleteSession ();
79+ $ form ->handleRequest ($ request );
8880
89- if (!$ session ) {
90- return Response::notFound ('not_found.phtml ' );
81+ if (!$ form ->validate ()) {
82+ \Minz \Flash::set ('error ' , $ form ->error ('@base ' ));
83+ return Response::redirect ('sessions ' );
9184 }
9285
9386 $ response = Response::redirect ('sessions ' );
9487
95- if (\App \Csrf::validate ($ csrf )) {
96- if ($ session ->id === $ current_session ->id ) {
97- auth \CurrentUser::deleteSession ();
98- $ response ->removeCookie ('session_token ' );
99- $ response ->removeCookie ('flusio_session_token ' );
100- } else {
101- $ session ->remove ();
102- }
88+ $ current_session = auth \CurrentUser::session ();
89+ assert ($ current_session !== null );
90+
91+ if ($ session ->id === $ current_session ->id ) {
92+ auth \CurrentUser::deleteSession ();
93+ $ response ->removeCookie ('session_token ' );
94+ $ response ->removeCookie ('flusio_session_token ' );
95+ } else {
96+ $ session ->remove ();
10397 }
10498
10599 return $ response ;
0 commit comments