/** * Get a condition object for item listings. * * @param string $objectKey * * @return Condition */ public function getListingCondition($objectKey) { $objectKey = Objects::normalizeObjectKey($objectKey); $obj = $this->objects->getStorageController($objectKey); $rules = self::getRules($objectKey, static::MODE_LISTING); if (count($rules) === 0) { return null; } if ($this->getCaching()) { $cacheKey = md5($objectKey); $cached = $this->cacher->getDistributedCache('core/acl/listing/' . $cacheKey); if (null !== $cached) { return $cached; } } $condition = ''; $primaryList = $this->objects->getPrimaryList($objectKey); $primaryKey = current($primaryList); $denyList = array(); $conditionObject = new Condition(null, $this->jarves); foreach ($rules as $rule) { if ($rule['constraint_type'] === ACL::CONSTRAINT_EXACT) { //todo $rule['constraint_code'] can be a (urlencoded) composite pk //todo constraint_code is always urlencoded; $condition = Condition::create(array($primaryKey, '=', Tools::urlDecode($rule['constraint_code'])), $this->jarves); } if ($rule['constraint_type'] === ACL::CONSTRAINT_CONDITION) { $condition = Condition::create($rule['constraint_code'], $this->jarves); } if ($rule['constraint_type'] === ACL::CONSTRAINT_ALL) { $condition = array('1', '=', '1'); } elseif ($rule['sub']) { $subCondition = $obj->getNestedSubCondition($condition); if ($subCondition) { $condition = array($condition, 'OR', $subCondition); } } if ($rule['access'] === 1) { if ($denyList) { $condition = array($condition, 'AND NOT', $denyList); $conditionObject->addOr($condition); // $conditionObject->add('AND NOT', $denyList); } else { $conditionObject->addOr($condition); } } if ($rule['access'] !== 1) { if ($denyList) { $denyList[] = 'AND NOT'; } $denyList[] = $condition; } } if (!$conditionObject->hasRules()) { $conditionObject->addAnd(array('1', '!=', '1')); } if ($this->getCaching()) { $cacheKey = md5($objectKey); $this->cacher->setDistributedCache('core/acl/listing/' . $cacheKey, $conditionObject); } return $conditionObject; }
/** * @param array $condition * @param string $objectKey * * @return array */ public function primaryKeyToCondition($condition, $objectKey = null) { $result = array(); // condition: // [ // ["bla", "=", 1], "and" // // ] // // pk: // 1 // // pk: // ["bla" => 2, "hosa" => 1] // // pk: // [ ["bla" => 1], ["bla" => 2] ] if ($condition instanceof Condition) { return $condition->getRules(); } if (is_array($condition) && array_key_exists(0, $condition) && is_array($condition[0]) && is_numeric(key($condition)) && is_numeric(key($condition[0]))) { //its already a condition object return $condition; } $primaries = null; if ($objectKey && $this->objects->getDefinition($objectKey)) { $primaries = $this->objects->getPrimaryList($objectKey); } if (array_key_exists(0, $condition)) { foreach ($condition as $idx => $group) { $cGroup = array(); if (is_array($group)) { foreach ($group as $primKey => $primValue) { if (!is_string($primKey)) { $primKey = $primaries[$primKey]; } if ($cGroup) { $cGroup[] = 'and'; } $cGroup[] = array($primKey, '=', $primValue); } } else { $primKey = $idx; if (!is_string($idx) && $primaries) { $primKey = $primaries[0]; } if ($cGroup) { $cGroup[] = 'and'; } $cGroup[] = array($primKey, '=', $group); } if ($result) { $result[] = 'or'; } $result[] = $cGroup; } } else { //we only have to select one row $group = array(); foreach ($condition as $primKey => $primValue) { if ($group) { $group[] = 'and'; } $group[] = array($primKey, '=', $primValue); } $result[] = $group; } return $result; }