1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: class Mage_Adminhtml_Api_UserController extends Mage_Adminhtml_Controller_Action
27: {
28:
29: protected function _initAction()
30: {
31: $this->loadLayout()
32: ->_setActiveMenu('system/services/users')
33: ->_addBreadcrumb($this->__('Web Services'), $this->__('Web Services'))
34: ->_addBreadcrumb($this->__('Permissions'), $this->__('Permissions'))
35: ->_addBreadcrumb($this->__('Users'), $this->__('Users'))
36: ;
37: return $this;
38: }
39:
40: public function indexAction()
41: {
42: $this->_title($this->__('System'))
43: ->_title($this->__('Web Services'))
44: ->_title($this->__('Users'));
45:
46: $this->_initAction()
47: ->_addContent($this->getLayout()->createBlock('adminhtml/api_user'))
48: ->renderLayout();
49: }
50:
51: public function newAction()
52: {
53: $this->_forward('edit');
54: }
55:
56: public function editAction()
57: {
58: $this->_title($this->__('System'))
59: ->_title($this->__('Web Services'))
60: ->_title($this->__('Users'));
61:
62: $id = $this->getRequest()->getParam('user_id');
63: $model = Mage::getModel('api/user');
64:
65: if ($id) {
66: $model->load($id);
67: if (! $model->getId()) {
68: Mage::getSingleton('adminhtml/session')->addError($this->__('This user no longer exists.'));
69: $this->_redirect('*/*/');
70: return;
71: }
72: }
73:
74: $this->_title($model->getId() ? $model->getName() : $this->__('New User'));
75:
76:
77: $data = Mage::getSingleton('adminhtml/session')->getUserData(true);
78: if (!empty($data)) {
79: $model->setData($data);
80: }
81:
82: Mage::register('api_user', $model);
83:
84: $this->_initAction()
85: ->_addBreadcrumb($id ? $this->__('Edit User') : $this->__('New User'), $id ? $this->__('Edit User') : $this->__('New User'))
86: ->_addContent($this->getLayout()->createBlock('adminhtml/api_user_edit')->setData('action', $this->getUrl('*/api_user/save')))
87: ->_addLeft($this->getLayout()->createBlock('adminhtml/api_user_edit_tabs'));
88:
89: $this->_addJs($this->getLayout()->createBlock('adminhtml/template')->setTemplate('api/user_roles_grid_js.phtml'));
90: $this->renderLayout();
91: }
92:
93: public function saveAction()
94: {
95: if ($data = $this->getRequest()->getPost()) {
96: $id = $this->getRequest()->getPost('user_id', false);
97: $model = Mage::getModel('api/user')->load($id);
98: if (!$model->getId() && $id) {
99: Mage::getSingleton('adminhtml/session')->addError($this->__('This user no longer exists.'));
100: $this->_redirect('*/*/');
101: return;
102: }
103: $model->setData($data);
104: try {
105: $model->save();
106: if ( $uRoles = $this->getRequest()->getParam('roles', false) ) {
107: 108:
109: if ( 1 == sizeof($uRoles) ) {
110: $model->setRoleIds($uRoles)
111: ->setRoleUserId($model->getUserId())
112: ->saveRelations();
113: } else if ( sizeof($uRoles) > 1 ) {
114:
115:
116: $rs = array();
117: $rs[0] = $uRoles[0];
118: $model->setRoleIds( $rs )->setRoleUserId( $model->getUserId() )->saveRelations();
119: }
120: }
121: Mage::getSingleton('adminhtml/session')->addSuccess($this->__('The user has been saved.'));
122: Mage::getSingleton('adminhtml/session')->setUserData(false);
123: $this->_redirect('*/*/edit', array('user_id' => $model->getUserId()));
124: return;
125: } catch (Exception $e) {
126: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
127: Mage::getSingleton('adminhtml/session')->setUserData($data);
128: $this->_redirect('*/*/edit', array('user_id' => $model->getUserId()));
129: return;
130: }
131: }
132: $this->_redirect('*/*/');
133: }
134:
135: public function deleteAction()
136: {
137: if ($id = $this->getRequest()->getParam('user_id')) {
138:
139: try {
140: $model = Mage::getModel('api/user')->load($id);
141: $model->delete();
142: Mage::getSingleton('adminhtml/session')->addSuccess($this->__('The user has been deleted.'));
143: $this->_redirect('*/*/');
144: return;
145: }
146: catch (Exception $e) {
147: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
148: $this->_redirect('*/*/edit', array('user_id' => $this->getRequest()->getParam('user_id')));
149: return;
150: }
151: }
152: Mage::getSingleton('adminhtml/session')->addError($this->__('Unable to find a user to delete.'));
153: $this->_redirect('*/*/');
154: }
155:
156: public function rolesGridAction()
157: {
158: $id = $this->getRequest()->getParam('user_id');
159: $model = Mage::getModel('api/user');
160:
161: if ($id) {
162: $model->load($id);
163: }
164:
165: Mage::register('api_user', $model);
166: $this->getResponse()->setBody($this->getLayout()->createBlock('adminhtml/api_user_edit_tab_roles')->toHtml());
167: }
168:
169: public function roleGridAction()
170: {
171: $this->getResponse()
172: ->setBody($this->getLayout()
173: ->createBlock('adminhtml/api_user_grid')
174: ->toHtml()
175: );
176: }
177:
178: protected function _isAllowed()
179: {
180: return Mage::getSingleton('admin/session')->isAllowed('system/api/users');
181: }
182:
183: }
184: