• Модуль: socialservices
  • Путь к файлу: ~/bitrix/modules/socialservices/classes/general/office365.php
  • Класс: CSocServOffice365OAuth
  • Вызов: CSocServOffice365OAuth::Authorize
public function Authorize()
{
	global $APPLICATION;

	$APPLICATION->RestartBuffer();

	$bProcessState = false;
	$bSuccess = SOCSERV_AUTHORISATION_ERROR;

	if(isset($_REQUEST["code"]) && $_REQUEST["code"] != '' && CSocServAuthManager::CheckUniqueKey())
	{
		$this->getEntityOAuth()->setCode($_REQUEST["code"]);

		$bProcessState = true;

		if($this->getEntityOAuth()->GetAccessToken() !== false)
		{
			$office365User = $this->getEntityOAuth()->GetCurrentUser();
			if(is_array($office365User) &&  ($office365User['id'] <> ''))
			{
				$office365User["tenant"] = preg_replace("/^.*@/", "", $office365User["userPrincipalName"]);

				$allowAuth = true;
				$tenantRestriction = self::GetOption("office365_tenant");
				if(!empty($tenantRestriction))
				{
					$allowAuth = $office365User["tenant"] == $tenantRestriction;
				}

				if($allowAuth)
				{
					$arFields = self::prepareUser($office365User);
					$bSuccess = $this->AuthorizeUser($arFields);
				}
			}
		}
	}

	if(!$bProcessState)
	{
		unset($_REQUEST["state"]);
	}

	$url = ($APPLICATION->GetCurDir() == "/login/") ? "" : $APPLICATION->GetCurDir();
	$aRemove = array("logout", "auth_service_error", "auth_service_id", "code", "error_reason", "error", "error_description", "check_key", "current_fieldset");

	$mode = 'opener';
	$addParams = true;
	if(isset($_REQUEST["state"]))
	{
		$arState = array();
		parse_str($_REQUEST["state"], $arState);
		if(isset($arState['backurl']) || isset($arState['redirect_url']))
		{
			$url = !empty($arState['redirect_url']) ? $arState['redirect_url'] : $arState['backurl'];
			if(mb_substr($url, 0, 1) !== "#")
			{
				$parseUrl = parse_url($url);
				$urlPath = $parseUrl["path"];
				$arUrlQuery = explode('&', $parseUrl["query"]);

				foreach($arUrlQuery as $key => $value)
				{
					foreach($aRemove as $param)
					{
						if(mb_strpos($value, $param."=") === 0)
						{
							unset($arUrlQuery[$key]);
							break;
						}
					}
				}

				$url = (!empty($arUrlQuery)) ? $urlPath.'?'.implode("&", $arUrlQuery) : $urlPath;
			}
			else
			{
				$addParams = false;
			}
		}

		if(isset($arState['mode']))
		{
			$mode = $arState['mode'];
		}
	}

	if($bSuccess === SOCSERV_REGISTRATION_DENY)
	{
		$url = (preg_match("/?/", $url)) ? $url.'&' : $url.'?';
		$url .= 'auth_service_id='.self::ID.'&auth_service_error='.SOCSERV_REGISTRATION_DENY;
	}
	elseif($bSuccess !== true)
	{
		$url = (isset($parseUrl))
			? $urlPath.'?auth_service_id='.self::ID.'&auth_service_error='.$bSuccess
			: $APPLICATION->GetCurPageParam(('auth_service_id='.self::ID.'&auth_service_error='.$bSuccess), $aRemove);
	}

	if($addParams && CModule::IncludeModule("socialnetwork") && mb_strpos($url, "current_fieldset=") === false)
		$url = (preg_match("/?/", $url)) ? $url."¤t_fieldset=SOCSERV" : $url."?current_fieldset=SOCSERV";

	$url = CUtil::JSEscape($url);

	if($addParams)
	{
		$location = ($mode == "opener") ? 'if(window.opener) window.opener.location = ''.$url.''; window.close();' : ' window.location = ''.$url.'';';
	}
	else
	{
		//fix for chrome
		$location = ($mode == "opener") ? 'if(window.opener) window.opener.location = window.opener.location.href + ''.$url.''; window.close();' : ' window.location = window.location.href + ''.$url.'';';
	}

	$JSScript = '
	
	';

	echo $JSScript;

	CMain::FinalActions();
}