• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/lib/security/authentication.php
  • Класс: BitrixMainSecurityAuthentication
  • Вызов: Authentication::setAuthentication
static function setAuthentication(CurrentUser $user, $isPersistent = false)
{
	/** @var $context BitrixMainHttpContext */
	$context = BitrixMainApplication::getInstance()->getContext();
	$context->setUser($user);

	static::copyToSession($user);

	/** @var $response BitrixMainHttpResponse */
	$response = $context->getResponse();

	if (!$user->isAuthenticated())
	{
		$cookie = new BitrixMainWebCookie("UIDH", "", time() - 3600);
		$response->addCookie($cookie);
		return;
	}

	$connection = BitrixMainApplication::getDbConnection();
	$sqlHelper = $connection->getSqlHelper();

	$connection->queryExecute(
		"UPDATE b_user SET ".
		"   STORED_HASH = NULL, ".
		"   LAST_LOGIN = ".$sqlHelper->getCurrentDateTimeFunction().", ".
		"   TIMESTAMP_X = TIMESTAMP_X,  ".
		"   LOGIN_ATTEMPTS = 0, ".
		"   TIME_ZONE_OFFSET = ".CTimeZone::getOffset()." ".
		"WHERE ID = ".$user->getUserId()." "
	);

	$cookie = new BitrixMainWebCookie("LOGIN", $user->getLogin(), time()+60*60*24*30*60);
	$cookie->setSpread((BitrixMainConfigOption::get("main", "auth_multisite", "N") == "Y") ? BitrixMainWebCookie::SPREAD_SITES : BitrixMainWebCookie::SPREAD_DOMAIN);
	$response->addCookie($cookie);

	if ($isPersistent || BitrixMainConfigOption::get("main", "auth_multisite", "N") == "Y")
	{
		$hash = $user->getSessionHash();

		/** @var $request BitrixMainHttpRequest */
		$request = $context->getRequest();

		if ($isPersistent)
			$cookie = new BitrixMainWebCookie("UIDH", $hash, time() + 60 * 60 * 24 * 30 * 60);
		else
			$cookie = new BitrixMainWebCookie("UIDH", $hash, 0);

		$cookie->setSecure(BitrixMainConfigOption::get("main", "use_secure_password_cookies", "N") == "Y" && $request->isHttps());
		$response->addCookie($cookie);

		$storedId = static::getStoredHashId($user, $hash);
		if ($storedId)
		{
			$connection->queryExecute(
				"UPDATE b_user_stored_auth SET ".
				"	LAST_AUTH = ".$sqlHelper->getCurrentDateTimeFunction().", ".
				"	".(($user->getAuthType() === static::AUTHENTICATED_BY_HASH) ? "" : "TEMP_HASH='".($isPersistent ? "N" : "Y")."', ")." ".
				"	IP_ADDR = '".sprintf("%u", ip2long($request->getRemoteAddress()))."' ".
				"WHERE ID = ".intval($storedId)
			);
		}
		else
		{
			$sqlTmp1 = "";
			$sqlTmp2 = "";
			if ($connection->getType() === "oracle")
			{
				$storedId = $connection->getIdentity("sq_b_user_stored_auth");
				$sqlTmp1 = "ID, ";
				$sqlTmp2 = intval($storedId).", ";
			}

			$sql =
				"INSERT INTO b_user_stored_auth (".$sqlTmp1."USER_ID, DATE_REG, LAST_AUTH, TEMP_HASH, ".
				"   IP_ADDR, STORED_HASH) ".
				"VALUES (".$sqlTmp2.intval($user->getUserId()).", ".$sqlHelper->getCurrentDateTimeFunction().", ".
				"   ".$sqlHelper->getCurrentDateTimeFunction().", '".($isPersistent ? "N" : "Y")."', ".
				"   '".$sqlHelper->forSql(sprintf("%u", ip2long($request->getRemoteAddress())))."', ".
				"   '".$sqlHelper->forSql($hash)."')";
			$connection->queryExecute($sql);

			if ($connection->getType() !== "oracle")
				$storedId = $connection->getIdentity();
		}

		$user->setStoredAuthId($storedId);
	}

	$event = new MainEvent(
		"main",
		"OnUserLogin",
		array("USER" => $user)
	);
	$event->send();

	if (BitrixMainConfigOption::get("main", "event_log_login_success", "N") === "Y")
		CEventLog::log("SECURITY", "USER_AUTHORIZE", "main", $user->getUserId());
}