8
8
9
9
namespace App \Actions \CRM \WebUser \Retina ;
10
10
11
- use App \Actions \CRM \WebUser \LogWebUserFailLogin ;
11
+ use App \Actions \CRM \WebUser \AuthoriseWebUserWithLegacyPassword ;
12
12
use App \Actions \CRM \WebUser \LogWebUserLogin ;
13
+ use App \Actions \SysAdmin \User \LogUserFailLogin ;
14
+ use App \Actions \Traits \WithLogin ;
15
+ use App \Enums \CRM \WebUser \WebUserAuthTypeEnum ;
13
16
use App \Models \CRM \WebUser ;
14
- use Illuminate \Auth \Events \Lockout ;
15
17
use Illuminate \Http \RedirectResponse ;
18
+ use Illuminate \Support \Arr ;
16
19
use Illuminate \Support \Facades \Auth ;
17
20
use Illuminate \Support \Facades \RateLimiter ;
18
21
use Illuminate \Support \Facades \Session ;
19
- use Illuminate \Support \Str ;
20
22
use Illuminate \Validation \ValidationException ;
21
23
use Lorisleiva \Actions \ActionRequest ;
22
24
use Lorisleiva \Actions \Concerns \AsController ;
23
25
24
26
class RetinaLogin
25
27
{
26
28
use AsController;
29
+ use WithLogin;
27
30
28
31
private string $ gate = 'retina ' ;
29
32
@@ -32,32 +35,83 @@ class RetinaLogin
32
35
*/
33
36
public function handle (ActionRequest $ request ): RedirectResponse
34
37
{
35
-
36
38
$ this ->ensureIsNotRateLimited ($ request );
37
39
38
- if (!Auth::guard ('retina ' )->attempt (
39
- array_merge ($ request ->validated (), ['status ' => true ]),
40
- $ request ->boolean ('remember ' )
41
- )) {
42
- RateLimiter::hit ($ this ->throttleKey ($ request ));
40
+ $ websiteId = $ request ->get ('website ' )->id ;
41
+ $ rememberMe = $ request ->boolean ('remember ' );
42
+
43
+ $ authorised = false ;
44
+ $ processed = false ;
45
+ if (config ('app.with_user_legacy_passwords ' )) {
46
+ $ handle = Arr::get ($ request ->validated (), 'username ' );
47
+
48
+
49
+ $ webUser = WebUser::where ('website_id ' , $ websiteId )
50
+ ->where ('status ' , true )
51
+ ->where ('username ' , $ handle )->first ();
52
+ if (!$ webUser ) {
53
+ $ webUser = WebUser::where ('website_id ' , $ websiteId )
54
+ ->where ('status ' , true )
55
+ ->where ('email ' , $ handle )->first ();
56
+ }
57
+
58
+ if ($ webUser and $ webUser ->auth_type == WebUserAuthTypeEnum::AURORA ) {
59
+ $ processed = true ;
60
+ $ authorised = AuthoriseWebUserWithLegacyPassword::run ($ webUser , $ request ->validated ());
61
+ if ($ authorised ) {
62
+ Auth::guard ('retina ' )->login ($ webUser , $ rememberMe );
63
+ }
64
+ }
65
+ }
66
+
67
+ if (!$ processed ) {
68
+ $ credentials = array_merge (
69
+ $ request ->validated (),
70
+ [
71
+ 'website_id ' => $ websiteId ,
72
+ 'status ' => true
73
+ ]
74
+ );
75
+
76
+ $ authorised = Auth::guard ('retina ' )->attempt ($ credentials , $ rememberMe );
77
+
43
78
79
+ if (!$ authorised ) {
80
+ // try now with email
81
+ data_set ($ credentials , 'email ' , $ credentials ['username ' ]);
82
+ data_forget ($ credentials , 'username ' );
44
83
45
- LogWebUserFailLogin::run (
46
- $ request ->get ('website ' ),
84
+ $ authorised = Auth::guard ('retina ' )->attempt ($ credentials , $ rememberMe );
85
+ }
86
+ }
87
+
88
+ if (!$ authorised ) {
89
+ RateLimiter::hit ($ this ->throttleKey ($ request ));
90
+
91
+ LogUserFailLogin::dispatch (
47
92
credentials: $ request ->validated (),
48
93
ip: request ()->ip (),
49
94
userAgent: $ request ->header ('User-Agent ' ),
50
95
datetime: now ()
51
96
);
52
97
53
-
54
-
55
98
throw ValidationException::withMessages ([
56
99
'username ' => trans ('auth.failed ' ),
57
100
]);
58
101
}
59
102
103
+ RateLimiter::clear ($ this ->throttleKey ($ request ));
60
104
105
+ $ retinaHome = 'app/dashboard ' ;
106
+ if ($ ref = $ request ->get ('ref ' )) {
107
+ $ retinaHome = $ ref ;
108
+ }
109
+
110
+ return $ this ->postProcessRetinaLogin ($ request , $ retinaHome );
111
+ }
112
+
113
+ public function postProcessRetinaLogin ($ request , $ retinaHome ): RedirectResponse
114
+ {
61
115
RateLimiter::clear ($ this ->throttleKey ($ request ));
62
116
63
117
/** @var WebUser $webUser */
@@ -80,58 +134,7 @@ public function handle(ActionRequest $request): RedirectResponse
80
134
app ()->setLocale ($ language ->code );
81
135
}
82
136
83
- $ retinaHome = 'app/dashboard ' ;
84
- if ($ ref = $ request ->get ('ref ' )) {
85
- $ retinaHome = $ ref ;
86
- }
87
-
88
137
return redirect ()->intended ($ retinaHome );
89
138
}
90
139
91
- public function rules (): array
92
- {
93
- return [
94
- 'username ' => ['required ' , 'string ' ],
95
- 'password ' => ['required ' , 'string ' ],
96
- ];
97
- }
98
-
99
- /**
100
- * @throws \Illuminate\Validation\ValidationException
101
- */
102
- public function asController (ActionRequest $ request ): RedirectResponse
103
- {
104
- return $ this ->handle ($ request );
105
- }
106
-
107
-
108
- /**
109
- * @throws \Illuminate\Validation\ValidationException
110
- */
111
- public function ensureIsNotRateLimited (ActionRequest $ request ): void
112
- {
113
- if (!RateLimiter::tooManyAttempts ($ this ->throttleKey ($ request ), 5 )) {
114
- return ;
115
- }
116
-
117
- event (new Lockout ($ request ));
118
-
119
- $ seconds = RateLimiter::availableIn ($ this ->throttleKey ($ request ));
120
-
121
- throw ValidationException::withMessages ([
122
- 'username ' => trans ('auth.throttle ' , [
123
- 'seconds ' => $ seconds ,
124
- 'minutes ' => ceil ($ seconds / 60 ),
125
- ]),
126
- ]);
127
- }
128
-
129
- /**
130
- * Get the rate limiting throttle key for the request.
131
- */
132
- public function throttleKey (ActionRequest $ request ): string
133
- {
134
- return Str::transliterate (Str::lower ($ request ->input ('username ' )).'| ' .$ request ->ip ());
135
- }
136
-
137
140
}
0 commit comments