2014-05-14 23:24:20 +10:00
|
|
|
<?php namespace Backend\Controllers;
|
|
|
|
|
|
|
|
use Mail;
|
|
|
|
use Flash;
|
|
|
|
use Backend;
|
|
|
|
use Validator;
|
|
|
|
use BackendAuth;
|
2014-07-30 17:33:26 +10:00
|
|
|
use Backend\Models\AccessLog;
|
2014-05-14 23:24:20 +10:00
|
|
|
use Backend\Classes\Controller;
|
2014-10-04 15:59:43 +10:00
|
|
|
use System\Classes\UpdateManager;
|
2015-01-28 18:03:35 +11:00
|
|
|
use ApplicationException;
|
|
|
|
use ValidationException;
|
2014-05-14 23:24:20 +10:00
|
|
|
use Exception;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authentication controller
|
|
|
|
*
|
|
|
|
* @package october\backend
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class Auth extends Controller
|
|
|
|
{
|
|
|
|
protected $publicActions = ['index', 'signin', 'signout', 'restore', 'reset'];
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
$this->layout = 'auth';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default route, redirects to signin.
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2015-02-11 18:35:39 +11:00
|
|
|
return Backend::redirect('backend/auth/signin');
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays the log in page.
|
|
|
|
*/
|
|
|
|
public function signin()
|
|
|
|
{
|
|
|
|
$this->bodyClass = 'signin';
|
|
|
|
|
|
|
|
try {
|
2014-10-10 23:26:57 +02:00
|
|
|
if (post('postback')) {
|
2014-05-14 23:24:20 +10:00
|
|
|
return $this->signin_onSubmit();
|
2014-11-04 17:41:48 +11:00
|
|
|
}
|
|
|
|
else {
|
2014-05-14 23:24:20 +10:00
|
|
|
$this->bodyClass .= ' preload';
|
2014-10-10 23:26:57 +02:00
|
|
|
}
|
2014-11-01 12:00:45 +11:00
|
|
|
}
|
|
|
|
catch (Exception $ex) {
|
2014-05-14 23:24:20 +10:00
|
|
|
Flash::error($ex->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function signin_onSubmit()
|
|
|
|
{
|
|
|
|
$rules = [
|
2015-12-19 09:53:17 +11:00
|
|
|
'login' => 'required|between:2,255',
|
|
|
|
'password' => 'required|between:4,255'
|
2014-05-14 23:24:20 +10:00
|
|
|
];
|
|
|
|
|
|
|
|
$validation = Validator::make(post(), $rules);
|
2014-10-10 23:26:57 +02:00
|
|
|
if ($validation->fails()) {
|
2014-05-14 23:24:20 +10:00
|
|
|
throw new ValidationException($validation);
|
2014-10-10 23:26:57 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
// Authenticate user
|
|
|
|
$user = BackendAuth::authenticate([
|
|
|
|
'login' => post('login'),
|
|
|
|
'password' => post('password')
|
|
|
|
], true);
|
|
|
|
|
|
|
|
// Load version updates
|
2014-10-04 15:59:43 +10:00
|
|
|
UpdateManager::instance()->update();
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2014-08-06 20:19:03 +10:00
|
|
|
// Log the sign in event
|
|
|
|
AccessLog::add($user);
|
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
// Redirect to the intended page after successful sign in
|
2015-02-11 18:35:39 +11:00
|
|
|
return Backend::redirectIntended('backend');
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs out a backend user.
|
|
|
|
*/
|
|
|
|
public function signout()
|
|
|
|
{
|
|
|
|
BackendAuth::logout();
|
2015-02-11 18:35:39 +11:00
|
|
|
return Backend::redirect('backend');
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
2015-09-27 02:28:12 +07:00
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
/**
|
|
|
|
* Request a password reset verification code.
|
|
|
|
*/
|
|
|
|
public function restore()
|
|
|
|
{
|
|
|
|
try {
|
2014-10-10 23:26:57 +02:00
|
|
|
if (post('postback')) {
|
2014-05-14 23:24:20 +10:00
|
|
|
return $this->restore_onSubmit();
|
2014-10-10 23:26:57 +02:00
|
|
|
}
|
2014-11-01 12:00:45 +11:00
|
|
|
}
|
|
|
|
catch (Exception $ex) {
|
2014-05-14 23:24:20 +10:00
|
|
|
Flash::error($ex->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function restore_onSubmit()
|
|
|
|
{
|
|
|
|
$rules = [
|
2015-12-19 09:53:17 +11:00
|
|
|
'login' => 'required|between:2,255'
|
2014-05-14 23:24:20 +10:00
|
|
|
];
|
|
|
|
|
|
|
|
$validation = Validator::make(post(), $rules);
|
2014-10-10 23:26:57 +02:00
|
|
|
if ($validation->fails()) {
|
2014-05-14 23:24:20 +10:00
|
|
|
throw new ValidationException($validation);
|
2014-10-10 23:26:57 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
$user = BackendAuth::findUserByLogin(post('login'));
|
|
|
|
if (!$user) {
|
|
|
|
throw new ValidationException([
|
|
|
|
'login' => trans('backend::lang.account.restore_error', ['login' => post('login')])
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Flash::success(trans('backend::lang.account.restore_success'));
|
|
|
|
|
|
|
|
$code = $user->getResetPasswordCode();
|
|
|
|
$link = Backend::url('backend/auth/reset/'.$user->id.'/'.$code);
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'name' => $user->full_name,
|
|
|
|
'link' => $link,
|
|
|
|
];
|
|
|
|
|
2014-10-10 23:26:57 +02:00
|
|
|
Mail::send('backend::mail.restore', $data, function ($message) use ($user) {
|
2014-05-14 23:24:20 +10:00
|
|
|
$message->to($user->email, $user->full_name)->subject(trans('backend::lang.account.password_reset'));
|
|
|
|
});
|
|
|
|
|
2015-02-11 18:35:39 +11:00
|
|
|
return Backend::redirect('backend/auth/signin');
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset backend user password using verification code.
|
|
|
|
*/
|
|
|
|
public function reset($userId = null, $code = null)
|
|
|
|
{
|
|
|
|
try {
|
2014-10-10 23:26:57 +02:00
|
|
|
if (post('postback')) {
|
2014-05-14 23:24:20 +10:00
|
|
|
return $this->reset_onSubmit();
|
2014-10-10 23:26:57 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2014-10-10 23:26:57 +02:00
|
|
|
if (!$userId || !$code) {
|
2014-05-14 23:24:20 +10:00
|
|
|
throw new ApplicationException(trans('backend::lang.account.reset_error'));
|
2014-10-10 23:26:57 +02:00
|
|
|
}
|
2014-11-01 12:00:45 +11:00
|
|
|
}
|
|
|
|
catch (Exception $ex) {
|
2014-05-14 23:24:20 +10:00
|
|
|
Flash::error($ex->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->vars['code'] = $code;
|
|
|
|
$this->vars['id'] = $userId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function reset_onSubmit()
|
|
|
|
{
|
2014-10-10 23:26:57 +02:00
|
|
|
if (!post('id') || !post('code')) {
|
2014-05-14 23:24:20 +10:00
|
|
|
throw new ApplicationException(trans('backend::lang.account.reset_error'));
|
2014-10-10 23:26:57 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
$rules = [
|
2015-12-19 09:53:17 +11:00
|
|
|
'password' => 'required|between:4,255'
|
2014-05-14 23:24:20 +10:00
|
|
|
];
|
|
|
|
|
|
|
|
$validation = Validator::make(post(), $rules);
|
2014-10-10 23:26:57 +02:00
|
|
|
if ($validation->fails()) {
|
2014-05-14 23:24:20 +10:00
|
|
|
throw new ValidationException($validation);
|
2014-10-10 23:26:57 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
$code = post('code');
|
|
|
|
$user = BackendAuth::findUserById(post('id'));
|
|
|
|
|
2014-10-10 23:26:57 +02:00
|
|
|
if (!$user->checkResetPasswordCode($code)) {
|
2014-05-14 23:24:20 +10:00
|
|
|
throw new ApplicationException(trans('backend::lang.account.reset_error'));
|
2014-10-10 23:26:57 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2014-10-10 23:26:57 +02:00
|
|
|
if (!$user->attemptResetPassword($code, post('password'))) {
|
2014-05-14 23:24:20 +10:00
|
|
|
throw new ApplicationException(trans('backend::lang.account.reset_fail'));
|
2014-10-10 23:26:57 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
$user->clearResetPassword();
|
|
|
|
|
|
|
|
Flash::success(trans('backend::lang.account.reset_success'));
|
|
|
|
|
2015-02-11 18:35:39 +11:00
|
|
|
return Backend::redirect('backend/auth/signin');
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
2014-10-10 23:26:57 +02:00
|
|
|
}
|