2015-03-23 05:20:33 +01:00
|
|
|
<?php namespace App\Http\Controllers\Auth;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-03-31 19:42:37 +02:00
|
|
|
use Auth;
|
|
|
|
use Event;
|
2015-04-28 22:13:52 +02:00
|
|
|
use Utils;
|
2015-06-16 21:35:35 +02:00
|
|
|
use Session;
|
2015-03-31 19:42:37 +02:00
|
|
|
use Illuminate\Http\Request;
|
2015-04-28 22:13:52 +02:00
|
|
|
use App\Models\User;
|
2015-03-31 19:42:37 +02:00
|
|
|
use App\Events\UserLoggedIn;
|
2015-03-23 05:20:33 +01:00
|
|
|
use App\Http\Controllers\Controller;
|
2015-06-16 21:35:35 +02:00
|
|
|
use App\Ninja\Repositories\AccountRepository;
|
2015-10-11 16:41:09 +02:00
|
|
|
use App\Services\AuthService;
|
2015-03-16 22:45:25 +01:00
|
|
|
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
|
|
|
|
|
|
|
|
class AuthController extends Controller {
|
|
|
|
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Registration & Login Controller
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
| This controller handles the registration of new users, as well as the
|
|
|
|
| authentication of existing users. By default, this controller uses
|
|
|
|
| a simple trait to add these behaviors. Why don't you explore it?
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
use AuthenticatesAndRegistersUsers;
|
|
|
|
|
2015-03-29 14:37:42 +02:00
|
|
|
protected $redirectTo = '/dashboard';
|
2015-10-11 16:41:09 +02:00
|
|
|
protected $authService;
|
2015-06-16 21:35:35 +02:00
|
|
|
protected $accountRepo;
|
2015-03-29 14:37:42 +02:00
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
/**
|
|
|
|
* Create a new authentication controller instance.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Auth\Guard $auth
|
|
|
|
* @param \Illuminate\Contracts\Auth\Registrar $registrar
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-02-22 18:47:19 +01:00
|
|
|
public function __construct(AccountRepository $repo, AuthService $authService)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-06-16 21:35:35 +02:00
|
|
|
$this->accountRepo = $repo;
|
2015-10-11 16:41:09 +02:00
|
|
|
$this->authService = $authService;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-06-16 21:35:35 +02:00
|
|
|
//$this->middleware('guest', ['except' => 'getLogout']);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-02-22 18:47:19 +01:00
|
|
|
public function validator(array $data)
|
|
|
|
{
|
|
|
|
return Validator::make($data, [
|
|
|
|
'name' => 'required|max:255',
|
|
|
|
'email' => 'required|email|max:255|unique:users',
|
|
|
|
'password' => 'required|confirmed|min:6',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new user instance after a valid registration.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return User
|
|
|
|
*/
|
|
|
|
public function create(array $data)
|
|
|
|
{
|
|
|
|
return User::create([
|
|
|
|
'name' => $data['name'],
|
|
|
|
'email' => $data['email'],
|
|
|
|
'password' => bcrypt($data['password']),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-10-11 16:41:09 +02:00
|
|
|
public function authLogin($provider, Request $request)
|
|
|
|
{
|
|
|
|
return $this->authService->execute($provider, $request->has('code'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function authUnlink()
|
|
|
|
{
|
|
|
|
$this->accountRepo->unlinkUserFromOauth(Auth::user());
|
|
|
|
|
|
|
|
Session::flash('message', trans('texts.updated_settings'));
|
2015-10-20 10:23:38 +02:00
|
|
|
return redirect()->to('/settings/' . ACCOUNT_USER_DETAILS);
|
2015-10-11 16:41:09 +02:00
|
|
|
}
|
|
|
|
|
2015-04-28 22:13:52 +02:00
|
|
|
public function getLoginWrapper()
|
|
|
|
{
|
|
|
|
if (!Utils::isNinja() && !User::count()) {
|
|
|
|
return redirect()->to('invoice_now');
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::getLogin();
|
|
|
|
}
|
|
|
|
|
2015-03-31 19:42:37 +02:00
|
|
|
public function postLoginWrapper(Request $request)
|
|
|
|
{
|
2015-11-02 05:29:38 +01:00
|
|
|
|
2015-06-16 21:35:35 +02:00
|
|
|
$userId = Auth::check() ? Auth::user()->id : null;
|
2015-07-07 22:08:16 +02:00
|
|
|
$user = User::where('email', '=', $request->input('email'))->first();
|
|
|
|
|
2015-11-21 22:10:26 +01:00
|
|
|
if ($user && $user->failed_logins >= MAX_FAILED_LOGINS) {
|
2015-10-11 16:41:09 +02:00
|
|
|
Session::flash('error', trans('texts.invalid_credentials'));
|
2015-07-07 22:08:16 +02:00
|
|
|
return redirect()->to('login');
|
|
|
|
}
|
|
|
|
|
2015-03-31 19:42:37 +02:00
|
|
|
$response = self::postLogin($request);
|
|
|
|
|
|
|
|
if (Auth::check()) {
|
|
|
|
Event::fire(new UserLoggedIn());
|
2015-06-16 21:35:35 +02:00
|
|
|
|
2015-07-07 22:08:16 +02:00
|
|
|
$users = false;
|
|
|
|
// we're linking a new account
|
2015-11-03 12:20:49 +01:00
|
|
|
if ($request->link_accounts && $userId && Auth::user()->id != $userId) {
|
2015-07-07 22:08:16 +02:00
|
|
|
$users = $this->accountRepo->associateAccounts($userId, Auth::user()->id);
|
2015-11-03 12:20:49 +01:00
|
|
|
Session::flash('message', trans('texts.associated_accounts'));
|
2015-07-07 22:08:16 +02:00
|
|
|
// check if other accounts are linked
|
|
|
|
} else {
|
|
|
|
$users = $this->accountRepo->loadAccounts(Auth::user()->id);
|
2015-06-16 21:35:35 +02:00
|
|
|
}
|
2015-07-07 22:08:16 +02:00
|
|
|
Session::put(SESSION_USER_ACCOUNTS, $users);
|
2015-11-01 23:10:20 +01:00
|
|
|
|
2015-07-07 22:08:16 +02:00
|
|
|
} elseif ($user) {
|
|
|
|
$user->failed_logins = $user->failed_logins + 1;
|
|
|
|
$user->save();
|
2015-03-31 19:42:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2015-11-02 05:29:38 +01:00
|
|
|
|
2015-06-16 21:35:35 +02:00
|
|
|
public function getLogoutWrapper()
|
|
|
|
{
|
2015-07-07 22:08:16 +02:00
|
|
|
if (Auth::check() && !Auth::user()->registered) {
|
|
|
|
$account = Auth::user()->account;
|
|
|
|
$this->accountRepo->unlinkAccount($account);
|
|
|
|
$account->forceDelete();
|
|
|
|
}
|
|
|
|
|
2015-06-16 21:35:35 +02:00
|
|
|
$response = self::getLogout();
|
|
|
|
|
|
|
|
Session::flush();
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|