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角色,把分類管理的權限去掉,保存,刷新頁面。

會發現商店菜單已經不顯示了,可能你會疑惑為什么連商店菜單都不顯示呢?這是因為商店菜單只有分類管理一個子菜單,如果有多個子菜單時,商店菜單就會顯示出來。

現在,整個分類管理功能終於完成。通過這個例子,我盡量通過最簡單的方式去實現,不管是自己做筆記還是讓他人學習,都比較易懂。