|
7 | 7 | namespace Magento\User\Controller\Adminhtml\User\Role;
|
8 | 8 |
|
9 | 9 | use Magento\Framework\Controller\ResultFactory;
|
| 10 | +use Magento\Framework\Exception\NotFoundException; |
10 | 11 |
|
| 12 | +/** |
| 13 | + * User roles delete action. |
| 14 | + */ |
11 | 15 | class Delete extends \Magento\User\Controller\Adminhtml\User\Role
|
12 | 16 | {
|
13 | 17 | /**
|
14 |
| - * Remove role action |
| 18 | + * Remove role action. |
15 | 19 | *
|
16 |
| - * @return \Magento\Backend\Model\View\Result\Redirect|void |
| 20 | + * @return \Magento\Backend\Model\View\Result\Redirect |
| 21 | + * @throws NotFoundException |
17 | 22 | */
|
18 | 23 | public function execute()
|
19 | 24 | {
|
| 25 | + if (!$this->getRequest()->isPost()) { |
| 26 | + throw new NotFoundException(__('Page not found')); |
| 27 | + } |
20 | 28 | /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
|
21 | 29 | $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
|
22 |
| - $rid = $this->getRequest()->getParam('rid', false); |
| 30 | + $rid = (int)$this->getRequest()->getParam('rid', false); |
23 | 31 | /** @var \Magento\User\Model\User $currentUser */
|
24 | 32 | $currentUser = $this->_userFactory->create()->setId($this->_authSession->getUser()->getId());
|
25 | 33 |
|
26 | 34 | if (in_array($rid, $currentUser->getRoles())) {
|
27 | 35 | $this->messageManager->addError(__('You cannot delete self-assigned roles.'));
|
| 36 | + |
28 | 37 | return $resultRedirect->setPath('adminhtml/*/editrole', ['rid' => $rid]);
|
29 | 38 | }
|
| 39 | + $role = $this->_initRole(); |
| 40 | + if (!$role->getId()) { |
| 41 | + $this->messageManager->addError(__('We can\'t find a role to delete.')); |
| 42 | + |
| 43 | + return $resultRedirect->setPath("*/*/"); |
| 44 | + } |
30 | 45 |
|
31 | 46 | try {
|
32 |
| - $this->_initRole()->delete(); |
| 47 | + $role->delete(); |
33 | 48 | $this->messageManager->addSuccess(__('You deleted the role.'));
|
34 | 49 | } catch (\Exception $e) {
|
35 | 50 | $this->messageManager->addError(__('An error occurred while deleting this role.'));
|
|
0 commit comments