Skip to content

Instantly share code, notes, and snippets.

@cab1729
Created July 20, 2012 19:20
Show Gist options
  • Save cab1729/3152698 to your computer and use it in GitHub Desktop.
Save cab1729/3152698 to your computer and use it in GitHub Desktop.
C# Site Map provider example - displays available user options based on security roles permissions
using System;
using System.Data;
using System.Configuration;
using System.Security.Permissions;
using System.Web;
using System.Web.Security;
// BO - Business Objects (Entities)
using Customer.BO;
using Customer.Services;
using Customer.Framework.Logging;
namespace Customer.Services
{
/**
* Site Map provider sample - displays available user options based on security roles permissions
**/
[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
public class AppSiteMapProvider : StaticSiteMapProvider
{
private SiteMapNode rootNode = null;
// App Service layer objects used for security trimming
// Some basic state to help track the initialization state of the provider.
private bool initialized = false;
private Logger logger = new Logger(typeof(AppSiteMapProvider));
/*
* TODO : Add Comments
*
*/
public virtual bool IsInitialized
{
get
{
return initialized;
}
}
/*
* TODO : Add Comments
*
*/
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection attributes)
{
if (IsInitialized)
return;
base.Initialize(name, attributes);
initialized = true;
}
/*
* Return the root node of the current site map.
*
*/
public override SiteMapNode RootNode
{
get
{
SiteMapNode temp = null;
temp = BuildSiteMap();
return temp;
}
}
/*
* TODO : Add Comments
*
*/
protected override SiteMapNode GetRootNodeCore()
{
return RootNode;
}
/*
* Clean up any collections or other state that an instance of this may hold.
*
*/
protected override void Clear()
{
lock (this)
{
rootNode = null;
base.Clear();
}
}
/*
* TODO : Add Comments
*
*/
public override SiteMapNode BuildSiteMap()
{
// prevent other threads from modifying the sitemap
lock (this)
{
// Start with a clean slate
Clear();
int rootNodeId = 0;
// create the default root node
rootNode =
new SiteMapNode(
this, rootNodeId.ToString(), "~", "Home");
// build the sitemap for the authenticated user
SessionParamBO m_SessionParamBO =
(SessionParamBO)HttpContext.Current.Session["SessionParamBO"];
UserBO m_UserBO = m_SessionParamBO.UserBO;
string roleId = "*"; // default - get all tasks
if (null != m_UserBO)
{
roleId = m_UserBO.RoleId;
if (null == roleId)
{
roleId = "*";
}
}
BOCollection<TaskBO> taskList =
(BOCollection<TaskBO>)HttpContext.Current.Session["UserTaskList"];
if (null == taskList)
{
taskList = ServiceFactoryAccess.RoleTaskService.getTaskList(roleId);
HttpContext.Current.Session["UserTaskList"] = taskList;
}
foreach (TaskBO task in taskList)
{
SiteMapNode taskNode =
new SiteMapNode(this, task.TaskId, task.TaskURL, task.TaskDescription);
if (task.Parent)
{
foreach (TaskBO child in task.ChildTasks)
{
SiteMapNode taskLeaf =
new SiteMapNode(
this, child.TaskId, child.TaskURL, child.TaskDescription);
AddNode(taskLeaf, taskNode);
}
}
AddNode(taskNode, rootNode);
}
return rootNode;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment