1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 00:11:35 +02:00
invoiceninja/app/Services/AuthService.php

132 lines
3.2 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2015-10-11 16:41:09 +02:00
2017-01-30 20:40:43 +01:00
namespace App\Services;
use App\Events\UserLoggedIn;
use App\Ninja\Repositories\AccountRepository;
2017-05-01 17:25:18 +02:00
use App\Models\LookupUser;
2015-10-11 16:41:09 +02:00
use Auth;
use Input;
2017-01-30 20:40:43 +01:00
use Session;
2015-10-11 16:41:09 +02:00
use Socialite;
2017-01-30 20:40:43 +01:00
use Utils;
2015-10-11 16:41:09 +02:00
/**
2017-01-30 20:40:43 +01:00
* Class AuthService.
*/
2015-10-11 16:41:09 +02:00
class AuthService
{
/**
* @var AccountRepository
*/
2015-10-11 16:41:09 +02:00
private $accountRepo;
/**
* @var array
*/
2015-10-11 16:41:09 +02:00
public static $providers = [
1 => SOCIAL_GOOGLE,
2 => SOCIAL_FACEBOOK,
3 => SOCIAL_GITHUB,
2017-01-30 20:40:43 +01:00
4 => SOCIAL_LINKEDIN,
2015-10-11 16:41:09 +02:00
];
/**
* AuthService constructor.
*
* @param AccountRepository $repo
*/
2015-10-11 16:41:09 +02:00
public function __construct(AccountRepository $repo)
{
$this->accountRepo = $repo;
}
2015-11-01 19:21:11 +01:00
public static function getProviders()
{
}
/**
* @param $provider
* @param $hasCode
2017-01-30 20:40:43 +01:00
*
* @return \Illuminate\Http\RedirectResponse
*/
2015-10-11 16:41:09 +02:00
public function execute($provider, $hasCode)
{
2017-01-30 20:40:43 +01:00
if (! $hasCode) {
2015-10-11 16:41:09 +02:00
return $this->getAuthorization($provider);
}
$socialiteUser = Socialite::driver($provider)->user();
2017-01-30 20:40:43 +01:00
$providerId = self::getProviderId($provider);
2015-10-11 16:41:09 +02:00
2017-05-01 17:25:18 +02:00
$email = $socialiteUser->email;
$oauthUserId = $socialiteUser->id;
$name = Utils::splitName($socialiteUser->name);
2015-10-11 16:41:09 +02:00
if (Auth::check()) {
$user = Auth::user();
$isRegistered = $user->registered;
$result = $this->accountRepo->updateUserFromOauth($user, $name[0], $name[1], $email, $providerId, $oauthUserId);
if ($result === true) {
2017-01-30 20:40:43 +01:00
if (! $isRegistered) {
2015-11-01 19:21:11 +01:00
Session::flash('warning', trans('texts.success_message'));
Session::flash('onReady', 'handleSignedUp();');
2015-10-11 16:41:09 +02:00
} else {
Session::flash('message', trans('texts.updated_settings'));
2017-01-30 20:40:43 +01:00
2015-10-20 10:23:38 +02:00
return redirect()->to('/settings/' . ACCOUNT_USER_DETAILS);
2015-10-11 16:41:09 +02:00
}
} else {
Session::flash('error', $result);
}
} else {
2017-05-10 17:18:48 +02:00
LookupUser::setServerByField('oauth_user_key', $providerId . '-' . $oauthUserId);
2017-05-01 17:25:18 +02:00
2017-05-10 17:18:48 +02:00
if ($user = $this->accountRepo->findUserByOauth($providerId, $oauthUserId)) {
2015-10-11 16:41:09 +02:00
Auth::login($user, true);
event(new UserLoggedIn());
} else {
Session::flash('error', trans('texts.invalid_credentials'));
2017-01-30 20:40:43 +01:00
2015-10-11 16:41:09 +02:00
return redirect()->to('login');
}
}
2016-10-05 20:51:08 +02:00
2015-10-11 16:41:09 +02:00
$redirectTo = Input::get('redirect_to') ?: 'dashboard';
2017-01-30 20:40:43 +01:00
2015-10-11 16:41:09 +02:00
return redirect()->to($redirectTo);
}
/**
* @param $provider
2017-01-30 20:40:43 +01:00
*
* @return mixed
*/
2015-10-11 16:41:09 +02:00
private function getAuthorization($provider)
{
return Socialite::driver($provider)->redirect();
}
/**
* @param $provider
2017-01-30 20:40:43 +01:00
*
* @return mixed
*/
2015-10-11 16:41:09 +02:00
public static function getProviderId($provider)
{
2017-01-30 20:40:43 +01:00
return array_search(strtolower($provider), array_map('strtolower', self::$providers));
2015-10-11 16:41:09 +02:00
}
/**
* @param $providerId
2017-01-30 20:40:43 +01:00
*
* @return mixed|string
*/
2015-10-11 16:41:09 +02:00
public static function getProviderName($providerId)
{
2017-01-30 20:40:43 +01:00
return $providerId ? self::$providers[$providerId] : '';
2015-10-11 16:41:09 +02:00
}
}