• Модуль: socialnetwork
  • Путь к файлу: ~/bitrix/modules/socialnetwork/classes/general/group_features_perms.php
  • Класс: CAllSocNetFeaturesPerms
  • Вызов: CAllSocNetFeaturesPerms::GetOperationPerm
static function GetOperationPerm($type, $id, $feature, $operation)
{
	global $arSocNetAllowedEntityTypes, $APPLICATION, $CACHE_MANAGER;

	static $arSonetGroupCache = array();

	$arSocNetFeaturesSettings = CSocNetAllowed::GetAllowedFeatures();

	$type = Trim($type);
	if (
		($type == '')
		|| !in_array($type, $arSocNetAllowedEntityTypes)
	)
	{
		$APPLICATION->ThrowException(GetMessage("SONET_GF_ERROR_NO_ENTITY_TYPE"), "ERROR_EMPTY_TYPE");
		if (is_array($id))
		{
			$arReturn = array();
			foreach($id as $TmpGroupID)
			{
				$arReturn[$TmpGroupID] = false;
			}

			return $arReturn;
		}

		return false;
	}

	$feature = mb_strtolower(trim($feature));
	if ($feature == '')
	{
		$APPLICATION->ThrowException(GetMessage("SONET_GF_EMPTY_FEATURE_ID"), "ERROR_EMPTY_FEATURE_ID");
		if (is_array($id))
		{
			$arReturn = array();
			foreach($id as $TmpGroupID)
			{
				$arReturn[$TmpGroupID] = false;
			}

			return $arReturn;
		}

		return false;
	}

	if (
		!array_key_exists($feature, $arSocNetFeaturesSettings) 
		|| !array_key_exists("allowed", $arSocNetFeaturesSettings[$feature])
		|| !in_array($type, $arSocNetFeaturesSettings[$feature]["allowed"])
	)
	{
		$APPLICATION->ThrowException(GetMessage("SONET_GF_ERROR_NO_FEATURE_ID"), "ERROR_NO_FEATURE_ID");
		if (is_array($id))
		{
			$arReturn = array();
			foreach($id as $TmpGroupID)
			{
				$arReturn[$TmpGroupID] = false;
			}

			return $arReturn;
		}

		return false;
	}

	$operation = mb_strtolower(Trim($operation));
	if (
		!array_key_exists("operations", $arSocNetFeaturesSettings[$feature])
		|| !array_key_exists($operation, $arSocNetFeaturesSettings[$feature]["operations"])
	)
	{
		if (is_array($id))
		{
			$arReturn = array();
			foreach($id as $TmpGroupID)
			{
				$arReturn[$TmpGroupID] = false;
			}

			return $arReturn;
		}

		return false;
	}

	global $arSonetFeaturesPermsCache;
	if (!isset($arSonetFeaturesPermsCache) || !is_array($arSonetFeaturesPermsCache))
	{
		$arSonetFeaturesPermsCache = array();
	}

	if (is_array($id))
	{
		$arFeaturesPerms = array();
		$arGroupToGet = array();
		foreach($id as $TmpGroupID)
		{
			$arFeaturesPerms[$TmpGroupID] = array();

			if (!array_key_exists($type."_".$TmpGroupID, $arSonetFeaturesPermsCache))
			{
				$arGroupToGet[] = $TmpGroupID;
			}
			else
			{
				$arFeaturesPerms[$TmpGroupID] = $arSonetFeaturesPermsCache[$type."_".$TmpGroupID];
			}
		}

		$arGroupToGet = array_unique($arGroupToGet);

		if (!empty($arGroupToGet))
		{
			$rsSonetGroup = CSocNetGroup::GetList(
				array(),
				array('@ID' => $arGroupToGet),
				false,
				false,
				array('ID', 'VISIBLE', 'OPENED')
			);
			while ($arSonetGroup = $rsSonetGroup->Fetch())
			{
				if (!isset($arSonetGroupCache[$arSonetGroup['ID']]))
				{
					$arSonetGroupCache[$arSonetGroup['ID']] = array(
						'VISIBLE' => $arSonetGroup['VISIBLE'],
						'OPENED' => $arSonetGroup['OPENED']
					);
				}
			}

			$dbResult = CSocNetFeaturesPerms::GetList(
				Array(),
				Array(
					"@FEATURE_ENTITY_ID" => $arGroupToGet,
					"FEATURE_ENTITY_TYPE" => $type,
					"FEATURE_ACTIVE" => "Y"
				),
				false,
				false,
				array("OPERATION_ID", "FEATURE_ENTITY_ID", "FEATURE_FEATURE", "ROLE")
			);
			while ($arResult = $dbResult->Fetch())
			{
				if (
					!array_key_exists($arResult["FEATURE_ENTITY_ID"], $arFeaturesPerms)
					|| !array_key_exists($arResult["FEATURE_FEATURE"], $arFeaturesPerms[$arResult["FEATURE_ENTITY_ID"]])
				)
				{
					$arFeaturesPerms[$arResult["FEATURE_ENTITY_ID"]][$arResult["FEATURE_FEATURE"]] = array();
				}
				$arFeaturesPerms[$arResult["FEATURE_ENTITY_ID"]][$arResult["FEATURE_FEATURE"]][$arResult["OPERATION_ID"]] = $arResult["ROLE"];
			}
		}

		$arReturn = array();

		foreach ($id as $TmpEntityID)
		{
			$arSonetFeaturesPermsCache[$type."_".$TmpEntityID] = $arFeaturesPerms[$TmpEntityID];

			if ($type === SONET_ENTITY_GROUP)
			{
				if (
					!array_key_exists($feature, $arFeaturesPerms[$TmpEntityID])
					|| !array_key_exists($operation, $arFeaturesPerms[$TmpEntityID][$feature])
				)
				{
					$perm = $arSocNetFeaturesSettings[$feature]["operations"][$operation][SONET_ENTITY_GROUP];

					if (
						isset($arSonetGroupCache[$TmpEntityID])
						&& $arSonetGroupCache[$TmpEntityID]['OPENED'] === 'Y'
						&& $arSonetGroupCache[$TmpEntityID]['VISIBLE'] === 'Y'
						&& $feature === "blog"
						&& ($perm === SONET_ROLES_USER)
						&& !empty($arSocNetFeaturesSettings[$feature]["minoperation"])
						&& (
							(
								is_array($arSocNetFeaturesSettings[$feature]["minoperation"])
								&& in_array($operation, $arSocNetFeaturesSettings[$feature]["minoperation"])
							)
							|| (
								!is_array($arSocNetFeaturesSettings[$feature]["minoperation"])
								&& $operation == $arSocNetFeaturesSettings[$feature]["minoperation"]
							)
						)
					)
					{
						$featureOperationPerms = SONET_ROLES_AUTHORIZED;
					}
					else
					{
						$featureOperationPerms = $perm;
					}
				}
				else
				{
					$featureOperationPerms = $arFeaturesPerms[$TmpEntityID][$feature][$operation];
				}
			}
			else
			{
				if (!array_key_exists($feature, $arFeaturesPerms[$TmpEntityID]))
				{
					$featureOperationPerms = $arSocNetFeaturesSettings[$feature]["operations"][$operation][SONET_ENTITY_USER];
				}
				elseif (!array_key_exists($operation, $arFeaturesPerms[$TmpEntityID][$feature]))
				{
					$featureOperationPerms = $arSocNetFeaturesSettings[$feature]["operations"][$operation][SONET_ENTITY_USER];
				}
				else
				{
					$featureOperationPerms = $arFeaturesPerms[$TmpEntityID][$feature][$operation];
				}

				if ($featureOperationPerms == SONET_RELATIONS_TYPE_FRIENDS2)
				{
					$featureOperationPerms = SONET_RELATIONS_TYPE_FRIENDS;
				}
			}

			$arReturn[$TmpEntityID] = $featureOperationPerms;
		}

		return $arReturn;
	}

	// not array
	$id = (int)$id;
	if ($id <= 0)
	{
		$APPLICATION->ThrowException(GetMessage("SONET_GF_EMPTY_ENTITY_ID"), "ERROR_EMPTY_ENTITY_ID");
		return false;
	}

	$arFeaturesPerms = array();
	if (array_key_exists($type."_".$id, $arSonetFeaturesPermsCache))
	{
		$arFeaturesPerms = $arSonetFeaturesPermsCache[$type."_".$id];
	}
	else
	{
		$cache = new CPHPCache;
		$cache_time = 31536000;
		$cache_id = "entity_"."_".$type."_".$id;
		$cache_path = self::getCachePath($type, $id);

		$arTmp = array();

		if ($cache->InitCache($cache_time, $cache_id, $cache_path))
		{
			$arCacheVars = $cache->GetVars();
			$arTmp = $arCacheVars["RESULT"];
		}
		else
		{
			$cache->StartDataCache($cache_time, $cache_id, $cache_path);
			if (defined("BX_COMP_MANAGED_CACHE"))
			{
				$CACHE_MANAGER->StartTagCache($cache_path);
			}

			$dbResult = CSocNetFeaturesPerms::GetList(
				Array(),
				Array(
					"FEATURE_ENTITY_ID" => $id,
					"FEATURE_ENTITY_TYPE" => $type,
					"FEATURE_ACTIVE" => "Y"
				),
				false,
				false,
				array("ID", "OPERATION_ID", "FEATURE_ID", "FEATURE_FEATURE", "ROLE")
			);
			while ($arResult = $dbResult->Fetch())
			{
				if (defined("BX_COMP_MANAGED_CACHE"))
				{
					$CACHE_MANAGER->RegisterTag("sonet_features2perms_".$arResult["ID"]);
				}
				$arTmp[] = $arResult;
			}

			if (defined("BX_COMP_MANAGED_CACHE"))
			{
				$dbResult = CSocNetFeatures::GetList(
					Array(),
					Array("ENTITY_ID" => $id, "ENTITY_TYPE" => $type),
					false,
					false,
					array("ID")
				);
				while ($arResult = $dbResult->Fetch())
				{
					$CACHE_MANAGER->RegisterTag("sonet_feature_".$arResult["ID"]);
				}
			}

			if (defined("BX_COMP_MANAGED_CACHE"))
			{
				if ($type == SONET_ENTITY_GROUP)
				{
					$CACHE_MANAGER->RegisterTag("sonet_group_".$id);
					$CACHE_MANAGER->RegisterTag("sonet_group");
				}
				elseif ($type == SONET_ENTITY_USER)
				{
					$CACHE_MANAGER->RegisterTag("USER_CARD_".intval($id / TAGGED_user_card_size));
				}

				$CACHE_MANAGER->RegisterTag("sonet_features_".$type."_".$id);
			}

			$arCacheData = Array(
				"RESULT" => $arTmp
			);

			if(defined("BX_COMP_MANAGED_CACHE"))
			{
				$CACHE_MANAGER->EndTagCache();
			}

			$cache->EndDataCache($arCacheData);
		}

		foreach($arTmp as $arResult)
		{
			if (!array_key_exists($arResult["FEATURE_FEATURE"], $arFeaturesPerms))
			{
				$arFeaturesPerms[$arResult["FEATURE_FEATURE"]] = array();
			}
			$arFeaturesPerms[$arResult["FEATURE_FEATURE"]][$arResult["OPERATION_ID"]] = $arResult["ROLE"];
		}
		$arSonetFeaturesPermsCache[$type."_".$id] = $arFeaturesPerms;
	}

	if ($type == SONET_ENTITY_GROUP)
	{
		if (
			!array_key_exists($feature, $arFeaturesPerms)
			|| !array_key_exists($operation, $arFeaturesPerms[$feature])
		)
		{
			if (
				!isset($arSonetGroupCache[$id])
				&& ($arSonetGroup = CSocNetGroup::GetByID($id))
			)
			{
				$arSonetGroupCache[$id] = array(
					'OPENED' => $arSonetGroup['OPENED'],
					'VISIBLE' => $arSonetGroup['VISIBLE']
				);
			}

			$perm = $arSocNetFeaturesSettings[$feature]["operations"][$operation][SONET_ENTITY_GROUP];

			if (
				isset($arSonetGroupCache[$id])
				&& $arSonetGroupCache[$id]['OPENED'] === 'Y'
				&& $arSonetGroupCache[$id]['VISIBLE'] === 'Y'
				&& $feature === "blog"
				&& ($perm === SONET_ROLES_USER)
				&& !empty($arSocNetFeaturesSettings[$feature]["minoperation"])
				&& (
					(
						is_array($arSocNetFeaturesSettings[$feature]["minoperation"])
						&& in_array($operation, $arSocNetFeaturesSettings[$feature]["minoperation"])
					)
					|| (
						!is_array($arSocNetFeaturesSettings[$feature]["minoperation"])
						&& $operation == $arSocNetFeaturesSettings[$feature]["minoperation"]
					)
				)
			)
			{
				$featureOperationPerms = SONET_ROLES_AUTHORIZED;
			}
			else
			{
				$featureOperationPerms = $perm;
			}
		}
		else
		{
			$featureOperationPerms = $arFeaturesPerms[$feature][$operation];
		}
	}
	else
	{
		if (!array_key_exists($feature, $arFeaturesPerms))
		{
			$featureOperationPerms = $arSocNetFeaturesSettings[$feature]["operations"][$operation][SONET_ENTITY_USER];
		}
		elseif (!array_key_exists($operation, $arFeaturesPerms[$feature]))
		{
			$featureOperationPerms = $arSocNetFeaturesSettings[$feature]["operations"][$operation][SONET_ENTITY_USER];
		}
		else
		{
			$featureOperationPerms = $arFeaturesPerms[$feature][$operation];
		}

		if ($featureOperationPerms == SONET_RELATIONS_TYPE_FRIENDS2)
		{
			$featureOperationPerms = SONET_RELATIONS_TYPE_FRIENDS;
		}
	}

	return $featureOperationPerms;

}