asp.net zero--15. An example (8) Commodity classification management-rights control _0__.net

Source: Internet
Author: User
Tags auth
1. Add Permission ConstantsOpen File AppPermissions.cs ". \mycompanyname.abpzerotemplate.core\authorization\apppermissions.cs Add the following constants at the end:
Classification Administration Permissions 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. Write code to display in the page also need to write code acquisition, open file AppAuthorizationProvider.cs "... \mycompanyname.abpzerotemplate.core\authorization\appauthorizationprovider.cs Add the following code at the end of the SetPermissions method:

Get var category=pages for categorization permissions
            . 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. Edit Language file Open language file Abpzerotemplate-zh-cn.xml ". \mycompanyname.abpzerotemplate.core\localization\abpzerotemplate\abpzerotemplate-zh-cn.xml Add the following code at the end:

<text name= "Category_create" value= "add category/> <text name=" Category_edit "value="
edit category/> <text
Name= "Category_delete" value= "delete category"/>

4. Test build project, open Role Management-edit role, the effect is as follows: The basic work is completed, next to the page button and the application layer method permissions. Client Rights Control 1. Open index.js file ". \mycompanyname.abpzerotemplate.web\areas\mpa\views\category\index.js Add the following code:

var _categoryservice = abp.services.app.category;
Permission
var _permissions = {
    create:abp.auth.hasPermission (' Pages.Category.Create '),
    edit: Abp.auth.hasPermission (' Pages.Category.Edit '),
    ' delete ': abp.auth.hasPermission (' Pages.Category.Delete ')
};

Also modify the code within the actions:

var $span = $ (' <span></span> '); if (_permissions.edit) {//To determine whether there is edit permission $ (' <button class= ' btn btn-default btn-xs ' title= ' + AP P.localize (' Edit ') + ' "><i class=" fa fa-edit "></i></button>"). Append to ($span). Click (function () {_editmodal.open ({Id:da
                                Ta.record.id});
                        }); } if (_permissions.delete) {//To determine whether there is Delete permission $ (' <button class= ' btn btn-d
                                Efault Btn-xs "title=" + app.localize (' Delete ') + ' ><i class= ' fa fa-trash-o ' ></i></button> ')
                                    . Appendto ($span). Click (function () {
                                Deletecategory (Data.record);
                        });
                       } return $span; 

Save, open role management--Modify the Admin role, plus edit the classification permissions, and then save, the effect is as follows: 2. Test and then open the Category Management page, you can now see the delete button does not appear, the effect is as follows: 3. Add a button and now put " Add Category "button plus permissions to open index View" ... \mycompanyname.abpzerotemplate.web\areas\mpa\views\category\index.cshtml the code to modify the Add Category button is as follows:

@if (isgranted (apppermissions.pages_category_create))//Determine if there is a permission to add categories
        {
            <button id= " Createnewcategorybutton "class=" btn btn-primary Blue "><i class=" FA fa-plus "></i> add category </button>
        }

No permissions are currently available for classification management to view only. Server- side permissions Control Next is the server-side permissions control, the server is the application layer and the Web layer, the front-end page belongs to the client. 1. Controller plus permissions first to the controller to add permissions, open Categorycontroller ". \mycompanyname.abpzerotemplate.web\areas\mpa\controllers\categorycontroller.cs the method headers separately, with the following code:

[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)
...

Save, build the project, refresh the classification management, you will find the jump to the login page, which is because I did not add any permissions to the Admin role for category management. Now the login connection%2fcategory this character, re-access Http://localhost:8019/Account/Login?ReturnUrl=%2Fmpa, and then log in. After successful login, enter role management-edit Admin role and add Category Admin permissions.   The effect is as follows: Now access to the classification management, has been normal display. 2. Method Plus permissions Here is not finished, continue to the application layer method also add permissions. Open File CategoryAppService.cs ". \mycompanyname.abpzerotemplate.application\categoryapp\categoryappservice.cs "Add annotations to classes and several methods, with the following code:

[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) ...

Build the Web project, no test methods are given here, but remember that permissions must be added. 3. Menu Plus Permissions finally there is a place to add permissions, that is, menu open file MpaNavigationProvider.cs. \mycompanyname.abpzerotemplate.web\areas\mpa\startup\mpanavigationprovider.cs the code to modify the category Management menu is as follows:

submenu
                        PageNames.App.Common.Category,
                        L ("Categorymanager"),
                        URL: "mpa/category",
                        icon: " Icon-globe ",
                        requiredpermissionname:apppermissions.pages_category//menu permission, the login user's role has this permission to appear
                        )

Build a Web project, open Role Management-edit Admin role, remove category Admin permissions, save, refresh page. You will find that the store menu is out of the question and you may wonder why the Store menu is not displayed. This is because the store menu has only a single submenu, and if you have more than one submenu, the store menu is displayed. Now, the entire classification management function is finally completed. Through this example, I try to achieve through the simplest way, whether it is to make their own notes or let others learn, are easier to understand.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.