Field Security Profile Helper

using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;

/// <summary>
/// 安全字段
/// </summary>
public class FieldSecurityProfileHelper
{
    public static readonly string entityName = "fieldsecurityprofile";
    public static readonly string FieldPermission = "FieldPermission";
    public Guid fieldSecurityProfileId = Guid.Empty;
    public Guid fieldPermissionId = Guid.Empty;

    /// <summary>
    /// 创建安全字段
    /// </summary> 
    public void Create(IOrganizationService service)
    {
        Entity en = new Entity() { LogicalName = entityName };
        en["name"] = "new_name";
        fieldSecurityProfileId = service.Create(en);
    }

    /// <summary>
    /// 给安全字段添加团队
    /// </summary>
    /// <param name="service">服务</param>
    /// <param name="teamId">团队</param>
    public void AddTeam(IOrganizationService service, Guid teamId)
    {
        AssociateRequest request = new AssociateRequest();
        request.Relationship = new Relationship("teamprofiles_association");
        request.Target = new EntityReference() { LogicalName = entityName, Id = fieldSecurityProfileId };
        request.RelatedEntities = new EntityReferenceCollection();
        request.RelatedEntities.Add(new EntityReference() { LogicalName = "team", Id = teamId });
        service.Execute(request);
    }

    /// <summary>
    /// 给安全字段移除团队
    /// </summary>
    /// <param name="service">服务</param>
    /// <param name="teamId">团队</param>
    public void RemoveTeam(IOrganizationService service, Guid teamId)
    {
        DisassociateRequest request = new DisassociateRequest();
        request.Relationship = new Relationship("teamprofiles_association");
        request.Target = new EntityReference() { LogicalName = entityName, Id = fieldSecurityProfileId };
        request.RelatedEntities = new EntityReferenceCollection();
        request.RelatedEntities.Add(new EntityReference() { LogicalName = "team", Id = teamId });
        service.Execute(request);
    }

    /// <summary>
    /// 给安全字段添加用户
    /// </summary>
    /// <param name="service">服务</param>
    /// <param name="teamId">用户</param>
    public void AddUser(IOrganizationService service, Guid userId)
    {
        AssociateRequest request = new AssociateRequest();
        request.Relationship = new Relationship("systemuserprofiles_association");
        request.Target = new EntityReference() { LogicalName = entityName, Id = fieldSecurityProfileId };
        request.RelatedEntities = new EntityReferenceCollection();
        request.RelatedEntities.Add(new EntityReference() { LogicalName = "systemuser", Id = userId });
        service.Execute(request);
    }

    /// <summary>
    /// 给安全字段移除用户
    /// </summary>
    /// <param name="service">服务</param>
    /// <param name="teamId">用户</param>
    public void RemoveUser(IOrganizationService service, Guid userId)
    {
        DisassociateRequest request = new DisassociateRequest();
        request.Relationship = new Relationship("systemuserprofiles_association");
        request.Target = new EntityReference() { LogicalName = entityName, Id = fieldSecurityProfileId };
        request.RelatedEntities = new EntityReferenceCollection();
        request.RelatedEntities.Add(new EntityReference() { LogicalName = "systemuser", Id = userId });
        service.Execute(request);
    }

    /// <summary>
    /// 把安全字段和实体关联
    /// </summary>
    /// <param name="service">服务</param>
    /// <param name="entity">实体</param>
    public void CreateAttribute(IOrganizationService service, string entity)
    {
        Entity en = new Entity() { LogicalName = FieldPermission };
        //实体名称
        en["entityname"] = entity;
        en["canread"] = new OptionSetValue(FieldPermissionType.Allowed);
        en["attributelogicalname"] = "new_attributename";
        en["fieldsecurityprofileid"] = new EntityReference() { LogicalName = entityName, Id = fieldSecurityProfileId };
        fieldSecurityProfileId = service.Create(en);
    }

    /// <summary>
    /// 删除实体里面的安全字段
    /// </summary>
    /// <param name="service">服务</param>
    public void RemoveAttribute(IOrganizationService service)
    {
        service.Delete(FieldPermission, fieldSecurityProfileId);
    }

    /// <summary>         /// 
    /// 删除安全字段         /// 
    /// </summary>         
    public void Delete(IOrganizationService service) { service.Delete(entityName, fieldSecurityProfileId); }
}

 

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