AclProvider.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Security\Acl\Dbal;
  11. use Doctrine\DBAL\Driver\Connection;
  12. use Doctrine\DBAL\Driver\Statement;
  13. use Symfony\Component\Security\Acl\Model\AclInterface;
  14. use Symfony\Component\Security\Acl\Domain\Acl;
  15. use Symfony\Component\Security\Acl\Domain\Entry;
  16. use Symfony\Component\Security\Acl\Domain\FieldEntry;
  17. use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
  18. use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
  19. use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
  20. use Symfony\Component\Security\Acl\Exception\AclNotFoundException;
  21. use Symfony\Component\Security\Acl\Exception\NotAllAclsFoundException;
  22. use Symfony\Component\Security\Acl\Model\AclCacheInterface;
  23. use Symfony\Component\Security\Acl\Model\AclProviderInterface;
  24. use Symfony\Component\Security\Acl\Model\ObjectIdentityInterface;
  25. use Symfony\Component\Security\Acl\Model\PermissionGrantingStrategyInterface;
  26. /**
  27. * An ACL provider implementation.
  28. *
  29. * This provider assumes that all ACLs share the same PermissionGrantingStrategy.
  30. *
  31. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  32. */
  33. class AclProvider implements AclProviderInterface
  34. {
  35. const MAX_BATCH_SIZE = 30;
  36. protected $cache;
  37. protected $connection;
  38. protected $loadedAces;
  39. protected $loadedAcls;
  40. protected $options;
  41. private $permissionGrantingStrategy;
  42. /**
  43. * Constructor.
  44. *
  45. * @param Connection $connection
  46. * @param PermissionGrantingStrategyInterface $permissionGrantingStrategy
  47. * @param array $options
  48. * @param AclCacheInterface $cache
  49. */
  50. public function __construct(Connection $connection, PermissionGrantingStrategyInterface $permissionGrantingStrategy, array $options, AclCacheInterface $cache = null)
  51. {
  52. $this->cache = $cache;
  53. $this->connection = $connection;
  54. $this->loadedAces = array();
  55. $this->loadedAcls = array();
  56. $this->options = $options;
  57. $this->permissionGrantingStrategy = $permissionGrantingStrategy;
  58. }
  59. /**
  60. * {@inheritDoc}
  61. */
  62. public function findChildren(ObjectIdentityInterface $parentOid, $directChildrenOnly = false)
  63. {
  64. $sql = $this->getFindChildrenSql($parentOid, $directChildrenOnly);
  65. $children = array();
  66. foreach ($this->connection->executeQuery($sql)->fetchAll() as $data) {
  67. $children[] = new ObjectIdentity($data['object_identifier'], $data['class_type']);
  68. }
  69. return $children;
  70. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. public function findAcl(ObjectIdentityInterface $oid, array $sids = array())
  75. {
  76. return $this->findAcls(array($oid), $sids)->offsetGet($oid);
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. public function findAcls(array $oids, array $sids = array())
  82. {
  83. $result = new \SplObjectStorage();
  84. $currentBatch = array();
  85. $oidLookup = array();
  86. for ($i=0,$c=count($oids); $i<$c; $i++) {
  87. $oid = $oids[$i];
  88. $oidLookupKey = $oid->getIdentifier().$oid->getType();
  89. $oidLookup[$oidLookupKey] = $oid;
  90. $aclFound = false;
  91. // check if result already contains an ACL
  92. if ($result->contains($oid)) {
  93. $aclFound = true;
  94. }
  95. // check if this ACL has already been hydrated
  96. if (!$aclFound && isset($this->loadedAcls[$oid->getType()][$oid->getIdentifier()])) {
  97. $acl = $this->loadedAcls[$oid->getType()][$oid->getIdentifier()];
  98. if (!$acl->isSidLoaded($sids)) {
  99. // FIXME: we need to load ACEs for the missing SIDs. This is never
  100. // reached by the default implementation, since we do not
  101. // filter by SID
  102. throw new \RuntimeException('This is not supported by the default implementation.');
  103. } else {
  104. $result->attach($oid, $acl);
  105. $aclFound = true;
  106. }
  107. }
  108. // check if we can locate the ACL in the cache
  109. if (!$aclFound && null !== $this->cache) {
  110. $acl = $this->cache->getFromCacheByIdentity($oid);
  111. if (null !== $acl) {
  112. if ($acl->isSidLoaded($sids)) {
  113. // check if any of the parents has been loaded since we need to
  114. // ensure that there is only ever one ACL per object identity
  115. $parentAcl = $acl->getParentAcl();
  116. while (null !== $parentAcl) {
  117. $parentOid = $parentAcl->getObjectIdentity();
  118. if (isset($this->loadedAcls[$parentOid->getType()][$parentOid->getIdentifier()])) {
  119. $acl->setParentAcl($this->loadedAcls[$parentOid->getType()][$parentOid->getIdentifier()]);
  120. break;
  121. } else {
  122. $this->loadedAcls[$parentOid->getType()][$parentOid->getIdentifier()] = $parentAcl;
  123. $this->updateAceIdentityMap($parentAcl);
  124. }
  125. $parentAcl = $parentAcl->getParentAcl();
  126. }
  127. $this->loadedAcls[$oid->getType()][$oid->getIdentifier()] = $acl;
  128. $this->updateAceIdentityMap($acl);
  129. $result->attach($oid, $acl);
  130. $aclFound = true;
  131. } else {
  132. $this->cache->evictFromCacheByIdentity($oid);
  133. foreach ($this->findChildren($oid) as $childOid) {
  134. $this->cache->evictFromCacheByIdentity($childOid);
  135. }
  136. }
  137. }
  138. }
  139. // looks like we have to load the ACL from the database
  140. if (!$aclFound) {
  141. $currentBatch[] = $oid;
  142. }
  143. // Is it time to load the current batch?
  144. if ((self::MAX_BATCH_SIZE === count($currentBatch) || ($i + 1) === $c) && count($currentBatch) > 0) {
  145. $loadedBatch = $this->lookupObjectIdentities($currentBatch, $sids, $oidLookup);
  146. foreach ($loadedBatch as $loadedOid) {
  147. $loadedAcl = $loadedBatch->offsetGet($loadedOid);
  148. if (null !== $this->cache) {
  149. $this->cache->putInCache($loadedAcl);
  150. }
  151. if (isset($oidLookup[$loadedOid->getIdentifier().$loadedOid->getType()])) {
  152. $result->attach($loadedOid, $loadedAcl);
  153. }
  154. }
  155. $currentBatch = array();
  156. }
  157. }
  158. // check that we got ACLs for all the identities
  159. foreach ($oids as $oid) {
  160. if (!$result->contains($oid)) {
  161. if (1 === count($oids)) {
  162. throw new AclNotFoundException(sprintf('No ACL found for %s.', $oid));
  163. }
  164. $partialResultException = new NotAllAclsFoundException('The provider could not find ACLs for all object identities.');
  165. $partialResultException->setPartialResult($result);
  166. throw $partialResultException;
  167. }
  168. }
  169. return $result;
  170. }
  171. /**
  172. * Constructs the query used for looking up object identities and associated
  173. * ACEs, and security identities.
  174. *
  175. * @param array $ancestorIds
  176. * @return string
  177. */
  178. protected function getLookupSql(array $ancestorIds)
  179. {
  180. // FIXME: add support for filtering by sids (right now we select all sids)
  181. $sql = <<<SELECTCLAUSE
  182. SELECT
  183. o.id as acl_id,
  184. o.object_identifier,
  185. o.parent_object_identity_id,
  186. o.entries_inheriting,
  187. c.class_type,
  188. e.id as ace_id,
  189. e.object_identity_id,
  190. e.field_name,
  191. e.ace_order,
  192. e.mask,
  193. e.granting,
  194. e.granting_strategy,
  195. e.audit_success,
  196. e.audit_failure,
  197. s.username,
  198. s.identifier as security_identifier
  199. FROM
  200. {$this->options['oid_table_name']} o
  201. INNER JOIN {$this->options['class_table_name']} c ON c.id = o.class_id
  202. LEFT JOIN {$this->options['entry_table_name']} e ON (
  203. e.class_id = o.class_id AND (e.object_identity_id = o.id OR {$this->connection->getDatabasePlatform()->getIsNullExpression('e.object_identity_id')})
  204. )
  205. LEFT JOIN {$this->options['sid_table_name']} s ON (
  206. s.id = e.security_identity_id
  207. )
  208. WHERE (o.id =
  209. SELECTCLAUSE;
  210. $sql .= implode(' OR o.id = ', $ancestorIds).')';
  211. return $sql;
  212. }
  213. protected function getAncestorLookupSql(array $batch)
  214. {
  215. $sql = <<<SELECTCLAUSE
  216. SELECT a.ancestor_id
  217. FROM
  218. {$this->options['oid_table_name']} o
  219. INNER JOIN {$this->options['class_table_name']} c ON c.id = o.class_id
  220. INNER JOIN {$this->options['oid_ancestors_table_name']} a ON a.object_identity_id = o.id
  221. WHERE (
  222. SELECTCLAUSE;
  223. $types = array();
  224. $count = count($batch);
  225. for ($i = 0; $i < $count; $i++) {
  226. if (!isset($types[$batch[$i]->getType()])) {
  227. $types[$batch[$i]->getType()] = true;
  228. // if there is more than one type we can safely break out of the
  229. // loop, because it is the differentiator factor on whether to
  230. // query for only one or more class types
  231. if (count($types) > 1) {
  232. break;
  233. }
  234. }
  235. }
  236. if (1 === count($types)) {
  237. $ids = array();
  238. for ($i = 0; $i < $count; $i++) {
  239. $ids[] = $this->connection->quote($batch[$i]->getIdentifier());
  240. }
  241. $sql .= sprintf(
  242. '(o.object_identifier IN (%s) AND c.class_type = %s)',
  243. implode(',', $ids),
  244. $this->connection->quote($batch[0]->getType())
  245. );
  246. } else {
  247. $where = '(o.object_identifier = %s AND c.class_type = %s)';
  248. for ($i = 0; $i < $count; $i++) {
  249. $sql .= sprintf(
  250. $where,
  251. $this->connection->quote($batch[$i]->getIdentifier()),
  252. $this->connection->quote($batch[$i]->getType())
  253. );
  254. if ($i+1 < $count) {
  255. $sql .= ' OR ';
  256. }
  257. }
  258. }
  259. $sql .= ')';
  260. return $sql;
  261. }
  262. /**
  263. * Constructs the SQL for retrieving child object identities for the given
  264. * object identities.
  265. *
  266. * @param ObjectIdentityInterface $oid
  267. * @param Boolean $directChildrenOnly
  268. * @return string
  269. */
  270. protected function getFindChildrenSql(ObjectIdentityInterface $oid, $directChildrenOnly)
  271. {
  272. if (false === $directChildrenOnly) {
  273. $query = <<<FINDCHILDREN
  274. SELECT o.object_identifier, c.class_type
  275. FROM
  276. {$this->options['oid_table_name']} as o
  277. INNER JOIN {$this->options['class_table_name']} as c ON c.id = o.class_id
  278. INNER JOIN {$this->options['oid_ancestors_table_name']} as a ON a.object_identity_id = o.id
  279. WHERE
  280. a.ancestor_id = %d AND a.object_identity_id != a.ancestor_id
  281. FINDCHILDREN;
  282. } else {
  283. $query = <<<FINDCHILDREN
  284. SELECT o.object_identifier, c.class_type
  285. FROM {$this->options['oid_table_name']} as o
  286. INNER JOIN {$this->options['class_table_name']} as c ON c.id = o.class_id
  287. WHERE o.parent_object_identity_id = %d
  288. FINDCHILDREN;
  289. }
  290. return sprintf($query, $this->retrieveObjectIdentityPrimaryKey($oid));
  291. }
  292. /**
  293. * Constructs the SQL for retrieving the primary key of the given object
  294. * identity.
  295. *
  296. * @param ObjectIdentityInterface $oid
  297. * @return string
  298. */
  299. protected function getSelectObjectIdentityIdSql(ObjectIdentityInterface $oid)
  300. {
  301. $query = <<<QUERY
  302. SELECT o.id
  303. FROM %s o
  304. INNER JOIN %s c ON c.id = o.class_id
  305. WHERE o.object_identifier = %s AND c.class_type = %s
  306. QUERY;
  307. return sprintf(
  308. $query,
  309. $this->options['oid_table_name'],
  310. $this->options['class_table_name'],
  311. $this->connection->quote($oid->getIdentifier()),
  312. $this->connection->quote($oid->getType())
  313. );
  314. }
  315. /**
  316. * Returns the primary key of the passed object identity.
  317. *
  318. * @param ObjectIdentityInterface $oid
  319. * @return integer
  320. */
  321. final protected function retrieveObjectIdentityPrimaryKey(ObjectIdentityInterface $oid)
  322. {
  323. return $this->connection->executeQuery($this->getSelectObjectIdentityIdSql($oid))->fetchColumn();
  324. }
  325. /**
  326. * This method is called when an ACL instance is retrieved from the cache.
  327. *
  328. * @param AclInterface $acl
  329. */
  330. private function updateAceIdentityMap(AclInterface $acl)
  331. {
  332. foreach (array('classAces', 'classFieldAces', 'objectAces', 'objectFieldAces') as $property) {
  333. $reflection = new \ReflectionProperty($acl, $property);
  334. $reflection->setAccessible(true);
  335. $value = $reflection->getValue($acl);
  336. if ('classAces' === $property || 'objectAces' === $property) {
  337. $this->doUpdateAceIdentityMap($value);
  338. } else {
  339. foreach ($value as $field => $aces) {
  340. $this->doUpdateAceIdentityMap($value[$field]);
  341. }
  342. }
  343. $reflection->setValue($acl, $value);
  344. $reflection->setAccessible(false);
  345. }
  346. }
  347. /**
  348. * Retrieves all the ids which need to be queried from the database
  349. * including the ids of parent ACLs.
  350. *
  351. * @param array $batch
  352. *
  353. * @return array
  354. */
  355. private function getAncestorIds(array $batch)
  356. {
  357. $sql = $this->getAncestorLookupSql($batch);
  358. $ancestorIds = array();
  359. foreach ($this->connection->executeQuery($sql)->fetchAll() as $data) {
  360. // FIXME: skip ancestors which are cached
  361. $ancestorIds[] = $data['ancestor_id'];
  362. }
  363. return $ancestorIds;
  364. }
  365. /**
  366. * Does either overwrite the passed ACE, or saves it in the global identity
  367. * map to ensure every ACE only gets instantiated once.
  368. *
  369. * @param array &$aces
  370. */
  371. private function doUpdateAceIdentityMap(array &$aces)
  372. {
  373. foreach ($aces as $index => $ace) {
  374. if (isset($this->loadedAces[$ace->getId()])) {
  375. $aces[$index] = $this->loadedAces[$ace->getId()];
  376. } else {
  377. $this->loadedAces[$ace->getId()] = $ace;
  378. }
  379. }
  380. }
  381. /**
  382. * This method is called for object identities which could not be retrieved
  383. * from the cache, and for which thus a database query is required.
  384. *
  385. * @param array $batch
  386. * @param array $sids
  387. * @param array $oidLookup
  388. *
  389. * @return \SplObjectStorage mapping object identities to ACL instances
  390. *
  391. * @throws AclNotFoundException
  392. */
  393. private function lookupObjectIdentities(array $batch, array $sids, array $oidLookup)
  394. {
  395. $ancestorIds = $this->getAncestorIds($batch);
  396. if (!$ancestorIds) {
  397. throw new AclNotFoundException('There is no ACL for the given object identity.');
  398. }
  399. $sql = $this->getLookupSql($ancestorIds);
  400. $stmt = $this->connection->executeQuery($sql);
  401. return $this->hydrateObjectIdentities($stmt, $oidLookup, $sids);
  402. }
  403. /**
  404. * This method is called to hydrate ACLs and ACEs.
  405. *
  406. * This method was designed for performance; thus, a lot of code has been
  407. * inlined at the cost of readability, and maintainability.
  408. *
  409. * Keep in mind that changes to this method might severely reduce the
  410. * performance of the entire ACL system.
  411. *
  412. * @param Statement $stmt
  413. * @param array $oidLookup
  414. * @param array $sids
  415. * @throws \RuntimeException
  416. * @return \SplObjectStorage
  417. */
  418. private function hydrateObjectIdentities(Statement $stmt, array $oidLookup, array $sids)
  419. {
  420. $parentIdToFill = new \SplObjectStorage();
  421. $acls = $aces = $emptyArray = array();
  422. $oidCache = $oidLookup;
  423. $result = new \SplObjectStorage();
  424. $loadedAces =& $this->loadedAces;
  425. $loadedAcls =& $this->loadedAcls;
  426. $permissionGrantingStrategy = $this->permissionGrantingStrategy;
  427. // we need these to set protected properties on hydrated objects
  428. $aclReflection = new \ReflectionClass('Symfony\Component\Security\Acl\Domain\Acl');
  429. $aclClassAcesProperty = $aclReflection->getProperty('classAces');
  430. $aclClassAcesProperty->setAccessible(true);
  431. $aclClassFieldAcesProperty = $aclReflection->getProperty('classFieldAces');
  432. $aclClassFieldAcesProperty->setAccessible(true);
  433. $aclObjectAcesProperty = $aclReflection->getProperty('objectAces');
  434. $aclObjectAcesProperty->setAccessible(true);
  435. $aclObjectFieldAcesProperty = $aclReflection->getProperty('objectFieldAces');
  436. $aclObjectFieldAcesProperty->setAccessible(true);
  437. $aclParentAclProperty = $aclReflection->getProperty('parentAcl');
  438. $aclParentAclProperty->setAccessible(true);
  439. // fetchAll() consumes more memory than consecutive calls to fetch(),
  440. // but it is faster
  441. foreach ($stmt->fetchAll(\PDO::FETCH_NUM) as $data) {
  442. list($aclId,
  443. $objectIdentifier,
  444. $parentObjectIdentityId,
  445. $entriesInheriting,
  446. $classType,
  447. $aceId,
  448. $objectIdentityId,
  449. $fieldName,
  450. $aceOrder,
  451. $mask,
  452. $granting,
  453. $grantingStrategy,
  454. $auditSuccess,
  455. $auditFailure,
  456. $username,
  457. $securityIdentifier) = $data;
  458. // has the ACL been hydrated during this hydration cycle?
  459. if (isset($acls[$aclId])) {
  460. $acl = $acls[$aclId];
  461. // has the ACL been hydrated during any previous cycle, or was possibly loaded
  462. // from cache?
  463. } elseif (isset($loadedAcls[$classType][$objectIdentifier])) {
  464. $acl = $loadedAcls[$classType][$objectIdentifier];
  465. // keep reference in local array (saves us some hash calculations)
  466. $acls[$aclId] = $acl;
  467. // attach ACL to the result set; even though we do not enforce that every
  468. // object identity has only one instance, we must make sure to maintain
  469. // referential equality with the oids passed to findAcls()
  470. if (!isset($oidCache[$objectIdentifier.$classType])) {
  471. $oidCache[$objectIdentifier.$classType] = $acl->getObjectIdentity();
  472. }
  473. $result->attach($oidCache[$objectIdentifier.$classType], $acl);
  474. // so, this hasn't been hydrated yet
  475. } else {
  476. // create object identity if we haven't done so yet
  477. $oidLookupKey = $objectIdentifier.$classType;
  478. if (!isset($oidCache[$oidLookupKey])) {
  479. $oidCache[$oidLookupKey] = new ObjectIdentity($objectIdentifier, $classType);
  480. }
  481. $acl = new Acl((integer) $aclId, $oidCache[$oidLookupKey], $permissionGrantingStrategy, $emptyArray, !!$entriesInheriting);
  482. // keep a local, and global reference to this ACL
  483. $loadedAcls[$classType][$objectIdentifier] = $acl;
  484. $acls[$aclId] = $acl;
  485. // try to fill in parent ACL, or defer until all ACLs have been hydrated
  486. if (null !== $parentObjectIdentityId) {
  487. if (isset($acls[$parentObjectIdentityId])) {
  488. $aclParentAclProperty->setValue($acl, $acls[$parentObjectIdentityId]);
  489. } else {
  490. $parentIdToFill->attach($acl, $parentObjectIdentityId);
  491. }
  492. }
  493. $result->attach($oidCache[$oidLookupKey], $acl);
  494. }
  495. // check if this row contains an ACE record
  496. if (null !== $aceId) {
  497. // have we already hydrated ACEs for this ACL?
  498. if (!isset($aces[$aclId])) {
  499. $aces[$aclId] = array($emptyArray, $emptyArray, $emptyArray, $emptyArray);
  500. }
  501. // has this ACE already been hydrated during a previous cycle, or
  502. // possible been loaded from cache?
  503. // It is important to only ever have one ACE instance per actual row since
  504. // some ACEs are shared between ACL instances
  505. if (!isset($loadedAces[$aceId])) {
  506. if (!isset($sids[$key = ($username?'1':'0').$securityIdentifier])) {
  507. if ($username) {
  508. $sids[$key] = new UserSecurityIdentity(
  509. substr($securityIdentifier, 1 + $pos = strpos($securityIdentifier, '-')),
  510. substr($securityIdentifier, 0, $pos)
  511. );
  512. } else {
  513. $sids[$key] = new RoleSecurityIdentity($securityIdentifier);
  514. }
  515. }
  516. if (null === $fieldName) {
  517. $loadedAces[$aceId] = new Entry((integer) $aceId, $acl, $sids[$key], $grantingStrategy, (integer) $mask, !!$granting, !!$auditFailure, !!$auditSuccess);
  518. } else {
  519. $loadedAces[$aceId] = new FieldEntry((integer) $aceId, $acl, $fieldName, $sids[$key], $grantingStrategy, (integer) $mask, !!$granting, !!$auditFailure, !!$auditSuccess);
  520. }
  521. }
  522. $ace = $loadedAces[$aceId];
  523. // assign ACE to the correct property
  524. if (null === $objectIdentityId) {
  525. if (null === $fieldName) {
  526. $aces[$aclId][0][$aceOrder] = $ace;
  527. } else {
  528. $aces[$aclId][1][$fieldName][$aceOrder] = $ace;
  529. }
  530. } else {
  531. if (null === $fieldName) {
  532. $aces[$aclId][2][$aceOrder] = $ace;
  533. } else {
  534. $aces[$aclId][3][$fieldName][$aceOrder] = $ace;
  535. }
  536. }
  537. }
  538. }
  539. // We do not sort on database level since we only want certain subsets to be sorted,
  540. // and we are going to read the entire result set anyway.
  541. // Sorting on DB level increases query time by an order of magnitude while it is
  542. // almost negligible when we use PHPs array sort functions.
  543. foreach ($aces as $aclId => $aceData) {
  544. $acl = $acls[$aclId];
  545. ksort($aceData[0]);
  546. $aclClassAcesProperty->setValue($acl, $aceData[0]);
  547. foreach (array_keys($aceData[1]) as $fieldName) {
  548. ksort($aceData[1][$fieldName]);
  549. }
  550. $aclClassFieldAcesProperty->setValue($acl, $aceData[1]);
  551. ksort($aceData[2]);
  552. $aclObjectAcesProperty->setValue($acl, $aceData[2]);
  553. foreach (array_keys($aceData[3]) as $fieldName) {
  554. ksort($aceData[3][$fieldName]);
  555. }
  556. $aclObjectFieldAcesProperty->setValue($acl, $aceData[3]);
  557. }
  558. // fill-in parent ACLs where this hasn't been done yet cause the parent ACL was not
  559. // yet available
  560. $processed = 0;
  561. foreach ($parentIdToFill as $acl) {
  562. $parentId = $parentIdToFill->offsetGet($acl);
  563. // let's see if we have already hydrated this
  564. if (isset($acls[$parentId])) {
  565. $aclParentAclProperty->setValue($acl, $acls[$parentId]);
  566. $processed += 1;
  567. continue;
  568. }
  569. }
  570. // reset reflection changes
  571. $aclClassAcesProperty->setAccessible(false);
  572. $aclClassFieldAcesProperty->setAccessible(false);
  573. $aclObjectAcesProperty->setAccessible(false);
  574. $aclObjectFieldAcesProperty->setAccessible(false);
  575. $aclParentAclProperty->setAccessible(false);
  576. // this should never be true if the database integrity hasn't been compromised
  577. if ($processed < count($parentIdToFill)) {
  578. throw new \RuntimeException('Not all parent ids were populated. This implies an integrity problem.');
  579. }
  580. return $result;
  581. }
  582. }