我創建了一個自定義模塊,可讓您刪除訂單。現在我想要的是,當另一個用戶以自定義角色(而不是管理員)登錄時,此功能將無法使用。如果我禁用了負責刪除自定義用戶訂單的資源,儘管禁用和啟用的設置不可見,但該用戶仍可以執行操作。
.php文件負責檢查模塊是否啟用,如果啟用,它將運行代碼。如果已禁用,則應拋出以下內容
else {
$this->messageManager->addError(__("Either you're not allowed to delete orders or the function has been disabled"));
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath($this->getComponentRefererUrl());
return $resultRedirect;
}
但是不幸的是,當管理員啟用了該功能後,其他用戶也會啟用該功能。
是否可以解決此問題,所以當管理員未登錄時,該功能將無法使用?
先謝謝了。
-解決方案(感謝Shireen N)-
添加以下功能並將其用作驗證,如下所示:
protected function _isAllowed() {
return $this->_authorization->isAllowed('Vendor_Module::resource');
}
if ($enabled and $this->_isAllowed()) {
// your code here
}