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 class Mage_Adminhtml_Api_UserController extends Mage_Adminhtml_Controller_Action
00027 {
00028
00029 protected function _initAction()
00030 {
00031 $this->loadLayout()
00032 ->_setActiveMenu('api/users')
00033 ->_addBreadcrumb($this->__('Web Services'), $this->__('Web Services'))
00034 ->_addBreadcrumb($this->__('Permissions'), $this->__('Permissions'))
00035 ->_addBreadcrumb($this->__('Users'), $this->__('Users'))
00036 ;
00037 return $this;
00038 }
00039
00040 public function indexAction()
00041 {
00042 $this->_initAction()
00043 ->_addContent($this->getLayout()->createBlock('adminhtml/api_user'))
00044 ->renderLayout();
00045 }
00046
00047 public function newAction()
00048 {
00049 $this->_forward('edit');
00050 }
00051
00052 public function editAction()
00053 {
00054 $id = $this->getRequest()->getParam('user_id');
00055 $model = Mage::getModel('api/user');
00056
00057 if ($id) {
00058 $model->load($id);
00059 if (! $model->getId()) {
00060 Mage::getSingleton('adminhtml/session')->addError($this->__('This user no longer exists'));
00061 $this->_redirect('*/*/');
00062 return;
00063 }
00064 }
00065
00066 $data = Mage::getSingleton('adminhtml/session')->getUserData(true);
00067 if (!empty($data)) {
00068 $model->setData($data);
00069 }
00070
00071 Mage::register('api_user', $model);
00072
00073 $this->_initAction()
00074 ->_addBreadcrumb($id ? $this->__('Edit User') : $this->__('New User'), $id ? $this->__('Edit User') : $this->__('New User'))
00075 ->_addContent($this->getLayout()->createBlock('adminhtml/api_user_edit')->setData('action', $this->getUrl('*/api_user/save')))
00076 ->_addLeft($this->getLayout()->createBlock('adminhtml/api_user_edit_tabs'));
00077
00078 $this->_addJs($this->getLayout()->createBlock('adminhtml/template')->setTemplate('api/user_roles_grid_js.phtml'));
00079 $this->renderLayout();
00080 }
00081
00082 public function saveAction()
00083 {
00084 if ($data = $this->getRequest()->getPost()) {
00085 $model = Mage::getModel('api/user');
00086 $model->setData($data);
00087 try {
00088 $model->save();
00089 if ( $uRoles = $this->getRequest()->getParam('roles', false) ) {
00090
00091
00092 if ( 1 == sizeof($uRoles) ) {
00093 $model->setRoleIds($uRoles)
00094 ->setRoleUserId($model->getUserId())
00095 ->saveRelations();
00096 } else if ( sizeof($uRoles) > 1 ) {
00097
00098
00099 $rs = array();
00100 $rs[0] = $uRoles[0];
00101 $model->setRoleIds( $rs )->setRoleUserId( $model->getUserId() )->saveRelations();
00102 }
00103 }
00104 Mage::getSingleton('adminhtml/session')->addSuccess($this->__('User was successfully saved'));
00105 Mage::getSingleton('adminhtml/session')->setUserData(false);
00106 $this->_redirect('*/*/edit', array('user_id' => $model->getUserId()));
00107 return;
00108 } catch (Exception $e) {
00109 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00110 Mage::getSingleton('adminhtml/session')->setUserData($data);
00111 $this->_redirect('*/*/edit', array('user_id' => $model->getUserId()));
00112 return;
00113 }
00114 }
00115 $this->_redirect('*/*/');
00116 }
00117
00118 public function deleteAction()
00119 {
00120 if ($id = $this->getRequest()->getParam('user_id')) {
00121
00122 try {
00123 $model = Mage::getModel('api/user');
00124 $model->setId($id);
00125 $model->delete();
00126 Mage::getSingleton('adminhtml/session')->addSuccess($this->__('User was successfully deleted'));
00127 $this->_redirect('*/*/');
00128 return;
00129 }
00130 catch (Exception $e) {
00131 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00132 $this->_redirect('*/*/edit', array('user_id' => $this->getRequest()->getParam('user_id')));
00133 return;
00134 }
00135 }
00136 Mage::getSingleton('adminhtml/session')->addError($this->__('Unable to find a user to delete'));
00137 $this->_redirect('*/*/');
00138 }
00139
00140 public function rolesGridAction()
00141 {
00142 $id = $this->getRequest()->getParam('user_id');
00143 $model = Mage::getModel('api/user');
00144
00145 if ($id) {
00146 $model->load($id);
00147 }
00148
00149 Mage::register('api_user', $model);
00150 $this->getResponse()->setBody($this->getLayout()->createBlock('adminhtml/api_user_edit_tab_roles')->toHtml());
00151 }
00152
00153 public function roleGridAction()
00154 {
00155 $this->getResponse()
00156 ->setBody($this->getLayout()
00157 ->createBlock('adminhtml/api_user_grid')
00158 ->toHtml()
00159 );
00160 }
00161
00162 protected function _isAllowed()
00163 {
00164 return Mage::getSingleton('admin/session')->isAllowed('api/users');
00165 }
00166
00167 }