PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/application/Espo/Core/AclManager.php

https://gitlab.com/johanlindberg/irvato-crm
PHP | 239 lines | 174 code | 38 blank | 27 comment | 24 complexity | 258f5595754f5f12c99fdba4e16edb2d MD5 | raw file
  1. <?php
  2. /************************************************************************
  3. * This file is part of EspoCRM.
  4. *
  5. * EspoCRM - Open Source CRM application.
  6. * Copyright (C) 2014-2015 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
  7. * Website: http://www.espocrm.com
  8. *
  9. * EspoCRM is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * EspoCRM is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with EspoCRM. If not, see http://www.gnu.org/licenses/.
  21. *
  22. * The interactive user interfaces in modified source and object code versions
  23. * of this program must display Appropriate Legal Notices, as required under
  24. * Section 5 of the GNU General Public License version 3.
  25. *
  26. * In accordance with Section 7(b) of the GNU General Public License version 3,
  27. * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
  28. ************************************************************************/
  29. namespace Espo\Core;
  30. use \Espo\Core\Exceptions\Error;
  31. use \Espo\ORM\Entity;
  32. use \Espo\Entities\User;
  33. use \Espo\Core\Utils\Util;
  34. class AclManager
  35. {
  36. private $container;
  37. private $metadata;
  38. private $implementationHashMap = array();
  39. private $tableHashMap = array();
  40. protected $tableClassName = '\\Espo\\Core\\Acl\\Table';
  41. public function __construct(Container $container)
  42. {
  43. $this->container = $container;
  44. $this->metadata = $container->get('metadata');
  45. }
  46. protected function getContainer()
  47. {
  48. return $this->container;
  49. }
  50. protected function getMetadata()
  51. {
  52. return $this->metadata;
  53. }
  54. public function getImplementation($scope)
  55. {
  56. if (empty($this->implementationHashMap[$scope])) {
  57. $normalizedName = Util::normilizeClassName($scope);
  58. $className = '\\Espo\\Custom\\Acl\\' . $normalizedName;
  59. if (!class_exists($className)) {
  60. $moduleName = $this->metadata->getScopeModuleName($scope);
  61. if ($moduleName) {
  62. $className = '\\Espo\\Modules\\' . $moduleName . '\\Acl\\' . $normalizedName;
  63. } else {
  64. $className = '\\Espo\\Acl\\' . $normalizedName;
  65. }
  66. if (!class_exists($className)) {
  67. $className = '\\Espo\\Core\\Acl\\Base';
  68. }
  69. }
  70. if (class_exists($className)) {
  71. $acl = new $className($scope);
  72. $dependencies = $acl->getDependencyList();
  73. foreach ($dependencies as $name) {
  74. $acl->inject($name, $this->getContainer()->get($name));
  75. }
  76. $this->implementationHashMap[$scope] = $acl;
  77. } else {
  78. throw new Error();
  79. }
  80. }
  81. return $this->implementationHashMap[$scope];
  82. }
  83. protected function getTable(User $user)
  84. {
  85. $key = $user->id;
  86. if (empty($key)) {
  87. $key = spl_object_hash($user);
  88. }
  89. if (empty($this->tableHashMap[$key])) {
  90. $config = $this->getContainer()->get('config');
  91. $fileManager = $this->getContainer()->get('fileManager');
  92. $metadata = $this->getContainer()->get('metadata');
  93. $fieldManager = $this->getContainer()->get('fieldManager');
  94. $this->tableHashMap[$key] = new $this->tableClassName($user, $config, $fileManager, $metadata, $fieldManager);
  95. }
  96. return $this->tableHashMap[$key];
  97. }
  98. public function getMap(User $user)
  99. {
  100. return $this->getTable($user)->getMap();
  101. }
  102. public function getLevel(User $user, $scope, $action)
  103. {
  104. if ($user->isAdmin()) {
  105. return 'all';
  106. }
  107. return $this->getTable($user)->getLevel($scope, $action);
  108. }
  109. public function get(User $user, $permission)
  110. {
  111. return $this->getTable($user)->get($permission);
  112. }
  113. public function checkReadOnlyTeam(User $user, $scope)
  114. {
  115. if ($user->isAdmin()) {
  116. return false;
  117. }
  118. $data = $this->getTable($user)->getScopeData($scope);
  119. return $this->getImplementation($scope)->checkReadOnlyTeam($user, $data);
  120. }
  121. public function checkReadOnlyOwn(User $user, $scope)
  122. {
  123. if ($user->isAdmin()) {
  124. return false;
  125. }
  126. $data = $this->getTable($user)->getScopeData($scope);
  127. return $this->getImplementation($scope)->checkReadOnlyOwn($user, $data);
  128. }
  129. public function check(User $user, $subject, $action = null)
  130. {
  131. if (is_string($subject)) {
  132. return $this->checkScope($user, $subject, $action);
  133. } else {
  134. $entity = $subject;
  135. if ($entity instanceof Entity) {
  136. return $this->checkEntity($user, $entity, $action);
  137. }
  138. }
  139. }
  140. public function checkEntity(User $user, Entity $entity, $action = 'read')
  141. {
  142. $scope = $entity->getEntityType();
  143. $data = $this->getTable($user)->getScopeData($scope);
  144. $impl = $this->getImplementation($scope);
  145. $methodName = 'checkEntity' . ucfirst($action);
  146. if (method_exists($impl, $methodName)) {
  147. return $impl->$methodName($user, $entity, $data);
  148. }
  149. return $impl->checkEntity($user, $entity, $data, $action);
  150. }
  151. public function checkIsOwner(User $user, Entity $entity)
  152. {
  153. return $this->getImplementation($entity->getEntityType())->checkIsOwner($user, $entity);
  154. }
  155. public function checkInTeam(User $user, Entity $entity)
  156. {
  157. return $this->getImplementation($entity->getEntityType())->checkInTeam($user, $entity);
  158. }
  159. public function checkScope(User $user, $scope, $action = null)
  160. {
  161. $data = $this->getTable($user)->getScopeData($scope);
  162. return $this->getImplementation($scope)->checkScope($user, $data, $action);
  163. }
  164. public function checkUser(User $user, $permission, User $entity)
  165. {
  166. if ($user->isAdmin()) {
  167. return true;
  168. }
  169. if ($this->get($user, $permission) === 'no') {
  170. if ($entity->id !== $user->id) {
  171. return false;
  172. }
  173. } else if ($this->get($user, $permission) === 'team') {
  174. if ($entity->id != $user->id) {
  175. $teamIdList1 = $user->getTeamIdList();
  176. $teamIdList2 = $entity->getTeamIdList();
  177. $inTeam = false;
  178. foreach ($teamIdList1 as $id) {
  179. if (in_array($id, $teamIdList2)) {
  180. $inTeam = true;
  181. break;
  182. }
  183. }
  184. if (!$inTeam) {
  185. return false;
  186. }
  187. }
  188. }
  189. return true;
  190. }
  191. public function getScopeForbiddenAttributeList(User $user, $scope, $action = 'read', $thresholdLevel = 'no')
  192. {
  193. if ($user->isAdmin()) return [];
  194. return $this->getTable($user)->getScopeForbiddenAttributeList($scope, $action, $thresholdLevel);
  195. }
  196. public function getScopeForbiddenFieldList(User $user, $scope, $action = 'read', $thresholdLevel = 'no')
  197. {
  198. if ($user->isAdmin()) return [];
  199. return $this->getTable($user)->getScopeForbiddenFieldList($scope, $action, $thresholdLevel);
  200. }
  201. }