From bb70bee6aefc9da4c49449f817d6350921ea9eca Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Wed, 10 Apr 2013 01:42:15 +0600 Subject: [PATCH] Fixes: #U4-2071 and cleans up some code. --- .../umbraco/dialogs/moveOrCopy.aspx.cs | 350 +++++++++--------- .../BasePages/UmbracoEnsuredPage.cs | 9 +- 2 files changed, 174 insertions(+), 185 deletions(-) diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/dialogs/moveOrCopy.aspx.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/dialogs/moveOrCopy.aspx.cs index 3bd289e1e0..24f1f5f088 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/dialogs/moveOrCopy.aspx.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/dialogs/moveOrCopy.aspx.cs @@ -9,11 +9,11 @@ using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Xml; +using Umbraco.Core.IO; using umbraco.cms.helpers; using umbraco.BasePages; using umbraco.presentation; using umbraco.cms.businesslogic.media; -using umbraco.IO; using System.Linq; using umbraco.cms.businesslogic; using umbraco.cms.presentation.user; @@ -28,7 +28,14 @@ namespace umbraco.dialogs public partial class moveOrCopy : UmbracoEnsuredPage { - protected void Page_Load(object sender, System.EventArgs e) + protected override void OnInit(EventArgs e) + { + CurrentApp = Request["app"]; + + base.OnInit(e); + } + + protected void Page_Load(object sender, EventArgs e) { JTree.DataBind(); @@ -37,41 +44,36 @@ namespace umbraco.dialogs { pp_relate.Text = ui.Text("moveOrCopy", "relateToOriginal"); - //Document Type copy Hack... - string app = helper.Request("app"); + //Document Type copy Hack... - if (app == "settings") { + if (CurrentApp == "settings") + { pane_form.Visible = false; pane_form_notice.Visible = false; - - - pane_settings.Visible = true; ok.Text = ui.Text("general", "ok", this.getUser()); ok.Attributes.Add("style", "width: 60px"); - cms.businesslogic.web.DocumentType dt = new umbraco.cms.businesslogic.web.DocumentType(int.Parse(helper.Request("id"))); + var dt = new cms.businesslogic.web.DocumentType(int.Parse(helper.Request("id"))); //Load master types... masterType.Attributes.Add("style", "width: 350px;"); masterType.Items.Add(new ListItem(ui.Text("none") + "...", "0")); - foreach (cms.businesslogic.web.DocumentType docT in cms.businesslogic.web.DocumentType.GetAllAsList()) { + foreach (cms.businesslogic.web.DocumentType docT in cms.businesslogic.web.DocumentType.GetAllAsList()) + { masterType.Items.Add(new ListItem(docT.Text, docT.Id.ToString())); } masterType.SelectedValue = dt.MasterContentType.ToString(); - //hack to close window if not a doctype... - if (dt == null) { - Response.Write(""); - } else { - rename.Text = dt.Text + " (copy)"; - pane_settings.Text = "Make a copy of the document type '" + dt.Text + "' and save it under a new name"; - } + rename.Text = dt.Text + " (copy)"; + pane_settings.Text = "Make a copy of the document type '" + dt.Text + "' and save it under a new name"; - } else { + } + else + { pane_form.Visible = true; pane_form_notice.Visible = true; @@ -83,28 +85,30 @@ namespace umbraco.dialogs ok.Attributes.Add("style", "width: 60px"); ok.Attributes.Add("disabled", "true"); - - string currentPath = ""; - CMSNode d = new CMSNode(int.Parse(helper.Request("id"))); - foreach (string s in d.Path.Split(',')) { + + var currentPath = ""; + var d = new CMSNode(int.Parse(helper.Request("id"))); + foreach (var s in d.Path.Split(',')) + { if (int.Parse(s) > 0) currentPath += "/" + new CMSNode(int.Parse(s)).Text; } - - // - - bool validAction = true; + + var validAction = true; // only validate permissions in content - if (app == "content" && d.HasChildren) + if (CurrentApp == "content" && d.HasChildren) { validAction = ValidAction(helper.Request("mode") == "cut" ? 'M' : 'O'); } - if (helper.Request("mode") == "cut") { + if (helper.Request("mode") == "cut") + { pane_form.Text = ui.Text("moveOrCopy", "moveTo", d.Text, base.getUser()); pp_relate.Visible = false; - } else { + } + else + { pane_form.Text = ui.Text("moveOrCopy", "copyTo", d.Text, base.getUser()); pp_relate.Visible = true; } @@ -119,192 +123,194 @@ namespace umbraco.dialogs } - private bool ValidAction(char actionLetter) + private static bool ValidAction(char actionLetter) { - CMSNode d = new CMSNode(int.Parse(helper.Request("id"))); - IAction currentAction = umbraco.BusinessLogic.Actions.Action.GetPermissionAssignable().Where(a => a.Letter == actionLetter).First(); + var d = new CMSNode(int.Parse(helper.Request("id"))); + var currentAction = BusinessLogic.Actions.Action.GetPermissionAssignable().First(a => a.Letter == actionLetter); return CheckPermissions(d, currentAction,actionLetter); } - private bool CheckPermissions(CMSNode node, IAction currentAction, char actionLetter) - { - - UserPermissions currUserPermissions = new UserPermissions(UmbracoEnsuredPage.CurrentUser); - List lstCurrUserActions = currUserPermissions.GetExistingNodePermission(node.Id); + private static bool CheckPermissions(CMSNode node, IAction currentAction, char actionLetter) + { + var currUserPermissions = new UserPermissions(CurrentUser); + var lstCurrUserActions = currUserPermissions.GetExistingNodePermission(node.Id); if (!lstCurrUserActions.Contains(currentAction)) return false; if (node.HasChildren) { - foreach (CMSNode c in node.Children) - if (!CheckPermissions(c,currentAction,actionLetter)) - return false; + return node.Children.Cast().All(c => CheckPermissions(c, currentAction, actionLetter)); } return true; } + //PPH moving multiple nodes and publishing them aswell. - private void handleChildNodes(cms.businesslogic.web.Document d) { - //store children array here because iterating over an Array object is very inneficient. - var c = d.Children; - foreach (cms.businesslogic.web.Document cd in c) + private static void HandleChildNodes(cms.businesslogic.web.Document d) + { + var c = d.Children; + foreach (var cd in c) + { + if (cd.Published) + { + cd.Publish(new BusinessLogic.User(0)); + //using library.publish to support load balancing. + library.UpdateDocumentCache(cd); + + + if (cd.HasChildren) + { + HandleChildNodes(cd); + } + } + } + } + + //PPH Handle doctype copies.. + private void HandleDocumentTypeCopy() + { + + var eDt = new cms.businesslogic.web.DocumentType(int.Parse(helper.Request("id"))); + + var alias = rename.Text; + var dt = cms.businesslogic.web.DocumentType.MakeNew(getUser(), alias.Replace("'", "''")); + + dt.IconUrl = eDt.IconUrl; + dt.Thumbnail = eDt.Thumbnail; + dt.Description = eDt.Description; + dt.allowedTemplates = eDt.allowedTemplates; + dt.DefaultTemplate = eDt.DefaultTemplate; + dt.AllowedChildContentTypeIDs = eDt.AllowedChildContentTypeIDs; + + dt.MasterContentType = int.Parse(masterType.SelectedValue); + + var oldNewTabIds = new Hashtable(); + foreach (var tab in eDt.getVirtualTabs.ToList()) { - if (cd.Published) { - cd.Publish(new umbraco.BusinessLogic.User(0)); - //using library.publish to support load balancing. - umbraco.library.UpdateDocumentCache(cd); + if (tab.ContentType == eDt.Id) + { + var tId = dt.AddVirtualTab(tab.Caption); + oldNewTabIds.Add(tab.Id, tId); + } + } + foreach (var pt in eDt.PropertyTypes) + { + if (pt.ContentTypeId == eDt.Id) + { + var nPt = cms.businesslogic.propertytype.PropertyType.MakeNew(pt.DataTypeDefinition, dt, pt.Name, pt.Alias); + nPt.ValidationRegExp = pt.ValidationRegExp; + nPt.SortOrder = pt.SortOrder; + nPt.Mandatory = pt.Mandatory; + nPt.Description = pt.Description; - if (cd.HasChildren) { - handleChildNodes(cd); + if (pt.TabId > 0 && oldNewTabIds[pt.TabId] != null) + { + var newTabId = (int)oldNewTabIds[pt.TabId]; + nPt.TabId = newTabId; } } } - } - //PPH Handle doctype copies.. - private void HandleDocumentTypeCopy() { + var returnUrl = SystemDirectories.Umbraco + "/settings/editNodeTypeNew.aspx?id=" + dt.Id.ToString(); - cms.businesslogic.web.DocumentType eDt = new umbraco.cms.businesslogic.web.DocumentType(int.Parse(helper.Request("id"))); - - //Documentype exists.. create new doc type... - if (eDt != null) { - string Alias = rename.Text; - cms.businesslogic.web.DocumentType dt = cms.businesslogic.web.DocumentType.MakeNew(base.getUser(), Alias.Replace("'", "''")); - - dt.IconUrl = eDt.IconUrl; - dt.Thumbnail = eDt.Thumbnail; - dt.Description = eDt.Description; - dt.allowedTemplates = eDt.allowedTemplates; - dt.DefaultTemplate = eDt.DefaultTemplate; - dt.AllowedChildContentTypeIDs = eDt.AllowedChildContentTypeIDs; - - dt.MasterContentType = int.Parse(masterType.SelectedValue); - - Hashtable oldNewTabIds = new Hashtable(); - foreach (cms.businesslogic.web.DocumentType.TabI tab in eDt.getVirtualTabs.ToList()) - { - if (tab.ContentType == eDt.Id) - { - int tId = dt.AddVirtualTab(tab.Caption); - oldNewTabIds.Add(tab.Id, tId); - } - } - - foreach (cms.businesslogic.propertytype.PropertyType pt in eDt.PropertyTypes) { - - if (pt.ContentTypeId == eDt.Id) - { - cms.businesslogic.propertytype.PropertyType nPt = umbraco.cms.businesslogic.propertytype.PropertyType.MakeNew(pt.DataTypeDefinition, dt, pt.Name, pt.Alias); - nPt.ValidationRegExp = pt.ValidationRegExp; - nPt.SortOrder = pt.SortOrder; - nPt.Mandatory = pt.Mandatory; - nPt.Description = pt.Description; - - if (pt.TabId > 0 && oldNewTabIds[pt.TabId] != null) - { - int newTabId = (int)oldNewTabIds[pt.TabId]; - nPt.TabId = newTabId; - } - } - } - - string returnUrl = SystemDirectories.Umbraco + "/settings/editNodeTypeNew.aspx?id=" + dt.Id.ToString(); - - dt.Save(); + dt.Save(); - pane_settings.Visible = false; - panel_buttons.Visible = false; + pane_settings.Visible = false; + panel_buttons.Visible = false; - feedback.Text = "Document type copied"; - feedback.type = umbraco.uicontrols.Feedback.feedbacktype.success; + feedback.Text = "Document type copied"; + feedback.type = uicontrols.Feedback.feedbacktype.success; - ClientTools.ChangeContentFrameUrl(returnUrl); - - } - } - - public void HandleMoveOrCopy(object sender, EventArgs e) { - if (UmbracoContext.Current.Request["app"] == "settings") - HandleDocumentTypeCopy(); - else - HandleDocumentMoveOrCopy(); - } + ClientTools.ChangeContentFrameUrl(returnUrl); + } - protected override void OnPreRender(EventArgs e) { - base.OnPreRender(e); + public void HandleMoveOrCopy(object sender, EventArgs e) + { + if (Request["app"] == "settings") + HandleDocumentTypeCopy(); + else + HandleDocumentMoveOrCopy(); + } - ScriptManager.GetCurrent(Page).Services.Add(new ServiceReference("../webservices/cmsnode.asmx")); - ScriptManager.GetCurrent(Page).Services.Add(new ServiceReference("../webservices/legacyAjaxCalls.asmx")); - } + protected override void OnPreRender(EventArgs e) + { + base.OnPreRender(e); + ScriptManager.GetCurrent(Page).Services.Add(new ServiceReference("../webservices/cmsnode.asmx")); + ScriptManager.GetCurrent(Page).Services.Add(new ServiceReference("../webservices/legacyAjaxCalls.asmx")); + } - private void HandleDocumentMoveOrCopy() + private void HandleDocumentMoveOrCopy() { if (helper.Request("copyTo") != "" && helper.Request("id") != "") { // Check if the current node is allowed at new position - bool nodeAllowed = false; + var nodeAllowed = false; - cms.businesslogic.Content currentNode = new cms.businesslogic.Content(int.Parse(helper.Request("id"))); - int oldParent = -1; + var currentNode = new cms.businesslogic.Content(int.Parse(helper.Request("id"))); + var oldParent = -1; if (currentNode.Level > 1) oldParent = currentNode.Parent.Id; - cms.businesslogic.Content newNode = new cms.businesslogic.Content(int.Parse(helper.Request("copyTo"))); + var newNode = new cms.businesslogic.Content(int.Parse(helper.Request("copyTo"))); // Check on contenttypes - if (int.Parse(helper.Request("copyTo")) == -1) - nodeAllowed = true; - else - { - foreach (int i in newNode.ContentType.AllowedChildContentTypeIDs.ToList()) - if (i == currentNode.ContentType.Id) - { - nodeAllowed = true; - break; - } - if (!nodeAllowed) { - feedback.Text = ui.Text("moveOrCopy", "notAllowedByContentType", base.getUser()); - feedback.type = umbraco.uicontrols.Feedback.feedbacktype.error; - } else { - // Check on paths - if (((string)("," + newNode.Path + ",")).IndexOf("," + currentNode.Id + ",") > -1) { - nodeAllowed = false; - feedback.Text = ui.Text("moveOrCopy", "notAllowedByPath", base.getUser()); - feedback.type = umbraco.uicontrols.Feedback.feedbacktype.error; - } - } - } + if (int.Parse(helper.Request("copyTo")) == -1) + { + nodeAllowed = true; + } + else + { + if (newNode.ContentType.AllowedChildContentTypeIDs.ToList().Any(i => i == currentNode.ContentType.Id)) + { + nodeAllowed = true; + } + if (!nodeAllowed) + { + feedback.Text = ui.Text("moveOrCopy", "notAllowedByContentType", base.getUser()); + feedback.type = uicontrols.Feedback.feedbacktype.error; + } + else + { + // Check on paths + if (("," + newNode.Path + ",").IndexOf("," + currentNode.Id + ",") > -1) + { + nodeAllowed = false; + feedback.Text = ui.Text("moveOrCopy", "notAllowedByPath", getUser()); + feedback.type = uicontrols.Feedback.feedbacktype.error; + } + } + } - if (nodeAllowed) + if (nodeAllowed) { pane_form.Visible = false; pane_form_notice.Visible = false; panel_buttons.Visible = false; - string newNodeCaption = newNode.Id == -1 ? ui.Text(helper.Request("app")) : newNode.Text; + var newNodeCaption = newNode.Id == -1 ? ui.Text(helper.Request("app")) : newNode.Text; string[] nodes = {currentNode.Text, newNodeCaption }; - if (UmbracoContext.Current.Request["mode"] == "cut") + if (Request["mode"] == "cut") { - if (UmbracoContext.Current.Request["app"] == "content") + if (Request["app"] == "content") { //PPH changed this to document instead of cmsNode to handle republishing. - cms.businesslogic.web.Document d = new umbraco.cms.businesslogic.web.Document(int.Parse(helper.Request("id"))); + var d = new cms.businesslogic.web.Document(int.Parse(helper.Request("id"))); d.Move(int.Parse(helper.Request("copyTo"))); if (d.Published) { - d.Publish(new umbraco.BusinessLogic.User(0)); + d.Publish(new BusinessLogic.User(0)); //using library.publish to support load balancing. //umbraco.library.PublishSingleNode(d.Id); - umbraco.library.UpdateDocumentCache(d); + library.UpdateDocumentCache(d); //PPH added handling of load balanced moving of multiple nodes... if (d.HasChildren) { - handleChildNodes(d); + HandleChildNodes(d); } //Using the general Refresh content method instead as it supports load balancing. @@ -315,14 +321,14 @@ namespace umbraco.dialogs } else { - Media m = new Media(int.Parse(UmbracoContext.Current.Request["id"])); - m.Move(int.Parse(UmbracoContext.Current.Request["copyTo"])); + var m = new Media(int.Parse(Request["id"])); + m.Move(int.Parse(Request["copyTo"])); m.XmlGenerate(new XmlDocument()); library.ClearLibraryCacheForMedia(m.Id); } - feedback.Text = ui.Text("moveOrCopy", "moveDone", nodes, base.getUser()) + "

