CBPVirtualDocument :: CanUserOperateDocument: Bitrix method

      
<?php 
//  CBPVirtualDocument :: CanUserOperateDocument()
//  /dist/bitrix/modules/bizproc/classes/general/virtualdocument.php:865

    
function CanUserOperateDocument($operation$userId$documentId$arParameters = array())
    {
        
$documentId trim($documentId);
        if (
strlen($documentId) <= 0)
            return 
false;

        
$userId intval($userId);

        if (
array_key_exists("UserIsAdmin"$arParameters))
        {
            if (
$arParameters["UserIsAdmin"] === true)
                return 
true;
        }
        else
        {
            
$arGroups CUser::GetUserGroup($userId);
            if (
in_array(1$arGroups))
                return 
true;
        }

        if (!
array_key_exists("TargetUser"$arParameters) || !array_key_exists("DocumentType"$arParameters))
        {
            
$dbElementList CIBlockElement::GetList(
                array(),
                array(
"ID" => $documentId"SHOW_NEW" => "Y"),
                
false,
                
false,
                array(
"ID""IBLOCK_ID""CREATED_BY")
            );
            
$arElement $dbElementList->Fetch();

            if (!
$arElement)
                return 
false;

            
$arParameters["TargetUser"] = $arElement["CREATED_BY"];
            
$arParameters["DocumentType"] = "type_".$arElement["IBLOCK_ID"];
        }

        if (!
array_key_exists("AllUserGroups"$arParameters))
        {
            if (!
array_key_exists("UserGroups"$arParameters))
                
$arParameters["UserGroups"] = CUser::GetUserGroup($userId);

            
$arParameters["AllUserGroups"] = $arParameters["UserGroups"];
            if (
$userId == $arParameters["TargetUser"])
                
$arParameters["AllUserGroups"][] = "author";
        }

        if (!
array_key_exists("DocumentStates"$arParameters))
        {
            
$arParameters["DocumentStates"] = CBPDocument::GetDocumentStates(
                array(
"bizproc""CBPVirtualDocument"$arParameters["DocumentType"]),
                array(
"bizproc""CBPVirtualDocument"$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"]
        );

        
// $arAllowableOperations == null - workflow is not a statemachine
        // $arAllowableOperations == array() - no allowable operations
        // $arAllowableOperations == array("read", ...) - allowable operations list
        
if (!is_array($arAllowableOperations))
            return 
in_array("author"$arParameters["AllUserGroups"]);

        
$r false;
        switch (
$operation)
        {
            case 
0:                // DOCUMENT_OPERATION_VIEW_WORKFLOW
                
$r in_array("read"$arAllowableOperations);
                break;
            case 
1:                // DOCUMENT_OPERATION_START_WORKFLOW
                
$r in_array("create"$arAllowableOperations);
                break;
            case 
4:                // DOCUMENT_OPERATION_CREATE_WORKFLOW
                
$r false;
                break;
            case 
2:                // DOCUMENT_OPERATION_WRITE_DOCUMENT
                
$r in_array("create"$arAllowableOperations);
                break;
            case 
3:                // DOCUMENT_OPERATION_READ_DOCUMENT
                
$r in_array("read"$arAllowableOperations);
                break;
            default:
                
$r false;
        }

        return 
$r;
    }