Exemplo n.º 1
0
        private static List <Tuple <string, string> > RemoveFiles(ICommandContext commandContext)
        {
            var errors = new List <Tuple <string, string> >();

            var targetHashes = (commandContext.Parameters["targets[]"] ?? string.Empty).Split(',');

            if (!targetHashes.Any())
            {
                return(errors);
            }

            var portal              = commandContext.CreatePortalContext();
            var website             = portal.Website.ToEntityReference();
            var security            = commandContext.CreateSecurityProvider();
            var dataServiceProvider = commandContext.CreateDependencyProvider().GetDependency <ICmsDataServiceProvider>();

            foreach (var targetHash in targetHashes)
            {
                var serviceContext = commandContext.CreateServiceContext();

                Entity target;

                if (!TryGetTargetEntity(serviceContext, targetHash, website, out target))
                {
                    errors.Add(new Tuple <string, string>(targetHash, ResourceManager.GetString("Unable_To_Retrieve_Target_Entity_For_Given_Hash_Error")));

                    continue;
                }

                try
                {
                    OrganizationServiceContextInfo serviceContextInfo;
                    EntitySetInfo entitySetInfo;

                    if (dataServiceProvider != null &&
                        OrganizationServiceContextInfo.TryGet(serviceContext.GetType(), out serviceContextInfo) &&
                        serviceContextInfo.EntitySetsByEntityLogicalName.TryGetValue(target.LogicalName, out entitySetInfo))
                    {
                        dataServiceProvider.DeleteEntity(serviceContext, entitySetInfo.Property.Name, target.Id);
                    }
                    else
                    {
                        if (!security.TryAssert(serviceContext, target, CrmEntityRight.Change))
                        {
                            errors.Add(new Tuple <string, string>(GetDisplayName(target), ResourceManager.GetString("Delete_Permission_Denied_For_Target_Entity_Error")));

                            continue;
                        }

                        CrmEntityInactiveInfo inactiveInfo;

                        if (CrmEntityInactiveInfo.TryGetInfo(target.LogicalName, out inactiveInfo))
                        {
                            serviceContext.SetState(inactiveInfo.InactiveState, inactiveInfo.InactiveStatus, target);
                        }
                        else
                        {
                            serviceContext.DeleteObject(target);
                            serviceContext.SaveChanges();
                        }
                    }
                }
                catch (Exception e)
                {
                    ADXTrace.Instance.TraceError(TraceCategory.Application, string.Format("{0} {1}", ResourceManager.GetString("Deleting_File_Exception"), e.ToString()));
                    errors.Add(new Tuple <string, string>(GetDisplayName(target), e.Message));
                }
            }

            return(errors);
        }
Exemplo n.º 2
0
        public CommandResponse GetResponse(ICommandContext commandContext)
        {
            var fileSystem = commandContext.CreateFileSystem();

            var hash = commandContext.Parameters["current"];

            DirectoryContentHash cwd;

            if (!DirectoryContentHash.TryParse(hash, out cwd))
            {
                return(new ErrorCommandResponse
                {
                    error = ResourceManager.GetString("Unable_To_Retrieve_Current_Directory_For_Upload_Error")
                });
            }

            DirectoryUploadInfo cwdInfo;

            try
            {
                cwdInfo = fileSystem.Using(cwd, fs => new DirectoryUploadInfo(fs.Current));
            }
            catch (InvalidOperationException)
            {
                return(new ErrorCommandResponse
                {
                    error = ResourceManager.GetString("Unable_To_Retrieve_Current_Directory_For_Upload_Error")
                });
            }

            if (!(cwdInfo.SupportsUpload && cwdInfo.CanWrite))
            {
                return(new ErrorCommandResponse
                {
                    error = ResourceManager.GetString("Upload_Permission_Denied_For_Current_Directory_Error")
                });
            }

            var files = GetUploadedFiles(commandContext.Files);

            if (!files.Any())
            {
                return(new ErrorCommandResponse
                {
                    error = "No valid files were uploaded"
                });
            }

            var portal          = commandContext.CreatePortalContext();
            var publishingState = GetPublishingState(portal, cwdInfo.Entity);

            if (publishingState == null)
            {
                return(new ErrorCommandResponse
                {
                    error = ResourceManager.GetString("Unable_To_Retrieve_Current_Directory_For_Upload_Error")
                });
            }

            List <string> @select;
            List <Tuple <string, string> > errors;

            CreateFiles(commandContext, cwdInfo, files, publishingState, out @select, out errors);

            try
            {
                return(fileSystem.Using(cwd, fs => GetResponse(commandContext, fs, @select, errors)));
            }
            catch (InvalidOperationException)
            {
                return(new ErrorCommandResponse
                {
                    error = ResourceManager.GetString("Unable_To_Retrieve_Current_Directory_For_Upload_Error")
                });
            }
        }