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

152 lines
4.3 KiB
PHP
Raw Normal View History

2015-03-23 14:20:33 +10:00
<?php namespace App\Http\Controllers\Auth;
2015-03-17 07:45:25 +10:00
2015-03-31 20:42:37 +03:00
use Auth;
use Event;
2015-04-28 23:13:52 +03:00
use Utils;
2015-06-16 22:35:35 +03:00
use Session;
2015-03-31 20:42:37 +03:00
use Illuminate\Http\Request;
2015-04-28 23:13:52 +03:00
use App\Models\User;
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;
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;
2015-03-17 07:45:25 +10:00
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
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 15:37:42 +03:00
protected $loginPath = '/login';
protected $redirectTo = '/dashboard';
2015-10-11 17:41:09 +03:00
protected $authService;
2015-06-16 22:35:35 +03:00
protected $accountRepo;
2015-03-29 15:37:42 +03:00
2015-03-17 07:45:25 +10:00
/**
* Create a new authentication controller instance.
*
* @param \Illuminate\Contracts\Auth\Guard $auth
* @param \Illuminate\Contracts\Auth\Registrar $registrar
* @return void
*/
2015-10-11 17:41:09 +03:00
public function __construct(Guard $auth, Registrar $registrar, AccountRepository $repo, AuthService $authService)
2015-03-17 07:45:25 +10:00
{
$this->auth = $auth;
$this->registrar = $registrar;
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
2015-06-16 22:35:35 +03:00
//$this->middleware('guest', ['except' => 'getLogout']);
2015-03-17 07:45:25 +10:00
}
2015-10-11 17:41:09 +03: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 11:23:38 +03:00
return redirect()->to('/settings/' . ACCOUNT_USER_DETAILS);
2015-10-11 17:41:09 +03:00
}
2015-04-28 23:13:52 +03:00
public function getLoginWrapper()
{
if (!Utils::isNinja() && !User::count()) {
return redirect()->to('invoice_now');
}
return self::getLogin();
}
2015-03-31 20:42:37 +03:00
public function postLoginWrapper(Request $request)
{
2015-11-02 15:29:38 +11:00
/** If request is from API*/
if($request->api_secret)
{
return $this->postLoginWrapperAPI($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-08-03 22:08:07 +03:00
if ($user && $user->failed_logins >= 3) {
2015-10-11 17:41:09 +03:00
Session::flash('error', trans('texts.invalid_credentials'));
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
2015-07-07 23:08:16 +03:00
$users = false;
// we're linking a new account
if ($userId && Auth::user()->id != $userId) {
$users = $this->accountRepo->associateAccounts($userId, Auth::user()->id);
2015-10-18 10:30:28 +03:00
Session::flash('warning', trans('texts.associated_accounts'));
2015-07-07 23:08:16 +03:00
// check if other accounts are linked
} else {
$users = $this->accountRepo->loadAccounts(Auth::user()->id);
2015-06-16 22:35:35 +03:00
}
2015-07-07 23:08:16 +03:00
Session::put(SESSION_USER_ACCOUNTS, $users);
2015-11-02 00:10:20 +02:00
2015-11-02 15:29:38 +11:00
2015-07-07 23:08:16 +03:00
} elseif ($user) {
$user->failed_logins = $user->failed_logins + 1;
$user->save();
2015-03-31 20:42:37 +03:00
}
return $response;
}
2015-11-02 15:29:38 +11:00
private function postLoginWrapperAPI(Request $request)
{
/**Auth check*/
/**Success*/
/* send back user object along with account token if it exists,
create token only if it does not exist*/
/**Failure*/
/* return json with failure message */
if ($request->create_token) {
if ( ! env(API_SECRET) || $request->api_secret !== env(API_SECRET)) {
return 'Invalid secret';
}
return $this->accountRepo->createToken($request->token_name);
}
}
2015-06-16 22:35:35 +03:00
public function getLogoutWrapper()
{
2015-07-07 23:08:16 +03:00
if (Auth::check() && !Auth::user()->registered) {
$account = Auth::user()->account;
$this->accountRepo->unlinkAccount($account);
$account->forceDelete();
}
2015-06-16 22:35:35 +03:00
$response = self::getLogout();
Session::flush();
return $response;
}
2015-03-17 07:45:25 +10:00
}