1
|
|
|
<?php namespace jlourenco\base\Controllers;
|
2
|
|
|
|
3
|
|
|
use App\Http\Controllers\Controller;
|
4
|
|
|
use Illuminate\Support\Facades\Request;
|
5
|
|
|
use Sentinel;
|
6
|
|
|
use View;
|
7
|
|
|
use Validator;
|
8
|
|
|
use Input;
|
9
|
|
|
use Session;
|
10
|
|
|
use Redirect;
|
11
|
|
|
use Lang;
|
12
|
|
|
use URL;
|
13
|
|
|
use Activation;
|
14
|
|
|
use Base;
|
15
|
|
|
use \Cartalyst\Sentinel\Checkpoints\NotActivatedException;
|
16
|
|
|
use \Cartalyst\Sentinel\Checkpoints\ThrottlingException;
|
17
|
|
|
use Reminder;
|
18
|
|
|
use Mail;
|
19
|
|
|
use Illuminate\Support\MessageBag;
|
20
|
|
|
|
21
|
|
|
class AuthController extends controller
|
22
|
|
|
{
|
23
|
|
|
use \jlourenco\support\Traits\CaptchaTrait;
|
24
|
|
|
|
25
|
|
|
/**
|
26
|
|
|
* Message bag.
|
27
|
|
|
*
|
28
|
|
|
* @var Illuminate\Support\MessageBag
|
29
|
|
|
*/
|
30
|
|
|
protected $messageBag = null;
|
31
|
|
|
|
32
|
|
|
/**
|
33
|
|
|
* Initializer.
|
34
|
|
|
*
|
35
|
|
|
* @return void
|
|
|
|
|
36
|
|
|
*/
|
37
|
|
|
public function __construct()
|
38
|
|
|
{
|
39
|
|
|
$this->messageBag = new MessageBag;
|
|
|
|
|
40
|
|
|
}
|
41
|
|
|
|
42
|
|
|
/**
|
43
|
|
|
* Account sign in.
|
44
|
|
|
*
|
45
|
|
|
* @return View
|
46
|
|
|
*/
|
47
|
|
View Code Duplication |
public function getSignin()
|
|
|
|
|
48
|
|
|
{
|
49
|
|
|
// Is the user logged in?
|
50
|
|
|
if (Sentinel::check())
|
51
|
|
|
{
|
52
|
|
|
if (Sentinel::inRole('admin'))
|
53
|
|
|
return Redirect::route('home');
|
54
|
|
|
else
|
55
|
|
|
return Redirect::route('user.home');
|
56
|
|
|
}
|
57
|
|
|
|
58
|
|
|
// Show the page
|
59
|
|
|
return View('auth.login');
|
60
|
|
|
}
|
61
|
|
|
|
62
|
|
|
/**
|
63
|
|
|
* Account sign up.
|
64
|
|
|
*
|
65
|
|
|
* @return View
|
66
|
|
|
*/
|
67
|
|
View Code Duplication |
public function getSignup()
|
|
|
|
|
68
|
|
|
{
|
69
|
|
|
// Is the user logged in?
|
70
|
|
|
if (Sentinel::check())
|
71
|
|
|
{
|
72
|
|
|
if (Sentinel::inRole('admin'))
|
73
|
|
|
return Redirect::route('home');
|
74
|
|
|
else
|
75
|
|
|
return Redirect::route('user.home');
|
76
|
|
|
}
|
77
|
|
|
|
78
|
|
|
// Show the page
|
79
|
|
|
return View('auth.register');
|
80
|
|
|
}
|
81
|
|
|
|
82
|
|
|
/**
|
83
|
|
|
* Account sign up.
|
84
|
|
|
*
|
85
|
|
|
* @return View
|
86
|
|
|
*/
|
87
|
|
View Code Duplication |
public function getLostPassword()
|
|
|
|
|
88
|
|
|
{
|
89
|
|
|
// Is the user logged in?
|
90
|
|
|
if (Sentinel::check())
|
91
|
|
|
{
|
92
|
|
|
if (Sentinel::inRole('admin'))
|
93
|
|
|
return Redirect::route('home');
|
94
|
|
|
else
|
95
|
|
|
return Redirect::route('user.home');
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
|
// Show the page
|
99
|
|
|
return View('auth.forgot-password');
|
100
|
|
|
}
|
101
|
|
|
|
102
|
|
|
/**
|
103
|
|
|
* Forgot Password Confirmation page.
|
104
|
|
|
*
|
105
|
|
|
* @param number $userId
|
106
|
|
|
* @param string $passwordResetCode
|
107
|
|
|
* @return View
|
108
|
|
|
*/
|
109
|
|
|
public function getForgotPasswordConfirm($userId, $passwordResetCode)
|
110
|
|
|
{
|
111
|
|
|
// Find the user using the password reset code
|
112
|
|
|
if(!$user = Sentinel::findById($userId))
|
113
|
|
|
{
|
114
|
|
|
// Redirect to the forgot password page
|
115
|
|
|
return Redirect::route('forgot-password')->with('error', Lang::get('base.auth.forgot_password_confirm.error'));
|
116
|
|
|
}
|
117
|
|
|
|
118
|
|
|
// Show the page
|
119
|
|
|
return View('auth.forgot-password-confirm', compact('userId', 'passwordResetCode'));
|
120
|
|
|
}
|
121
|
|
|
|
122
|
|
|
/**
|
123
|
|
|
* Logout page.
|
124
|
|
|
*
|
125
|
|
|
* @return Redirect
|
126
|
|
|
*/
|
127
|
|
|
public function getLogout()
|
128
|
|
|
{
|
129
|
|
|
// Log the user out
|
130
|
|
|
Sentinel::logout();
|
131
|
|
|
|
132
|
|
|
// Redirect to the users page
|
133
|
|
|
return Redirect::to('/')->with('success', Lang::get('base.auth.logged_out'));
|
134
|
|
|
}
|
135
|
|
|
|
136
|
|
|
/**
|
137
|
|
|
* User account activation page.
|
138
|
|
|
*
|
139
|
|
|
* @param number $userId
|
140
|
|
|
* @param string $activationCode
|
141
|
|
|
* @return
|
142
|
|
|
*/
|
143
|
|
|
public function getActivate($userId, $activationCode)
|
144
|
|
|
{
|
145
|
|
|
// Is user logged in?
|
146
|
|
|
if (Sentinel::check())
|
147
|
|
|
{
|
148
|
|
|
if (Sentinel::inRole('admin'))
|
149
|
|
|
return Redirect::route('home');
|
150
|
|
|
else
|
151
|
|
|
return Redirect::route('user.home');
|
152
|
|
|
}
|
153
|
|
|
|
154
|
|
|
// Find the user using the password reset code
|
155
|
|
|
if(!$user = Sentinel::findById($userId))
|
156
|
|
|
{
|
157
|
|
|
// Redirect to the forgot password page
|
158
|
|
|
return Redirect::route('login')->with('error', Lang::get('base.auth.activate.error'));
|
159
|
|
|
}
|
160
|
|
|
// $activation = Activation::exists($user);
|
|
|
|
|
161
|
|
|
|
162
|
|
|
if (Activation::complete($user, $activationCode))
|
163
|
|
|
{
|
164
|
|
|
$user->status = 1;
|
165
|
|
|
$user->save();
|
166
|
|
|
|
167
|
|
|
// Activation was successful
|
168
|
|
|
// Redirect to the login page
|
169
|
|
|
return Redirect::route('login')->with('success', Lang::get('base.auth.activate.success'));
|
170
|
|
|
}
|
171
|
|
|
else
|
172
|
|
|
{
|
173
|
|
|
// Activation not found or not completed.
|
174
|
|
|
$error = Lang::get('base.auth.activate.error');
|
175
|
|
|
return Redirect::route('login')->with('error', $error);
|
176
|
|
|
}
|
177
|
|
|
|
178
|
|
|
}
|
179
|
|
|
|
180
|
|
|
/**
|
181
|
|
|
* Account sign in form processing.
|
182
|
|
|
*
|
183
|
|
|
* @return Redirect
|
184
|
|
|
*/
|
185
|
|
|
public function postSignin()
|
186
|
|
|
{
|
187
|
|
|
$isEmail = preg_match('/@/', Input::get('email'));
|
188
|
|
|
|
189
|
|
|
// Declare the rules for the form validation
|
190
|
|
|
$rules = array(
|
191
|
|
|
'email' => 'required|email',
|
192
|
|
|
'password' => 'required|between:3,32',
|
193
|
|
|
);
|
194
|
|
|
|
195
|
|
|
if (!$isEmail)
|
196
|
|
|
$rules['email'] = 'required';
|
197
|
|
|
|
198
|
|
|
// Create a new validator instance from our validation rules
|
199
|
|
|
$validator = Validator::make(Input::all(), $rules);
|
200
|
|
|
|
201
|
|
|
// If validation fails, we'll exit the operation now.
|
202
|
|
|
if ($validator->fails()) {
|
203
|
|
|
// Ooops.. something went wrong
|
204
|
|
|
return back()->withInput()->withErrors($validator);
|
205
|
|
|
}
|
206
|
|
|
try {
|
207
|
|
|
foreach(Sentinel::createModel()->getLoginNames() as $loginName)
|
208
|
|
|
{
|
209
|
|
|
$data = array(
|
210
|
|
|
$loginName => Input::only('email')["email"],
|
211
|
|
|
"password" => Input::only('password')["password"]
|
212
|
|
|
);
|
213
|
|
|
|
214
|
|
|
// Try to log the user in
|
215
|
|
|
if(Sentinel::authenticate($data, Input::get('remember-me', false)))
|
216
|
|
|
{
|
217
|
|
|
$user = Sentinel::check();
|
218
|
|
|
$user->update(['ip' => Request::ip()]);
|
219
|
|
|
Base::Log($user->username . ' (' . $user->first_name . ' ' . $user->last_name . ') logged in with IP ' . Request::ip() );
|
220
|
|
|
|
221
|
|
|
// Redirect to the dashboard page
|
222
|
|
|
return Redirect::route("home")->with('success', Lang::get('base.auth.signin.success'));
|
223
|
|
|
}
|
224
|
|
|
}
|
225
|
|
|
|
226
|
|
|
$this->messageBag->add('email', Lang::get('base.auth.account.not_found'));
|
227
|
|
|
} catch (NotActivatedException $e) {
|
228
|
|
|
$this->messageBag->add('email', Lang::get('base.auth.account.not_activated'));
|
229
|
|
|
} catch (ThrottlingException $e) {
|
230
|
|
|
$delay = $e->getDelay();
|
231
|
|
|
$this->messageBag->add('email', Lang::get('base.auth.account.suspended', compact('delay' )));
|
232
|
|
|
}
|
233
|
|
|
Base::Log('Login attempt registred for ' . Input::only('email')["email"] . ' from IP ' . Request::ip() );
|
234
|
|
|
|
235
|
|
|
// Ooops.. something went wrong
|
236
|
|
|
return back()->withInput()->withErrors($this->messageBag);
|
237
|
|
|
}
|
238
|
|
|
|
239
|
|
|
/**
|
240
|
|
|
* Account sign up form processing.
|
241
|
|
|
*
|
242
|
|
|
* @return Redirect
|
243
|
|
|
*/
|
244
|
|
|
public function postSignup()
|
245
|
|
|
{
|
246
|
|
|
/************TEMP VARIABLE************/
|
247
|
|
|
/*
|
248
|
|
|
* 0 - Disabled
|
249
|
|
|
* 1 - Enabled and no activation
|
250
|
|
|
* 2 - User activation
|
251
|
|
|
* 3 - Admin activation
|
252
|
|
|
*/
|
253
|
|
|
|
254
|
|
|
$signupStatus = \Base::getSetting('USER_REGISTRATION');
|
255
|
|
|
/************TEMP VARIABLE************/
|
256
|
|
|
|
257
|
|
|
$signupEnabled = $signupStatus != 0;
|
258
|
|
|
$userActivation = $signupStatus == 2;
|
259
|
|
|
$adminActivation = $signupStatus == 3;
|
260
|
|
|
|
261
|
|
|
if (!$signupEnabled)
|
262
|
|
|
return Redirect::to(URL::previous())->withInput()->with('error', Lang::get('base.auth.account.registration_disabled'));
|
|
|
|
|
263
|
|
|
|
264
|
|
|
$rules = array();
|
265
|
|
|
|
266
|
|
|
// Declare the rules for the form validation
|
267
|
|
|
foreach(Sentinel::createModel()->getRegisterFields() as $fieldid => $field)
|
268
|
|
|
$rules[$fieldid] = $field['validator'];
|
269
|
|
|
|
270
|
|
|
$rules['g-recaptcha-response'] = 'required';
|
271
|
|
|
|
272
|
|
|
// Create a new validator instance from our validation rules
|
273
|
|
|
$validator = Validator::make(Input::all(), $rules);
|
274
|
|
|
|
275
|
|
|
$err = false;
|
276
|
|
|
|
277
|
|
|
// If validation fails, we'll exit the operation now.
|
278
|
|
|
if ($validator->fails() || ($err = $this->captchaCheck()) == false) {
|
|
|
|
|
279
|
|
|
if ($err)
|
280
|
|
|
return Redirect::to(URL::previous())->withInput()->withErrors(['g-recaptcha-response' => Lang::get('base.captcha.error')]);
|
|
|
|
|
281
|
|
|
|
282
|
|
|
// Ooops.. something went wrong
|
283
|
|
|
return Redirect::to(URL::previous())->withInput()->withErrors($validator);
|
|
|
|
|
284
|
|
|
}
|
285
|
|
|
|
286
|
|
|
try {
|
287
|
|
|
$data = array();
|
288
|
|
|
|
289
|
|
|
// Set the data to the user from the User class
|
290
|
|
|
foreach(Sentinel::createModel()->getRegisterFields() as $fieldid => $field)
|
291
|
|
|
$data[$fieldid] = Input::get($fieldid);
|
292
|
|
|
|
293
|
|
|
// Set the standard data to the user
|
294
|
|
|
$data['ip'] = Request::getClientIP();
|
295
|
|
|
$data['status'] = 0;
|
296
|
|
|
$data['staff'] = 0;
|
297
|
|
|
if ($data['birthday'] != null)
|
298
|
|
|
$data['birthday'] = \Carbon\Carbon::createFromFormat('d/m/Y', $data['birthday']);
|
299
|
|
|
|
300
|
|
|
// Find the user if it exists and needs to be created
|
301
|
|
|
$user = Sentinel::getUserRepository()->findByCredentials(['email' => Input::get('email')]);
|
302
|
|
|
|
303
|
|
|
if ($user != null)
|
304
|
|
|
{
|
305
|
|
|
// Update the temporary user to the new one
|
306
|
|
|
if (Sentinel::validForUpdate($data, ['email' => Input::get('email')])) {
|
307
|
|
|
$testing = Sentinel::createModel()->getRegisterFields();
|
308
|
|
|
$user = Sentinel::findById($user->id);
|
309
|
|
|
|
310
|
|
|
foreach($data as $fieldid => $field)
|
311
|
|
|
if (!isset($testing[$fieldid]) || $testing[$fieldid]['save'] == true)
|
312
|
|
|
$user[$fieldid] = $field;
|
313
|
|
|
|
314
|
|
|
$user['password'] = bcrypt($user['password']);
|
315
|
|
|
Sentinel::update($user, ['email' => Input::get('email')]);
|
316
|
|
|
}
|
317
|
|
|
else
|
318
|
|
|
return Redirect::to(URL::previous())->withInput()->with('error', Lang::get('base.auth.account.registration_failed'));
|
|
|
|
|
319
|
|
|
}
|
320
|
|
|
// Register the user
|
321
|
|
|
else
|
322
|
|
|
$user = Sentinel::register($data, false);
|
323
|
|
|
|
324
|
|
|
// If the user needs to activate the account send him an email
|
325
|
|
|
if ($userActivation)
|
326
|
|
|
{
|
327
|
|
|
$activation = Activation::create($user);
|
328
|
|
|
|
329
|
|
|
// Data to be used on the email view
|
330
|
|
|
$data = array(
|
331
|
|
|
'user' => $user,
|
332
|
|
|
'activationUrl' => URL::route('activate', [$user->id, $activation->code]),
|
333
|
|
|
);
|
334
|
|
|
|
335
|
|
|
// Send the activation code through email
|
336
|
|
|
Mail::queue('emails.auth.register-activate', $data, function ($m) use ($user) {
|
337
|
|
|
$m->to($user->email, $user->first_name . ' ' . $user->last_name);
|
338
|
|
|
$m->subject(Lang::get('base.mails.welcome') . ' ' . $user->first_name);
|
339
|
|
|
});
|
340
|
|
|
}
|
341
|
|
|
|
342
|
|
|
// If the administrator needs to activate the account send the user a warning saying that
|
343
|
|
|
if ($adminActivation)
|
344
|
|
|
{
|
345
|
|
|
Mail::queue('emails.auth.register-admin-activate', ['user' => $user], function ($m) use ($user) {
|
346
|
|
|
$m->to($user->email, $user->first_name . ' ' . $user->last_name);
|
347
|
|
|
$m->subject(Lang::get('base.mails.welcome') . ' ' . $user->first_name);
|
348
|
|
|
});
|
349
|
|
|
}
|
350
|
|
|
|
351
|
|
|
// Log the user in
|
352
|
|
|
if (!$adminActivation && !$userActivation)
|
353
|
|
|
{
|
354
|
|
|
$activation = Activation::create($user);
|
355
|
|
|
|
356
|
|
|
if (Activation::complete($user, $activation->code)) {
|
357
|
|
|
Sentinel::login($user, false);
|
358
|
|
|
|
359
|
|
|
Mail::queue('emails.auth.activated', ['user' => $user], function ($m) use ($user) {
|
360
|
|
|
$m->to($user->email, $user->first_name . ' ' . $user->last_name);
|
361
|
|
|
$m->subject(Lang::get('base.mails.welcome') . ' ' . $user->first_name);
|
362
|
|
|
});
|
363
|
|
|
}
|
364
|
|
|
}
|
365
|
|
|
|
366
|
|
|
Base::Log('New account registred for ' . $user->username . ' (' . $user->first_name . ' ' . $user->last_name . ') from IP ' . Request::ip() );
|
367
|
|
|
|
368
|
|
|
// Redirect to the home page with success menu
|
369
|
|
|
return Redirect::to("login")->with('success', Lang::get('base.auth.signup.success') . $adminActivation ? Lang::get('base.auth.signup.admin') : $userActivation ? Lang::get('base.auth.signup.self') : Lang::get('base.auth.signup.ready'));
|
|
|
|
|
370
|
|
|
} catch (UserExistsException $e) {
|
|
|
|
|
371
|
|
|
$this->messageBag->add('email', Lang::get('base.auth.account.already_exists'));
|
372
|
|
|
}
|
373
|
|
|
|
374
|
|
|
Base::Log('New account registration attempt from IP ' . Request::ip() );
|
375
|
|
|
|
376
|
|
|
// Ooops.. something went wrong
|
377
|
|
|
return Redirect::back()->withInput()->withErrors($this->messageBag);
|
378
|
|
|
}
|
379
|
|
|
|
380
|
|
|
/**
|
381
|
|
|
* Forgot password form processing page.
|
382
|
|
|
*
|
383
|
|
|
* @return Redirect
|
384
|
|
|
*/
|
385
|
|
|
public function postForgotPassword()
|
386
|
|
|
{
|
387
|
|
|
// Declare the rules for the validator
|
388
|
|
|
$rules = array(
|
389
|
|
|
'email' => 'required|email',
|
390
|
|
|
);
|
391
|
|
|
|
392
|
|
|
// Create a new validator instance from our dynamic rules
|
393
|
|
|
$validator = Validator::make(Input::all(), $rules);
|
394
|
|
|
|
395
|
|
|
// If validation fails, we'll exit the operation now.
|
396
|
|
|
if ($validator->fails()) {
|
397
|
|
|
// Ooops.. something went wrong
|
398
|
|
|
return Redirect::to(URL::previous())->withInput()->withErrors($validator);
|
399
|
|
|
}
|
400
|
|
|
|
401
|
|
|
// Get the user password recovery code
|
402
|
|
|
$user = Sentinel::findByCredentials(['email' => Input::get('email')]);
|
403
|
|
|
|
404
|
|
|
if($user)
|
405
|
|
|
{
|
406
|
|
|
//get reminder for user
|
407
|
|
|
$reminder = Reminder::exists($user) ?: Reminder::create($user);
|
408
|
|
|
|
409
|
|
|
// Data to be used on the email view
|
410
|
|
|
$data = array(
|
411
|
|
|
'user' => $user,
|
412
|
|
|
'forgotPasswordUrl' => URL::route('forgot-password-confirm',[$user->id, $reminder->code]),
|
413
|
|
|
);
|
414
|
|
|
|
415
|
|
|
Base::Log('Forgot password request for ' . $user->username . ' (' . $user->first_name . ' ' . $user->last_name . ') from IP ' . Request::ip() );
|
416
|
|
|
|
417
|
|
|
// Send the activation code through email
|
418
|
|
|
Mail::queue('emails.auth.forgot-password', $data, function ($m) use ($user) {
|
419
|
|
|
$m->to($user->email, $user->first_name . ' ' . $user->last_name);
|
420
|
|
|
$m->subject(Lang::get('base.mails.recovery'));
|
421
|
|
|
});
|
422
|
|
|
}
|
423
|
|
|
|
424
|
|
|
// Redirect to the forgot password
|
425
|
|
|
return Redirect::to(URL::previous())->with('success', Lang::get('base.auth.forgot_password.success'));
|
426
|
|
|
}
|
427
|
|
|
|
428
|
|
|
/**
|
429
|
|
|
* Forgot Password Confirmation form processing page.
|
430
|
|
|
*
|
431
|
|
|
* @param number $userId
|
432
|
|
|
* @param string $passwordResetCode
|
433
|
|
|
* @return Redirect
|
434
|
|
|
*/
|
435
|
|
|
public function postForgotPasswordConfirm($userId, $passwordResetCode)
|
436
|
|
|
{
|
437
|
|
|
// Declare the rules for the form validation
|
438
|
|
|
$rules = array(
|
439
|
|
|
'password' => 'required|between:3,32',
|
440
|
|
|
'password_confirm' => 'required|same:password'
|
441
|
|
|
);
|
442
|
|
|
|
443
|
|
|
// Create a new validator instance from our dynamic rules
|
444
|
|
|
$validator = Validator::make(Input::all(), $rules);
|
445
|
|
|
|
446
|
|
|
// If validation fails, we'll exit the operation now.
|
447
|
|
|
if ($validator->fails()) {
|
448
|
|
|
// Ooops.. something went wrong
|
449
|
|
|
return Redirect::route('forgot-password-confirm', compact(['userId','passwordResetCode']))->withInput()->withErrors($validator);
|
450
|
|
|
}
|
451
|
|
|
|
452
|
|
|
// Find the user using the password reset code
|
453
|
|
|
$user = Sentinel::findById($userId);
|
454
|
|
|
if(!$reminder = Reminder::complete($user, $passwordResetCode, Input::get('password')))
|
455
|
|
|
{
|
456
|
|
|
Base::Log('Forgot password confirm failed for ' . $user->username . ' (' . $user->first_name . ' ' . $user->last_name . ') from IP ' . Request::ip() );
|
457
|
|
|
|
458
|
|
|
// Ooops.. something went wrong
|
459
|
|
|
return Redirect::route('login')->with('error', Lang::get('base.auth.forgot_password_confirm.error'));
|
460
|
|
|
} else {
|
461
|
|
|
// Send the activation code through email
|
462
|
|
|
Mail::queue('emails.auth.password-changed', [ 'user' => $user], function ($m) use ($user) {
|
463
|
|
|
$m->to($user->email, $user->first_name . ' ' . $user->last_name);
|
464
|
|
|
$m->subject(Lang::get('base.mails.password_changed'));
|
465
|
|
|
});
|
466
|
|
|
}
|
467
|
|
|
|
468
|
|
|
Base::Log('Forgot password confirmed for ' . $user->username . ' (' . $user->first_name . ' ' . $user->last_name . ') from IP ' . Request::ip() );
|
469
|
|
|
|
470
|
|
|
// Password successfully reseted
|
471
|
|
|
return Redirect::route('login')->with('success', Lang::get('base.auth.forgot_password_confirm.success'));
|
472
|
|
|
}
|
473
|
|
|
|
474
|
|
|
}
|
475
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.