Beispiel #1
0
        public static TrashBag BagThis(GenericContent node)
        {
            var bin = TrashBin.Instance;

            if (bin == null)
            {
                return(null);
            }

            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            //creating a bag has nothing to do with user permissions: Move will handle that
            TrashBag bag            = null;
            var      wsId           = 0;
            var      wsRelativePath = string.Empty;
            var      ws             = node.Workspace;

            if (ws != null)
            {
                wsId           = ws.Id;
                wsRelativePath = node.Path.Substring(ws.Path.Length);
            }

            using (new SystemAccount())
            {
                bag = new TrashBag(bin)
                {
                    KeepUntil             = DateTime.UtcNow.AddDays(bin.MinRetentionTime),
                    OriginalPath          = RepositoryPath.GetParentPath(node.Path),
                    WorkspaceRelativePath = wsRelativePath,
                    WorkspaceId           = wsId,
                    DisplayName           = node.DisplayName,
                    Link = node
                };
                bag.Save();

                CopyPermissions(node, bag);

                //add delete permission for the owner
                //bag.Security.SetPermission(User.Current, true, PermissionType.Delete, PermissionValue.Allow);
            }

            try
            {
                Node.Move(node.Path, bag.Path);
            }
            catch (Exception ex)
            {
                Logger.WriteException(ex);

                bag.Destroy();

                throw new InvalidOperationException("Error moving item to the trash", ex);
            }

            return(bag);
        }
Beispiel #2
0
        public static bool DeleteNode(GenericContent n)
        {
            if (Instance != null && Instance.IsActive && n.IsTrashable)
            {
                if (Instance.BagCapacity > 0 && n.NodesInTree > Instance.BagCapacity)
                {
                    throw new ApplicationException("Node tree size exceeds trash bag limit, use ForceDelete to purge physically.");
                }

                TrashBag.BagThis(n);
            }
            else
            {
                ForceDelete(n);
            }
            return(true);
        }
Beispiel #3
0
        public static void Restore(TrashBag trashBag, string targetPath, bool addNewName)
        {
            if (trashBag == null || string.IsNullOrEmpty(targetPath))
            {
                throw new RestoreException(RestoreResultType.Nonedefined);
            }

            targetPath = targetPath.TrimEnd(new [] { '/' });

            var node = trashBag.DeletedContent;

            if (node == null)
            {
                throw new InvalidOperationException("TrashBag is empty");
            }

            var targetContentPath = RepositoryPath.Combine(targetPath, node.Name);
            var targetParent      = Node.Load <GenericContent>(targetPath);

            if (targetParent == null)
            {
                throw new RestoreException(RestoreResultType.NoParent,
                                           RepositoryPath.GetParentPath(targetPath));
            }

            //assert permissions
            if (!targetParent.Security.HasPermission(PermissionType.Open))
            {
                throw new RestoreException(RestoreResultType.PermissionError, targetContentPath);
            }

            //target type check: ContentTypes field
            AssertRestoreContentType(targetParent, node);

            if (Node.Exists(targetContentPath))
            {
                var newName = ContentNamingHelper.IncrementNameSuffixToLastName(node.Name, targetParent.Id);
                targetContentPath = RepositoryPath.Combine(targetPath, newName);

                if (addNewName)
                {
                    try
                    {
                        //there is no other way right now (rename and move cannot be done at the same time)
                        node.Name = newName;
                        node.Save();
                    }
                    catch (SecurityException ex)
                    {
                        Logger.WriteException(ex);
                        throw new RestoreException(RestoreResultType.PermissionError,
                                                   targetContentPath, ex);
                    }
                    catch (Exception ex)
                    {
                        Logger.WriteException(ex);
                        throw new RestoreException(RestoreResultType.UnknownError,
                                                   targetContentPath, ex);
                    }
                }
                else
                {
                    throw new RestoreException(RestoreResultType.ExistingName,
                                               targetContentPath);
                }
            }

            var originalUser = User.Current;

            try
            {
                node.MoveTo(targetParent);

                AccessProvider.Current.SetCurrentUser(User.Administrator);

                trashBag.KeepUntil = DateTime.Today.AddDays(-1);
                trashBag.ForceDelete();
            }
            catch (SecurityException ex)
            {
                Logger.WriteException(ex);
                throw new RestoreException(RestoreResultType.PermissionError,
                                           targetContentPath, ex);
            }
            catch (Exception ex)
            {
                Logger.WriteException(ex);
                throw new RestoreException(RestoreResultType.UnknownError,
                                           targetContentPath, ex);
            }
            finally
            {
                AccessProvider.Current.SetCurrentUser(originalUser);
            }
        }
Beispiel #4
0
 public static void Restore(TrashBag trashBag, string targetPath)
 {
     Restore(trashBag, targetPath, false);
 }
Beispiel #5
0
 public static void Restore(TrashBag trashBag, bool addNewName)
 {
     Restore(trashBag, trashBag.OriginalPath, addNewName);
 }
Beispiel #6
0
 public static void Restore(TrashBag trashBag)
 {
     Restore(trashBag, trashBag.OriginalPath, false);
 }