PageRenderTime 67ms CodeModel.GetById 22ms RepoModel.GetById 8ms app.codeStats 0ms

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

https://bitbucket.org/jit_bec/shopifine
PHP | 157 lines | 73 code | 11 blank | 73 comment | 9 complexity | 2634bb6b3b2bc2e7ea19fd0b696e7517 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_Api
  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_Api
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Api_Model_Resource_Acl extends Mage_Core_Model_Resource_Db_Abstract
  34. {
  35. /**
  36. * Initialize resource connections
  37. *
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init('api/role', 'role_id');
  42. }
  43. /**
  44. * Load ACL for the user
  45. *
  46. * @return Mage_Api_Model_Acl
  47. */
  48. public function loadAcl()
  49. {
  50. $acl = Mage::getModel('api/acl');
  51. $adapter = $this->_getReadAdapter();
  52. Mage::getSingleton('api/config')->loadAclResources($acl);
  53. $rolesArr = $adapter->fetchAll(
  54. $adapter->select()
  55. ->from($this->getTable('api/role'))
  56. ->order(array('tree_level', 'role_type'))
  57. );
  58. $this->loadRoles($acl, $rolesArr);
  59. $rulesArr = $adapter->fetchAll(
  60. $adapter->select()
  61. ->from(array('r'=>$this->getTable('api/rule')))
  62. ->joinLeft(
  63. array('a'=>$this->getTable('api/assert')),
  64. 'a.assert_id=r.assert_id',
  65. array('assert_type', 'assert_data')
  66. ));
  67. $this->loadRules($acl, $rulesArr);
  68. return $acl;
  69. }
  70. /**
  71. * Load roles
  72. *
  73. * @param Mage_Api_Model_Acl $acl
  74. * @param array $rolesArr
  75. * @return Mage_Api_Model_Resource_Acl
  76. */
  77. public function loadRoles(Mage_Api_Model_Acl $acl, array $rolesArr)
  78. {
  79. foreach ($rolesArr as $role) {
  80. $parent = $role['parent_id']>0 ? Mage_Api_Model_Acl::ROLE_TYPE_GROUP.$role['parent_id'] : null;
  81. switch ($role['role_type']) {
  82. case Mage_Api_Model_Acl::ROLE_TYPE_GROUP:
  83. $roleId = $role['role_type'].$role['role_id'];
  84. $acl->addRole(Mage::getModel('api/acl_role_group', $roleId), $parent);
  85. break;
  86. case Mage_Api_Model_Acl::ROLE_TYPE_USER:
  87. $roleId = $role['role_type'].$role['user_id'];
  88. if (!$acl->hasRole($roleId)) {
  89. $acl->addRole(Mage::getModel('api/acl_role_user', $roleId), $parent);
  90. } else {
  91. $acl->addRoleParent($roleId, $parent);
  92. }
  93. break;
  94. }
  95. }
  96. return $this;
  97. }
  98. /**
  99. * Load rules
  100. *
  101. * @param Mage_Api_Model_Acl $acl
  102. * @param array $rulesArr
  103. * @return Mage_Api_Model_Resource_Acl
  104. */
  105. public function loadRules(Mage_Api_Model_Acl $acl, array $rulesArr)
  106. {
  107. foreach ($rulesArr as $rule) {
  108. $role = $rule['role_type'].$rule['role_id'];
  109. $resource = $rule['resource_id'];
  110. $privileges = !empty($rule['api_privileges']) ? explode(',', $rule['api_privileges']) : null;
  111. $assert = null;
  112. if (0!=$rule['assert_id']) {
  113. $assertClass = Mage::getSingleton('api/config')->getAclAssert($rule['assert_type'])->getClassName();
  114. $assert = new $assertClass(unserialize($rule['assert_data']));
  115. }
  116. try {
  117. if ($rule['api_permission'] == 'allow') {
  118. $acl->allow($role, $resource, $privileges, $assert);
  119. } else if ($rule['api_permission'] == 'deny') {
  120. $acl->deny($role, $resource, $privileges, $assert);
  121. }
  122. } catch (Exception $e) {
  123. //$m = $e->getMessage();
  124. //if ( eregi("^Resource '(.*)' not found", $m) ) {
  125. // Deleting non existent resource rule from rules table
  126. //$cond = $this->_write->quoteInto('resource_id = ?', $resource);
  127. //$this->_write->delete(Mage::getSingleton('core/resource')->getTableName('admin/rule'), $cond);
  128. //} else {
  129. //TODO: We need to log such exceptions to somewhere like a system/errors.log
  130. //}
  131. }
  132. /*
  133. switch ($rule['api_permission']) {
  134. case Mage_Api_Model_Acl::RULE_PERM_ALLOW:
  135. $acl->allow($role, $resource, $privileges, $assert);
  136. break;
  137. case Mage_Api_Model_Acl::RULE_PERM_DENY:
  138. $acl->deny($role, $resource, $privileges, $assert);
  139. break;
  140. }
  141. */
  142. }
  143. return $this;
  144. }
  145. }