示例#1
0
        protected override void ExecuteWorkflowLogic()
        {
            var target = ConvertToEntityReference(Record.Get(Context.ExecutionContext));

            #region Build Sharing Mask

            var rights = AccessRights.None;

            if (ReadAccess.Get(Context.ExecutionContext))
            {
                rights |= AccessRights.ReadAccess;
            }

            if (WriteAccess.Get(Context.ExecutionContext))
            {
                rights |= AccessRights.WriteAccess;
            }

            if (DeleteAccess.Get(Context.ExecutionContext))
            {
                rights |= AccessRights.DeleteAccess;
            }

            if (AppendAccess.Get(Context.ExecutionContext))
            {
                rights |= AccessRights.AppendAccess;
            }

            if (AppendToAccess.Get(Context.ExecutionContext))
            {
                rights |= AccessRights.AppendToAccess;
            }

            if (AssignAccess.Get(Context.ExecutionContext))
            {
                rights |= AccessRights.AssignAccess;
            }

            if (ShareAccess.Get(Context.ExecutionContext))
            {
                rights |= AccessRights.ShareAccess;
            }

            #endregion Build Sharing Mask

            Context.SystemService.Execute(new ModifyAccessRequest()
            {
                PrincipalAccess = new PrincipalAccess()
                {
                    AccessMask = rights,
                    Principal  = User.Get(Context.ExecutionContext)
                },
                Target = target
            });
        }
示例#2
0
        protected override void ExecuteWorkflowLogic()
        {
            var target      = ConvertToEntityReference(Record.Get(Context.ExecutionContext));
            var principal   = Principal;
            var readAccess  = ReadAccess.Get(Context.ExecutionContext);
            var writeAccess = WriteAccess.Get(Context.ExecutionContext);

            //Let's retrieve attributes first
            var retrieveEntityRequest = new RetrieveEntityRequest()
            {
                LogicalName           = target.LogicalName,
                EntityFilters         = EntityFilters.Attributes,
                RetrieveAsIfPublished = true
            };

            var retrieveEntityResponse = (RetrieveEntityResponse)Context.SystemService.Execute(retrieveEntityRequest);

            var fields = Fields.Get(Context.ExecutionContext).ToLowerInvariant().Split(',').ToArray();

            foreach (var field in fields)
            {
                var crmAttribute =
                    retrieveEntityResponse.EntityMetadata.Attributes.FirstOrDefault(a => a.LogicalName == field);

                if (crmAttribute?.IsSecured == null)
                {
                    throw new InvalidPluginExecutionException($"{field} attribute is not available in {target.LogicalName} entity");
                }

                if (!crmAttribute.IsSecured.Value)
                {
                    throw new InvalidPluginExecutionException($"{field} attribute is not secured");
                }

                var queryPOAA = new QueryByAttribute("principalobjectattributeaccess")
                {
                    ColumnSet = new ColumnSet("readaccess", "updateaccess")
                };
                queryPOAA.AddAttributeValue("attributeid", crmAttribute.MetadataId.Value);
                queryPOAA.AddAttributeValue("objectid", target.Id);
                queryPOAA.AddAttributeValue("principalid", principal.Id);

                var poaa = Context.SystemService.RetrieveMultiple(queryPOAA).Entities.FirstOrDefault();

                if (poaa != null)
                {
                    if (readAccess || writeAccess)
                    {
                        poaa["readaccess"]   = readAccess;
                        poaa["updateaccess"] = writeAccess;
                        Context.SystemService.Update(poaa);
                    }
                    else
                    {
                        Context.SystemService.Delete("principalobjectattributeaccess", poaa.Id);
                    }
                }
                else if (readAccess || writeAccess)
                {
                    poaa = new Entity("principalobjectattributeaccess")
                    {
                        ["attributeid"]  = crmAttribute.MetadataId.Value,
                        ["objectid"]     = target,
                        ["readaccess"]   = readAccess,
                        ["updateaccess"] = writeAccess,
                        ["principalid"]  = principal
                    };

                    Context.SystemService.Create(poaa);
                }
            }
        }