CAdvContract :: GetUserPermissions: Bitrix method

      
<?php 
//  CAdvContract :: GetUserPermissions()
//  /dist/bitrix/modules/advertising/classes/general/advertising.php:265

    
public static function GetUserPermissions($CONTRACT_ID=0$USER_ID=false)
    {
        
$err_mess = (CAdvContract_all::err_mess())."<br>Function: GetUserPermissions<br>Line: ";
        global 
$DB$USER;
        static 
$CONTRACT_RIGHTS = array();
        
$USER_ID = ($USER_ID===false) ? intval($USER->GetID()) : intval($USER_ID);
        if (
intval($USER_ID)<=0) return false;
        
$CONTRACT_ID intval($CONTRACT_ID);
        
$arrRes = array();

        if (
$CONTRACT_ID>&& is_set($CONTRACT_RIGHTS[$USER_ID], $CONTRACT_ID))
        {
            
$arrRes $CONTRACT_RIGHTS[$USER_ID];
        }
        else
        {
            
$isManager CAdvContract::IsManager($USER_ID);
            
$isAdmin CAdvContract::IsAdmin($USER_ID);

            if (
$isAdmin$arrRes[0] = CAdvContract::GetMaxPermissionsArray();
            elseif (
$isManager$arrRes[0] = array("VIEW""ADD");
            else 
$arrRes[0] = array();

            
$strSqlSearch "";
            if (
$CONTRACT_ID>0)
                
$strSqlSearch " and C.ID= $CONTRACT_ID ";
            
$strSql "
                SELECT
                    C.ID,
                    CU.PERMISSION
                FROM
                    b_adv_contract C
                LEFT JOIN b_adv_contract_2_user CU ON (CU.CONTRACT_ID=C.ID and CU.USER_ID=
$USER_ID)
                WHERE
                    1=1
                
$strSqlSearch
                "
;
            
$rs $DB->Query($strSqlfalse$err_mess.__LINE__);
            while (
$ar $rs->Fetch()) $arrRes[$ar["ID"]][] = $ar["PERMISSION"];
            if (
$isAdmin || $isManager)
            {
                
reset($arrRes);
                while (list(
$cid$arrPerm) = each($arrRes))
                {
                    if (
$isAdmin$arrPerm CAdvContract::GetMaxPermissionsArray();
                    elseif (
$isManager)
                    {
                        
$arrPerm[] = "VIEW";
                        
$arrPerm[] = "ADD";
                        
$arrPerm array_unique($arrPerm);
                    }
                    
$arrRes[$cid] = $arrPerm;
                }
            }
            
reset($arrRes);
            while (list(
$cid$arrPerm) = each($arrRes))
            {
                
TrimArr($arrPerm);
                
$CONTRACT_RIGHTS[$USER_ID][$cid] = $arrPerm;
                
$arrRes[$cid] = $arrPerm;
            }
        }

        return 
$arrRes;
    }