modaccessnamespace.class.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. /**
  11. * @package modx
  12. * @subpackage mysql
  13. */
  14. class modAccessNamespace extends modAccess {
  15. /**
  16. * Load the attributes for the ACLs for the context
  17. *
  18. * @static
  19. * @param modX $modx A reference to the modX instance
  20. * @param string $context The context to load from. If empty, will use the current context.
  21. * @param int $userId The ID of the user to grab ACL records for.
  22. * @return array An array of loaded attributes
  23. */
  24. public static function loadAttributes(&$modx, $context = '', $userId = 0) {
  25. $attributes = array();
  26. $accessTable = $modx->getTableName('modAccessNamespace');
  27. $policyTable = $modx->getTableName('modAccessPolicy');
  28. $memberTable = $modx->getTableName('modUserGroupMember');
  29. $memberRoleTable = $modx->getTableName('modUserGroupRole');
  30. if ($userId > 0) {
  31. $sql = "SELECT acl.target, acl.principal, mr.authority, acl.policy, p.data FROM {$accessTable} acl " .
  32. "LEFT JOIN {$policyTable} p ON p.id = acl.policy " .
  33. "JOIN {$memberTable} mug ON acl.principal_class = 'modUserGroup' " .
  34. "AND mug.member = :principal " .
  35. "AND mug.user_group = acl.principal " .
  36. "JOIN {$memberRoleTable} mr ON mr.id = mug.role " .
  37. "AND mr.authority <= acl.authority " .
  38. "ORDER BY acl.target, acl.principal, mr.authority, acl.policy";
  39. $bindings = array(
  40. ':principal' => $userId
  41. );
  42. $query = new xPDOCriteria($modx, $sql, $bindings);
  43. if ($query->stmt && $query->stmt->execute()) {
  44. while ($row = $query->stmt->fetch(PDO::FETCH_ASSOC)) {
  45. $attributes[$row['target']][] = array(
  46. 'principal' => $row['principal'],
  47. 'authority' => $row['authority'],
  48. 'policy' => $row['data'] ? $modx->fromJSON($row['data'], true) : array(),
  49. );
  50. }
  51. }
  52. } else {
  53. $sql = "SELECT acl.target, acl.principal, 0 AS authority, acl.policy, p.data FROM {$accessTable} acl " .
  54. "LEFT JOIN {$policyTable} p ON p.id = acl.policy " .
  55. "WHERE acl.principal_class = 'modUserGroup' " .
  56. "AND acl.principal = 0 " .
  57. "ORDER BY acl.target, acl.principal, acl.authority, acl.policy";
  58. $query = new xPDOCriteria($modx, $sql);
  59. if ($query->stmt && $query->stmt->execute()) {
  60. while ($row = $query->stmt->fetch(PDO::FETCH_ASSOC)) {
  61. $attributes[$row['target']][] = array(
  62. 'principal' => 0,
  63. 'authority' => $row['authority'],
  64. 'policy' => $row['data'] ? $modx->fromJSON($row['data'], true) : array(),
  65. );
  66. }
  67. }
  68. }
  69. return $attributes;
  70. }
  71. }