PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/magento/app/code/core/Mage/Admin/Model/Resource/Acl.php

https://bitbucket.org/jit_bec/shopifine
PHP | 171 lines | 79 code | 19 blank | 73 comment | 11 complexity | 5cdd112bd8000cbb6412c3259abdce1e MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Admin
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Resource model for admin ACL
  28. *
  29. * @category Mage
  30. * @package Mage_Admin
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Admin_Model_Resource_Acl extends Mage_Core_Model_Resource_Db_Abstract
  34. {
  35. const ACL_ALL_RULES = 'all';
  36. /**
  37. * Initialize resource
  38. *
  39. */
  40. protected function _construct()
  41. {
  42. $this->_init('admin/role', 'role_id');
  43. }
  44. /**
  45. * Load ACL for the user
  46. *
  47. * @return Mage_Admin_Model_Acl
  48. */
  49. public function loadAcl()
  50. {
  51. $acl = Mage::getModel('admin/acl');
  52. Mage::getSingleton('admin/config')->loadAclResources($acl);
  53. $roleTable = $this->getTable('admin/role');
  54. $ruleTable = $this->getTable('admin/rule');
  55. $assertTable = $this->getTable('admin/assert');
  56. $adapter = $this->_getReadAdapter();
  57. $select = $adapter->select()
  58. ->from($roleTable)
  59. ->order('tree_level');
  60. $rolesArr = $adapter->fetchAll($select);
  61. $this->loadRoles($acl, $rolesArr);
  62. $select = $adapter->select()
  63. ->from(array('r' => $ruleTable))
  64. ->joinLeft(
  65. array('a' => $assertTable),
  66. 'a.assert_id = r.assert_id',
  67. array('assert_type', 'assert_data')
  68. );
  69. $rulesArr = $adapter->fetchAll($select);
  70. $this->loadRules($acl, $rulesArr);
  71. return $acl;
  72. }
  73. /**
  74. * Load roles
  75. *
  76. * @param Mage_Admin_Model_Acl $acl
  77. * @param array $rolesArr
  78. * @return Mage_Admin_Model_Resource_Acl
  79. */
  80. public function loadRoles(Mage_Admin_Model_Acl $acl, array $rolesArr)
  81. {
  82. foreach ($rolesArr as $role) {
  83. $parent = ($role['parent_id'] > 0) ? Mage_Admin_Model_Acl::ROLE_TYPE_GROUP . $role['parent_id'] : null;
  84. switch ($role['role_type']) {
  85. case Mage_Admin_Model_Acl::ROLE_TYPE_GROUP:
  86. $roleId = $role['role_type'] . $role['role_id'];
  87. $acl->addRole(Mage::getModel('admin/acl_role_group', $roleId), $parent);
  88. break;
  89. case Mage_Admin_Model_Acl::ROLE_TYPE_USER:
  90. $roleId = $role['role_type'] . $role['user_id'];
  91. if (!$acl->hasRole($roleId)) {
  92. $acl->addRole(Mage::getModel('admin/acl_role_user', $roleId), $parent);
  93. } else {
  94. $acl->addRoleParent($roleId, $parent);
  95. }
  96. break;
  97. }
  98. }
  99. return $this;
  100. }
  101. /**
  102. * Load rules
  103. *
  104. * @param Mage_Admin_Model_Acl $acl
  105. * @param array $rulesArr
  106. * @return Mage_Admin_Model_Resource_Acl
  107. */
  108. public function loadRules(Mage_Admin_Model_Acl $acl, array $rulesArr)
  109. {
  110. foreach ($rulesArr as $rule) {
  111. $role = $rule['role_type'] . $rule['role_id'];
  112. $resource = $rule['resource_id'];
  113. $privileges = !empty($rule['privileges']) ? explode(',', $rule['privileges']) : null;
  114. $assert = null;
  115. if (0 != $rule['assert_id']) {
  116. $assertClass = Mage::getSingleton('admin/config')->getAclAssert($rule['assert_type'])->getClassName();
  117. $assert = new $assertClass(unserialize($rule['assert_data']));
  118. }
  119. try {
  120. if ( $rule['permission'] == 'allow' ) {
  121. if ($resource === self::ACL_ALL_RULES) {
  122. $acl->allow($role, null, $privileges, $assert);
  123. }
  124. $acl->allow($role, $resource, $privileges, $assert);
  125. } else if ( $rule['permission'] == 'deny' ) {
  126. $acl->deny($role, $resource, $privileges, $assert);
  127. }
  128. } catch (Exception $e) {
  129. //$m = $e->getMessage();
  130. //if ( eregi("^Resource '(.*)' not found", $m) ) {
  131. // Deleting non existent resource rule from rules table
  132. //$cond = $this->_write->quoteInto('resource_id = ?', $resource);
  133. //$this->_write->delete(Mage::getSingleton('core/resource')->getTableName('admin/rule'), $cond);
  134. //} else {
  135. //TODO: We need to log such exceptions to somewhere like a system/errors.log
  136. //}
  137. }
  138. /*
  139. switch ($rule['permission']) {
  140. case Mage_Admin_Model_Acl::RULE_PERM_ALLOW:
  141. $acl->allow($role, $resource, $privileges, $assert);
  142. break;
  143. case Mage_Admin_Model_Acl::RULE_PERM_DENY:
  144. $acl->deny($role, $resource, $privileges, $assert);
  145. break;
  146. }
  147. */
  148. }
  149. return $this;
  150. }
  151. }