• Модуль: socialnetwork
  • Путь к файлу: ~/bitrix/modules/socialnetwork/lib/integration/ui/entityselector/userprovider.php
  • Класс: BitrixSocialnetworkIntegrationUIEntitySelectorUserProvider
  • Вызов: UserProvider::hasUserRole
static function hasUserRole(?int $userId, string $role): bool
{
	static $roles = [
		'intranet' => [],
		'extranet' => []
	];

	if (!isset($roles[$role]) || !ModuleManager::isModuleInstalled('intranet'))
	{
		return false;
	}

	if (is_null($userId))
	{
		$userId = self::getCurrentUserId();
		if ($userId <= 0)
		{
			return false;
		}
	}

	if (
		$userId === self::getCurrentUserId()
		&& CSocNetUser::isCurrentUserModuleAdmin()
	)
	{
		return true;
	}

	if (isset($roles[$role][$userId]))
	{
		return $roles[$role][$userId];
	}

	$cacheId = 'UserRole:'.$role;
	$cachePath = '/external_user_info/'.substr(md5($userId),-2).'/'.$userId.'/';
	$cache = Application::getInstance()->getCache();
	$ttl = 2592000; // 1 month

	if ($cache->initCache($ttl, $cacheId, $cachePath))
	{
		$roles[$role][$userId] = (bool)$cache->getVars();
	}
	else
	{
		$cache->startDataCache();

		$taggedCache = Application::getInstance()->getTaggedCache();
		$taggedCache->startTagCache($cachePath);
		$taggedCache->registerTag('USER_NAME_'.$userId);
		$taggedCache->endTagCache();

		$filter = [
			'=ID' => $userId,
			'=IS_REAL_USER' => true
		];

		if ($role === 'intranet')
		{
			$filter['!UF_DEPARTMENT'] = false;
		}
		else if ($role === 'extranet')
		{
			$filter['UF_DEPARTMENT'] = false;
		}

		$roles[$role][$userId] =
			UserTable::getList(['select' => ['ID'], 'filter' => $filter])
				->fetchCollection()->count() === 1
		;

		$cache->endDataCache($roles[$role][$userId]);
	}

	return $roles[$role][$userId];
}