1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Auth; |
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
6
|
|
|
use App\Models\Activation; |
7
|
|
|
use App\Models\Profile; |
8
|
|
|
use App\Traits\ActivationTrait; |
9
|
|
|
use App\Traits\CaptureIpTrait; |
10
|
|
|
use Auth; |
11
|
|
|
use Carbon\Carbon; |
12
|
|
|
use Illuminate\Support\Facades\Log; |
13
|
|
|
use Illuminate\Support\Facades\Route; |
14
|
|
|
use jeremykenedy\LaravelRoles\Models\Role; |
15
|
|
|
|
16
|
|
|
class ActivateController extends Controller |
17
|
|
|
{ |
18
|
|
|
use ActivationTrait; |
|
|
|
|
19
|
|
|
|
20
|
|
|
private static $userHomeRoute = 'public.home'; |
21
|
|
|
private static $adminHomeRoute = 'public.home'; |
22
|
|
|
private static $activationView = 'auth.activation'; |
23
|
|
|
private static $activationRoute = 'activation-required'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create a new controller instance. |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
$this->middleware('auth'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public static function getUserHomeRoute() |
36
|
|
|
{ |
37
|
|
|
return self::$userHomeRoute; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function getAdminHomeRoute() |
41
|
|
|
{ |
42
|
|
|
return self::$adminHomeRoute; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public static function getActivationView() |
46
|
|
|
{ |
47
|
|
|
return self::$activationView; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function getActivationRoute() |
51
|
|
|
{ |
52
|
|
|
return self::$activationRoute; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public static function activeRedirect($user, $currentRoute) |
56
|
|
|
{ |
57
|
|
|
if ($user->activated) { |
58
|
|
|
Log::info('Activated user attempted to visit '.$currentRoute.'. ', [$user]); |
59
|
|
|
|
60
|
|
|
if ($user->isAdmin()) { |
61
|
|
|
return redirect()->route(self::getAdminHomeRoute()) |
62
|
|
|
->with('status', 'info') |
63
|
|
|
->with('message', trans('auth.alreadyActivated')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return redirect()->route(self::getUserHomeRoute()) |
67
|
|
|
->with('status', 'info') |
68
|
|
|
->with('message', trans('auth.alreadyActivated')); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function initial() |
75
|
|
|
{ |
76
|
|
|
$user = Auth::user(); |
77
|
|
|
$lastActivation = Activation::where('user_id', $user->id)->get()->last(); |
|
|
|
|
78
|
|
|
$currentRoute = Route::currentRouteName(); |
79
|
|
|
|
80
|
|
|
$rCheck = $this->activeRedirect($user, $currentRoute); |
81
|
|
|
if ($rCheck) { |
82
|
|
|
return $rCheck; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$data = [ |
86
|
|
|
'email' => $user->email, |
|
|
|
|
87
|
|
|
'date' => $lastActivation->created_at->format('m/d/Y'), |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
return view($this->getActivationView())->with($data); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function activationRequired() |
94
|
|
|
{ |
95
|
|
|
$user = Auth::user(); |
96
|
|
|
$lastActivation = Activation::where('user_id', $user->id)->get()->last(); |
|
|
|
|
97
|
|
|
$currentRoute = Route::currentRouteName(); |
98
|
|
|
|
99
|
|
|
$rCheck = $this->activeRedirect($user, $currentRoute); |
100
|
|
|
if ($rCheck) { |
101
|
|
|
return $rCheck; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($user->activated == false) { |
|
|
|
|
105
|
|
|
$activationsCount = Activation::where('user_id', $user->id) |
106
|
|
|
->where('created_at', '>=', Carbon::now()->subHours(config('settings.timePeriod'))) |
107
|
|
|
->count(); |
108
|
|
|
|
109
|
|
|
if ($activationsCount > config('settings.timePeriod')) { |
110
|
|
|
Log::info('Exceded max resends in last '.config('settings.timePeriod').' hours. '.$currentRoute.'. ', [$user]); |
111
|
|
|
|
112
|
|
|
$data = [ |
113
|
|
|
'email' => $user->email, |
|
|
|
|
114
|
|
|
'hours' => config('settings.timePeriod'), |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
return view('auth.exceeded')->with($data); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
Log::info('Registered attempted to navigate while unactivate. '.$currentRoute.'. ', [$user]); |
122
|
|
|
|
123
|
|
|
$data = [ |
124
|
|
|
'email' => $user->email, |
125
|
|
|
'date' => $lastActivation->created_at->format('m/d/Y'), // |
126
|
|
|
]; |
127
|
|
|
|
128
|
|
|
return view($this->getActivationView())->with($data); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function activate($token) |
132
|
|
|
{ |
133
|
|
|
$user = Auth::user(); |
134
|
|
|
$currentRoute = Route::currentRouteName(); |
135
|
|
|
$ipAddress = new CaptureIpTrait(); |
136
|
|
|
$role = Role::where('name', '=', 'user')->first(); |
137
|
|
|
$profile = new Profile(); |
138
|
|
|
|
139
|
|
|
$rCheck = $this->activeRedirect($user, $currentRoute); |
140
|
|
|
if ($rCheck) { |
141
|
|
|
return $rCheck; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$activation = Activation::where('token', $token)->get() |
145
|
|
|
->where('user_id', $user->id) |
|
|
|
|
146
|
|
|
->first(); |
147
|
|
|
|
148
|
|
|
if (empty($activation)) { |
149
|
|
|
Log::info('Registered user attempted to activate with an invalid token: '.$currentRoute.'. ', [$user]); |
150
|
|
|
|
151
|
|
|
return redirect()->route(self::getActivationRoute()) |
152
|
|
|
->with('error', trans('auth.invalidToken')); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$user->activated = true; |
|
|
|
|
156
|
|
|
$user->detachAllRoles(); |
|
|
|
|
157
|
|
|
$user->attachRole($role); |
|
|
|
|
158
|
|
|
$user->signup_confirmation_ip_address = $ipAddress->getClientIp(); |
|
|
|
|
159
|
|
|
$user->profile()->save($profile); |
|
|
|
|
160
|
|
|
$user->save(); |
161
|
|
|
|
162
|
|
|
$allActivations = Activation::where('user_id', $user->id)->get(); |
163
|
|
|
foreach ($allActivations as $anActivation) { |
164
|
|
|
$anActivation->delete(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
Log::info('Registered user successfully activated. '.$currentRoute.'. ', [$user]); |
168
|
|
|
|
169
|
|
|
if ($user->isAdmin()) { |
|
|
|
|
170
|
|
|
return redirect()->route(self::$getAdminHomeRoute()) |
|
|
|
|
171
|
|
|
->with('success', trans('auth.successActivated')); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return redirect()->route(self::getUserHomeRoute()) |
175
|
|
|
->with('success', trans('auth.successActivated')); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function resend() |
179
|
|
|
{ |
180
|
|
|
$user = Auth::user(); |
181
|
|
|
$lastActivation = Activation::where('user_id', $user->id)->get()->last(); |
|
|
|
|
182
|
|
|
$currentRoute = Route::currentRouteName(); |
183
|
|
|
|
184
|
|
|
if ($user->activated == false) { |
|
|
|
|
185
|
|
|
$activationsCount = Activation::where('user_id', $user->id) |
186
|
|
|
->where('created_at', '>=', Carbon::now()->subHours(config('settings.timePeriod'))) |
187
|
|
|
->count(); |
188
|
|
|
|
189
|
|
|
if ($activationsCount >= config('settings.maxAttempts')) { |
190
|
|
|
Log::info('Exceded max resends in last '.config('settings.timePeriod').' hours. '.$currentRoute.'. ', [$user]); |
191
|
|
|
|
192
|
|
|
$data = [ |
193
|
|
|
'email' => $user->email, |
|
|
|
|
194
|
|
|
'hours' => config('settings.timePeriod'), |
195
|
|
|
]; |
196
|
|
|
|
197
|
|
|
return view('auth.exceeded')->with($data); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$sendEmail = $this->initiateEmailActivation($user); |
|
|
|
|
201
|
|
|
|
202
|
|
|
Log::info('Activation resent to registered user. '.$currentRoute.'. ', [$user]); |
203
|
|
|
|
204
|
|
|
return redirect()->route(self::getActivationRoute()) |
205
|
|
|
->with('success', trans('auth.activationSent')); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
Log::info('Activated user attempte to navigate to '.$currentRoute.'. ', [$user]); |
209
|
|
|
|
210
|
|
|
return $this->activeRedirect($user, $currentRoute) |
211
|
|
|
->with('status', 'info') |
212
|
|
|
->with('message', trans('auth.alreadyActivated')); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function exceeded() |
216
|
|
|
{ |
217
|
|
|
$user = Auth::user(); |
218
|
|
|
$currentRoute = Route::currentRouteName(); |
219
|
|
|
$timePeriod = config('settings.timePeriod'); |
220
|
|
|
$lastActivation = Activation::where('user_id', $user->id)->get()->last(); |
|
|
|
|
221
|
|
|
$activationsCount = Activation::where('user_id', $user->id) |
222
|
|
|
->where('created_at', '>=', Carbon::now()->subHours($timePeriod)) |
223
|
|
|
->count(); |
224
|
|
|
|
225
|
|
|
if ($activationsCount >= config('settings.maxAttempts')) { |
226
|
|
|
Log::info('Locked non-activated user attempted to visit '.$currentRoute.'. ', [$user]); |
227
|
|
|
|
228
|
|
|
$data = [ |
229
|
|
|
'hours' => config('settings.timePeriod'), |
230
|
|
|
'email' => $user->email, |
|
|
|
|
231
|
|
|
'lastDate' => $lastActivation->created_at->format('m/d/Y'), |
232
|
|
|
]; |
233
|
|
|
|
234
|
|
|
return view('auth.exceeded')->with($data); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $this->activeRedirect($user, $currentRoute) |
238
|
|
|
->with('status', 'info') |
239
|
|
|
->with('message', trans('auth.alreadyActivated')); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|