modaccesscontext.class.php 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * An ACL for restricting or allowing access to Contexts
  12. *
  13. * @package modx
  14. */
  15. class modAccessContext extends modAccess {
  16. /**
  17. * Load the attributes for the ACLs for the context
  18. *
  19. * @static
  20. * @param modX $modx A reference to the modX instance
  21. * @param string $context The context to load from. If empty, will use the current context.
  22. * @param int $userId The ID of the user to grab ACL records for.
  23. * @return array An array of loaded attributes
  24. */
  25. public static function loadAttributes(&$modx, $context = '', $userId = 0) {
  26. $attributes = array();
  27. if (empty($context)) {
  28. $context = $modx->context->get('key');
  29. }
  30. $enabled = (boolean) $modx->getOption('access_context_enabled', null, true);
  31. if ($context !== $modx->context->get('key') && $modx->getContext($context)) {
  32. $enabled = (boolean) $modx->contexts[$context]->getOption('access_context_enabled', $enabled);
  33. }
  34. if ($enabled) {
  35. $accessTable = $modx->getTableName('modAccessContext');
  36. $policyTable = $modx->getTableName('modAccessPolicy');
  37. $memberTable = $modx->getTableName('modUserGroupMember');
  38. $memberRoleTable = $modx->getTableName('modUserGroupRole');
  39. if ($userId > 0) {
  40. $sql = "SELECT acl.target, acl.principal, mr.authority, acl.policy, p.data FROM {$accessTable} acl " .
  41. "LEFT JOIN {$policyTable} p ON p.id = acl.policy " .
  42. "JOIN {$memberTable} mug ON acl.principal_class = 'modUserGroup' " .
  43. "AND mug.member = :principal " .
  44. "AND mug.user_group = acl.principal " .
  45. "JOIN {$memberRoleTable} mr ON mr.id = mug.role " .
  46. "AND mr.authority <= acl.authority " .
  47. "ORDER BY acl.target, acl.principal, mr.authority, acl.policy";
  48. $bindings = array(
  49. ':principal' => $userId
  50. );
  51. $query = new xPDOCriteria($modx, $sql, $bindings);
  52. if ($query->stmt && $query->stmt->execute()) {
  53. while ($row = $query->stmt->fetch(PDO::FETCH_ASSOC)) {
  54. $attributes[$row['target']][] = array(
  55. 'principal' => $row['principal'],
  56. 'authority' => $row['authority'],
  57. 'policy' => $row['data'] ? $modx->fromJSON($row['data'], true) : array(),
  58. );
  59. }
  60. }
  61. } else {
  62. $sql = "SELECT acl.target, acl.principal, 0 AS authority, acl.policy, p.data FROM {$accessTable} acl " .
  63. "LEFT JOIN {$policyTable} p ON p.id = acl.policy " .
  64. "WHERE acl.principal_class = 'modUserGroup' " .
  65. "AND acl.principal = 0 " .
  66. "ORDER BY acl.target, acl.principal, acl.authority, acl.policy";
  67. $query = new xPDOCriteria($modx, $sql);
  68. if ($query->stmt && $query->stmt->execute()) {
  69. while ($row = $query->stmt->fetch(PDO::FETCH_ASSOC)) {
  70. $attributes[$row['target']][] = array(
  71. 'principal' => 0,
  72. 'authority' => $row['authority'],
  73. 'policy' => $row['data'] ? $modx->fromJSON($row['data'], true) : array(),
  74. );
  75. }
  76. }
  77. }
  78. }
  79. return $attributes;
  80. }
  81. }