1
+ <?php
2
+
3
+ namespace Darryldecode \Backend \Components \Auth \Commands ;
4
+
5
+ use Darryldecode \Backend \Base \Commands \Command ;
6
+ use Darryldecode \Backend \Base \Commands \CommandResult ;
7
+ use Illuminate \Contracts \Bus \SelfHandling ;
8
+ use Illuminate \Contracts \Validation \Factory ;
9
+ use Darryldecode \Backend \Components \User \Models \Throttle ;
10
+ use Darryldecode \Backend \Components \User \Models \User ;
11
+ use Illuminate \Support \Facades \Auth ;
12
+ use Carbon \Carbon ;
13
+
14
+ class AuthenticateCommand extends Command implements SelfHandling {
15
+ /**
16
+ * @var null
17
+ */
18
+ private $ email ;
19
+ /**
20
+ * @var null
21
+ */
22
+ private $ password ;
23
+ /**
24
+ * @var bool
25
+ */
26
+ private $ remember ;
27
+
28
+ /**
29
+ * @param string $email
30
+ * @param string $password
31
+ * @param bool $remember
32
+ */
33
+ public function __construct ($ email , $ password , $ remember = false )
34
+ {
35
+ parent ::__construct ();
36
+ $ this ->email = $ email ;
37
+ $ this ->password = $ password ;
38
+ $ this ->remember = $ remember ;
39
+ $ this ->args = func_get_args ();
40
+ }
41
+
42
+ /**
43
+ * @param Factory $validator
44
+ * @param Throttle $throttle
45
+ * @param User $user
46
+ * @return CommandResult
47
+ */
48
+ public function handle (Factory $ validator , Throttle $ throttle , User $ user )
49
+ {
50
+ // validate data
51
+ $ validationResult = $ validator ->make (array (
52
+ 'email ' => $ this ->email ,
53
+ 'password ' => $ this ->password ,
54
+ ), array (
55
+ 'email ' => 'required|email ' ,
56
+ 'password ' => 'required ' ,
57
+ ));
58
+
59
+ if ( $ validationResult ->fails () )
60
+ {
61
+ return new CommandResult (false , $ validationResult ->getMessageBag ()->first (), null , 400 );
62
+ }
63
+
64
+ // we need to flag that a user that is authenticating has no throttle entry by default
65
+ $ throttleEntry = false ;
66
+
67
+ // check if the user exist and get its throttle entry
68
+ // then we will check if the user is suspended or banned
69
+ if ( $ user = $ user ->where ('email ' ,$ this ->email )->first () )
70
+ {
71
+ if ( ! $ throttleEntry = $ throttle ->where ('user_id ' ,$ user ->id )->first () )
72
+ {
73
+ $ throttleEntry = $ throttle ::create (array (
74
+ 'user_id ' => $ user ->id
75
+ ));
76
+ }
77
+
78
+ // if the user is currently suspended, lets check its suspension is already expire
79
+ // so we can clear its login attempts and attempt it to login again,
80
+ // if not expired yet, then we will redirect it back with the suspended notice
81
+ if ( $ throttleEntry ->isSuspended () )
82
+ {
83
+ $ now = Carbon::now ();
84
+ $ suspendedUntil = Carbon::createFromTimeStamp (strtotime ($ throttleEntry ->suspended_at ))->addMinutes ($ throttle ->getSuspensionTime ());
85
+
86
+ if ( $ now > $ suspendedUntil )
87
+ {
88
+ $ throttleEntry ->clearLoginAttempts ();
89
+ $ throttleEntry ->unSuspend ();
90
+ }
91
+ else
92
+ {
93
+ $ minsRemaining = $ now ->diffInMinutes ($ suspendedUntil );
94
+
95
+ return new CommandResult (false , 'This account is currently suspended. You can login after ' .$ minsRemaining .' minutes. ' , null , 401 );
96
+ }
97
+ }
98
+
99
+ // if the user is currently banned, no need to do anything
100
+ // we will just redirect it back with banned notice
101
+ elseif ( $ throttleEntry ->isBanned () )
102
+ {
103
+ return new CommandResult (false , "This account is currently banned. " , null , 401 );
104
+ }
105
+ }
106
+
107
+ // attempt to login
108
+ if (Auth::attempt (array ('email ' =>$ this ->email , 'password ' =>$ this ->password ), $ this ->remember ))
109
+ {
110
+ $ throttleEntry ->clearLoginAttempts ();
111
+
112
+ return new CommandResult (true , "Authentication Successful. " , Auth::user (), 200 );
113
+ }
114
+
115
+ // login attempt failed, let's increment login attempt
116
+ if ( $ throttleEntry )
117
+ {
118
+ $ throttleEntry ->addLoginAttempt ();
119
+
120
+ return new CommandResult (false , "These credentials do not match our records. Login attempt remaining: " .$ throttleEntry ->getRemainingLoginAttempts (), null , 401 );
121
+ }
122
+
123
+ return new CommandResult (false , "These credentials do not match our records. " , null , 401 );
124
+ }
125
+ }
0 commit comments