modaccessmediasource.class.php 3.9 KB

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