static function CanUserOperateDocument($operation, $userId, $documentId, $arParameters = array())
{
$documentId = trim($documentId);
if ($documentId == '')
return false;
if (!array_key_exists("IBlockId", $arParameters)
&& (
!array_key_exists("IBlockPermission", $arParameters)
|| !array_key_exists("DocumentStates", $arParameters)
|| !array_key_exists("IBlockRightsMode", $arParameters)
|| array_key_exists("IBlockRightsMode", $arParameters) && ($arParameters["IBlockRightsMode"] === "E")
)
|| !array_key_exists("CreatedBy", $arParameters) && !array_key_exists("AllUserGroups", $arParameters))
{
$dbElementList = CIBlockElement::GetList(
array(),
array("ID" => $documentId, "SHOW_NEW" => "Y", "SHOW_HISTORY" => "Y"),
false,
false,
array("ID", "IBLOCK_ID", "CREATED_BY")
);
$arElement = $dbElementList->Fetch();
if (!$arElement)
return false;
$arParameters["IBlockId"] = $arElement["IBLOCK_ID"];
$arParameters["CreatedBy"] = $arElement["CREATED_BY"];
}
if (!array_key_exists("IBlockRightsMode", $arParameters))
$arParameters["IBlockRightsMode"] = CIBlock::GetArrayByID($arParameters["IBlockId"], "RIGHTS_MODE");
if ($arParameters["IBlockRightsMode"] === "E")
{
if ($operation === CBPCanUserOperateOperation::ReadDocument)
return CIBlockElementRights::UserHasRightTo($arParameters["IBlockId"], $documentId, "element_read");
elseif ($operation === CBPCanUserOperateOperation::WriteDocument)
return CIBlockElementRights::UserHasRightTo($arParameters["IBlockId"], $documentId, "element_edit");
elseif (
$operation === CBPCanUserOperateOperation::StartWorkflow
|| $operation === CBPCanUserOperateOperation::ViewWorkflow
)
{
if (CIBlockElementRights::UserHasRightTo($arParameters["IBlockId"], $documentId, "element_edit"))
return true;
if (!array_key_exists("WorkflowId", $arParameters))
return false;
if (!CIBlockElementRights::UserHasRightTo($arParameters["IBlockId"], $documentId, "element_read"))
return false;
$userId = intval($userId);
if (!array_key_exists("AllUserGroups", $arParameters))
{
if (!array_key_exists("UserGroups", $arParameters))
$arParameters["UserGroups"] = CUser::GetUserGroup($userId);
$arParameters["AllUserGroups"] = $arParameters["UserGroups"];
if ($userId == $arParameters["CreatedBy"])
$arParameters["AllUserGroups"][] = "Author";
}
if (!array_key_exists("DocumentStates", $arParameters))
{
if ($operation === CBPCanUserOperateOperation::StartWorkflow)
$arParameters["DocumentStates"] = CBPWorkflowTemplateLoader::GetDocumentTypeStates(array("iblock", "CIBlockDocument", "iblock_".$arParameters["IBlockId"]));
else
$arParameters["DocumentStates"] = CBPDocument::GetDocumentStates(
array("iblock", "CIBlockDocument", "iblock_".$arParameters["IBlockId"]),
array("iblock", "CIBlockDocument", $documentId)
);
}
if (array_key_exists($arParameters["WorkflowId"], $arParameters["DocumentStates"]))
$arParameters["DocumentStates"] = array($arParameters["WorkflowId"] => $arParameters["DocumentStates"][$arParameters["WorkflowId"]]);
else
return false;
$arAllowableOperations = CBPDocument::GetAllowableOperations(
$userId,
$arParameters["AllUserGroups"],
$arParameters["DocumentStates"],
true
);
if (!is_array($arAllowableOperations))
return false;
if (($operation === CBPCanUserOperateOperation::ViewWorkflow) && in_array("read", $arAllowableOperations)
|| ($operation === CBPCanUserOperateOperation::StartWorkflow) && in_array("write", $arAllowableOperations))
return true;
$chop = ($operation === CBPCanUserOperateOperation::ViewWorkflow) ? "element_read" : "element_edit";
foreach ($arAllowableOperations as $op)
{
$ar = CTask::GetOperations($op, true);
if (in_array($chop, $ar))
return true;
}
}
elseif (
$operation === CBPCanUserOperateOperation::CreateWorkflow
)
{
return CBPDocument::CanUserOperateDocumentType(
CBPCanUserOperateOperation::CreateWorkflow,
$userId,
array("iblock", "CIBlockDocument", $documentId),
$arParameters
);
}
return false;
}
if (!array_key_exists("IBlockPermission", $arParameters))
{
if (CModule::IncludeModule('lists'))
$arParameters["IBlockPermission"] = CLists::GetIBlockPermission($arParameters["IBlockId"], $userId);
else
$arParameters["IBlockPermission"] = CIBlock::GetPermission($arParameters["IBlockId"], $userId);
}
if ($arParameters["IBlockPermission"] <= "R")
return false;
elseif ($arParameters["IBlockPermission"] >= "W")
return true;
$userId = intval($userId);
if (!array_key_exists("AllUserGroups", $arParameters))
{
if (!array_key_exists("UserGroups", $arParameters))
$arParameters["UserGroups"] = CUser::GetUserGroup($userId);
$arParameters["AllUserGroups"] = $arParameters["UserGroups"];
if ($userId == $arParameters["CreatedBy"])
$arParameters["AllUserGroups"][] = "Author";
}
if (!array_key_exists("DocumentStates", $arParameters))
{
$arParameters["DocumentStates"] = CBPDocument::GetDocumentStates(
array("iblock", "CIBlockDocument", "iblock_".$arParameters["IBlockId"]),
array("iblock", "CIBlockDocument", $documentId)
);
}
if (array_key_exists("WorkflowId", $arParameters))
{
if (array_key_exists($arParameters["WorkflowId"], $arParameters["DocumentStates"]))
$arParameters["DocumentStates"] = array($arParameters["WorkflowId"] => $arParameters["DocumentStates"][$arParameters["WorkflowId"]]);
else
return false;
}
$arAllowableOperations = CBPDocument::GetAllowableOperations(
$userId,
$arParameters["AllUserGroups"],
$arParameters["DocumentStates"]
);
if (!is_array($arAllowableOperations))
return false;
$r = false;
switch ($operation)
{
case CBPCanUserOperateOperation::ViewWorkflow:
$r = in_array("read", $arAllowableOperations);
break;
case CBPCanUserOperateOperation::StartWorkflow:
$r = in_array("write", $arAllowableOperations);
break;
case CBPCanUserOperateOperation::CreateWorkflow:
$r = false;
break;
case CBPCanUserOperateOperation::WriteDocument:
$r = in_array("write", $arAllowableOperations);
break;
case CBPCanUserOperateOperation::ReadDocument:
$r = in_array("read", $arAllowableOperations) || in_array("write", $arAllowableOperations);
break;
default:
$r = false;
}
return $r;
}