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_Permissions_RoleController extends Mage_Adminhtml_Controller_Action
00035 {
00036
00037 protected function _initAction()
00038 {
00039 $this->loadLayout();
00040 $this->_setActiveMenu('system/acl');
00041 $this->_addBreadcrumb($this->__('System'), $this->__('System'));
00042 $this->_addBreadcrumb($this->__('Permissions'), $this->__('Permissions'));
00043 $this->_addBreadcrumb($this->__('Roles'), $this->__('Roles'));
00044 return $this;
00045 }
00046
00047 protected function _initRole($requestVariable = 'rid')
00048 {
00049 Mage::register('current_role', Mage::getModel('admin/roles')->load($this->getRequest()->getParam($requestVariable)));
00050 return Mage::registry('current_role');
00051 }
00052
00053 public function indexAction()
00054 {
00055 $this->_initAction();
00056
00057 $this->renderLayout();
00058 }
00059
00060 public function roleGridAction()
00061 {
00062 $this->loadLayout();
00063 $this->getResponse()->setBody($this->getLayout()->getBlock('adminhtml.permission.role.grid')->toHtml());
00064 }
00065
00066 public function editRoleAction()
00067 {
00068 $role = $this->_initRole();
00069 $this->_initAction();
00070
00071 if ($role->getId()) {
00072 $breadCrumb = $this->__('Edit Role');
00073 $breadCrumbTitle = $this->__('Edit Role');
00074 } else {
00075 $breadCrumb = $this->__('Add new Role');
00076 $breadCrumbTitle = $this->__('Add new Role');
00077 }
00078 $this->_addBreadcrumb($breadCrumb, $breadCrumbTitle);
00079
00080 $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
00081
00082 $this->_addContent(
00083 $this->getLayout()->createBlock('adminhtml/permissions_buttons')
00084 ->setRoleId($role->getId())
00085 ->setRoleInfo($role)
00086 ->setTemplate('permissions/roleinfo.phtml')
00087 );
00088 $this->_addJs($this->getLayout()->createBlock('adminhtml/template')->setTemplate('permissions/role_users_grid_js.phtml'));
00089 $this->renderLayout();
00090 }
00091
00092 public function deleteAction()
00093 {
00094 $rid = $this->getRequest()->getParam('rid', false);
00095 $currentUser = Mage::getModel('admin/user')->setId(Mage::getSingleton('admin/session')->getUser()->getId());
00096 if ( in_array($rid, $currentUser->getRoles()) ) {
00097 Mage::getSingleton('adminhtml/session')->addError($this->__('You can not delete self assigned roles.'));
00098 $this->_redirect('*/*/editrole', array('rid' => $rid));
00099 return;
00100 }
00101
00102 try {
00103 Mage::getModel("admin/roles")->setId($rid)->delete();
00104 Mage::getSingleton('adminhtml/session')->addSuccess($this->__('Role successfully deleted.'));
00105 } catch (Exception $e) {
00106 Mage::getSingleton('adminhtml/session')->addError($this->__('Error while deleting this role. Please try again later.'));
00107 }
00108
00109 $this->_redirect("*/*/");
00110 }
00111
00112 public function saveRoleAction()
00113 {
00114 $rid = $this->getRequest()->getParam('role_id', false);
00115 $resource = explode(',', $this->getRequest()->getParam('resource', false));
00116 $roleUsers = $this->getRequest()->getParam('in_role_user', null);
00117 parse_str($roleUsers, $roleUsers);
00118 $roleUsers = array_keys($roleUsers);
00119
00120 $isAll = $this->getRequest()->getParam('all');
00121 if ($isAll)
00122 $resource = array("all");
00123
00124 try {
00125 $role = Mage::getModel("admin/roles")
00126 ->setId($rid)
00127 ->setName($this->getRequest()->getParam('rolename', false))
00128 ->setPid($this->getRequest()->getParam('parent_id', false))
00129 ->setRoleType('G');
00130 Mage::dispatchEvent('admin_permissions_role_prepare_save', array('object' => $role, 'request' => $this->getRequest()));
00131 $role->save();
00132
00133 Mage::getModel("admin/rules")
00134 ->setRoleId($role->getId())
00135 ->setResources($resource)
00136 ->saveRel();
00137
00138 $oldRoleUsers = Mage::getModel("admin/roles")->setId($role->getId())->getRoleUsers($role);
00139 if ( sizeof($oldRoleUsers) > 0 ) {
00140 foreach($oldRoleUsers as $oUid) {
00141 $this->_deleteUserFromRole($oUid, $role->getId());
00142 }
00143 }
00144 if ( $roleUsers ) {
00145 foreach ($roleUsers as $nRuid) {
00146 $this->_addUserToRole($nRuid, $role->getId());
00147 }
00148 }
00149 $rid = $role->getId();
00150 Mage::getSingleton('adminhtml/session')->addSuccess($this->__('Role successfully saved.'));
00151 } catch (Mage_Core_Exception $e) {
00152 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00153 } catch (Exception $e) {
00154 Mage::getSingleton('adminhtml/session')->addError($this->__('Error while saving this role. Please try again later.'));
00155 }
00156
00157
00158 $this->_redirect('*/*/editrole', array('rid' => $rid));
00159 return;
00160 }
00161
00162 public function editrolegridAction()
00163 {
00164 $this->getResponse()->setBody($this->getLayout()->createBlock('adminhtml/permissions_role_grid_user')->toHtml());
00165 }
00166
00167 protected function _deleteUserFromRole($userId, $roleId)
00168 {
00169 try {
00170 Mage::getModel("admin/user")
00171 ->setRoleId($roleId)
00172 ->setUserId($userId)
00173 ->deleteFromRole();
00174 } catch (Exception $e) {
00175 throw $e;
00176 return false;
00177 }
00178 return true;
00179 }
00180
00181 protected function _addUserToRole($userId, $roleId)
00182 {
00183 $user = Mage::getModel("admin/user")->load($userId);
00184 $user->setRoleId($roleId)->setUserId($userId);
00185
00186 if( $user->roleUserExists() === true ) {
00187 return false;
00188 } else {
00189 $user->add();
00190 return true;
00191 }
00192 }
00193
00194 protected function _isAllowed()
00195 {
00196 return Mage::getSingleton('admin/session')->isAllowed('system/acl/roles');
00197 }
00198 }