invoiceninja/app/Http/Controllers/Auth/AuthController.php

214 lines
5.7 KiB
PHP
Raw Normal View History

2017-01-30 21:40:43 +02:00
<?php
namespace App\Http\Controllers\Auth;
2015-03-17 07:45:25 +10:00
2015-03-31 20:42:37 +03:00
use App\Events\UserLoggedIn;
2015-03-23 14:20:33 +10:00
use App\Http\Controllers\Controller;
2017-01-30 21:40:43 +02:00
use App\Models\User;
2015-06-16 22:35:35 +03:00
use App\Ninja\Repositories\AccountRepository;
2015-10-11 17:41:09 +03:00
use App\Services\AuthService;
2017-01-30 21:40:43 +02:00
use Auth;
use Event;
2015-03-17 07:45:25 +10:00
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
2017-01-30 21:40:43 +02:00
use Illuminate\Http\Request;
use Session;
use Utils;
2015-03-17 07:45:25 +10:00
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-17 07:45:25 +10:00
/**
* @var string
*/
2015-03-29 15:37:42 +03:00
protected $redirectTo = '/dashboard';
/**
* @var AuthService
*/
2015-10-11 17:41:09 +03:00
protected $authService;
/**
* @var AccountRepository
*/
2015-06-16 22:35:35 +03:00
protected $accountRepo;
2015-03-29 15:37:42 +03:00
/**
* Create a new authentication controller instance.
*
* @param AccountRepository $repo
2017-01-30 21:40:43 +02:00
* @param AuthService $authService
*
* @internal param \Illuminate\Contracts\Auth\Guard $auth
* @internal param \Illuminate\Contracts\Auth\Registrar $registrar
*/
public function __construct(AccountRepository $repo, AuthService $authService)
{
2015-06-16 22:35:35 +03:00
$this->accountRepo = $repo;
2015-10-11 17:41:09 +03:00
$this->authService = $authService;
}
2015-03-17 07:45:25 +10:00
/**
* @param array $data
2017-01-30 21:40:43 +02:00
*
* @return mixed
*/
2016-02-22 19:47:19 +02: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.
*
2017-01-30 21:40:43 +02:00
* @param array $data
*
2016-02-22 19:47:19 +02:00
* @return User
*/
public function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
/**
* @param $provider
* @param Request $request
*
* @return \Illuminate\Http\RedirectResponse
*/
2015-10-11 17:41:09 +03:00
public function authLogin($provider, Request $request)
{
return $this->authService->execute($provider, $request->has('code'));
}
/**
* @return \Illuminate\Http\RedirectResponse
*/
2015-10-11 17:41:09 +03:00
public function authUnlink()
{
$this->accountRepo->unlinkUserFromOauth(Auth::user());
Session::flash('message', trans('texts.updated_settings'));
2017-01-30 21:40:43 +02:00
2015-10-20 11:23:38 +03:00
return redirect()->to('/settings/' . ACCOUNT_USER_DETAILS);
2015-10-11 17:41:09 +03:00
}
/**
* @return \Illuminate\Http\Response
*/
2015-04-28 23:13:52 +03:00
public function getLoginWrapper()
{
if (auth()->check()) {
return redirect('/');
}
2017-01-30 21:40:43 +02:00
if (! Utils::isNinja() && ! User::count()) {
2017-04-18 16:53:00 +03:00
return redirect()->to('/setup');
}
2017-04-18 17:04:44 +03:00
if (Utils::isNinja() && ! Utils::isTravis()) {
// make sure the user is on SITE_URL/login to ensure OAuth works
$requestURL = request()->url();
$loginURL = SITE_URL . '/login';
$subdomain = Utils::getSubdomain(request()->url());
if ($requestURL != $loginURL && ! strstr($subdomain, 'webapp-')) {
return redirect()->to($loginURL);
}
2015-04-28 23:13:52 +03:00
}
2017-04-18 17:04:44 +03:00
2015-04-28 23:13:52 +03:00
return self::getLogin();
}
/**
* @param Request $request
*
* @return \Illuminate\Http\Response
*/
2015-03-31 20:42:37 +03:00
public function postLoginWrapper(Request $request)
{
2015-06-16 22:35:35 +03:00
$userId = Auth::check() ? Auth::user()->id : null;
2015-07-07 23:08:16 +03:00
$user = User::where('email', '=', $request->input('email'))->first();
2015-11-21 23:10:26 +02:00
if ($user && $user->failed_logins >= MAX_FAILED_LOGINS) {
2015-10-11 17:41:09 +03:00
Session::flash('error', trans('texts.invalid_credentials'));
2017-01-30 21:40:43 +02:00
2015-07-07 23:08:16 +03:00
return redirect()->to('login');
}
2015-03-31 20:42:37 +03:00
$response = self::postLogin($request);
if (Auth::check()) {
Event::fire(new UserLoggedIn());
2015-06-16 22:35:35 +03:00
2016-12-29 22:24:11 +02:00
/*
2015-07-07 23:08:16 +03:00
$users = false;
// we're linking a new account
2015-11-03 13:20:49 +02:00
if ($request->link_accounts && $userId && Auth::user()->id != $userId) {
2015-07-07 23:08:16 +03:00
$users = $this->accountRepo->associateAccounts($userId, Auth::user()->id);
2015-11-03 13:20:49 +02:00
Session::flash('message', trans('texts.associated_accounts'));
// check if other accounts are linked
2015-07-07 23:08:16 +03:00
} else {
$users = $this->accountRepo->loadAccounts(Auth::user()->id);
2015-06-16 22:35:35 +03:00
}
2016-12-29 22:24:11 +02:00
*/
$users = $this->accountRepo->loadAccounts(Auth::user()->id);
2015-07-07 23:08:16 +03:00
Session::put(SESSION_USER_ACCOUNTS, $users);
} elseif ($user) {
$user->failed_logins = $user->failed_logins + 1;
$user->save();
2015-03-31 20:42:37 +03:00
}
return $response;
}
/**
* @return \Illuminate\Http\Response
*/
2015-06-16 22:35:35 +03:00
public function getLogoutWrapper()
{
2017-01-30 21:40:43 +02:00
if (Auth::check() && ! Auth::user()->registered) {
2017-03-24 16:44:01 +03:00
if (request()->force_logout) {
$account = Auth::user()->account;
$this->accountRepo->unlinkAccount($account);
if (! $account->hasMultipleAccounts()) {
$account->company->forceDelete();
}
$account->forceDelete();
} else {
return redirect('/');
}
2015-07-07 23:08:16 +03:00
}
2015-06-16 22:35:35 +03:00
$response = self::getLogout();
Session::flush();
2017-01-31 13:09:25 +02:00
if ($reason = request()->reason) {
Session::flash('warning', trans("texts.{$reason}_logout"));
}
2015-06-16 22:35:35 +03:00
return $response;
}
2015-03-17 07:45:25 +10:00
}