public static function BuildPermSql($aliasPrefix = 'A', $permType = 'READ', $arOptions = array()) { if (!is_array($arOptions)) { $arOptions = array(); } $userPermissions = isset($arOptions['PERMS']) ? $arOptions['PERMS'] : null; $userID = $userPermissions !== null && is_object($userPermissions) ? $userPermissions->GetUserID() : 0; if (CCrmPerms::IsAdmin($userID)) { return ''; } if (!CCrmPerms::IsAccessEnabled($userPermissions)) { // User does not have permissions at all. return false; } $entitiesSql = array(); $permOptions = array_merge(array('IDENTITY_COLUMN' => 'OWNER_ID'), $arOptions); $entitiesSql[strval(CCrmOwnerType::Lead)] = CCrmLead::BuildPermSql($aliasPrefix, $permType, $permOptions); $entitiesSql[strval(CCrmOwnerType::Deal)] = CCrmDeal::BuildPermSql($aliasPrefix, $permType, $permOptions); $entitiesSql[strval(CCrmOwnerType::Contact)] = CCrmContact::BuildPermSql($aliasPrefix, $permType, $permOptions); $entitiesSql[strval(CCrmOwnerType::Company)] = CCrmCompany::BuildPermSql($aliasPrefix, $permType, $permOptions); $entitiesSql[strval(CCrmOwnerType::Invoice)] = CCrmInvoice::BuildPermSql($aliasPrefix, $permType, $permOptions); foreach ($entitiesSql as $entityTypeID => $entitySql) { if (!is_string($entitySql)) { //If $entityPermSql is not string - acces denied. Clear permission SQL and related records will be ignored. unset($entitiesSql[$entityTypeID]); continue; } if ($entitySql !== '') { $entitiesSql[$entityTypeID] = '(' . $aliasPrefix . '.OWNER_TYPE_ID = ' . $entityTypeID . ' AND (' . $entitySql . ') )'; } else { // No permissions check - fetch all related records $entitiesSql[$entityTypeID] = '(' . $aliasPrefix . '.OWNER_TYPE_ID = ' . $entityTypeID . ')'; } } //If $entitiesSql is empty - user does not have permissions at all. if (empty($entitiesSql)) { return false; } $userID = CCrmSecurityHelper::GetCurrentUserID(); if ($userID > 0) { //Allow responsible user to view activity without permissions check. return $aliasPrefix . '.RESPONSIBLE_ID = ' . $userID . ' OR ' . implode(' OR ', $entitiesSql); } else { return implode(' OR ', $entitiesSql); } }
public static function beforeViewDataQuery(&$select, &$filter, &$group, &$order, &$limit, &$options, &$runtime) { if (!isset($select['CRM_INVOICE_INVOICE_UTS_COMPANY_BY_ID'])) { foreach ($select as $k => $v) { if (strpos($k, 'CRM_INVOICE_INVOICE_UTS_COMPANY_BY_') === 0) { $select['CRM_INVOICE_INVOICE_UTS_COMPANY_BY_ID'] = 'INVOICE_UTS.COMPANY_BY.ID'; break; } } } if (!isset($select['CRM_INVOICE_INVOICE_UTS_CONTACT_BY_ID'])) { foreach ($select as $k => $v) { if (strpos($k, 'CRM_INVOICE_INVOICE_UTS_CONTACT_BY_') === 0) { $select['CRM_INVOICE_INVOICE_UTS_CONTACT_BY_ID'] = 'INVOICE_UTS.CONTACT_BY.ID'; break; } } } // HACK: Switch to order by SATTUS_BY.SORT instead STATUS_BY.STATUS_ID // We are trying to adhere user defined sort rules. if (isset($order['STATUS_ID'])) { $select['CRM_INVOICE_STATUS_BY_SORT'] = 'STATUS_BY.SORT'; $order['CRM_INVOICE_STATUS_BY_SORT'] = $order['STATUS_ID']; unset($order['STATUS_ID']); } if (!isset($select['CRM_INVOICE_INVOICE_UTS_CONTACT_BY_ID'])) { foreach ($select as $k => $v) { if (strpos($k, 'CRM_INVOICE_INVOICE_UTS_CONTACT_BY_') === 0) { $select['CRM_INVOICE_INVOICE_UTS_CONTACT_BY_ID'] = 'INVOICE_UTS.CONTACT_BY.ID'; break; } } } if (!isset($select['CRM_INVOICE_INVOICE_UTS_COMPANY_BY_ID'])) { foreach ($select as $k => $v) { if (strpos($k, 'CRM_INVOICE_INVOICE_UTS_COMPANY_BY_') === 0) { $select['CRM_INVOICE_INVOICE_UTS_COMPANY_BY_ID'] = 'INVOICE_UTS.COMPANY_BY.ID'; break; } } } // permission $addClause = CCrmInvoice::BuildPermSql('crm_invoice'); if (!empty($addClause)) { global $DB; // HACK: add escape chars for ORM $addClause = str_replace('crm_invoice.ID', $DB->escL . 'crm_invoice' . $DB->escR . '.ID', $addClause); $filter = array($filter, '=IS_ALLOWED' => '1'); $runtime['IS_ALLOWED'] = array('data_type' => 'integer', 'expression' => array('CASE WHEN ' . $addClause . ' THEN 1 ELSE 0 END')); } }