" + ui.Text("closeThisWindow") + ""; - feedback.type = umbraco.uicontrols.Feedback.feedbacktype.success; + feedback.Text = ui.Text("moveOrCopy", "moveDone", nodes, getUser()) + "

" + ui.Text("closeThisWindow") + ""; + feedback.type = uicontrols.Feedback.feedbacktype.success; // refresh tree ClientTools.MoveNode(currentNode.Id.ToString(), newNode.Path); @@ -330,33 +336,15 @@ namespace umbraco.dialogs } else { - cms.businesslogic.web.Document d = new cms.businesslogic.web.Document(int.Parse(helper.Request("id"))); - d.Copy(int.Parse(helper.Request("copyTo")), this.getUser(), RelateDocuments.Checked); - feedback.Text = ui.Text("moveOrCopy", "copyDone", nodes, base.getUser()) + "

" + ui.Text("closeThisWindow") + ""; - feedback.type = umbraco.uicontrols.Feedback.feedbacktype.success; + var d = new cms.businesslogic.web.Document(int.Parse(helper.Request("id"))); + d.Copy(int.Parse(helper.Request("copyTo")), getUser(), RelateDocuments.Checked); + feedback.Text = ui.Text("moveOrCopy", "copyDone", nodes, getUser()) + "

" + ui.Text("closeThisWindow") + ""; + feedback.type = uicontrols.Feedback.feedbacktype.success; ClientTools.CopyNode(currentNode.Id.ToString(), newNode.Path); } } } } - #region Web Form Designer generated code - override protected void OnInit(EventArgs e) - { - // - // CODEGEN: This call is required by the ASP.NET Web Form Designer. - // - InitializeComponent(); - base.OnInit(e); - } - - ///

