基于ABP框架的权限设置

需求:在界面展示中,“定向包管理”,"竞价管理",“竞拍管理”,"发布定向资源","添加竞价资源","添加竞价资源",根据当前用户登录账号判断是否分配了相应权限而展示。

如图:

基于ABP框架的权限设置

如下图:在现有的“定向包管理”,"竞价管理",“竞拍管理” 的权限下 一次新增 "发布定向资源","添加竞价资源","添加竞价资源"三个权限

基于ABP框架的权限设置

新增扩展权限:

基于ABP框架的权限设置

1:首先定义权限变量字段

基于ABP框架的权限设置

2:权限配置

/// <summary>
    /// 权限配置都在这里。
    /// 给权限默认设置服务
    /// See <see cref="ResourceAppPermissions"/> for all permission names.
    /// </summary>
    public class ResourceAppAuthorizationProvider : AuthorizationProvider
    {
        public override void SetPermissions(IPermissionDefinitionContext context)
        {
            //在这里配置了Resource 的权限。

var pages = context.GetPermissionOrNull(AppPermissions.Pages) ?? context.CreatePermission(AppPermissions.Pages, L("Pages"));

var entityNameModel = pages.Children.FirstOrDefault(p => p.Name == AppPermissions.Pages_ResourceManagement)
              ?? pages.CreateChildPermission(AppPermissions.Pages_ResourceManagement, L("ResourceManagement"));
             // 资源管理
            var resource = entityNameModel.CreateChildPermission(ResourceAppPermissions.Resource, L("Resource"));
            resource.CreateChildPermission(ResourceAppPermissions.Resource_CreateResource, L("CreateResource"));
            resource.CreateChildPermission(ResourceAppPermissions.Resource_EditResource, L("EditResource"));
            resource.CreateChildPermission(ResourceAppPermissions.Resource_DeleteResource, L("DeleteResource"));

// 竞价管理
            var bidResource = resource.CreateChildPermission(ResourceAppPermissions.Resource_Bidding,
                L("Resource_Bidding"));
            // 添加竞价资源
            bidResource.CreateChildPermission(ResourceAppPermissions.Resource_AddBidding, L("Resource_AddBidding"));
            // 竞拍管理
            var aucResource = resource.CreateChildPermission(ResourceAppPermissions.Resource_Auction, L("Resource_Auction"));
            // 添加竞拍资源
            aucResource.CreateChildPermission(ResourceAppPermissions.Resource_AddAuction, L("Resource_AddAuction"));
            // 定向包管理
            var DirResource = resource.CreateChildPermission(ResourceAppPermissions.Resource_Directional, L("Resource_Directional"));
            // 发布定向资源
            DirResource.CreateChildPermission(ResourceAppPermissions.Resource_AddDirectional, L("Resource_AddDirectional"));

}

private static ILocalizableString L(string name)
        {
            return new LocalizableString(name, MaterialCirculationConsts.LocalizationSourceName);
        }
    }

3:在JS中注入权限

// 页面权限
        var _permissions = {
            // 资源列表权限
            create: abp.auth.hasPermission("Pages.Resource.CreateResource"),
            edit: abp.auth.hasPermission("Pages.Resource.EditResource"),
            'delete': abp.auth.hasPermission("Pages.Resource.DeleteResource"),
            // 页面tab显示权限
            bidding: abp.auth.hasPermission("Pages.Resource.Bidding"),
            directional: abp.auth.hasPermission("Pages.Resource.Directional"),
            auction: abp.auth.hasPermission("Pages.Resource.Auction"),
            // 添加资源类型权限
            addBidding: abp.auth.hasPermission("Pages.Resource.AddBidding"),
            addDirectional: abp.auth.hasPermission("Pages.Resource.AddDirectional"),
            addAuction: abp.auth.hasPermission("Pages.Resource.AddAuction")

};

4:前端Razor语法判断

<div class="dropdown-menu pull-right">
                @if (IsGranted(ResourceAppPermissions.Resource_AddBidding))
                {    <li>
                    <a href="/Mpa/ResourceManage/CreateOrEditResourceModal?type=0"> 添加竞价资源</a>
                </li>
                }

@if (IsGranted(ResourceAppPermissions.Resource_AddAuction))
                { <li>
                    <a href="/Mpa/ResourceManage/CreateOrEditResourceModal?type=1">添加竞拍资源 </a>
                </li>
                }

@if (IsGranted(ResourceAppPermissions.Resource_AddDirectional))
                {    <li>
                    <a href="/Mpa/ResourceManage/CreateOrEditResourceModal?type=2">发布定向资源</a>
                </li>
                }
            </div>

<div id="SettingsTabPanel" class="tabbable-line">
            <ul id="uResourcesType" class="nav nav-tabs ">
                @if (IsGranted(ResourceAppPermissions.Resource))
                {

<li class="active">
                        <a href="#CachesTab" data-toggle="tab" aria-expanded="false">
                            资源信息
                        </a>
                    </li>

}
                @if (IsGranted(ResourceAppPermissions.Resource_Auction))
                {
                    <li class="">
                        <a href="#biddingTab" value="Auction" data-toggle="tab" aria-expanded="true">
                            竞拍管理
                        </a>
                    </li>
                }

@if (IsGranted(ResourceAppPermissions.Resource_Bidding))
                {
                    <li class="">
                        <a href="#DarkPatTab" value="Bidding" data-toggle="tab" aria-expanded="true">
                            竞价管理
                        </a>
                    </li>
                }

@if (IsGranted(ResourceAppPermissions.Resource_Directional))
                {

<li class="">
                        <a href="#OrientationPackageTab" value="Orientation" data-toggle="tab" aria-expanded="true">
                            定向包管理
                        </a>
                    </li>

}

</ul>

<div class="tab-content">
                <div class="tab-pane active" id="CachesTab">
                    <div id="ResourcesTable"></div>
                </div>
                <div class="tab-pane" id="biddingTab">
                    <div id="biddingResourcesTable"></div>
                </div>
                <div class="tab-pane" id="DarkPatTab">
                    <div id="DarkPatTable"></div>
                </div>
                <div class="tab-pane" id="OrientationPackageTab">
                    <div id="OrientationPackageTable"></div>
                </div>
            </div>
        </div>

上一篇:Visual Studio Code——Angular2 Hello World 之 2.0


下一篇:ABP开发框架前后端开发系列---(8)ABP框架之Winform界面的开发过程