Skip to content

Instantly share code, notes, and snippets.

@iamandycohen
Created July 8, 2016 12:07
Show Gist options
  • Save iamandycohen/49efcfee64842603ca29019667d57b1a to your computer and use it in GitHub Desktop.
Save iamandycohen/49efcfee64842603ca29019667d57b1a to your computer and use it in GitHub Desktop.
<%@ Page Language="C#" AutoEventWireup="true" Debug="true" Inherits="Sitecore.sitecore.admin.AdminPage" %>
<%@ Import Namespace="System.Linq" %>
<%--
#############################################################
Author: Sean Kearney
Date: 10/04/09
Description: View security set on items
Overview:
#############################################################
--%>
<script runat="server">
protected override void OnInit(EventArgs e)
{
base.CheckSecurity(true); //Required!
base.OnInit(e);
}
protected string StartingPath
{
get
{
return txtStartPath.Text;
}
}
protected string OutputString = string.Empty;
protected void btnReport_Click(object sender, EventArgs e)
{
this.Execute(this.OutputIfAnySecurity);
}
protected void btnDelete_Click(object sender, EventArgs e)
{
this.Execute(this.ResetSecurity, this.OldNewSecurityHeader, this.NoSecurityChangesMessage);
}
protected void btnNonStandardReport_Click(object sender, EventArgs e)
{
this.Execute(this.OutputIfNonStandardSecurity);
}
protected void btnRemoveSecurity_Click(object sender, EventArgs e)
{
this.Execute(this.SetSecurityToEmpty, OldNewSecurityHeader);
}
protected void Execute(Action<Sitecore.Data.Items.Item> processItem, Action showHeader = null, Action showEmptyMessage = null)
{
if (string.IsNullOrEmpty(StartingPath))
throw new Exception("Starting Path Not Set");
var dbName = rdMaster.Checked ? "master" : "web";
var db = Sitecore.Configuration.Factory.GetDatabase(dbName);
OutputString += "<style type='text/css'>*{font-family:monospace;font-size:10pt;}</style><table border=1>";
if (showHeader != null)
{
showHeader();
}
using (new Sitecore.SecurityModel.SecurityDisabler())
{
this.ShowEmptyMessage = true;
var item = db.GetItem(StartingPath);
if (item != null)
{
Recurse(item, this.rdExcludeRoot.Checked, processItem);
}
}
if(this.ShowEmptyMessage && showEmptyMessage != null)
{
showEmptyMessage();
}
Response.Write("</table>");
}
private bool ShowEmptyMessage { get; set; }
private void Recurse(Sitecore.Data.Items.Item item, bool ignoreItem, Action<Sitecore.Data.Items.Item> processItem)
{
if (!ignoreItem)
{
processItem(item);
}
foreach (Sitecore.Data.Items.Item child in item.Children)
{
Recurse(child, false, processItem);
}
}
private void NoSecurityChangesMessage()
{
OutputString += "<tr><td nowrap='nowrap' colspan='3'>No security changes were made by resetting items to their standard values.</td></tr>";
}
private void OldNewSecurityHeader()
{
OutputString += "<th>Item Path</th><th>Old Security</th><th>New Security</th>";
}
private void ResetSecurity(Sitecore.Data.Items.Item item)
{
if (item.Fields["__security"].HasValue)
{
this.ShowEmptyMessage = false;
var oldSecurity = item["__security"];
using (new Sitecore.Data.Items.EditContext(item))
{
item.Fields["__security"].Reset();
}
string newSecurity = item["__security"];
OutputString += "<tr><td nowrap='nowrap'>" + item.Paths.ContentPath + "</td><td>" + oldSecurity + "</td><td>" + newSecurity + "</td></tr>";
}
}
private void OutputIfNonStandardSecurity(Sitecore.Data.Items.Item item)
{
if(item.Fields["__security"].HasValue)
{
OutputString += "<tr><td nowrap='nowrap'>" + item.Paths.ContentPath + "</td><td>" + item["__security"] + "</td></tr>";
}
}
private void OutputIfAnySecurity(Sitecore.Data.Items.Item item)
{
if(!string.IsNullOrEmpty(item["__security"]))
{
OutputString += "<tr><td nowrap='nowrap'>" + item.Paths.ContentPath + "</td><td>" + item["__security"] + "</td></tr>";
}
}
private void SetSecurityToEmpty(Sitecore.Data.Items.Item item)
{
var oldSecurity = item["__security"];
using (new Sitecore.Data.Items.EditContext(item))
{
item.Fields["__security"].SetValue(String.Empty, true);
}
string newSecurity = item["__security"];
OutputString += "<tr><td nowrap='nowrap'>" + item.Paths.ContentPath + "</td><td>" + oldSecurity + "</td><td>" + newSecurity + "</td></tr>";
}
protected void btnGetItemsInDraft_Click (object sender, EventArgs e)
{
GetDraftItems();
}
private class ItemInfo
{
public string Guid { get; set; }
public string Name { get; set; }
}
private List<ItemInfo> draftItems;
private Sitecore.Data.ID tanksOfThanksId = new Sitecore.Data.ID("{8DE157E3-8880-4231-9A6F-CA3A3A0078B5}");
private Sitecore.Data.ID doNotPublishId = new Sitecore.Data.ID("{24F505AE-1BB3-48A1-934B-6B9A336CAE82}");
private Sitecore.Data.ID newsArticlesId = new Sitecore.Data.ID("{924DEFF9-F935-4CFA-AD65-52B0EAD0551C}");
private void GetDraftItems()
{
var contentNodeId = new Sitecore.Data.ID("{0DE95AE4-41AB-4D01-9EB0-67441B7C2450}");
this.draftItems = new List<ItemInfo>();
var contentItem = Sitecore.Data.Database.GetDatabase("master").GetItem(contentNodeId);
CheckIfItemAndChildrenAreDraft(contentItem);
OutputString = string.Empty;
foreach (var item in this.draftItems)
{
OutputString += string.Format("{0} - {1} <br />", item.Guid, item.Name);
}
}
private void CheckIfItemAndChildrenAreDraft(Sitecore.Data.Items.Item item)
{
// I don't care about the Do Not Publish folder, and tanks of thanks and news are huge, so lets just skip them
if (item.ID == this.tanksOfThanksId
|| item.ID == this.doNotPublishId
|| item.ID == this.newsArticlesId)
{
return;
}
var hasVersionInFinalWorkflow = false;
foreach (var version in item.Versions.GetVersions(false))
{
if (version.State.GetWorkflowState() == null || version.State.GetWorkflowState().FinalState)
{
hasVersionInFinalWorkflow = true;
}
}
if (!hasVersionInFinalWorkflow)
{
this.draftItems.Add(new ItemInfo
{
Guid = item.ID.ToString(),
Name = item.Paths.FullPath
});
}
foreach (Sitecore.Data.Items.Item child in item.GetChildren())
{
this.CheckIfItemAndChildrenAreDraft(child);
}
}
</script>
<html>
<body>
<form id="form1" runat="server">
<fieldset>
<asp:RadioButton runat="server" ID="rdMaster" Text="Master" GroupName="grpDatasbase" Checked="true" /><br />
<asp:RadioButton runat="server" ID="rdWeb" Text="Web" GroupName="grpDatasbase" /><br />
Starting Path: <br />
<asp:TextBox ID="txtStartPath" runat="server" Text="/sitecore/content/home" Width="250"></asp:TextBox><br />
<asp:RadioButton runat="server" ID="rdIncludeRoot" Text="Include Root Item" GroupName="grpRoot" Checked="true" /><br />
<asp:RadioButton runat="server" ID="rdExcludeRoot" Text="Exclude Root Item" GroupName="grpRoot" /><br />
<br />
<asp:Button ID="btnReport" runat="server" OnClick="btnReport_Click" Text="Report: All Security" /><br />
<asp:Button ID="btnNonStandardReport" runat="server" OnClick="btnNonStandardReport_Click" Text="Report: Items with security that differs from Standard Values" /><br />
<br />
<asp:Button ID="btnDelete" runat="server" Text="Reset '__security' to standard values" OnClick="btnDelete_Click" /><br />
<asp:Button ID="btnRemoveSecurity" runat="server" Text="Set '__security' to String.Empty" OnClick="btnRemoveSecurity_Click" /><br />
</fieldset>
<br />
<fieldset>
<asp:Button ID="btnGetItemsInDraft" runat="server" Text="Get All Items With No Versions In Final Workflow State" OnClick="btnGetItemsInDraft_Click" /><br />
</fieldset>
</form>
<%= OutputString %>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment