Показать сообщение отдельно
Старый 29.01.2009, 11:38   #20  
Bakai Madybaev is offline
Bakai Madybaev
Участник
 
201 / 16 (1) ++
Регистрация: 22.04.2008
X++:
        public void Execute(IPluginExecutionContext context)
        {
            if (context.Depth > 1)
                return;

            Microsoft.Crm.Sdk.Moniker entity = null;

            if (context.InputParameters.Properties.Contains("Target") &&
               context.InputParameters.Properties["Target"] is Microsoft.Crm.Sdk.Moniker)
            {
                entity = (Microsoft.Crm.Sdk.Moniker)context.InputParameters.Properties["Target"];
                if (entity.Name != Microsoft.Crm.SdkTypeProxy.EntityName.account.ToString()) { return; }
            }
            else
            {
                return;
            }
            ICrmService serv = context.CreateCrmService(true);
            TargetRetrieveDynamic targetRetrieve = new TargetRetrieveDynamic();

            // Set the properties of the target.
            targetRetrieve.EntityName = entity.Name;
            targetRetrieve.EntityId = entity.Id;

            // Create the request object.
            RetrieveRequest retrieve = new RetrieveRequest();

            // Set the properties of the request object.
            ColumnSet cols = new ColumnSet();
            cols.Attributes.Add("customertypecode");
            cols.Attributes.Add("name");

            retrieve.Target = targetRetrieve;
            retrieve.ColumnSet = cols;

            // Indicate that the BusinessEntity should be retrieved as a DynamicEntity.
            retrieve.ReturnDynamicEntities = true;

            // Execute the request
            RetrieveResponse retrieved = (RetrieveResponse)serv.Execute(retrieve);

            DynamicEntity retrievedentity = (DynamicEntity)retrieved.BusinessEntity;

            if (retrievedentity.Properties["customertypecode"] != null)
            {
                if (((Picklist)retrievedentity.Properties["customertypecode"]).name == "Клиент")
                {
                    // Create a QueryExpression.
                    QueryExpression qe = new QueryExpression();
                    qe.EntityName = "role";
                    qe.ColumnSet = new AllColumns();

                    // Set up the join between the role entity
                    // and the intersect table systemuserroles.
                    LinkEntity le = new LinkEntity();
                    le.LinkFromEntityName = "role";
                    le.LinkFromAttributeName = "roleid";
                    le.LinkToEntityName = "systemuserroles";
                    le.LinkToAttributeName = "roleid";

                    // Set up the join between the intersect table
                    // systemuserroles and the systemuser entity.
                    LinkEntity le2 = new LinkEntity();
                    le2.LinkFromEntityName = "systemuserroles";
                    le2.LinkFromAttributeName = "systemuserid";
                    le2.LinkToEntityName = "systemuser";
                    le2.LinkToAttributeName = "systemuserid";

                    // The condition is to find the user ID.
                    //Microsoft.Crm.Sdk.Query.c
                    ConditionExpression ce = new ConditionExpression();
                    ce.AttributeName = "systemuserid";
                    ce.Operator = ConditionOperator.Equal;
                    ce.Values = new object[] { context.UserId };

                    FilterExpression fe = new FilterExpression();
                    fe.AddCondition(ce); 
                    le2.LinkCriteria = fe;
                    le.LinkEntities.Add(le2);
                    qe.LinkEntities.Add(le); 

                    // Execute the query.
                    BusinessEntityCollection bec = serv.RetrieveMultiple(qe);

                    string rolename = "Системный администратор";
                    bool roleFound = false;
                    if (bec.BusinessEntities.Count > 0)
                    {
                        for (int i = 0; i < bec.BusinessEntities.Count; i++)
                        {
                            if (rolename == ((role)bec.BusinessEntities[i]).name)
                                roleFound = true;
                        }
                    }

                    if (!roleFound)
                    {
                        throw new InvalidPluginExecutionException("У вас нет достаточных прав на просмотр данных о клиенте.");
                    }
                }
            }
        }