1.添加許可權常量 開啟檔案AppPermissions.cs 【..\MyCompanyName.AbpZeroTemplate.Core\Authorization\AppPermissions.cs】 在末尾添加如下常量:
//分類系統管理權限public const string Pages_Category = "Pages.Category";public const string Pages_Category_Create = "Pages.Category.Create";public const string Pages_Category_Edit = "Pages.Category.Edit";public const string Pages_Category_Delete = "Pages.Category.Delete";
2.編寫代碼 想在頁面顯示還需編寫代碼擷取,開啟檔案AppAuthorizationProvider.cs 【..\MyCompanyName.AbpZeroTemplate.Core\Authorization\AppAuthorizationProvider.cs】 在SetPermissions方法最後添加如下代碼:
//分類許可權的擷取 var category=pages.CreateChildPermission(AppPermissions.Pages_Category, L("CategoryManager")); category.CreateChildPermission(AppPermissions.Pages_Category_Create, L("Category_Create")); category.CreateChildPermission(AppPermissions.Pages_Category_Edit, L("Category_Edit")); category.CreateChildPermission(AppPermissions.Pages_Category_Delete, L("Category_Delete"));
3.編輯語言檔案 開啟語言檔案AbpZeroTemplate-zh-CN.xml 【..\MyCompanyName.AbpZeroTemplate.Core\Localization\AbpZeroTemplate\AbpZeroTemplate-zh-CN.xml】 在最後添加如下代碼:
<text name="Category_Create" value="添加分類" /><text name="Category_Edit" value="編輯分類" /><text name="Category_Delete" value="刪除分類" />
4.測試 產生項目,開啟角色管理--編輯角色,效果如下: 基礎工作已完成,接下來給頁面按鈕和應用程式層方法加許可權。 用戶端許可權控制 1.開啟Index.js檔案 【..\mycompanyname.abpzerotemplate.web\areas\mpa\views\category\index.js】 添加如下代碼:
var _categoryService = abp.services.app.category;//許可權var _permissions = { create: abp.auth.hasPermission('Pages.Category.Create'), edit: abp.auth.hasPermission('Pages.Category.Edit'), 'delete': abp.auth.hasPermission('Pages.Category.Delete')};
同時修改actions裡面的代碼:
var $span = $('<span></span>'); if (_permissions.edit) {//判斷是否有編輯許可權 $('<button class="btn btn-default btn-xs" title="' + app.localize('Edit') + '"><i class="fa fa-edit"></i></button>') .appendTo($span) .click(function() { _editModal.open({ id: data.record.id }); }); } if (_permissions.delete) {//判斷是否有刪除許可權 $('<button class="btn btn-default btn-xs" title="' + app.localize('Delete') + '"><i class="fa fa-trash-o"></i></button>') .appendTo($span) .click(function() { deleteCategory(data.record); }); } return $span;
儲存,開啟角色管理--修改Admin角色,加上編輯分類的許可權,然後儲存,效果如下: 2.測試 然後開啟分類管理頁面,現在可看到刪除按鈕不會顯示出來了,效果如下: 3.添加按鈕加許可權 現在把"添加分類"按鈕也加上許可權 開啟Index視圖【..\mycompanyname.abpzerotemplate.web\areas\mpa\views\category\index.cshtml】 修改"添加分類"按鈕代碼如下:
@if (IsGranted(AppPermissions.Pages_Category_Create))//判斷是否有添加分類的許可權 { <button id="CreateNewCategoryButton" class="btn btn-primary blue"><i class="fa fa-plus"></i>添加分類</button> }
現在沒有分類管理的任何許可權只能查看。 服務端許可權控制 接下來就是服務端許可權的控制,服務端就是應用程式層和Web層,前端頁面就屬於用戶端。 1.控制器加許可權 先來給控制器加許可權,開啟CategoryController 【..\MyCompanyName.AbpZeroTemplate.Web\Areas\Mpa\Controllers\CategoryController.cs】 分別給方法頭加上注釋,代碼如下:
[AbpMvcAuthorize(AppPermissions.Pages_Category)] public class CategoryController : AbpZeroTemplateControllerBase {...[AbpMvcAuthorize(AppPermissions.Pages_Category_Create)] public ActionResult CreateModal()[AbpMvcAuthorize(AppPermissions.Pages_Category_Edit)] public ActionResult EditModal(int id)...
儲存,產生項目,重新整理分類管理,你會發現跳轉到登入頁面了,這是因為我並沒有給Admin角色添加分類管理的任何許可權。 現在把登入串連%2FCategory這段字元刪除,重新訪問http://localhost:8019/Account/Login?ReturnUrl=%2Fmpa,然後登入。 登入成功後,進入角色管理--編輯Admin角色,加入分類管理的許可權。效果如下: 現在訪問分類管理,已經正常顯示了。 2.方法加許可權 到這裡還沒完,繼續給應用程式層方法也加入許可權。 開啟檔案CategoryAppService.cs 【..\MyCompanyName.AbpZeroTemplate.Application\CategoryApp\CategoryAppService.cs】 分別給類和幾個方法加入註解,代碼如下:
[AbpAuthorize(AppPermissions.Pages_Category)] public class CategoryAppService : AbpZeroTemplateAppServiceBase, ICategoryAppService{...[AbpAuthorize(AppPermissions.Pages_Category_Create)] public void CreateCategory(CreateCategoryInput input)...[AbpAuthorize(AppPermissions.Pages_Category_Delete)] public void DeleteCategory(EntityRequestInput input)...[AbpAuthorize(AppPermissions.Pages_Category_Edit)] public void UpdateCategory(CreateCategoryInput input)...
產生Web項目,這裡不給出測試方法,但記住許可權一定要加上。 3.菜單加許可權 最後還有一個地方要加許可權,那就是菜單 開啟檔案MpaNavigationProvider.cs 【..\MyCompanyName.AbpZeroTemplate.Web\Areas\Mpa\Startup\MpaNavigationProvider.cs】 修改分類管理菜單的代碼如下:
//子功能表 PageNames.App.Common.Category, L("CategoryManager"), url:"Mpa/Category", icon: "icon-globe", requiredPermissionName: AppPermissions.Pages_Category//菜單許可權,登入使用者所在角色有此許可權才會顯示出來 ))
產生Web項目,開啟角色管理--編輯Admin角色,把分類管理的許可權去掉,儲存,重新整理頁面。 會發現商店菜單已經不顯示了,可能你會疑惑為什麼連商店菜單都不顯示呢。這是因為商店菜單只有分類管理一個子功能表,如果有多個子功能表時,商店菜單就會顯示出來。 現在,整個分類管理功能終於完成。通過這個例子,我盡量通過最簡單的方式去實現,不管是自己做筆記還是讓他人學習,都比較易懂。