public function Authorize($context, $bSave = false, $bUpdate = true, $applicationId = null, $onlyActive = true)
{
global $DB;
// compatibility magic
if (!($context instanceof AuthenticationContext))
{
$context = (new AuthenticationContext())
->setUserId($context)
->setApplicationId($applicationId)
;
}
$arUser = $this->UpdateSessionData($context, $onlyActive);
if ($arUser !== false)
{
$regenerateIdAfterLogin = MainConfigConfiguration::getInstance()->get('session')['regenerateIdAfterLogin'] ?? false;
if ($regenerateIdAfterLogin === true)
{
MainApplication::getInstance()->getCompositeSessionManager()->regenerateId();
}
self::$CURRENT_USER = false;
$this->justAuthorized = true;
//sometimes we don't need to update db (REST)
if ($bUpdate)
{
$tz = '';
if (CTimeZone::Enabled())
{
if (!CTimeZone::IsAutoTimeZone(trim((string)$arUser["AUTO_TIME_ZONE"])) || CTimeZone::getTzCookie() !== null)
{
$tz = ', TIME_ZONE_OFFSET = ' . CTimeZone::GetOffset();
}
}
$bxUid = '';
if (!empty($_COOKIE['BX_USER_ID']) && preg_match('/^[0-9a-f]{32}$/', $_COOKIE['BX_USER_ID']))
{
if ($_COOKIE['BX_USER_ID'] != $arUser['BX_USER_ID'])
{
// save new bxuid value
$bxUid = ", BX_USER_ID = '" . $_COOKIE['BX_USER_ID'] . "'";
$arUser['BX_USER_ID'] = $_COOKIE['BX_USER_ID'];
}
}
$languageId = '';
if ($arUser['LANGUAGE_ID'] === '')
{
$arUser['LANGUAGE_ID'] = LANGUAGE_ID;
$languageId = ", LANGUAGE_ID='" . $DB->ForSql(LANGUAGE_ID) . "'";
}
$DB->Query("
UPDATE b_user SET
STORED_HASH = NULL,
LAST_LOGIN = " . $DB->GetNowFunction() . ",
TIMESTAMP_X = TIMESTAMP_X,
LOGIN_ATTEMPTS = 0
" . $tz . "
" . $bxUid . "
" . $languageId . "
WHERE
ID=" . $arUser["ID"]
);
if ($bSave || Option::get('main', 'auth_multisite', 'N') == 'Y')
{
if (($hash = $context->getStoredAuthHash()) === null)
{
$hash = Random::getString(32, true);
}
$this->setStoredAuthCookies($arUser["LOGIN"], $hash, $bSave);
$date = new MainTypeDateTime();
$ipAddress = new MainWebIpAddress(MainContext::getCurrent()->getServer()->getRemoteAddr());
$ipExpr = new MainDBSqlExpression($ipAddress->toUnsigned());
if ($context->getStoredAuthId() > 0)
{
UserStoredAuthTable::update($context->getStoredAuthId(), [
'LAST_AUTH' => $date,
'IP_ADDR' => $ipExpr,
]);
}
else
{
UserStoredAuthTable::add([
'USER_ID' => $arUser["ID"],
'DATE_REG' => $date,
'LAST_AUTH' => $date,
'TEMP_HASH' => ($bSave ? 'N' : 'Y'),
'IP_ADDR' => $ipExpr,
'STORED_HASH' => $hash,
]);
}
}
if (($applicationPassId = $context->getApplicationPasswordId()) !== null)
{
//update usage statistics for the application
ApplicationPasswordTable::update($applicationPassId, [
'DATE_LOGIN' => new MainTypeDateTime(),
'LAST_IP' => $_SERVER["REMOTE_ADDR"],
]);
}
if (Option::get('main', 'event_log_login_success', 'N') === 'Y')
{
CEventLog::Log('SECURITY', 'USER_AUTHORIZE', 'main', $arUser['ID'], $context->getApplicationId());
}
if (Option::get('main', 'user_device_history', 'N') === 'Y')
{
Device::addLogin($context, $arUser);
}
}
$arParams = [
"user_fields" => $arUser,
"save" => $bSave,
"update" => $bUpdate,
"applicationId" => $context->getApplicationId(),
];
foreach (GetModuleEvents('main', 'OnAfterUserAuthorize', true) as $arEvent)
{
ExecuteModuleEventEx($arEvent, [$arParams]);
}
foreach (GetModuleEvents('main', 'OnUserLogin', true) as $arEvent)
{
ExecuteModuleEventEx($arEvent, [$this->GetID(), $arParams]);
}
if ($bUpdate)
{
MainCompositeEngine::onUserLogin();
}
//we need it mostrly for the $this->justAuthorized flag
$this->CheckAuthActions();
return true;
}
return false;
}