modnamespace.class.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. * Represents a Component in the MODX framework. Isolates controllers, lexicons and other logic into the virtual
  12. * containment space defined by the path of the namespace.
  13. *
  14. * @property string $name The key of the namespace
  15. * @property string $path The absolute path of the namespace. May use {core_path}, {base_path} or {assets_path} as
  16. * placeholders for the path.
  17. *
  18. * @package modx
  19. */
  20. class modNamespace extends modAccessibleObject {
  21. public function save($cacheFlag = null) {
  22. $saved = parent::save();
  23. if ($saved && !$this->getOption(xPDO::OPT_SETUP)) {
  24. $this->xpdo->call('modNamespace','clearCache',array(&$this->xpdo));
  25. }
  26. return $saved;
  27. }
  28. public function remove(array $ancestors = array()) {
  29. $removed = parent::remove($ancestors);
  30. if ($removed && !$this->getOption(xPDO::OPT_SETUP)) {
  31. $this->xpdo->call('modNamespace','clearCache',array(&$this->xpdo));
  32. }
  33. return $removed;
  34. }
  35. public static function loadCache(modX $modx) {
  36. if (!$modx->getCacheManager()) {
  37. return array();
  38. }
  39. $cacheKey= 'namespaces';
  40. $cache = $modx->cacheManager->get($cacheKey, array(
  41. xPDO::OPT_CACHE_KEY => $modx->getOption('cache_namespaces_key', null, 'namespaces'),
  42. xPDO::OPT_CACHE_HANDLER => $modx->getOption('cache_namespaces_handler', null,$modx->getOption(xPDO::OPT_CACHE_HANDLER)),
  43. xPDO::OPT_CACHE_FORMAT => (integer) $modx->getOption('cache_namespaces_format', null, $modx->getOption(xPDO::OPT_CACHE_FORMAT, null, xPDOCacheManager::CACHE_PHP)),
  44. ));
  45. if (empty($cache)) {
  46. $cache = $modx->cacheManager->generateNamespacesCache($cacheKey);
  47. }
  48. return $cache;
  49. }
  50. public static function clearCache(modX $modx) {
  51. $cacheKey= 'namespaces';
  52. $cleared = $modx->cacheManager->delete($cacheKey, array(
  53. xPDO::OPT_CACHE_KEY => $modx->getOption('cache_namespaces_key', null, 'namespaces'),
  54. xPDO::OPT_CACHE_HANDLER => $modx->getOption('cache_namespaces_handler', null,$modx->getOption(xPDO::OPT_CACHE_HANDLER)),
  55. xPDO::OPT_CACHE_FORMAT => (integer) $modx->getOption('cache_namespaces_format', null, $modx->getOption(xPDO::OPT_CACHE_FORMAT, null, xPDOCacheManager::CACHE_PHP)),
  56. ));
  57. return $cleared;
  58. }
  59. public function getCorePath() {
  60. $path = $this->get('path');
  61. return $this->xpdo->call('modNamespace','translatePath',array(&$this->xpdo,$path));
  62. }
  63. public function getAssetsPath() {
  64. $path = $this->get('assets_path');
  65. return $this->xpdo->call('modNamespace','translatePath',array(&$this->xpdo,$path));
  66. }
  67. public static function translatePath(xPDO &$xpdo,$path) {
  68. return str_replace(array(
  69. '{core_path}',
  70. '{base_path}',
  71. '{assets_path}',
  72. ),array(
  73. $xpdo->getOption('core_path',null,MODX_CORE_PATH),
  74. $xpdo->getOption('base_path',null,MODX_BASE_PATH),
  75. $xpdo->getOption('assets_path',null,MODX_ASSETS_PATH),
  76. ),$path);
  77. }
  78. public function checkPolicy($criteria, $targets = null, modUser $user = null)
  79. {
  80. return parent::checkPolicy($criteria, $targets, $user);
  81. }
  82. /**
  83. * Find all policies for this object
  84. *
  85. * @param string $context
  86. * @return array
  87. */
  88. public function findPolicy($context = '') {
  89. $policy = array();
  90. $context = 'mgr';
  91. if (empty($this->_policies) || !isset($this->_policies[$context])) {
  92. $accessTable = $this->xpdo->getTableName('modAccessNamespace');
  93. $namespaceTable = $this->xpdo->getTableName('modNamespace');
  94. $policyTable = $this->xpdo->getTableName('modAccessPolicy');
  95. $sql = "SELECT Acl.target, Acl.principal, Acl.authority, Acl.policy, Policy.data FROM {$accessTable} Acl " .
  96. "LEFT JOIN {$policyTable} Policy ON Policy.id = Acl.policy " .
  97. "JOIN {$namespaceTable} Namespace ON Acl.principal_class = 'modUserGroup' " .
  98. "AND (Acl.context_key = :context OR Acl.context_key IS NULL OR Acl.context_key = '') " .
  99. "AND Namespace.name = Acl.target " .
  100. "WHERE Acl.target = :namespace " .
  101. "GROUP BY Acl.target, Acl.principal, Acl.authority, Acl.policy";
  102. $bindings = array(
  103. ':namespace' => $this->get('name'),
  104. ':context' => $context,
  105. );
  106. $query = new xPDOCriteria($this->xpdo, $sql, $bindings);
  107. if ($query->stmt && $query->stmt->execute()) {
  108. while ($row = $query->stmt->fetch(PDO::FETCH_ASSOC)) {
  109. $policy['modAccessNamespace'][$row['target']][] = array(
  110. 'principal' => $row['principal'],
  111. 'authority' => $row['authority'],
  112. 'policy' => $row['data'] ? $this->xpdo->fromJSON($row['data'], true) : array(),
  113. );
  114. }
  115. }
  116. $this->_policies[$context] = $policy;
  117. } else {
  118. $policy = $this->_policies[$context];
  119. }
  120. return $policy;
  121. }
  122. }