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