• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/classes/general/user.php
  • Класс: CAllUser
  • Вызов: CAllUser::CheckStoredHash
static function CheckStoredHash($context, $hash, $tempHash = false)
{
	if (!($context instanceof AuthenticationContext))
	{
		$context = (new AuthenticationContext())
			->setUserId($context)
		;
	}

	$cnt = 0;
	$hashId = false;

	$res = UserStoredAuthTable::query()
		->setSelect(['*'])
		->where('USER_ID', $context->getUserId())
		->setOrder(['LAST_AUTH' => 'DESC'])
		->exec()
	;

	$policy = static::getPolicy($context->getUserId());

	$maxStoreNum = $policy->getMaxStoreNum();
	$storeTimeout = $policy->getStoreTimeout();
	$sessionTimeout = $policy->getSessionTimeout();
	$storeIpMask = ip2long($policy->getStoreIpMask());

	$ipAddress = MainContext::getCurrent()->getServer()->getRemoteAddr();

	while ($ar = $res->fetch())
	{
		if ($ar["TEMP_HASH"] == 'N')
		{
			$cnt++;
		}

		$lastAuthTime = 0;
		if ($ar["LAST_AUTH"] instanceof MainTypeDateTime)
		{
			$lastAuthTime = $ar["LAST_AUTH"]->getTimestamp();
		}

		if (
			$cnt > $maxStoreNum
			|| ($ar["TEMP_HASH"] == 'N' && time() - ($storeTimeout * 60) > $lastAuthTime)
			|| ($ar["TEMP_HASH"] == 'Y' && time() - ($sessionTimeout * 60) > $lastAuthTime)
		)
		{
			UserStoredAuthTable::delete($ar['ID']);
		}
		elseif (!$hashId)
		{
			//for domain spreaded external auth we should check only temporary hashes
			if (!$tempHash || $ar["TEMP_HASH"] == 'Y')
			{
				$remote_net = $storeIpMask & ip2long($ipAddress);
				$stored_net = $storeIpMask & (float)$ar["IP_ADDR"];

				if ($hash === $ar["STORED_HASH"] && $remote_net == $stored_net)
				{
					$hashId = $ar["ID"];

					$context
						->setStoredAuthId($hashId)
						->setStoredAuthHash($hash)
					;
				}
			}
		}
	}
	return $hashId;
}