00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Adminhtml_Api_RoleController extends Mage_Adminhtml_Controller_Action
00035 {
00036
00037 protected function _initAction()
00038 {
00039 $this->loadLayout();
00040 $this->_setActiveMenu('api/roles');
00041 $this->_addBreadcrumb($this->__('Web services'), $this->__('Web services'));
00042 $this->_addBreadcrumb($this->__('Permissions'), $this->__('Permissions'));
00043 $this->_addBreadcrumb($this->__('Roles'), $this->__('Roles'));
00044 return $this;
00045 }
00046
00047 public function indexAction()
00048 {
00049 $this->_initAction();
00050
00051 $this->_addContent($this->getLayout()->createBlock('adminhtml/api_roles'));
00052
00053 $this->renderLayout();
00054 }
00055
00056 public function roleGridAction()
00057 {
00058 $this->getResponse()
00059 ->setBody($this->getLayout()
00060 ->createBlock('adminhtml/api_grid_role')
00061 ->toHtml()
00062 );
00063 }
00064
00065 public function editRoleAction()
00066 {
00067 $this->_initAction();
00068
00069 $roleId = $this->getRequest()->getParam('rid');
00070 if( intval($roleId) > 0 ) {
00071 $breadCrumb = $this->__('Edit Role');
00072 $breadCrumbTitle = $this->__('Edit Role');
00073 } else {
00074 $breadCrumb = $this->__('Add new Role');
00075 $breadCrumbTitle = $this->__('Add new Role');
00076 }
00077 $this->_addBreadcrumb($breadCrumb, $breadCrumbTitle);
00078
00079 $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
00080
00081 $this->_addLeft(
00082 $this->getLayout()->createBlock('adminhtml/api_editroles')
00083 );
00084 $resources = Mage::getModel('api/roles')->getResourcesList();
00085 $this->_addContent(
00086 $this->getLayout()->createBlock('adminhtml/api_buttons')
00087 ->setRoleId($roleId)
00088 ->setRoleInfo(Mage::getModel('api/roles')->load($roleId))
00089 ->setTemplate('api/roleinfo.phtml')
00090 );
00091 $this->_addJs($this->getLayout()->createBlock('adminhtml/template')->setTemplate('api/role_users_grid_js.phtml'));
00092 $this->renderLayout();
00093 }
00094
00095 public function deleteAction()
00096 {
00097 $rid = $this->getRequest()->getParam('rid', false);
00098
00099 try {
00100 Mage::getModel("api/roles")->setId($rid)->delete();
00101 Mage::getSingleton('adminhtml/session')->addSuccess($this->__('Role successfully deleted.'));
00102 } catch (Exception $e) {
00103 Mage::getSingleton('adminhtml/session')->addError($this->__('Error while deleting this role. Please try again later.'));
00104 }
00105
00106 $this->_redirect("*/*/");
00107 }
00108
00109 public function saveRoleAction()
00110 {
00111 $rid = $this->getRequest()->getParam('role_id', false);
00112 $resource = explode(',', $this->getRequest()->getParam('resource', false));
00113 $roleUsers = $this->getRequest()->getParam('in_role_user', null);
00114 parse_str($roleUsers, $roleUsers);
00115 $roleUsers = array_keys($roleUsers);
00116
00117 $isAll = $this->getRequest()->getParam('all');
00118 if ($isAll) {
00119 $resource = array("all");
00120 }
00121
00122 try {
00123 $role = Mage::getModel("api/roles")
00124 ->setId($rid)
00125 ->setName($this->getRequest()->getParam('rolename', false))
00126 ->setPid($this->getRequest()->getParam('parent_id', false))
00127 ->setRoleType('G')
00128 ->save();
00129
00130 Mage::getModel("api/rules")
00131 ->setRoleId($role->getId())
00132 ->setResources($resource)
00133 ->saveRel();
00134
00135 $oldRoleUsers = Mage::getModel("api/roles")->setId($role->getId())->getRoleUsers($role);
00136 if ( sizeof($oldRoleUsers) > 0 ) {
00137 foreach($oldRoleUsers as $oUid) {
00138 $this->_deleteUserFromRole($oUid, $role->getId());
00139 }
00140 }
00141 if ( $roleUsers ) {
00142 foreach ($roleUsers as $nRuid) {
00143 $this->_addUserToRole($nRuid, $role->getId());
00144 }
00145 }
00146 $rid = $role->getId();
00147 Mage::getSingleton('adminhtml/session')->addSuccess($this->__('Role successfully saved.'));
00148 } catch (Exception $e) {
00149 Mage::getSingleton('adminhtml/session')->addError($this->__('Error while saving this role. Please try again later.'));
00150 }
00151
00152
00153 $this->_redirect('*/*/editrole', array('rid' => $rid));
00154 return;
00155 }
00156
00157 public function editrolegridAction()
00158 {
00159 $this->getResponse()->setBody($this->getLayout()->createBlock('adminhtml/api_role_grid_user')->toHtml());
00160 }
00161
00162 protected function _deleteUserFromRole($userId, $roleId)
00163 {
00164 try {
00165 Mage::getModel("api/user")
00166 ->setRoleId($roleId)
00167 ->setUserId($userId)
00168 ->deleteFromRole();
00169 } catch (Exception $e) {
00170 throw $e;
00171 return false;
00172 }
00173 return true;
00174 }
00175
00176 protected function _addUserToRole($userId, $roleId)
00177 {
00178 $user = Mage::getModel("api/user")->load($userId);
00179 $user->setRoleId($roleId)->setUserId($userId);
00180
00181 if( $user->roleUserExists() === true ) {
00182 return false;
00183 } else {
00184 $user->add();
00185 return true;
00186 }
00187 }
00188
00189 protected function _isAllowed()
00190 {
00191 return Mage::getSingleton('admin/session')->isAllowed('api/roles');
00192 }
00193 }