Role Helper

using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using System.Collections.Generic;

/// <summary>
/// 安全角色
/// </summary>
public class RoleHelper
{
    public static readonly string entityName = "role";
    public Guid roleId = Guid.Empty;

    /// <summary>
    /// 创建安全角色
    /// </summary>
    /// <param name="service">服务</param>
    /// <param name="name">角色名称</param>
    /// <param name="unitId">业务部门</param>
    public void Create(IOrganizationService service, string name, Guid unitId)
    {
        Entity en = new Entity() { LogicalName = entityName };
        en["name"] = name;
        en["businessunitid"] = new EntityReference() { LogicalName = "businessunit", Id = unitId };
        roleId = service.Create(en);
    }

    /// <summary>
    /// 修改安全角色
    /// </summary>
    /// <param name="service">服务</param>
    /// <param name="name">角色名称</param>
    /// <param name="unitId">业务部门</param>
    public void Update(IOrganizationService service, string name, Guid unitId)
    {
        Entity en = new Entity() { LogicalName = entityName, Id = roleId };
        en["name"] = name;
        en["businessunitid"] = new EntityReference() { LogicalName = "businessunit", Id = unitId };
        service.Update(en);
    }

    /// <summary>
    /// 给安全角色添加权限
    /// </summary>
    /// <param name="service">服务</param>
    /// <param name="businessUnitId">业务部门id</param>
    /// <param name="privilegeId">权限id</param>
    public void AddPrivilegesRole(IOrganizationService service, Guid businessUnitId, Guid privilegeId)
    {
        AddPrivilegesRoleRequest request = new AddPrivilegesRoleRequest();
        request.RoleId = roleId;
        RolePrivilege rp = new RolePrivilege();
        //Basic: 1/4,Local : 1/2,Deep : 3/4,Global: 4/4 组织
        rp.Depth = PrivilegeDepth.Basic;
        //权限
        rp.PrivilegeId = privilegeId;
        //业务部门
        rp.BusinessUnitId = businessUnitId;
        //权限
        request.Privileges = new RolePrivilege[] { rp };

        service.Execute(request);
    }

    /// <summary>
    /// 给安全角色移除权限
    /// </summary>
    /// <param name="service">服务</param>
    /// <param name="privilergeId">安全角色</param>
    public void RemovePrivilegeRole(IOrganizationService service, Guid privilergeId)
    {
        RemovePrivilegeRoleRequest roleRequest = new RemovePrivilegeRoleRequest();
        roleRequest.RoleId = roleId;
        roleRequest.PrivilegeId = privilergeId;
        service.Execute(roleRequest);
    }

    /// <summary>
    /// 安全角色替换权限(删除以前的角色,添加现有的角色)
    /// </summary>
    /// <param name="service">服务</param>
    /// <param name="businessUnitId">业务部门id</param>
    /// <param name="privilegeId">权限id</param>
    public void ReplacePrivilegeRpole(IOrganizationService service, Guid businessUnitId, Guid privilegeId)
    {
        ReplacePrivilegesRoleRequest roleRequest = new ReplacePrivilegesRoleRequest();
        roleRequest.RoleId = roleId;
        RolePrivilege rp = new RolePrivilege();
        //Basic: 1/4,Local : 1/2,Deep : 3/4,Global: 4/4 组织
        rp.Depth = PrivilegeDepth.Basic;
        //权限
        rp.PrivilegeId = privilegeId;
        //业务部门
        rp.BusinessUnitId = businessUnitId;
        //权限
        roleRequest.Privileges = new RolePrivilege[] { rp };

        service.Execute(roleRequest);
    }

    /// <summary>
    /// 检索分派给指定角色的权限
    /// </summary>
    /// <param name="service">服务</param>
    public void SearchPrivilegeRole(IOrganizationService service)
    {
        RetrieveRolePrivilegesRoleRequest request = new RetrieveRolePrivilegesRoleRequest();
        request.RoleId = roleId;

        ReplacePrivilegesRoleResponse response = (ReplacePrivilegesRoleResponse)service.Execute(request);
        if (response != null && response.Results != null)
        {
            ParameterCollection result = response.Results;
        }
    }

    /// <summary>         /// 
    /// 删除安全角色         /// 
    /// </summary>         /// 
    /// <param name="service">服务</param>         
    public void Delete(IOrganizationService service) { service.Delete(entityName, roleId); }
}

 

posted @ 2018-11-07 11:25  大漠银狐  阅读(226)  评论(0编辑  收藏  举报