2
2
3
3
namespace Webkul \Admin \Http \Controllers \User ;
4
4
5
+ use Illuminate \Http \RedirectResponse ;
6
+ use Illuminate \Support \Collection ;
7
+ use Illuminate \View \View ;
5
8
use Webkul \Admin \Http \Controllers \Controller ;
9
+ use Webkul \Core \Menu \MenuItem ;
6
10
7
11
class SessionController extends Controller
8
12
{
9
13
/**
10
14
* Show the form for creating a new resource.
11
- *
12
- * @return \Illuminate\View\View
13
15
*/
14
- public function create ()
16
+ public function create (): RedirectResponse | View
15
17
{
16
18
if (auth ()->guard ('user ' )->check ()) {
17
19
return redirect ()->route ('admin.dashboard.index ' );
18
- } else {
19
- if (strpos (url ()->previous (), 'admin ' ) !== false ) {
20
- $ intendedUrl = url ()->previous ();
21
- } else {
22
- $ intendedUrl = route ('admin.dashboard.index ' );
23
- }
20
+ }
24
21
25
- session ()->put ( ' url.intended ' , $ intendedUrl );
22
+ $ previousUrl = url ()->previous ( );
26
23
27
- return view ('admin::sessions.login ' );
28
- }
24
+ $ intendedUrl = str_contains ($ previousUrl , 'admin ' )
25
+ ? $ previousUrl
26
+ : route ('admin.dashboard.index ' );
27
+
28
+ session ()->put ('url.intended ' , $ intendedUrl );
29
+
30
+ return view ('admin::sessions.login ' );
29
31
}
30
32
31
33
/**
32
34
* Store a newly created resource in storage.
33
- *
34
- * @return \Illuminate\Http\Response
35
35
*/
36
- public function store ()
36
+ public function store (): RedirectResponse
37
37
{
38
38
$ this ->validate (request (), [
39
39
'email ' => 'required|email ' ,
@@ -54,9 +54,11 @@ public function store()
54
54
return redirect ()->route ('admin.session.create ' );
55
55
}
56
56
57
- if (! bouncer ()->hasPermission ('dashboard ' )) {
58
- $ availableNextMenu = menu ()->getItems ('admin ' )?->first();
57
+ $ menus = menu ()->getItems ('admin ' );
59
58
59
+ $ availableNextMenu = $ menus ?->first();
60
+
61
+ if (! bouncer ()->hasPermission ('dashboard ' )) {
60
62
if (is_null ($ availableNextMenu )) {
61
63
session ()->flash ('error ' , trans ('admin::app.users.not-permission ' ));
62
64
@@ -68,18 +70,48 @@ public function store()
68
70
return redirect ()->to ($ availableNextMenu ->getUrl ());
69
71
}
70
72
71
- return redirect ()->intended (route ('admin.dashboard.index ' ));
73
+ $ hasAccessToIntendedUrl = $ this ->canAccessIntendedUrl ($ menus , redirect ()->getIntendedUrl ());
74
+
75
+ if ($ hasAccessToIntendedUrl ) {
76
+ return redirect ()->intended (route ('admin.dashboard.index ' ));
77
+ }
78
+
79
+ return redirect ()->to ($ availableNextMenu ->getUrl ());
72
80
}
73
81
74
82
/**
75
83
* Remove the specified resource from storage.
76
- *
77
- * @return \Illuminate\Http\Response
78
84
*/
79
- public function destroy ()
85
+ public function destroy (): RedirectResponse
80
86
{
81
87
auth ()->guard ('user ' )->logout ();
82
88
83
89
return redirect ()->route ('admin.session.create ' );
84
90
}
91
+
92
+ /**
93
+ * Find menu item by URL.
94
+ */
95
+ protected function canAccessIntendedUrl (Collection $ menus , ?string $ url ): ?MenuItem
96
+ {
97
+ if (is_null ($ url )) {
98
+ return null ;
99
+ }
100
+
101
+ foreach ($ menus as $ menu ) {
102
+ if ($ menu ->getUrl () === $ url ) {
103
+ return $ menu ;
104
+ }
105
+
106
+ if ($ menu ->haveChildren ()) {
107
+ $ found = $ this ->canAccessIntendedUrl ($ menu ->getChildren (), $ url );
108
+
109
+ if ($ found ) {
110
+ return $ found ;
111
+ }
112
+ }
113
+ }
114
+
115
+ return null ;
116
+ }
85
117
}
0 commit comments