crm操作权限

系统 1758 0

    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; }
        }
    }

crm操作权限


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论