• Модуль: lists
  • Путь к файлу: ~/bitrix/modules/lists/lib/bizprocdocument.php
  • Класс: BizprocDocument
  • Вызов: BizprocDocument::CanUserOperateDocument
static function CanUserOperateDocument($operation, $userId, $documentId, $parameters = array())
{
	$documentId = trim($documentId);
	if ($documentId == '')
		return false;

	if (self::isAdmin())
	{
		return true;
	}

	if (!array_key_exists("IBlockId", $parameters)
		&& (
			!array_key_exists("IBlockPermission", $parameters)
			|| !array_key_exists("DocumentStates", $parameters)
			|| !array_key_exists("IBlockRightsMode", $parameters)
			|| array_key_exists("IBlockRightsMode", $parameters) && ($parameters["IBlockRightsMode"] === "E")
		)
		|| !array_key_exists("CreatedBy", $parameters) && !array_key_exists("AllUserGroups", $parameters))
	{
		if (empty(self::$elements[$documentId]))
		{
			$elementListQuery = CIBlockElement::getList(
				array(),
				array("ID" => $documentId, "SHOW_NEW" => "Y", "SHOW_HISTORY" => "Y"),
				false,
				false,
				array("ID", "IBLOCK_ID", "CREATED_BY")
			);
			self::$elements[$documentId] = $elementListQuery->fetch();
		}

		if (empty(self::$elements[$documentId]))
			return false;

		$element = self::$elements[$documentId];

		$parameters["IBlockId"] = $element["IBLOCK_ID"];
		$parameters["CreatedBy"] = $element["CREATED_BY"];
	}

	if (!array_key_exists("IBlockRightsMode", $parameters))
		$parameters["IBlockRightsMode"] = CIBlock::getArrayByID($parameters["IBlockId"], "RIGHTS_MODE");

	if ($parameters["IBlockRightsMode"] === "E")
	{
		if (
			$operation === CBPCanUserOperateOperation::ReadDocument ||
			$operation === CBPCanUserOperateOperation::ViewWorkflow
		)
			return CIBlockElementRights::userHasRightTo($parameters["IBlockId"], $documentId, "element_read");
		elseif ($operation === CBPCanUserOperateOperation::WriteDocument)
			return CIBlockElementRights::userHasRightTo($parameters["IBlockId"], $documentId, "element_edit");
		elseif ($operation === CBPCanUserOperateOperation::StartWorkflow)
		{
			if (CIBlockElementRights::userHasRightTo($parameters["IBlockId"], $documentId, "element_edit"))
				return true;

			if (!array_key_exists("WorkflowId", $parameters))
				return false;

			if (!CIBlockElementRights::userHasRightTo($parameters["IBlockId"], $documentId, "element_read"))
				return false;

			$userId = intval($userId);
			if (!array_key_exists("AllUserGroups", $parameters))
			{
				if (!array_key_exists("UserGroups", $parameters))
					$parameters["UserGroups"] = CUser::getUserGroup($userId);

				$parameters["AllUserGroups"] = $parameters["UserGroups"];
				if ($userId == $parameters["CreatedBy"])
					$parameters["AllUserGroups"][] = "Author";
			}

			if (!array_key_exists("DocumentStates", $parameters))
			{
				if ($operation === CBPCanUserOperateOperation::StartWorkflow)
					$parameters["DocumentStates"] = CBPWorkflowTemplateLoader::getDocumentTypeStates(array('lists', get_called_class(), self::generateDocumentType($parameters["IBlockId"])));
				else
					$parameters["DocumentStates"] = CBPDocument::getDocumentStates(
						array('lists', get_called_class(), self::generateDocumentType($parameters["IBlockId"])),
						array('lists', get_called_class(), $documentId)
					);
			}

			if (array_key_exists($parameters["WorkflowId"], $parameters["DocumentStates"]))
				$parameters["DocumentStates"] = array($parameters["WorkflowId"] => $parameters["DocumentStates"][$parameters["WorkflowId"]]);
			else
				return false;

			$allowableOperations = CBPDocument::getAllowableOperations(
				$userId,
				$parameters["AllUserGroups"],
				$parameters["DocumentStates"],
				true
			);

			if (!is_array($allowableOperations))
				return false;

			if (($operation === CBPCanUserOperateOperation::ViewWorkflow) && in_array("read", $allowableOperations)
				|| ($operation === CBPCanUserOperateOperation::StartWorkflow) && in_array("write", $allowableOperations))
				return true;

			$chop = ($operation === CBPCanUserOperateOperation::ViewWorkflow) ? "element_read" : "element_edit";

			$tasks = self::getRightsTasks();
			foreach ($allowableOperations as $op)
			{
				if (isset($tasks[$op]))
					$op = $tasks[$op]['ID'];
				$ar = CTask::getOperations($op, true);
				if (in_array($chop, $ar))
					return true;
			}
		}
		elseif ($operation === CBPCanUserOperateOperation::CreateWorkflow)
		{
			return CBPDocument::canUserOperateDocumentType(
				CBPCanUserOperateOperation::CreateWorkflow,
				$userId,
				array('lists', get_called_class(), $parameters['IBlockId']),
				$parameters
			);
		}

		return false;
	}

	if (!array_key_exists("IBlockPermission", $parameters))
	{
		if (CModule::includeModule('lists'))
			$parameters["IBlockPermission"] = CLists::getIBlockPermission($parameters["IBlockId"], $userId);
		else
			$parameters["IBlockPermission"] = CIBlock::getPermission($parameters["IBlockId"], $userId);
	}

	if ($parameters["IBlockPermission"] <= "R")
		return false;
	elseif ($parameters["IBlockPermission"] >= "W")
		return true;

	$userId = intval($userId);
	if (!array_key_exists("AllUserGroups", $parameters))
	{
		if (!array_key_exists("UserGroups", $parameters))
			$parameters["UserGroups"] = CUser::getUserGroup($userId);

		$parameters["AllUserGroups"] = $parameters["UserGroups"];
		if ($userId == $parameters["CreatedBy"])
			$parameters["AllUserGroups"][] = "Author";
	}

	if (!array_key_exists("DocumentStates", $parameters))
	{
		$parameters["DocumentStates"] = CBPDocument::getDocumentStates(
			array("lists", get_called_class(), "iblock_".$parameters["IBlockId"]),
			array('lists', get_called_class(), $documentId)
		);
	}

	if (array_key_exists("WorkflowId", $parameters))
	{
		if (array_key_exists($parameters["WorkflowId"], $parameters["DocumentStates"]))
			$parameters["DocumentStates"] = array($parameters["WorkflowId"] => $parameters["DocumentStates"][$parameters["WorkflowId"]]);
		else
			return false;
	}

	$allowableOperations = CBPDocument::getAllowableOperations(
		$userId,
		$parameters["AllUserGroups"],
		$parameters["DocumentStates"]
	);

	if (!is_array($allowableOperations))
		return false;

	$r = false;
	switch ($operation)
	{
		case CBPCanUserOperateOperation::ViewWorkflow:
			$r = in_array("read", $allowableOperations);
			break;
		case CBPCanUserOperateOperation::StartWorkflow:
			$r = in_array("write", $allowableOperations);
			break;
		case CBPCanUserOperateOperation::CreateWorkflow:
			$r = false;
			break;
		case CBPCanUserOperateOperation::WriteDocument:
			$r = in_array("write", $allowableOperations);
			break;
		case CBPCanUserOperateOperation::ReadDocument:
			$r = in_array("read", $allowableOperations) || in_array("write", $allowableOperations);
			break;
		default:
			$r = false;
	}

	return $r;
}