modaccessresourcegroup.class.php 4.7 KB

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