Add (real) support for roles

Topics: Developer Forum, User Forum
Mar 29, 2007 at 1:01 PM
We did it this way:

App_Code/WebPages.cs
• Add

//custom: roles
/// <summary>
/// Roles
/// </summary>
public string Roles
{
get { return _data.Roles; }
set { _data.Roles = value; }
}

before
public List<string> SectionIds


• Add
public string Roles; //custom

before
public List<string> Sections;


App_Code/Providers/CustomXmlSitemapProvider.cs
• Add
//custom: check for roles --- BEGIN
if (node.Roles.Count > 0)
{
bool isInRole = false;
for (int i = 0; i < node.Roles.Count; i++)
{
isInRole = isInRole ||
context.User.IsInRole(node.Rolesi.ToString());
}
return isInRole;

}
else
//custom: check for roles --- END

after
//first we create the WebPage for the node
WebPage page = new WebPage(node"pageId");


• Replace
//Administrators have permissions on all nodes, so we can always
return true
if ((context.User != null) &&
(context.User.Identity.IsAuthenticated) && context.User.IsInRole(RoleNames.Administrators.ToString()))
return true;


with

//custom --- BEGIN
if ((context.User != null) && (context.User.Identity.IsAuthenticated)
&& context.User.IsInRole(RoleNames.Administrators.ToString()))
{
if (node"pageId" != null)
{
mypage = new WebPage(node"pageId");

if (!string.IsNullOrEmpty(mypage.Roles))
{
string[] roles = mypage.Roles.Split(new char[] {
',' });

foreach (string role in roles)
{
boBerechtigt = boBerechtigt ||
context.User.IsInRole(role);
}
}
else
{
boBerechtigt = true;
}

return boBerechtigt;
}
else
{
return true;
}
}
//custom --- END


App_Code/SitemapEditor.cs
• Add
//custom: save roles --- BEGIN
if (node.Attributes"roles" == null)
node.Attributes.Append(_sitemap.CreateAttribute("roles"));
node.Attributes"roles".Value = page.Roles;
//custom: save roles --- END

after
//update visible attribute
if (node.Attributes"visible" == null)
node.Attributes.Append(_sitemap.CreateAttribute("visible"));
node.Attributes"visible".Value = page.Visible.ToString();


Administration/Navigation.aspx.cs
• Add
bool boBerechtigt = false;

in private void populateList after
WebPage page = null;

• Replace
lstPages.Items.Add(new ListItem(title, node"pageId"));

with
//custom --- BEGIN
page = new WebPage(node"pageId");

if (!string.IsNullOrEmpty(page.Roles))
{
string[] roles = page.Roles.Split(new char[] { ',' });

foreach (string role in roles)
{
boBerechtigt = boBerechtigt || User.IsInRole(role);
}
}
else
{
boBerechtigt = true;
}

if (boBerechtigt)
{
lstPages.Items.Add(new ListItem(title, node"pageId"));
boBerechtigt = false;
populateList(node, level + 1);
}
//custom --- END

• Add
//custom --- BEGIN
page.Roles = txtRoles.Text.Trim();
page.Params = txtParams.Text.Trim();
//custom --- END

in protected void btnSave_Click after
page.AllowAnonymousAccess = chkAnonymousAccess.Checked;

• Add
//custom --- BEGIN
txtRoles.Text = page.Roles;
//custom --- END

in private void showPageDetails after
txtTitle.Text = page.Title;


Administration/Navigation.aspx
• Add
<%--custom --- BEGIN--%>
<tr>
<td class="fieldlabel">
<asp:Localize ID="Localize1" runat="server" Text="<%$
Resources:stringsRes, dmNavigationRoles%>">
</asp:Localize>
</td>
<td class="field">
<asp:TextBox runat="server" ID="txtRoles">
</asp:TextBox>
</td>
</tr>
<%--custom --- END--%>

in table tblPageDetails
(Note: Add admNavigationRoles in App_GlobalResources/StringsRes.resx!)


Now you can add roles to users and pages. If a role is assigned to a page, only users which are assigned to this role can see the page.

I like it!

cu
André

P.S. We also have added a function in admin-mode to copy a complete page - including the data. If someone is interested I could post it too.
May 13, 2007 at 12:01 PM
Hi,
can you send me for mail the modified file? this is a my email flapiello@icbox.it....
I have modified the file as you said but it does not work....
thx


May 14, 2007 at 7:43 AM
Hi,

wheres the problem? Did you get an error?

cu
André
May 14, 2007 at 9:47 AM
i have restored the orginal project file, the problem was here

App_Code/Providers/CustomXmlSitemapProvider.cs
• Add
//custom: check for roles --- BEGIN
if (node.Roles.Count > 0)
{
bool isInRole = false;
for (int i = 0; i < node.Roles.Count; i++)
{
isInRole = isInRole ||
context.User.IsInRole(node.Rolesi.ToString());
}
return isInRole;

}
else
//custom: check for roles --- END


can you send me for mail the modified file? this is a my email flapiello@icbox.it....