1. 程式人生 > >Dynamics CRM Plugin 校驗使用者角色

Dynamics CRM Plugin 校驗使用者角色

        /// <summary>
        /// 校驗使用者角色
        /// </summary>
        /// <param name="roleName">角色名稱</param>
        /// <returns></returns>
        public bool CheckHasRole(IOrganizationService SysService, Guid userId, string roleName)
        {
            QueryExpression query = new QueryExpression()
            {
                EntityName = "role",
                ColumnSet = new ColumnSet("name"),
                LinkEntities = 
                               {
                                   new LinkEntity
                                   {
                                       LinkFromEntityName = "role",
                                       LinkFromAttributeName = "roleid",
                                       LinkToEntityName = "systemuserroles",
                                       LinkToAttributeName = "roleid",
                                       LinkCriteria = new FilterExpression
                                       {
                                           FilterOperator = LogicalOperator.And,
                                           Conditions = 
                                           {
                                               new ConditionExpression
                                               {
                                                   AttributeName = "systemuserid",
                                                   Operator = ConditionOperator.Equal,
                                                   Values = { userId }
                                               }
                                           }
                                       }
                                   }
                               },
                Criteria =
                {
                    Conditions = { 
                       new ConditionExpression("name",ConditionOperator.Equal,roleName)
                    }
                }
            };
            EntityCollection roleCollection = SysService.RetrieveMultiple(query);
            if (roleCollection != null && roleCollection.Entities.Count > 0)
                return true;
            return false;
        }