Skip to content
This repository has been archived by the owner on May 25, 2020. It is now read-only.

Commit

Permalink
fix extra sql acl fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
prophet777 committed Feb 9, 2015
1 parent d4faac1 commit d6f6c6c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
73 changes: 55 additions & 18 deletions ORM/AclFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,44 +33,81 @@ public function __construct(
list($this->aclWalker, $this->roleHierarchy) = $options;
}


/**
* @param Query|QueryBuilder $criteria
*
* @return string (sql)
*/
protected function getSqlCriteria($criteria)
{
if($criteria instanceof QueryBuilder) {
return $criteria->getQuery()->getSQL();
} elseif($criteria instanceof Query){
return $criteria->getSQL();
} else{
throw new \Exception(sprintf(
'Criteria must be instance of Query or QueryBuilder, instance of %s given',
get_class($criteria)
));
}
}

/**
* @param Query|QueryBuilder|array $extraCriteria
*
* @return array
* @throws \Exception
*/
protected function getExtraCriteria($extraCriteria)
{
$sqlQueries = [];

if (!$extraCriteria) {
return $sqlQueries;
}

if (!is_array($extraCriteria)) {
$sqlQueries[] = $this->getSqlCriteria($extraCriteria);
}

if ($extraCriteria && !is_array($extraCriteria)) {
$extraCriteria = array($extraCriteria);
}

if (is_array($extraCriteria)) {
foreach ($extraCriteria as $criteria) {
$sqlQueries[] = $this->getSqlCriteria($criteria);
}
}

return $sqlQueries;
}

/**
* Apply ACL filter
*
* @param QueryBuilder | Query $query
* @param array $permissions
* @param string | UserInterface $identity
* @param string $alias
* @param array|Query|QueryBuilder $extraCriteria
*
* @return Query
*/
public function apply(
$query,
array $permissions = array('VIEW'),
$identity = null,
$alias = null,
$extraCriteria = false
$extraCriteria = false //Allow one or many
) {
if (null === $identity) {
$token = $this->securityContext->getToken();
$identity = $token->getUser();
}

if(!is_array($extraCriteria)){
$extraCriteria = array($extraCriteria);
}

$sqlQueries = [];
foreach($extraCriteria as $criteria){
if($criteria instanceof QueryBuilder) {
$sqlQueries[] = $criteria->getQuery()->getSQL();
} elseif($criteria instanceof Query){
$sqlQueries[] = $criteria->getSQL();
} else{
$sqlQueries[] = $criteria;
}
}


$query->setHint(static::HINT_ACL_EXTRA_CRITERIA, $sqlQueries);
$query->setHint(static::HINT_ACL_EXTRA_CRITERIA, $this->getExtraCriteria($extraCriteria));

if ($query instanceof QueryBuilder) {
$query = $this->cloneQuery($query->getQuery());
Expand Down
2 changes: 1 addition & 1 deletion ORM/AclWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function walkFromClause($fromClause)
if ($extraQueries) {
$extraCriteriaSql = $this->parseExtraQueries($extraQueries, $tableAlias);
$aclSql = <<<ACL_SQL
INNER JOIN ({$query}) {$aclAlias} ON ({$tableAlias}.id = {$aclAlias}.id OR ({{$extraCriteriaSql}))
INNER JOIN ({$query}) {$aclAlias} ON ({$tableAlias}.id = {$aclAlias}.id OR ({$extraCriteriaSql}))
ACL_SQL;
} else {
$aclSql = <<<ACL_SQL
Expand Down

0 comments on commit d6f6c6c

Please sign in to comment.