首页 > 代码库 > crm操作权限

crm操作权限

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

    /// <summary>
    /// 权限
    /// </summary>
    public class PrivilegeHelper
    {
        public static readonly string entityName = "privilege";

        /// <summary>
        /// 查询一个实体的权限
        /// 一般的实体都有8个基本的权限
        /// </summary>
        public List<Privilege> SearchPrivilegeByEntityName(IOrganizationService service, string name)
        {
            List<Privilege> list = new List<Privilege>();
            //一般的实体都有8个权限,这里假设实体是:account
            //prvCreateAccount: 创建客户,prvReadAccount: 读取客户记录,prvWriteAccount: 修改客户
            //prvDeleteAccount: 删除客户,prvAppendAccount: 追加,prvAppendToAccount: 追加到
            //prvAssignAccount: 分配客户,prvShareAccount: 共享客户
            QueryExpression query = new QueryExpression();
            query.EntityName = entityName;
            query.ColumnSet = new ColumnSet("name");
            query.Criteria.AddCondition(new ConditionExpression("name",ConditionOperator.Like,"prv%" + name));
            EntityCollection ec = service.RetrieveMultiple(query);
            if (ec != null && ec.Entities.Count > 0)
            {
                foreach(Entity en in ec.Entities)
                {
                    Privilege p = new Privilege();
                    p.id = en.Id;
                    p.name = en["name"].ToString();
                    list.Add(p);
                }
            }
            return list;
        }

        /// <summary>
        /// 通过id查询一个权限
        /// </summary>
        /// <param name="service">服务</param>
        /// <param name="privilegeId">权限id</param>
        public Privilege SearchPrivilegeById(IOrganizationService service,Guid privilegeId)
        {
            Entity en = service.Retrieve(entityName, privilegeId, new ColumnSet("name"));
            Privilege p = new Privilege() { id = privilegeId };
            p.name = en["name"].ToString();
            return p;
        }

        /// <summary>
        /// 查询一个用户具有的权限
        /// </summary>
        /// <param name="service">服务</param>
        /// <param name="userId">用户</param>
        public void SearchPrivilegeByUserId(IOrganizationService service,Guid userId)
        {
            RetrieveUserPrivilegesRequest request = new RetrieveUserPrivilegesRequest();
            request.UserId = userId;

            RetrieveUserPrivilegesResponse response = (RetrieveUserPrivilegesResponse)service.Execute(request);
            if (response.RolePrivileges != null)
            {
                RolePrivilege[] arrayResult = response.RolePrivileges;
            }
        }

        /// <summary>
        /// 检索系统中定义的特权集
        /// </summary>
        /// <param name="service">服务</param>
        public List<Privilege> SearchAllPrivilege(IOrganizationService service)
        {
            List<Privilege> list = new List<Privilege>();
            RetrievePrivilegeSetRequest request = new RetrievePrivilegeSetRequest();
            RetrievePrivilegeSetResponse response = (RetrievePrivilegeSetResponse)service.Execute(request);
            if (response.EntityCollection != null)
            {
                EntityCollection ec = response.EntityCollection;
                if (ec != null && ec.Entities.Count > 0)
                {
                    foreach (Entity en in ec.Entities)
                    {
                        Privilege p = new Privilege();
                        p.id = en.Id;
                        p.name = en["name"].ToString();
                        list.Add(p);
                    }
                }
            }
            return list;
        }

        public class Privilege
        {
            public Guid id { get; set; }
            public string name { get; set; }
        }
    }