/** * @return string|boolean */ protected function preparePermissionSql() { if ($this->permissionSql !== null) { return $this->permissionSql; } if (\CCrmPerms::IsAdmin($this->userID)) { $this->permissionSql = ''; } else { $this->permissionSql = \CCrmPerms::BuildSql(\CCrmOwnerType::DealName, '', 'READ', array('RAW_QUERY' => true, 'PERMS' => \CCrmPerms::GetUserPermissions($this->userID))); } return $this->permissionSql; }
/** * @return Duplicate */ public function createDuplicate($entityTypeID, $rootEntityID, $userID, $enablePermissionCheck, $enableRanking, $limit = 0) { if ($entityTypeID !== \CCrmOwnerType::Lead && $entityTypeID !== \CCrmOwnerType::Contact && $entityTypeID !== \CCrmOwnerType::Company) { throw new Main\NotSupportedException("Entity type: '" . \CCrmOwnerType::ResolveName($entityTypeID) . "' is not supported in current context"); } /** @var Duplicate $dup **/ $dup = new Duplicate($this, array()); $query = static::createQuery(); $query->addSelect('ENTITY_ID'); $query->addFilter('=ENTITY_TYPE_ID', $entityTypeID); static::setQueryFilter($query, $this->getMatches()); if ($enablePermissionCheck) { $permissions = isset($params['PERMISSIONS']) ? $params['PERMISSIONS'] : null; if ($permissions === null) { $permissions = \CCrmPerms::GetUserPermissions($userID); } $permissionSql = \CCrmPerms::BuildSql(\CCrmOwnerType::ResolveName($entityTypeID), '', 'READ', array('RAW_QUERY' => true, 'PERMS' => $permissions)); if ($permissionSql === false) { //Access denied; return null; } if ($permissionSql !== '') { $query->addFilter('@ENTITY_ID', new Main\DB\SqlExpression($permissionSql)); } } if ($limit > 0) { $query->setLimit($limit); } if ($rootEntityID > 0) { $dup->setRootEntityID($rootEntityID); $query->addFilter('!ENTITY_ID', $rootEntityID); $query->addFilter('!@ENTITY_ID', DuplicateIndexMismatch::prepareQueryField($this, $entityTypeID, $rootEntityID, $userID)); } $dbResult = $query->exec(); $rankings = array(); while ($fields = $dbResult->fetch()) { $entityID = isset($fields['ENTITY_ID']) ? intval($fields['ENTITY_ID']) : 0; if ($entityID <= 0) { continue; } $entity = new DuplicateEntity($entityTypeID, $entityID); if ($enableRanking) { $rankings[] = $entity->getRanking(); } $dup->addEntity($entity); } $this->onAfterDuplicateCreated($dup, $entityTypeID, $userID, $enablePermissionCheck, $enableRanking, $rankings); if ($enableRanking) { DuplicateEntityRanking::initializeBulk($rankings, array('CHECK_PERMISSIONS' => $enablePermissionCheck, 'USER_ID' => $userID)); } return $dup; }
protected function preparePermissionSql() { if ($this->permissionSql !== null) { return $this->permissionSql; } $userID = $this->getUserID(); if (\CCrmPerms::IsAdmin($userID)) { $this->permissionSql = ''; } else { $this->permissionSql = \CCrmPerms::BuildSql(\CCrmOwnerType::ResolveName($this->getEntityTypeID()), '', 'READ', array('RAW_QUERY' => true, 'PERMS' => \CCrmPerms::GetUserPermissions($userID))); } return $this->permissionSql; }
public static function OnBuildSocNetLogPerms(&$perms, $params) { if (!is_array($params)) { $params = array(); } $aliasPrefix = isset($params['ALIAS_PREFIX']) ? $params['ALIAS_PREFIX'] : 'L'; $permType = isset($params['PERM_TYPE']) ? $params['PERM_TYPE'] : 'READ'; $options = isset($params['OPTIONS']) ? $params['OPTIONS'] : null; if (!is_array($options)) { $options = array(); } //The parameter 'IDENTITY_COLUMN' is required for CCrmPerms::BuildSql if (!(isset($options['IDENTITY_COLUMN']) && is_string($options['IDENTITY_COLUMN']) && $options['IDENTITY_COLUMN'] !== '')) { $options['IDENTITY_COLUMN'] = 'ENTITY_ID'; } $filterParams = isset($params['FILTER_PARAMS']) ? $params['FILTER_PARAMS'] : null; if (!is_array($filterParams)) { $filterParams = array(); } //$entityType = isset($filterParams['ENTITY_TYPE']) ? $filterParams['ENTITY_TYPE'] : ''; //$entityID = isset($filterParams['ENTITY_ID']) ? intval($filterParams['ENTITY_ID']) : 0; $affectedEntityTypes = isset($filterParams['AFFECTED_TYPES']) && is_array($filterParams['AFFECTED_TYPES']) ? $filterParams['AFFECTED_TYPES'] : array(); $result = array(); if (empty($affectedEntityTypes)) { //By default preparing SQL for all CRM types $activityPerms = array(); $result[CCrmLiveFeedEntity::Lead] = CCrmPerms::BuildSql(CCrmOwnerType::LeadName, $aliasPrefix, $permType, $options); $activityPerms[CCrmLiveFeedEntity::Lead] = CCrmPerms::BuildSql(CCrmOwnerType::LeadName, 'R', $permType, array('IDENTITY_COLUMN' => 'PARENT_ENTITY_ID')); $result[CCrmLiveFeedEntity::Contact] = CCrmPerms::BuildSql(CCrmOwnerType::ContactName, $aliasPrefix, $permType, $options); $activityPerms[CCrmLiveFeedEntity::Contact] = CCrmPerms::BuildSql(CCrmOwnerType::ContactName, 'R', $permType, array('IDENTITY_COLUMN' => 'PARENT_ENTITY_ID')); $result[CCrmLiveFeedEntity::Company] = CCrmPerms::BuildSql(CCrmOwnerType::CompanyName, $aliasPrefix, $permType, $options); $activityPerms[CCrmLiveFeedEntity::Company] = CCrmPerms::BuildSql(CCrmOwnerType::CompanyName, 'R', $permType, array('IDENTITY_COLUMN' => 'PARENT_ENTITY_ID')); $result[CCrmLiveFeedEntity::Deal] = CCrmPerms::BuildSql(CCrmOwnerType::DealName, $aliasPrefix, $permType, $options); $activityPerms[CCrmLiveFeedEntity::Deal] = CCrmPerms::BuildSql(CCrmOwnerType::DealName, 'R', $permType, array('IDENTITY_COLUMN' => 'PARENT_ENTITY_ID')); $result[CCrmLiveFeedEntity::Invoice] = CCrmPerms::BuildSql(CCrmOwnerType::InvoiceName, $aliasPrefix, $permType, $options); $isRestricted = false; $activityFeedEnityType = CCrmLiveFeedEntity::Activity; $relationTableName = CCrmSonetRelation::TABLE_NAME; foreach ($activityPerms as $type => $sql) { if ($sql === '') { $activityPerms[$type] = "SELECT R.ENTITY_ID FROM {$relationTableName} R WHERE R.SL_ENTITY_TYPE = '{$activityFeedEnityType}' AND R.SL_PARENT_ENTITY_TYPE = '{$type}'"; continue; } if (!$isRestricted) { $isRestricted = true; } if ($sql === false) { unset($activityPerms[$type]); continue; } $activityPerms[$type] = "SELECT R.ENTITY_ID FROM {$relationTableName} R WHERE R.SL_ENTITY_TYPE = '{$activityFeedEnityType}' AND R.SL_PARENT_ENTITY_TYPE = '{$type}' AND {$sql}"; } if (!$isRestricted) { $result[CCrmLiveFeedEntity::Activity] = ''; } elseif (!empty($activityPerms)) { $result[CCrmLiveFeedEntity::Activity] = $aliasPrefix . '.' . $options['IDENTITY_COLUMN'] . ' IN (' . implode(' UNION ALL ', $activityPerms) . ')'; } } else { if (in_array(CCrmLiveFeedEntity::Activity, $affectedEntityTypes, true)) { $activityPerms = array(); $activityPerms[CCrmLiveFeedEntity::Lead] = CCrmPerms::BuildSql(CCrmOwnerType::LeadName, 'R', $permType, array('IDENTITY_COLUMN' => 'PARENT_ENTITY_ID')); $activityPerms[CCrmLiveFeedEntity::Contact] = CCrmPerms::BuildSql(CCrmOwnerType::ContactName, 'R', $permType, array('IDENTITY_COLUMN' => 'PARENT_ENTITY_ID')); $activityPerms[CCrmLiveFeedEntity::Company] = CCrmPerms::BuildSql(CCrmOwnerType::CompanyName, 'R', $permType, array('IDENTITY_COLUMN' => 'PARENT_ENTITY_ID')); $activityPerms[CCrmLiveFeedEntity::Deal] = CCrmPerms::BuildSql(CCrmOwnerType::DealName, 'R', $permType, array('IDENTITY_COLUMN' => 'PARENT_ENTITY_ID')); $isRestricted = false; $activityFeedEnityType = CCrmLiveFeedEntity::Activity; $relationTableName = CCrmSonetRelation::TABLE_NAME; foreach ($activityPerms as $type => $sql) { if ($sql === '') { $activityPerms[$type] = "SELECT R.ENTITY_ID FROM {$relationTableName} R WHERE R.SL_ENTITY_TYPE = '{$activityFeedEnityType}' AND R.SL_PARENT_ENTITY_TYPE = '{$type}'"; continue; } if (!$isRestricted) { $isRestricted = true; } if ($sql === false) { unset($activityPerms[$type]); continue; } $activityPerms[$type] = "SELECT R.ENTITY_ID FROM {$relationTableName} R WHERE R.SL_ENTITY_TYPE = '{$activityFeedEnityType}' AND R.SL_PARENT_ENTITY_TYPE = '{$type}' AND {$sql}"; } if (!$isRestricted) { $result[CCrmLiveFeedEntity::Activity] = ''; } elseif (!empty($activityPerms)) { $result[CCrmLiveFeedEntity::Activity] = $aliasPrefix . '.' . $options['IDENTITY_COLUMN'] . ' IN (' . implode(' UNION ALL ', $activityPerms) . ')'; } } if (in_array(CCrmLiveFeedEntity::Lead, $affectedEntityTypes, true)) { $result[CCrmLiveFeedEntity::Lead] = CCrmPerms::BuildSql(CCrmOwnerType::LeadName, $aliasPrefix, $permType, $options); } if (in_array(CCrmLiveFeedEntity::Contact, $affectedEntityTypes, true)) { $result[CCrmLiveFeedEntity::Contact] = CCrmPerms::BuildSql(CCrmOwnerType::ContactName, $aliasPrefix, $permType, $options); } if (in_array(CCrmLiveFeedEntity::Company, $affectedEntityTypes, true)) { $result[CCrmLiveFeedEntity::Company] = CCrmPerms::BuildSql(CCrmOwnerType::CompanyName, $aliasPrefix, $permType, $options); } if (in_array(CCrmLiveFeedEntity::Deal, $affectedEntityTypes, true)) { $result[CCrmLiveFeedEntity::Deal] = CCrmPerms::BuildSql(CCrmOwnerType::DealName, $aliasPrefix, $permType, $options); } if (in_array(CCrmLiveFeedEntity::Invoice, $affectedEntityTypes, true)) { $result[CCrmLiveFeedEntity::Invoice] = CCrmPerms::BuildSql(CCrmOwnerType::InvoiceName, $aliasPrefix, $permType, $options); } } $resultSql = ''; $isRestricted = false; if (!empty($result)) { $entityTypeCol = 'ENTITY_TYPE'; if (isset($options['ENTITY_TYPE_COLUMN']) && is_string($options['ENTITY_TYPE_COLUMN']) && $options['ENTITY_TYPE_COLUMN'] !== '') { $entityTypeCol = $options['ENTITY_TYPE_COLUMN']; } foreach ($result as $type => &$sql) { if ($sql === false) { //Access denied //$resultSql .= "({$aliasPrefix}.{$entityTypeCol} = '{$type}' AND 1<>1)"; if (!$isRestricted) { $isRestricted = true; } } elseif (is_string($sql) && $sql !== '') { if ($resultSql !== '') { $resultSql .= ' OR '; } $resultSql .= "({$aliasPrefix}.{$entityTypeCol} = '{$type}' AND {$sql})"; if (!$isRestricted) { $isRestricted = true; } } else { if ($resultSql !== '') { $resultSql .= ' OR '; } //All entities are allowed $resultSql .= "{$aliasPrefix}.{$entityTypeCol} = '{$type}'"; } } unset($sql); } if ($isRestricted) { if ($resultSql !== '') { $perms[] = "({$resultSql})"; } else { //Access denied $perms[] = false; } } }
public static function BuildPermSql($sAliasPrefix = self::TABLE_ALIAS, $mPermType = 'READ', $arOptions = array()) { return CCrmPerms::BuildSql('QUOTE', $sAliasPrefix, $mPermType, $arOptions); }
public static function BuildPermSql($sAliasPrefix = 'L', $mPermType = 'READ', $arOptions = array()) { return CCrmPerms::BuildSql('DEAL', $sAliasPrefix, $mPermType, $arOptions); }
protected function onAfterDuplicateCreated(Duplicate $dup, $entityTypeID, $userID, $enablePermissionCheck, $enableRanking, array &$rankings) { $name = $this->name; $lastName = $this->lastName; $secondName = $this->secondName; $rootEntityID = $dup->getRootEntityID(); if ($secondName === '' && $name === '') { return; } $permissionSql = ''; if ($enablePermissionCheck) { $permissions = isset($params['PERMISSIONS']) ? $params['PERMISSIONS'] : null; if ($permissions === null) { $permissions = \CCrmPerms::GetUserPermissions($userID); } $permissionSql = \CCrmPerms::BuildSql(\CCrmOwnerType::ResolveName($entityTypeID), '', 'READ', array('RAW_QUERY' => true, 'PERMS' => $permissions)); if ($permissionSql === false) { //Access denied; return; } } if ($secondName !== '') { $query = new Main\Entity\Query(DuplicatePersonMatchCodeTable::getEntity()); $query->addSelect('ENTITY_ID'); $query->addFilter('=ENTITY_TYPE_ID', $entityTypeID); $query->addFilter('=LAST_NAME', $lastName); $query->addFilter('=NAME', $name); $query->addFilter('=SECOND_NAME', ''); if ($rootEntityID) { $query->addFilter('!ENTITY_ID', $rootEntityID); $query->addFilter('!@ENTITY_ID', DuplicateIndexMismatch::prepareQueryField(self::createFromMatches(array('LAST_NAME' => $lastName, 'NAME' => $name)), $entityTypeID, $rootEntityID, $userID)); } if ($enablePermissionCheck && $permissionSql !== '') { $query->addFilter('@ENTITY_ID', new Main\DB\SqlExpression($permissionSql)); } $dbResult = $query->exec(); while ($fields = $dbResult->fetch()) { $entityID = isset($fields['ENTITY_ID']) ? intval($fields['ENTITY_ID']) : 0; if ($entityID <= 0) { continue; } $entity = new DuplicateEntity($entityTypeID, $entityID); $entity->setCriterion(self::createFromMatches(array('LAST_NAME' => $lastName, 'NAME' => $name))); if ($enableRanking) { $rankings[] = $entity->getRanking(); } $dup->addEntity($entity); } } if ($name !== '') { $query = new Main\Entity\Query(DuplicatePersonMatchCodeTable::getEntity()); $query->addSelect('ENTITY_ID'); $query->addFilter('=ENTITY_TYPE_ID', $entityTypeID); $query->addFilter('=LAST_NAME', $lastName); $query->addFilter('=NAME', ''); $query->addFilter('=SECOND_NAME', ''); if ($rootEntityID) { $query->addFilter('!ENTITY_ID', $rootEntityID); $query->addFilter('!@ENTITY_ID', DuplicateIndexMismatch::prepareQueryField(self::createFromMatches(array('LAST_NAME' => $lastName)), $entityTypeID, $rootEntityID, $userID)); } if ($enablePermissionCheck && $permissionSql !== '') { $query->addFilter('@ENTITY_ID', new Main\DB\SqlExpression($permissionSql)); } $dbResult = $query->exec(); while ($fields = $dbResult->fetch()) { $entityID = isset($fields['ENTITY_ID']) ? intval($fields['ENTITY_ID']) : 0; if ($entityID <= 0) { continue; } $entity = new DuplicateEntity($entityTypeID, $entityID); $entity->setCriterion(self::createFromMatches(array('LAST_NAME' => $lastName))); if ($enableRanking) { $rankings[] = $entity->getRanking(); } $dup->addEntity($entity); } } }
/** * @return Main\Entity\Query */ private function createQuery($offset = 0, $limit = 0) { if (!is_int($offset)) { $offset = intval($offset); } if (!is_int($limit)) { $limit = intval($limit); } $typeIDs = $this->getTypeIDs(); if (empty($typeIDs)) { throw new Main\NotSupportedException("Criterion types are required."); } $query = new Main\Entity\Query(Entity\DuplicateIndexTable::getEntity()); $query->addSelect('ROOT_ENTITY_ID'); $query->addSelect('ROOT_ENTITY_NAME'); $query->addSelect('ROOT_ENTITY_TITLE'); $query->addSelect('QUANTITY'); $query->addSelect('TYPE_ID'); $query->addSelect('MATCHES'); $query->addSelect('IS_JUNK'); $permissionSql = ''; if ($this->enablePermissionCheck) { $permissions = \CCrmPerms::GetUserPermissions($this->userID); $permissionSql = \CCrmPerms::BuildSql(\CCrmOwnerType::ResolveName($this->entityTypeID), '', 'READ', array('RAW_QUERY' => true, 'PERMS' => $permissions)); if ($permissionSql === false) { //Access denied; return null; } } $query->addFilter('=USER_ID', $this->userID); $query->addFilter('=ENTITY_TYPE_ID', $this->entityTypeID); $query->addFilter('@TYPE_ID', $typeIDs); if ($this->enablePermissionCheck && $permissionSql !== '') { $query->addFilter('@ROOT_ENTITY_ID', new Main\DB\SqlExpression($permissionSql)); } if ($offset > 0) { $query->setOffset($offset); } if ($limit > 0) { $query->setLimit($limit); } $enableSorting = $this->sortTypeID !== DuplicateIndexType::UNDEFINED; if ($enableSorting) { $order = $this->sortOrder === SORT_DESC ? 'DESC' : 'ASC'; if ($this->sortTypeID === DuplicateIndexType::COMMUNICATION_EMAIL) { $query->addOrder('ROOT_ENTITY_EMAIL_FLAG', $order); $query->addOrder('ROOT_ENTITY_EMAIL', $order); } elseif ($this->sortTypeID === DuplicateIndexType::COMMUNICATION_PHONE) { $query->addOrder('ROOT_ENTITY_PHONE_FLAG', $order); $query->addOrder('ROOT_ENTITY_PHONE', $order); } elseif ($this->sortTypeID === DuplicateIndexType::PERSON) { $query->addOrder('ROOT_ENTITY_NAME_FLAG', $order); $query->addOrder('ROOT_ENTITY_NAME', $order); } elseif ($this->sortTypeID === DuplicateIndexType::ORGANIZATION) { $query->addOrder('ROOT_ENTITY_TITLE_FLAG', $order); $query->addOrder('ROOT_ENTITY_TITLE', $order); } } return $query; }
public static function BuildPermSql($sAliasPrefix = 'O', $mPermType = 'READ', $arOptions = array()) { $resultSql = CCrmPerms::BuildSql('INVOICE', $sAliasPrefix, $mPermType, $arOptions); if ($resultSql === false) { return '(1=0)'; } else { if ($resultSql === '') { return '(1=1)'; } } return '(' . $resultSql . ')'; }
public static function getRegisteredCodes($entityTypeID, $entityID, $enablePermissionCheck = false, $userID = 0, $limit = 50) { if (!is_int($entityTypeID)) { throw new Main\ArgumentTypeException('entityTypeID', 'integer'); } if (!is_int($entityID)) { throw new Main\ArgumentTypeException('entityID', 'integer'); } if (!is_int($userID)) { throw new Main\ArgumentTypeException('userID', 'integer'); } if (!is_bool($enablePermissionCheck)) { throw new Main\ArgumentTypeException('enablePermissionCheck', 'boolean'); } if (!is_int($limit)) { throw new Main\ArgumentTypeException('limit', 'integer'); } $query = new Main\Entity\Query(DuplicateCommunicationMatchCodeTable::getEntity()); $query->addSelect('TYPE'); $query->addSelect('VALUE'); $query->addFilter('=ENTITY_TYPE_ID', $entityTypeID); $query->addFilter('=ENTITY_ID', $entityID); if ($enablePermissionCheck && $userID > 0) { $permissions = isset($params['PERMISSIONS']) ? $params['PERMISSIONS'] : null; if ($permissions === null) { $permissions = \CCrmPerms::GetUserPermissions($userID); } $permissionSql = \CCrmPerms::BuildSql(\CCrmOwnerType::ResolveName($entityTypeID), '', 'READ', array('RAW_QUERY' => true, 'PERMS' => $permissions)); if ($permissionSql === false) { //Access denied; return array(); } elseif ($permissionSql !== '') { $query->addFilter('@ENTITY_ID', new Main\DB\SqlExpression($permissionSql)); } } if ($limit > 0) { $query->setLimit($limit); } $dbResult = $query->exec(); $results = array(); while ($fields = $dbResult->fetch()) { $type = isset($fields['TYPE']) ? $fields['TYPE'] : ''; $value = isset($fields['VALUE']) ? $fields['VALUE'] : ''; if (!isset($results[$type])) { $results[$type] = array(); } $results[$type][] = $value; } return $results; }