- /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - } - #endregion } } diff --git a/src/umbraco.businesslogic/BasePages/UmbracoEnsuredPage.cs b/src/umbraco.businesslogic/BasePages/UmbracoEnsuredPage.cs index 16109f91dc..03d5ad81c1 100644 --- a/src/umbraco.businesslogic/BasePages/UmbracoEnsuredPage.cs +++ b/src/umbraco.businesslogic/BasePages/UmbracoEnsuredPage.cs @@ -1,8 +1,9 @@ using System; using System.Linq; +using Umbraco.Core.IO; +using Umbraco.Core.Logging; using umbraco.BusinessLogic; using umbraco.businesslogic.Exceptions; -using umbraco.IO; namespace umbraco.BasePages { @@ -54,7 +55,7 @@ namespace umbraco.BasePages if (permissions.IndexOf(Action) > -1 && (Path.Contains("-20") || ("," + Path + ",").Contains("," + getUser().StartNodeId.ToString() + ","))) return true; - Log.Add(LogTypes.LoginFailure, getUser(), -1, "Insufficient permissions in UmbracoEnsuredPage: '" + Path + "', '" + permissions + "', '" + Action + "'"); + LogHelper.Info("Insufficient permissions in UmbracoEnsuredPage: '" + Path + "', '" + permissions + "', '" + Action + "'"); return false; } @@ -81,7 +82,7 @@ namespace umbraco.BasePages { ensureContext(); - if (!String.IsNullOrEmpty(CurrentApp)) + if (!string.IsNullOrEmpty(CurrentApp)) { if (!ValidateUserApp(CurrentApp)) throw new UserAuthorizationException(String.Format("The current user doesn't have access to the section/app '{0}'", CurrentApp)); @@ -89,7 +90,7 @@ namespace umbraco.BasePages } catch (UserAuthorizationException) { - Log.Add(LogTypes.Error, CurrentUser, -1, String.Format("Tried to access '{0}'", CurrentApp)); + LogHelper.Warn(string.Format("{0} tried to access '{1}'", CurrentUser.Id, CurrentApp)); throw; } catch