public static function Process($mResult) { $sExternalServicesRedirect = ''; $sError = ''; $sErrorMessage = ''; if (isset($_COOKIE["external-services-redirect"])) { $sExternalServicesRedirect = $_COOKIE["external-services-redirect"]; @setcookie('external-services-redirect', null); } if ($sExternalServicesRedirect === 'helpdesk') { self::SetValuesToCookie($mResult); $sTenantHash = self::GetTenantHashFromCookie(); \CApi::Location($sTenantHash ? './?helpdesk=' . $sTenantHash : './?helpdesk'); } else { $oApiUsers = \CApi::Manager('users'); $oInviteAccount = null; if (isset($_COOKIE["external-services-invite-hash"])) { $oInviteAccount = $oApiUsers->getAccountById($_COOKIE["external-services-invite-hash"], true); @setcookie('external-services-invite-hash', null); } $sEmail = trim($mResult['email']); if (empty($sEmail) && $oInviteAccount) { $mResult['email'] = $oInviteAccount->Email; } $oApiSocial = \CApi::Manager('social'); $oSocial = new \CSocial(); $oSocial->TypeStr = $mResult['type']; $oSocial->AccessToken = isset($mResult['access_token']) ? $mResult['access_token'] : ''; $oSocial->RefreshToken = isset($mResult['refresh_token']) ? $mResult['refresh_token'] : ''; $oSocial->IdSocial = $mResult['id']; $oSocial->Name = $mResult['name']; $oSocial->Email = $mResult['email']; if ($sExternalServicesRedirect === 'login') { self::SetValuesToCookie($mResult); $oSocialOld = $oApiSocial->getSocialById($oSocial->IdSocial, $oSocial->TypeStr); if ($oSocialOld) { if ($oInviteAccount && $oInviteAccount->IdAccount === $oSocialOld->IdAccount || !$oInviteAccount) { if (!$oSocialOld->Disabled && $oSocialOld->issetScope('auth')) { $oSocialOld->setScope('auth'); $oSocial->Scopes = $oSocialOld->Scopes; $oApiSocial->updateSocial($oSocial); $oInviteAccount = $oApiUsers->getAccountById($oSocialOld->IdAccount); } else { $bResult = false; $sError = '?error=es-002'; if (!empty($mResult['email'])) { $sError = $sError . '&email=' . $mResult['email']; } else { $sError = $sError . '&service=' . $mResult['type']; } } } else { $oInviteAccount = null; $bResult = false; $sError = '?error=es-003'; } } else { $oInviteAccount = $oApiUsers->getAccountByEmail($mResult['email']); if ($oInviteAccount) { $oSocial->IdAccount = $oInviteAccount->IdAccount; $oSocial->setScopes($mResult['scopes']); $oApiSocial->createSocial($oSocial); } else { $sError = '?error=es-002'; if (!empty($mResult['email'])) { $sError = $sError . '&email=' . $mResult['email']; } else { $sError = $sError . '&service=' . $mResult['type']; } } } if ($oInviteAccount) { $oApiIntegrator = \CApi::Manager('integrator'); $oApiIntegrator->setAccountAsLoggedIn($oInviteAccount, true); $oApiUsers->updateAccountLastLoginAndCount($oInviteAccount->IdUser); } \CApi::Location2('./' . $sError); } else { $oInviteAccount = \api_Utils::GetDefaultAccount(); if ($oInviteAccount) { $bResult = false; $oSocial->IdAccount = $oInviteAccount->IdAccount; $oSocialOld = $oApiSocial->getSocialById($oSocial->IdSocial, $oSocial->TypeStr); if ($oSocialOld) { if ($oSocialOld->IdAccount === $oSocial->IdAccount) { if (in_array('null', $mResult['scopes']) || count($mResult['scopes']) === 0) { $bResult = $oSocial->deleteSocial($oInviteAccount->IdAccount, $oSocial->TypeStr); } else { $oSocial->setScopes($mResult['scopes']); $bResult = $oApiSocial->updateSocial($oSocial); } } else { $bResult = false; $oPlugin = \CApi::Plugin()->GetPluginByName('external-services'); if ($oPlugin) { $sErrorMessage = $oPlugin->I18N('PLUGIN_EXTERNAL_SERVICES/INFO_ACCOUNT_ALREADY_ASSIGNED', $oInviteAccount); } } } else { if (!in_array('null', $mResult['scopes']) && count($mResult['scopes']) > 0) { $oSocial->setScopes($mResult['scopes']); $bResult = $oApiSocial->createSocial($oSocial); } } $sResult = $bResult ? 'true' : 'false'; echo "<script>" . "if (typeof(window.opener.servicesSettingsViewModelCallback) !== 'undefined') {" . "window.opener.servicesSettingsViewModelCallback('" . $mResult['type'] . "', " . $sResult . ", '" . $sErrorMessage . "');" . "window.close();" . "}" . "</script>"; } } } }