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