Hi All,
I didn't really like the fact that roles for the site were not managed in their own module, so I decided to create my own functionality for role management.
I, like many here, am relatively new to ASP.Net, though I'm fairly learned in Classic ASP. What I'm getting at is that the code here may not be the most efficient methodology, but it's clean and works well.
As a note, if you use this, you'll want to remove the create roles section(not the Edit User Roles section) from the EditUser.aspx page, since it will be extraneous.
I've made the User and Admin roles in my site required, so there is a section to filter them from being deleted. For your site, you may or may not want to do this, and/or you may need to change the filter code to match your particular role names.
So for your consideration:
ManageRoles.aspx (code placed in Content container)
Code:
<table cellpadding="2" cellspacing="2" width="450px">
<tr>
<td>
<div class="subsectionTitle">Current Roles</div>
<p></p>
Select roles to be deleted
<br />
<asp:CheckBoxList ID="chklRoles" runat="server" />
<br />
<asp:Button ID="btnDeleteRoles" runat="server" Text="Delete Roles" OnClick="btnDeleteRoles_Click"
OnClientClick="if (confirm('Are you sure you want to delete this role?\n\nIf this role is deleted, users in this role will be defaulted to basic users.') == false) return false" />
<br />
<asp:Label ID="lblDeleteRoles" runat="server" />
</td>
</tr>
<tr>
<td>
<br />
</td>
</tr>
<tr>
<td class="left">
Create New Role:
<br />
<asp:TextBox ID="txtNewRole" runat="server" />
<p></p>
<asp:Button ID="btnCreateRole" runat="server" Text="Create Role" ValidationGroup="newRole" OnClick="btnCreateRole_Click" />
</td>
</tr>
<tr>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorNewRole" runat="server" ControlToValidate="txtNewRole" SetFocusOnError="true"
ErrorMessage="Role name is required" ValidationGroup="newRole" Display="dynamic">*</asp:RequiredFieldValidator>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="false" ShowSummary="true" ValidationGroup="newRole" />
</td>
</tr>
<tr>
<td class="left">
<asp:Label ID="lblFeedbackOK" runat="server" Text="New role successfully created" SkinID="FeedbackOK" />
<asp:Label ID="lblFeedbackKO" runat="server" SkinID="FeedbackKO" />
</td>
</tr>
</table>
ManageRoles.aspx.cs
Code:
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Admin_manageRoles : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblFeedbackOK.Visible = false;
if (!IsPostBack)
{
BindRoles();
}
}
protected void btnDeleteRoles_Click(object sender, EventArgs e)
{
// Get roles to delete from the chklRoles
foreach (ListItem item in chklRoles.Items)
{
if (item.Selected)
{
if (item.Text == "User" || item.Text == "Admin")
{
lblDeleteRoles.Text = "The roles 'Admin' and 'User' can not be deleted, they are required for the site.";
lblDeleteRoles.ForeColor = Color.Red;
lblDeleteRoles.Visible = true;
break;
}
else
{
// For each role in the list get the usrs in that role
string[] usersInRole = Roles.GetUsersInRole(item.Text);
// For each user, remove the user from the current loop's role, then add the user back into
// the 'User'(default) role, if not already a member.
foreach (string user in usersInRole)
{
Roles.RemoveUserFromRole(user, item.Text);
if (!Roles.IsUserInRole(user, "User"))
{
Roles.AddUserToRole(user, "User");
}
}
}
// Once all the users have been removed from the current loop's role, remove the role.
Roles.DeleteRole(item.Text);
}
}
BindRoles();
}
protected void btnCreateRole_Click(object sender, EventArgs e)
{
if (!Roles.RoleExists(txtNewRole.Text.Trim()))
{
Roles.CreateRole(txtNewRole.Text.Trim());
BindRoles();
txtNewRole.Text = "";
}
else
{
lblFeedbackKO.Text = "The role '" + txtNewRole.Text.Trim() + "' already exists. It can not be created twice.";
lblFeedbackKO.Visible = true;
txtNewRole.Text = "";
}
}
private void BindRoles()
{
List<string> lstRoles = new List<string>();
foreach (string item in Roles.GetAllRoles())
{
lstRoles.Add(item.ToString());
}
chklRoles.DataSource = lstRoles;
chklRoles.DataBind();
}
}