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_Permissions_UserController extends Mage_Adminhtml_Controller_Action
27: {
28:
29: protected function _initAction()
30: {
31: $this->loadLayout()
32: ->_setActiveMenu('system/acl')
33: ->_addBreadcrumb($this->__('System'), $this->__('System'))
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->__('Permissions'))
44: ->_title($this->__('Users'));
45:
46: $this->_initAction()
47: ->_addContent($this->getLayout()->createBlock('adminhtml/permissions_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->__('Permissions'))
60: ->_title($this->__('Users'));
61:
62: $id = $this->getRequest()->getParam('user_id');
63: $model = Mage::getModel('admin/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('permissions_user', $model);
83:
84: if (isset($id)) {
85: $breadcrumb = $this->__('Edit User');
86: } else {
87: $breadcrumb = $this->__('New User');
88: }
89: $this->_initAction()
90: ->_addBreadcrumb($breadcrumb, $breadcrumb);
91:
92: $this->getLayout()->getBlock('adminhtml.permissions.user.edit')
93: ->setData('action', $this->getUrl('*/permissions_user/save'));
94:
95: $this->renderLayout();
96: }
97:
98: public function saveAction()
99: {
100: if ($data = $this->getRequest()->getPost()) {
101:
102: $id = $this->getRequest()->getParam('user_id');
103: $model = Mage::getModel('admin/user')->load($id);
104: if (!$model->getId() && $id) {
105: Mage::getSingleton('adminhtml/session')->addError($this->__('This user no longer exists.'));
106: $this->_redirect('*/*/');
107: return;
108: }
109: $model->setData($data);
110:
111: 112: 113:
114: if ($model->hasNewPassword() && $model->getNewPassword() === '') {
115: $model->unsNewPassword();
116: }
117: if ($model->hasPasswordConfirmation() && $model->getPasswordConfirmation() === '') {
118: $model->unsPasswordConfirmation();
119: }
120:
121: $result = $model->validate();
122: if (is_array($result)) {
123: Mage::getSingleton('adminhtml/session')->setUserData($data);
124: foreach ($result as $message) {
125: Mage::getSingleton('adminhtml/session')->addError($message);
126: }
127: $this->_redirect('*/*/edit', array('_current' => true));
128: return $this;
129: }
130:
131: try {
132: $model->save();
133: if ( $uRoles = $this->getRequest()->getParam('roles', false) ) {
134: 135:
136: if ( 1 == sizeof($uRoles) ) {
137: $model->setRoleIds($uRoles)
138: ->setRoleUserId($model->getUserId())
139: ->saveRelations();
140: } else if ( sizeof($uRoles) > 1 ) {
141:
142:
143: $rs = array();
144: $rs[0] = $uRoles[0];
145: $model->setRoleIds( $rs )->setRoleUserId( $model->getUserId() )->saveRelations();
146: }
147: }
148: Mage::getSingleton('adminhtml/session')->addSuccess($this->__('The user has been saved.'));
149: Mage::getSingleton('adminhtml/session')->setUserData(false);
150: $this->_redirect('*/*/');
151: return;
152: } catch (Mage_Core_Exception $e) {
153: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
154: Mage::getSingleton('adminhtml/session')->setUserData($data);
155: $this->_redirect('*/*/edit', array('user_id' => $model->getUserId()));
156: return;
157: }
158: }
159: $this->_redirect('*/*/');
160: }
161:
162: public function deleteAction()
163: {
164: $currentUser = Mage::getSingleton('admin/session')->getUser();
165:
166: if ($id = $this->getRequest()->getParam('user_id')) {
167: if ( $currentUser->getId() == $id ) {
168: Mage::getSingleton('adminhtml/session')->addError($this->__('You cannot delete your own account.'));
169: $this->_redirect('*/*/edit', array('user_id' => $id));
170: return;
171: }
172: try {
173: $model = Mage::getModel('admin/user');
174: $model->setId($id);
175: $model->delete();
176: Mage::getSingleton('adminhtml/session')->addSuccess($this->__('The user has been deleted.'));
177: $this->_redirect('*/*/');
178: return;
179: }
180: catch (Exception $e) {
181: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
182: $this->_redirect('*/*/edit', array('user_id' => $this->getRequest()->getParam('user_id')));
183: return;
184: }
185: }
186: Mage::getSingleton('adminhtml/session')->addError($this->__('Unable to find a user to delete.'));
187: $this->_redirect('*/*/');
188: }
189:
190: public function rolesGridAction()
191: {
192: $id = $this->getRequest()->getParam('user_id');
193: $model = Mage::getModel('admin/user');
194:
195: if ($id) {
196: $model->load($id);
197: }
198:
199: Mage::register('permissions_user', $model);
200: $this->getResponse()->setBody(
201: $this->getLayout()
202: ->createBlock('adminhtml/permissions_user_edit_tab_roles')
203: ->toHtml()
204: );
205: }
206:
207: public function roleGridAction()
208: {
209: $this->getResponse()
210: ->setBody($this->getLayout()
211: ->createBlock('adminhtml/permissions_user_grid')
212: ->toHtml()
213: );
214: }
215:
216: protected function _isAllowed()
217: {
218: return Mage::getSingleton('admin/session')->isAllowed('system/acl/users');
219: }
220:
221: }
222: