1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-06 03:02:34 +01:00
invoiceninja/app/Libraries/OAuth.php

109 lines
2.8 KiB
PHP
Raw Normal View History

<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Libraries;
use App\Models\User;
use Illuminate\Support\Facades\Session;
use Laravel\Socialite\Facades\Socialite;
/**
* Class OAuth
* @package App\Libraries
*/
class OAuth
{
/**
* Socialite Providers
*/
const SOCIAL_GOOGLE = 1;
const SOCIAL_FACEBOOK = 2;
const SOCIAL_GITHUB = 3;
const SOCIAL_LINKEDIN = 4;
const SOCIAL_TWITTER = 5;
const SOCIAL_BITBUCKET = 6;
/**
* @param Socialite $user
*/
public static function handleAuth(object $user, string $provider) : ?User
{
/** 1. Ensure user arrives on the correct provider **/
$query = [
2019-01-25 12:14:09 +01:00
'oauth_user_id' =>$user->getId(),
'oauth_provider_id'=>$provider
];
2019-01-25 12:20:02 +01:00
if($user = MultiDB::hasUser($query))
{
return $user;
}
/** 2. If email exists, then they already have an account did they select the wrong provider? redirect to a guest error screen */
if(MultiDB::checkUserEmailExists($user->getEmail()))
{
Session::flash('error', 'User exists in system, but not with this authentication method'); //todo add translations
return view('auth.login');
}
/*
Session::flash('error', 'User does not exist'); //todo add translations
return view('auth.login');
*/
/** 3. We will not handle automagically creating a new account here. */
}
public static function providerToString(int $social_provider) : string
{
switch ($social_provider)
{
case SOCIAL_GOOGLE:
return 'google';
case SOCIAL_FACEBOOK:
return 'facebook';
case SOCIAL_GITHUB:
return 'github';
case SOCIAL_LINKEDIN:
return 'linkedin';
case SOCIAL_TWITTER:
return 'twitter';
case SOCIAL_BITBUCKET:
return 'bitbucket';
}
}
public static function providerToInt(string $social_provider) : int
{
switch ($social_provider)
{
case 'google':
return SOCIAL_GOOGLE;
case 'facebook':
return SOCIAL_FACEBOOK;
case 'github':
return SOCIAL_GITHUB;
case 'linkedin':
return SOCIAL_LINKEDIN;
case 'twitter':
return SOCIAL_TWITTER;
case 'bitbucket':
return SOCIAL_BITBUCKET;
}
}
}