| @@ -325,7 +325,7 @@ | ||
| 325 | 325 | |
| 326 | 326 | |
| 327 | 327 | /** | 
| 328 | - * @param boolean|\Data\Filter $filter The filter to user when reading users | |
| 328 | + * @param \Data\Filter $filter The filter to user when reading users | |
| 329 | 329 | * @param boolean|array $select The fields to return | 
| 330 | 330 | * @param boolean|integer $top The number of records to return | 
| 331 | 331 | * @param boolean|integer $skip The number of records to skip | 
| @@ -43,7 +43,7 @@ | ||
| 43 | 43 | * | 
| 44 | 44 | * @param string $fieldName The name of the field to retrieve | 
| 45 | 45 | * | 
| 46 | - * @return mixed string the value of the field | |
| 46 | + * @return string string the value of the field | |
| 47 | 47 | */ | 
| 48 | 48 | protected function getFieldSingleValue($fieldName) | 
| 49 | 49 |      { | 
| @@ -2,20 +2,20 @@ discard block | ||
| 2 | 2 | namespace Auth; | 
| 3 | 3 | require dirname(__FILE__).'/../libs/google/src/Google/autoload.php'; | 
| 4 | 4 | |
| 5 | -class GoogleAuthenticator extends Authenticator | |
| 6 | -{
 | |
| 5 | +class GoogleAuthenticator extends Authenticator | |
| 6 | +{ | |
| 7 | 7 | protected $client; | 
| 8 | 8 | protected $token = null; | 
| 9 | 9 | |
| 10 | - public function __construct($params) | |
| 11 | -    {
 | |
| 10 | + public function __construct($params) | |
| 11 | +    { | |
| 12 | 12 | parent::__construct($params); | 
| 13 | - if(!isset($params['client_secrets_path'])) | |
| 14 | -        {
 | |
| 13 | + if(!isset($params['client_secrets_path'])) | |
| 14 | +        { | |
| 15 | 15 |              throw new \Exception('Missing required parameter client_secrets_path!');
 | 
| 16 | 16 | } | 
| 17 | - if(!isset($params['redirect_url'])) | |
| 18 | -        {
 | |
| 17 | + if(!isset($params['redirect_url'])) | |
| 18 | +        { | |
| 19 | 19 | $params['redirect_url'] = 'https://'.$_SERVER['HTTP_HOST'].'/oauth2callback.php?src=google'; | 
| 20 | 20 | } | 
| 21 | 21 |          $this->token = \FlipSession::getVar('GoogleToken', null);
 | 
| @@ -30,64 +30,64 @@ discard block | ||
| 30 | 30 | * | 
| 31 | 31 | * @return string The link to login using this method | 
| 32 | 32 | */ | 
| 33 | - public function getSupplementLink() | |
| 34 | -    {
 | |
| 33 | + public function getSupplementLink() | |
| 34 | +    { | |
| 35 | 35 | $authUrl = $this->client->createAuthUrl(); | 
| 36 | 36 | return '<a href="'.filter_var($authUrl, FILTER_SANITIZE_URL).'"><img src="/img/common/google_sign_in.png" style="width: 2em;"/></a>'; | 
| 37 | 37 | } | 
| 38 | 38 | |
| 39 | - public function authenticate($code, &$currentUser = false) | |
| 40 | -    {
 | |
| 39 | + public function authenticate($code, &$currentUser = false) | |
| 40 | +    { | |
| 41 | 41 | $googleUser = false; | 
| 42 | - try | |
| 43 | -        {
 | |
| 42 | + try | |
| 43 | +        { | |
| 44 | 44 | $this->client->authenticate($code); | 
| 45 | 45 | $this->token = $this->client->getAccessToken(); | 
| 46 | 46 |              \FlipSession::setVar('GoogleToken', $this->token);
 | 
| 47 | 47 | $oauth2Service = new \Google_Service_Oauth2($this->client); | 
| 48 | 48 | $googleUser = $oauth2Service->userinfo->get(); | 
| 49 | - } | |
| 50 | - catch(\Exception $ex) | |
| 51 | -        {
 | |
| 49 | + } | |
| 50 | + catch(\Exception $ex) | |
| 51 | +        { | |
| 52 | 52 | return self::LOGIN_FAILED; | 
| 53 | 53 | } | 
| 54 | 54 | |
| 55 | 55 | $auth = \AuthProvider::getInstance(); | 
| 56 | 56 |          $localUsers = $auth->getUsersByFilter(new \Data\Filter('mail eq '.$googleUser->email));
 | 
| 57 | - if($localUsers !== false && isset($localUsers[0])) | |
| 58 | -        {
 | |
| 59 | -            if($localUsers[0]->canLoginWith('google.com'))
 | |
| 60 | -            {
 | |
| 57 | + if($localUsers !== false && isset($localUsers[0])) | |
| 58 | +        { | |
| 59 | +            if($localUsers[0]->canLoginWith('google.com')) | |
| 60 | +            { | |
| 61 | 61 | $auth->impersonateUser($localUsers[0]); | 
| 62 | 62 | return self::SUCCESS; | 
| 63 | 63 | } | 
| 64 | 64 | $currentUser = $localUsers[0]; | 
| 65 | 65 | return self::ALREADY_PRESENT; | 
| 66 | - } | |
| 67 | - else | |
| 68 | -        {
 | |
| 66 | + } | |
| 67 | + else | |
| 68 | +        { | |
| 69 | 69 | $user = new PendingUser(); | 
| 70 | 70 | $user->setEmail($googleUser->email); | 
| 71 | 71 | $user->setGivenName($googleUser->givenName); | 
| 72 | 72 | $user->setLastName($googleUser->familyName); | 
| 73 | 73 |              $user->addLoginProvider('google.com');
 | 
| 74 | 74 | $ret = $auth->activatePendingUser($user); | 
| 75 | - if($ret === false) | |
| 76 | -            {
 | |
| 75 | + if($ret === false) | |
| 76 | +            { | |
| 77 | 77 |                  throw new \Exception('Unable to create user! '.$res);
 | 
| 78 | 78 | } | 
| 79 | 79 | return self::SUCCESS; | 
| 80 | 80 | } | 
| 81 | 81 | } | 
| 82 | 82 | |
| 83 | - public function getUser($data = false) | |
| 84 | -    {
 | |
| 85 | - if($data === false) | |
| 86 | -        {
 | |
| 83 | + public function getUser($data = false) | |
| 84 | +    { | |
| 85 | + if($data === false) | |
| 86 | +        { | |
| 87 | 87 | $data = $this->token; | 
| 88 | 88 | } | 
| 89 | - try | |
| 90 | -        {
 | |
| 89 | + try | |
| 90 | +        { | |
| 91 | 91 | $this->client->setAccessToken($data); | 
| 92 | 92 | $oauth2Service = new \Google_Service_Oauth2($this->client); | 
| 93 | 93 | $googleUser = $oauth2Service->userinfo->get(); | 
| @@ -98,9 +98,9 @@ discard block | ||
| 98 | 98 | $profileUser['displayName'] = $googleUser->name; | 
| 99 | 99 | $profileUser['jpegPhoto'] = base64_encode(file_get_contents($googleUser->picture)); | 
| 100 | 100 | return $profileUser; | 
| 101 | - } | |
| 102 | - catch(\Exception $e) | |
| 103 | -        {
 | |
| 101 | + } | |
| 102 | + catch(\Exception $e) | |
| 103 | +        { | |
| 104 | 104 | return false; | 
| 105 | 105 | } | 
| 106 | 106 | } |