/** * Stores user identity * * @param \MongoId $userId * @param \Vegas\Security\OAuth\Identity $oAuthIdentity * @return bool */ public static function addUserIdentity(\MongoId $userId, \Vegas\Security\OAuth\Identity $oAuthIdentity) { $identity = new Identity(); $identity->writeAttribute('user_id', $userId); $identity->writeAttribute('id', $oAuthIdentity->getId()); $identity->writeAttribute('email', $oAuthIdentity->getEmail()); $identity->writeAttribute('service', $oAuthIdentity->getService()); return $identity->save(); }
/** * Authenticates authorized user * * @param Identity $oauthIdentity * @throws \User\Services\Exception\SignUpFailedException */ public function authenticate(Identity $oauthIdentity) { $auth = $this->di->get('serviceManager')->getService('auth:auth'); try { $authIdentity = $auth->authenticateByEmail($oauthIdentity->getEmail()); } catch (IdentityNotFoundException $ex) { $userService = $this->di->get('serviceManager')->getService('user:user'); $userModel = new User(); //store identity values without id, service and accessToken $identityValues = $oauthIdentity->toArray(); unset($identityValues['id']); unset($identityValues['service']); unset($identityValues['accessToken']); $userModel->writeAttributes($identityValues); //try to create new account if (!$userService->createAccount($userModel)) { throw new SignUpFailedException(); } $authIdentity = $auth->authenticateByEmail($oauthIdentity->getEmail()); } $this->afterAuthentication($authIdentity, $oauthIdentity); }