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:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Api2_Adminhtml_Api2_RoleController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38:
39: public function indexAction()
40: {
41: $this->_title($this->__('System'))
42: ->_title($this->__('Web Services'))
43: ->_title($this->__('REST Roles'));
44:
45: $this->loadLayout()->_setActiveMenu('system/services/roles');
46: $this->_addBreadcrumb($this->__('Web services'), $this->__('Web services'));
47: $this->_addBreadcrumb($this->__('REST Roles'), $this->__('REST Roles'));
48: $this->_addBreadcrumb($this->__('Roles'), $this->__('Roles'));
49:
50: $this->renderLayout();
51: }
52:
53: 54: 55:
56: public function gridAction()
57: {
58: $this->loadLayout();
59: $this->renderLayout();
60: }
61:
62: 63: 64:
65: public function usersGridAction()
66: {
67: $id = $this->getRequest()->getParam('id', false);
68:
69: $this->loadLayout();
70:
71: $grid = $this->getLayout()->getBlock('adminhtml.role.edit.tab.users');
72: $grid->setUsers($this->_getUsers($id));
73:
74: $this->renderLayout();
75: }
76:
77: 78: 79:
80: public function newAction()
81: {
82: $this->_title($this->__('System'))
83: ->_title($this->__('Web Services'))
84: ->_title($this->__('Rest Roles'));
85:
86: $this->loadLayout()->_setActiveMenu('system/services/roles');
87: $this->_addBreadcrumb($this->__('Web services'), $this->__('Web services'));
88: $this->_addBreadcrumb($this->__('REST Roles'), $this->__('REST Roles'));
89: $this->_addBreadcrumb($this->__('Roles'), $this->__('Roles'));
90:
91: $breadCrumb = $this->__('Add New Role');
92: $breadCrumbTitle = $this->__('Add New Role');
93: $this->_title($this->__('New Role'));
94:
95: $this->_addBreadcrumb($breadCrumb, $breadCrumbTitle);
96:
97: $this->renderLayout();
98: }
99:
100: 101: 102:
103: public function editAction()
104: {
105: $id = (int) $this->getRequest()->getParam('id');
106:
107: $role = Mage::getModel('api2/acl_global_role')->load($id);
108:
109: if (!$role->getId()) {
110: $this->_getSession()->addError($this->__('Role "%s" not found.', $id));
111: $this->_redirect('*/*/');
112: return;
113: }
114:
115: $this->loadLayout()->_setActiveMenu('system/services/roles');
116:
117: $this->_title($this->__('System'))
118: ->_title($this->__('Web Services'))
119: ->_title($this->__('Rest Roles'));
120:
121: $breadCrumb = $this->__('Edit Role');
122: $breadCrumbTitle = $this->__('Edit Role');
123: $this->_title($this->__('Edit Role'));
124: $this->_addBreadcrumb($breadCrumb, $breadCrumbTitle);
125:
126:
127: $tabs = $this->getLayout()->getBlock('adminhtml.role.edit.tabs');
128: $tabs->setRole($role);
129:
130: foreach ($tabs->getChild() as $child) {
131: $child->setData('role', $role);
132: }
133:
134:
135: $buttons = $this->getLayout()->getBlock('adminhtml.roles.buttons');
136: $buttons->setRole($role);
137:
138:
139: $users = $this->getLayout()->getBlock('adminhtml.role.edit.tab.users');
140: $users->setUsers($this->_getUsers($id));
141:
142:
143:
144:
145: $this->renderLayout();
146: }
147:
148: 149: 150:
151: public function saveAction()
152: {
153: $request = $this->getRequest();
154:
155: $id = $request->getParam('id', false);
156:
157: $role = Mage::getModel('api2/acl_global_role')->load($id);
158:
159: if (!$role->getId() && $id) {
160: $this->_getSession()->addError(
161: $this->__('Role "%s" no longer exists', $role->getData('role_name')));
162: $this->_redirect('*/*/');
163: return;
164: }
165:
166: $roleUsers = $request->getParam('in_role_users', null);
167: parse_str($roleUsers, $roleUsers);
168: $roleUsers = array_keys($roleUsers);
169:
170: $oldRoleUsers = $this->getRequest()->getParam('in_role_users_old');
171: parse_str($oldRoleUsers, $oldRoleUsers);
172: $oldRoleUsers = array_keys($oldRoleUsers);
173:
174:
175: $session = $this->_getSession();
176:
177: try {
178: $role->setRoleName($this->getRequest()->getParam('role_name', false))
179: ->save();
180:
181: foreach($oldRoleUsers as $oUid) {
182: $this->_deleteUserFromRole($oUid, $role->getId());
183: }
184:
185: foreach ($roleUsers as $nRuid) {
186: $this->_addUserToRole($nRuid, $role->getId());
187: }
188:
189: 190: 191:
192:
193: $rule = Mage::getModel('api2/acl_global_rule');
194: if ($id) {
195: $collection = $rule->getCollection();
196: $collection->addFilterByRoleId($role->getId());
197:
198:
199: foreach ($collection as $model) {
200: $model->delete();
201: }
202: }
203:
204:
205: $ruleTree = Mage::getSingleton(
206: 'api2/acl_global_rule_tree',
207: array('type' => Mage_Api2_Model_Acl_Global_Rule_Tree::TYPE_PRIVILEGE)
208: );
209: $resources = $ruleTree->getPostResources();
210: $id = $role->getId();
211: foreach ($resources as $resourceId => $privileges) {
212: foreach ($privileges as $privilege => $allow) {
213: if (!$allow) {
214: continue;
215: }
216:
217: $rule->setId(null)
218: ->isObjectNew(true);
219:
220: $rule->setRoleId($id)
221: ->setResourceId($resourceId)
222: ->setPrivilege($privilege)
223: ->save();
224: }
225: }
226:
227: $session->addSuccess($this->__('The role has been saved.'));
228: } catch (Mage_Core_Exception $e) {
229: $session->addError($e->getMessage());
230: } catch (Exception $e) {
231: $session->addException($e, $this->__('An error occurred while saving role.'));
232: }
233:
234: $this->_redirect('*/*/edit', array('id'=>$id));
235: }
236:
237: 238: 239:
240: public function deleteAction()
241: {
242: $id = $this->getRequest()->getParam('id', false);
243:
244: try {
245:
246: $model = Mage::getModel("api2/acl_global_role");
247: $model->load($id)->delete();
248: $this->_getSession()->addSuccess($this->__('Role has been deleted.'));
249: } catch (Mage_Core_Exception $e) {
250: $this->_getSession()->addError($e->getMessage());
251: } catch (Exception $e) {
252: $this->_getSession()->addException($e, $this->__('An error occurred while deleting the role.'));
253: }
254:
255: $this->_redirect("*/*/");
256: }
257:
258: 259: 260: 261: 262:
263: protected function _isAllowed()
264: {
265:
266: $session = Mage::getSingleton('admin/session');
267: return $session->isAllowed('system/api/roles_rest');
268: }
269:
270: 271: 272:
273: public function rolesGridAction()
274: {
275:
276: $model = Mage::getModel('admin/user');
277: $model->load($this->getRequest()->getParam('user_id'));
278:
279: Mage::register('permissions_user', $model);
280: $this->getResponse()
281: ->setBody($this->getLayout()->createBlock('api2/adminhtml_permissions_user_edit_tab_roles')->toHtml());
282: }
283:
284: 285: 286: 287: 288: 289:
290: protected function _getUsers($id)
291: {
292: if ( $this->getRequest()->getParam('in_role_users') != "" ) {
293: return $this->getRequest()->getParam('in_role_users');
294: }
295:
296:
297: $role = Mage::getModel('api2/acl_global_role');
298: $role->setId($id);
299:
300:
301: $resource = $role->getResource();
302: $users = $resource->getRoleUsers($role);
303:
304: if (sizeof($users) == 0) {
305: $users = array();
306: }
307:
308: return $users;
309: }
310:
311: 312: 313: 314: 315: 316: 317:
318: protected function _deleteUserFromRole($adminId, $roleId)
319: {
320:
321: $resourceModel = Mage::getResourceModel('api2/acl_global_role');
322: $resourceModel->deleteAdminToRoleRelation($adminId, $roleId);
323: return $this;
324: }
325:
326: 327: 328: 329: 330: 331: 332:
333: protected function _addUserToRole($adminId, $roleId)
334: {
335:
336: $resourceModel = Mage::getResourceModel('api2/acl_global_role');
337: $resourceModel->saveAdminToRoleRelation($adminId, $roleId);
338: return $this;
339: }
340: }
341: