PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Api2/controllers/Adminhtml/Api2/RoleController.php

https://bitbucket.org/kdms/sh-magento
PHP | 340 lines | 185 code | 50 blank | 105 comment | 9 complexity | 7edb7e95f635664d0acca02fe31b32cc MD5 | raw file
  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  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_Api2
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * API2 roles controller
  28. *
  29. * @category Mage
  30. * @package Mage_Api2
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Api2_Adminhtml_Api2_RoleController extends Mage_Adminhtml_Controller_Action
  34. {
  35. /**
  36. * Show grid
  37. */
  38. public function indexAction()
  39. {
  40. $this->_title($this->__('System'))
  41. ->_title($this->__('Web Services'))
  42. ->_title($this->__('REST Roles'));
  43. $this->loadLayout()->_setActiveMenu('system/services/roles');
  44. $this->_addBreadcrumb($this->__('Web services'), $this->__('Web services'));
  45. $this->_addBreadcrumb($this->__('REST Roles'), $this->__('REST Roles'));
  46. $this->_addBreadcrumb($this->__('Roles'), $this->__('Roles'));
  47. $this->renderLayout();
  48. }
  49. /**
  50. * Updating grid by ajax
  51. */
  52. public function gridAction()
  53. {
  54. $this->loadLayout();
  55. $this->renderLayout();
  56. }
  57. /**
  58. * Updating users grid by ajax
  59. */
  60. public function usersGridAction()
  61. {
  62. $id = $this->getRequest()->getParam('id', false);
  63. $this->loadLayout();
  64. /** @var $grid Mage_Api2_Block_Adminhtml_Roles_Tab_Users */
  65. $grid = $this->getLayout()->getBlock('adminhtml.role.edit.tab.users');
  66. $grid->setUsers($this->_getUsers($id));
  67. $this->renderLayout();
  68. }
  69. /**
  70. * Create new role
  71. */
  72. public function newAction()
  73. {
  74. $this->_title($this->__('System'))
  75. ->_title($this->__('Web Services'))
  76. ->_title($this->__('Rest Roles'));
  77. $this->loadLayout()->_setActiveMenu('system/services/roles');
  78. $this->_addBreadcrumb($this->__('Web services'), $this->__('Web services'));
  79. $this->_addBreadcrumb($this->__('REST Roles'), $this->__('REST Roles'));
  80. $this->_addBreadcrumb($this->__('Roles'), $this->__('Roles'));
  81. $breadCrumb = $this->__('Add New Role');
  82. $breadCrumbTitle = $this->__('Add New Role');
  83. $this->_title($this->__('New Role'));
  84. $this->_addBreadcrumb($breadCrumb, $breadCrumbTitle);
  85. $this->renderLayout();
  86. }
  87. /**
  88. * Edit role
  89. */
  90. public function editAction()
  91. {
  92. $id = (int) $this->getRequest()->getParam('id');
  93. /** @var $role Mage_Api2_Model_Acl_Global_Role */
  94. $role = Mage::getModel('api2/acl_global_role')->load($id);
  95. if (!$role->getId()) {
  96. $this->_getSession()->addError($this->__('Role "%s" not found.', $id));
  97. $this->_redirect('*/*/');
  98. return;
  99. }
  100. $this->loadLayout()->_setActiveMenu('system/services/roles');
  101. $this->_title($this->__('System'))
  102. ->_title($this->__('Web Services'))
  103. ->_title($this->__('Rest Roles'));
  104. $breadCrumb = $this->__('Edit Role');
  105. $breadCrumbTitle = $this->__('Edit Role');
  106. $this->_title($this->__('Edit Role'));
  107. $this->_addBreadcrumb($breadCrumb, $breadCrumbTitle);
  108. /** @var $tabs Mage_Api2_Block_Adminhtml_Roles_Tabs */
  109. $tabs = $this->getLayout()->getBlock('adminhtml.role.edit.tabs');
  110. $tabs->setRole($role);
  111. /** @var $child Mage_Adminhtml_Block_Template */
  112. foreach ($tabs->getChild() as $child) {
  113. $child->setData('role', $role);
  114. }
  115. /** @var $buttons Mage_Api2_Block_Adminhtml_Roles_Buttons */
  116. $buttons = $this->getLayout()->getBlock('adminhtml.roles.buttons');
  117. $buttons->setRole($role);
  118. /** @var $users Mage_Api2_Block_Adminhtml_Roles_Tab_Users */
  119. $users = $this->getLayout()->getBlock('adminhtml.role.edit.tab.users');
  120. $users->setUsers($this->_getUsers($id));
  121. //$this->getLayout()->getBlock('adminhtml.role.edit.tab.resources')->getResTreeJson();
  122. //exit;
  123. $this->renderLayout();
  124. }
  125. /**
  126. * Save role
  127. */
  128. public function saveAction()
  129. {
  130. $request = $this->getRequest();
  131. $id = $request->getParam('id', false);
  132. /** @var $role Mage_Api2_Model_Acl_Global_Role */
  133. $role = Mage::getModel('api2/acl_global_role')->load($id);
  134. if (!$role->getId() && $id) {
  135. $this->_getSession()->addError(
  136. $this->__('Role "%s" no longer exists', $role->getData('role_name')));
  137. $this->_redirect('*/*/');
  138. return;
  139. }
  140. $roleUsers = $request->getParam('in_role_users', null);
  141. parse_str($roleUsers, $roleUsers);
  142. $roleUsers = array_keys($roleUsers);
  143. $oldRoleUsers = $this->getRequest()->getParam('in_role_users_old');
  144. parse_str($oldRoleUsers, $oldRoleUsers);
  145. $oldRoleUsers = array_keys($oldRoleUsers);
  146. /** @var $session Mage_Adminhtml_Model_Session */
  147. $session = $this->_getSession();
  148. try {
  149. $role->setRoleName($this->getRequest()->getParam('role_name', false))
  150. ->save();
  151. foreach($oldRoleUsers as $oUid) {
  152. $this->_deleteUserFromRole($oUid, $role->getId());
  153. }
  154. foreach ($roleUsers as $nRuid) {
  155. $this->_addUserToRole($nRuid, $role->getId());
  156. }
  157. /**
  158. * Save rules with resources
  159. */
  160. /** @var $rule Mage_Api2_Model_Acl_Global_Rule */
  161. $rule = Mage::getModel('api2/acl_global_rule');
  162. if ($id) {
  163. $collection = $rule->getCollection();
  164. $collection->addFilterByRoleId($role->getId());
  165. /** @var $model Mage_Api2_Model_Acl_Global_Rule */
  166. foreach ($collection as $model) {
  167. $model->delete();
  168. }
  169. }
  170. /** @var $ruleTree Mage_Api2_Model_Acl_Global_Rule_Tree */
  171. $ruleTree = Mage::getSingleton(
  172. 'api2/acl_global_rule_tree',
  173. array('type' => Mage_Api2_Model_Acl_Global_Rule_Tree::TYPE_PRIVILEGE)
  174. );
  175. $resources = $ruleTree->getPostResources();
  176. $id = $role->getId();
  177. foreach ($resources as $resourceId => $privileges) {
  178. foreach ($privileges as $privilege => $allow) {
  179. if (!$allow) {
  180. continue;
  181. }
  182. $rule->setId(null)
  183. ->isObjectNew(true);
  184. $rule->setRoleId($id)
  185. ->setResourceId($resourceId)
  186. ->setPrivilege($privilege)
  187. ->save();
  188. }
  189. }
  190. $session->addSuccess($this->__('The role has been saved.'));
  191. } catch (Mage_Core_Exception $e) {
  192. $session->addError($e->getMessage());
  193. } catch (Exception $e) {
  194. $session->addException($e, $this->__('An error occurred while saving role.'));
  195. }
  196. $this->_redirect('*/*/edit', array('id'=>$id));
  197. }
  198. /**
  199. * Delete role
  200. */
  201. public function deleteAction()
  202. {
  203. $id = $this->getRequest()->getParam('id', false);
  204. try {
  205. /** @var $model Mage_Api2_Model_Acl_Global_Role */
  206. $model = Mage::getModel("api2/acl_global_role");
  207. $model->load($id)->delete();
  208. $this->_getSession()->addSuccess($this->__('Role has been deleted.'));
  209. } catch (Mage_Core_Exception $e) {
  210. $this->_getSession()->addError($e->getMessage());
  211. } catch (Exception $e) {
  212. $this->_getSession()->addException($e, $this->__('An error occurred while deleting the role.'));
  213. }
  214. $this->_redirect("*/*/");
  215. }
  216. /**
  217. * Check against ACL
  218. *
  219. * @return bool
  220. */
  221. protected function _isAllowed()
  222. {
  223. /** @var $session Mage_Admin_Model_Session */
  224. $session = Mage::getSingleton('admin/session');
  225. return $session->isAllowed('system/api/roles_rest');
  226. }
  227. /**
  228. * Get API2 roles ajax grid action
  229. */
  230. public function rolesGridAction()
  231. {
  232. /** @var $model Mage_Admin_Model_User */
  233. $model = Mage::getModel('admin/user');
  234. $model->load($this->getRequest()->getParam('user_id'));
  235. Mage::register('permissions_user', $model);
  236. $this->getResponse()
  237. ->setBody($this->getLayout()->createBlock('api2/adminhtml_permissions_user_edit_tab_roles')->toHtml());
  238. }
  239. /**
  240. * Get users possessing the role
  241. *
  242. * @param int $id
  243. * @return array|mixed
  244. */
  245. protected function _getUsers($id)
  246. {
  247. if ( $this->getRequest()->getParam('in_role_users') != "" ) {
  248. return $this->getRequest()->getParam('in_role_users');
  249. }
  250. /** @var $role Mage_Api2_Model_Acl_Global_Role */
  251. $role = Mage::getModel('api2/acl_global_role');
  252. $role->setId($id);
  253. /** @var $resource Mage_Api2_Model_Resource_Acl_Global_Role */
  254. $resource = $role->getResource();
  255. $users = $resource->getRoleUsers($role);
  256. if (sizeof($users) == 0) {
  257. $users = array();
  258. }
  259. return $users;
  260. }
  261. /**
  262. * Take away user role
  263. *
  264. * @param int $adminId
  265. * @param int $roleId
  266. * @return Mage_Api2_Adminhtml_Api2_RoleController
  267. */
  268. protected function _deleteUserFromRole($adminId, $roleId)
  269. {
  270. /** @var $resourceModel Mage_Api2_Model_Resource_Acl_Global_Role */
  271. $resourceModel = Mage::getResourceModel('api2/acl_global_role');
  272. $resourceModel->deleteAdminToRoleRelation($adminId, $roleId);
  273. return $this;
  274. }
  275. /**
  276. * Give user a role
  277. *
  278. * @param int $adminId
  279. * @param int $roleId
  280. * @return Mage_Api2_Adminhtml_Api2_RoleController
  281. */
  282. protected function _addUserToRole($adminId, $roleId)
  283. {
  284. /** @var $resourceModel Mage_Api2_Model_Resource_Acl_Global_Role */
  285. $resourceModel = Mage::getResourceModel('api2/acl_global_role');
  286. $resourceModel->saveAdminToRoleRelation($adminId, $roleId);
  287. return $this;
  288. }
  289. }