PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Ecart/Admin/Model/Acl.php

https://code.google.com/p/ecartcommerce/
PHP | 162 lines | 87 code | 18 blank | 57 comment | 14 complexity | 01ac6a582702e500172dfee61b46e3cb MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Ecart
  4. *
  5. * This file is part of Ecart.
  6. *
  7. * Ecart is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Ecart is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Ecart. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @category Ecart
  21. * @package Ecart_Admin
  22. * @copyright Copyright 2008-2009 E-Cart LLC
  23. * @license GNU Public License V3.0
  24. */
  25. /**
  26. *
  27. * @category Ecart
  28. * @package Ecart_Admin
  29. * @subpackage Model
  30. * @author Ecart Core Team <core@ecartcommerce.com>
  31. */
  32. class Ecart_Admin_Model_Acl extends Zend_Acl
  33. {
  34. private $_rescs;
  35. public function __construct()
  36. {
  37. $this->_loadResources();
  38. }
  39. protected function _loadResources()
  40. {
  41. foreach ($this->_getResources() as $resource) {
  42. if (false !== ($pos = strrpos($resource['resource_id'], '/'))) {
  43. $parentId = substr($resource['resource_id'], 0, $pos);
  44. } else {
  45. $parentId = null;
  46. }
  47. $this->add(new Zend_Acl_Resource($resource['resource_id']), $parentId);
  48. }
  49. }
  50. /**
  51. * Load rules of $role and all parent roles
  52. *
  53. * @param Zend_Acl_Role_Interface|string $role
  54. * @return boolean
  55. */
  56. public function loadRules($role)
  57. {
  58. if ($role instanceof Zend_Acl_Role_Interface) {
  59. $roleId = $role->getRoleId();
  60. } else {
  61. $roleId = (string) $role;
  62. }
  63. $this->addRoleRecursive($role);
  64. $rolesForLoad = Ecart::single('admin/acl_role')->getAllParents($roleId);
  65. $rolesForLoad[] = $roleId;
  66. $stmt = Ecart::single('admin/acl_rule')
  67. ->select('*')
  68. ->where('role_id IN(?)', $rolesForLoad)
  69. ->query()
  70. ;
  71. while ($row = $stmt->fetch()) {
  72. if ($row['permission'] == 'allow') {
  73. $this->allow($row['role_id'], $row['resource_id']);
  74. } elseif ($row['permission'] == 'deny') {
  75. $this->deny($row['role_id'], $row['resource_id']);
  76. }
  77. }
  78. }
  79. /**
  80. * Add role with all parent roles
  81. *
  82. * @param Zend_Acl_Role_Interface|string $role
  83. */
  84. public function addRoleRecursive($role)
  85. {
  86. if ($role instanceof Zend_Acl_Role_Interface) {
  87. $roleId = $role->getRoleId();
  88. } else {
  89. $roleId = (string) $role;
  90. }
  91. $rolesTree = Ecart::single('admin/acl_role')->getRolesTree();
  92. if (isset($rolesTree[$roleId]['parents'])) {
  93. foreach ($rolesTree[$roleId]['parents'] as $parentRoleId) {
  94. $this->addRoleRecursive($parentRoleId);
  95. }
  96. }
  97. if (!$this->hasRole($roleId))
  98. $this->addRole(
  99. new Zend_Acl_Role($roleId),
  100. isset($rolesTree[$roleId]['parents']) ?
  101. $rolesTree[$roleId]['parents'] : null
  102. );
  103. }
  104. /**
  105. *
  106. * @return array
  107. */
  108. protected function _getResources()
  109. {
  110. if (null === $this->_rescs) {
  111. $this->_rescs = Ecart::single('admin/acl_resource')->select()
  112. ->order('resource_id ASC')
  113. ->fetchAll();
  114. }
  115. return $this->_rescs;
  116. }
  117. /**
  118. * Return allows array for gived roles as it will be parent roles
  119. *
  120. * @param $roles array
  121. * @return array
  122. */
  123. public function getParentRolesAllows(array $roles)
  124. {
  125. /*
  126. * Load rules
  127. */
  128. foreach ($roles as $role) {
  129. $this->loadRules($role);
  130. }
  131. /*
  132. * Create tmp role that inherit from $roles
  133. */
  134. $this->addRole(new Zend_Acl_Role('tmp'), $roles);
  135. $allows = array();
  136. foreach ($this->_getResources() as $resource) {
  137. if ($this->isAllowed('tmp', $resource['resource_id'])) {
  138. $allows[] = $resource['resource_id'];
  139. }
  140. }
  141. $this->removeRole('tmp');
  142. return $allows;
  143. }
  144. }