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); }
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); }
/// <summary> /// Returns a new <see cref="TrashBag"/> instance that packages the /// given <see cref="GenericContent"/> instance. /// </summary> /// <param name="node">The <see cref="GenericContent"/> instance that will be wrapped.</param> 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 currentUserId = User.Current.Id; var wsId = 0; var wsRelativePath = string.Empty; var ws = SystemAccount.Execute(() => 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, Owner = node.Owner }; bag.Save(); CopyPermissions(node, bag); // Add Delete permission for the owner to let them remove it later and also // AddNew permission to let the move operation below actually move // the content into the TrashBag. Providers.Instance.SecurityHandler.CreateAclEditor() .Allow(bag.Id, node.OwnerId, false, PermissionType.Delete, PermissionType.AddNew) .Allow(bag.Id, currentUserId, true, PermissionType.Delete, PermissionType.AddNew) .Apply(); } try { Node.Move(node.Path, bag.Path); } catch (SenseNetSecurityException ex) { SnLog.WriteException(ex); bag.Destroy(); if (ex.Data.Contains("PermissionType") && (string)ex.Data["PermissionType"] == "Delete") { throw new InvalidOperationException("You do not have enough permissions to delete this content to the Trash.", ex); } throw new InvalidOperationException("Error moving item to the trash", ex); } catch (Exception ex) { SnLog.WriteException(ex); bag.Destroy(); throw new InvalidOperationException("Error moving item to the trash", ex); } return(bag); }
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 (SenseNetSecurityException 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 (SenseNetSecurityException 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); } }
public static void Restore(TrashBag trashBag, string targetPath) { Restore(trashBag, targetPath, false); }
public static void Restore(TrashBag trashBag, bool addNewName) { Restore(trashBag, trashBag.OriginalPath, addNewName); }
public static void Restore(TrashBag trashBag) { Restore(trashBag, trashBag.OriginalPath, false); }
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; using (new SystemAccount()) { bag = new TrashBag(bin) { KeepUntil = DateTime.Now.AddDays(bin.MinRetentionTime), OriginalPath = RepositoryPath.GetParentPath(node.Path), 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; }
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 (SenseNetSecurityException 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 (SenseNetSecurityException 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); } }
//====================================================================== Helper methods private void RedirectSafely(TrashBag bag, string targetPath) { if (bag == null) { CallDone(); return; } var bagName = bag.Name; var back = PortalContext.Current.BackUrl; if (string.IsNullOrEmpty(back)) { HttpContext.Current.Response.Redirect(TrashBin.TrashBinPath); } if (back.Contains(bagName)) { //Redirect to the original location HttpContext.Current.Response.Redirect(targetPath); //ALTERNATIVE BEHAVIOR: redirect to the back url that is stored in the current back url... //if (back.Contains(PortalContext.BackUrlParamName)) //{ //var nextBack = back.Substring(back.IndexOf(PortalContext.BackUrlParamName + "=")).Remove(0, PortalContext.BackUrlParamName.Length + 1); //if (nextBack.Contains("&")) // nextBack = nextBack.Remove(nextBack.IndexOf("&")); //HttpContext.Current.Response.Redirect(HttpUtility.UrlDecode(nextBack)); //} //else //{ // HttpContext.Current.Response.Redirect(TrashBin.TrashBinPath); //} } else { CallDone(); } }