<script runat="server">
//-----------------------------------------------
public static string error_text_create_role = "Error create role: ";
public static string error_text_create_role_invalid_name = "Error create role, invalid rolename: ";
public static string error_text_delete_role = "Error deleting role: ";
public static string error_text_remove_user_from_role = "Error removing the user from the role: ";
public static string error_text_remove_users_from_role = "Error removing users from the role: ";
public static string text_no_role_selected = "no role selected";
public static string text_select_a_role = "Please select a role.";
public static string text_select_user = "Please select one or more users.";
//-----------------------------------------------
public string role_selected = text_no_role_selected;
public string[] roles_array;
public MembershipUserCollection users;
//-----------------------------------------------
protected override void Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender, e);
if (!IsPostBack)
{
//----- Bind roles to ListBox.
Bind_Roles_To_Listbox_From_All_Roles();
//----- Bind users to ListBox.
Bind_Users_To_Listbox_From_All_Users();
}
if (RolesListBox.SelectedItem != null)
//-- Show users in role. Bind user list to GridView.
Bind_Users_To_Grid_From_Role_Selected();
}
//-----------------------------------------------
/// <summary>
/// Adds users to role.
/// </summary>
/// <param name="rolename"></param>
/// <param name="newusers"></param>
/// <returns>Empty string if good; Error message if bad.</returns>
public string Add_Users_To_Role(string rolename, string[] newusers)
{
//----- Add the users to the selected role.
try
{
Roles.AddUsersToRole(newusers, rolename);
}
catch (Exception e)
{
return e.Message;
}
return string.Empty;
}
//-----------------------------------------------
public void Add_Users_To_Role_OnClick(object sender, EventArgs args)
{
string m = Add_Users_To_Role_Form(RolesListBox, UsersListBox);
Message_Set(m);
}
//-----------------------------------------------
public string Add_Users_To_Role_Form(ListBox RolesListBox, ListBox UsersListBox)
{
if (RolesListBox.SelectedItem == null) // Verify that a role is selected.
return text_select_a_role;
if (UsersListBox.SelectedItem == null) // Verify that at least one user is selected
return text_select_user;
//----- Create list of users to be added to the selected role
string[] newusers = new string[UsersListBox.GetSelectedIndices().Length];
for (int i = 0; i < newusers.Length; i++)
newusers[i] = UsersListBox.Items[UsersListBox.GetSelectedIndices()]i[].Value;
//----- Add the users to the selected role.
string rolename = RolesListBox.SelectedItem.Value;
string m = Add_Users_To_Role(rolename, newusers);
if (!string.IsNullOrEmpty(m)) return m; // Error
//----- Good. Re-bind users in role to GridView.
Bind_Users_To_Grid_From_Role_Selected();
return string.Empty;
}
//-----------------------------------------------
public void Bind_Roles_To_Listbox(string[] roles)
{
//----- Bind roles to ListBox.
RolesListBox.DataSource = roles;
RolesListBox.DataBind();
}
//-----------------------------------------------
public void Bind_Roles_To_Listbox_From_All_Roles()
{
//----- Bind roles to ListBox.
roles_array = Roles.GetAllRoles();
Bind_Roles_To_Listbox(roles_array);
}
//-----------------------------------------------
public void Bind_Users_To_Grid(string[] usernames)
{
//-- Bind user list to GridView.
UsersInRoleGrid.DataSource = usernames;
UsersInRoleGrid.DataBind();
}
//-----------------------------------------------
public void Bind_Users_To_Grid_From_Role(string rolename)
{
//-- Show users in role. Bind user list to GridView.
string[] usernamesinrole = Roles.GetUsersInRole(rolename);
Bind_Users_To_Grid(usernamesinrole);
}
//-----------------------------------------------
public void Bind_Users_To_Grid_From_Role_Selected()
{
//-- Show users in role. Bind user list to GridView.
string roleselected = RolesListBox.SelectedItem.Value;
Bind_Users_To_Grid_From_Role(roleselected);
role_selected = roleselected; // Set public variable to show on page
}
//-----------------------------------------------
public void Bind_Users_To_Listbox(MembershipUserCollection users)
{
//----- Bind users to ListBox.
UsersListBox.DataSource = users;
UsersListBox.DataBind();
}
//-----------------------------------------------
public void Bind_Users_To_Listbox_From_All_Users()
{
//----- Bind users to ListBox.
users = Membership.GetAllUsers();
Bind_Users_To_Listbox(users);
}
//-----------------------------------------------
public string Create_Role(string rolename)
{
rolename = br_string_regex_Class.Remove_Non_Word_Characters(rolename);
if (string.IsNullOrEmpty(rolename))
return error_text_create_role_invalid_name + rolename;
//---- Create the role if does not exist
try
{
Roles.CreateRole(rolename);
}
catch (Exception e)
{
return error_text_create_role
+ rolename + br_string_Class.text_comma
+ e.GetType().ToString();
}
return string.Empty;
}
//-----------------------------------------------
public void Create_Roll_OnClick(object sender, EventArgs args)
{
string m = Create_Roll_Form(Create_Delete_Role_TextBox);
Message_Set(m);
}
//-----------------------------------------------
public string Create_Roll_Form(TextBox Create_Delete_Role_TextBox)
{
string rolename = Create_Delete_Role_TextBox.Text;
string m = Create_Role(rolename);
if (!string.IsNullOrEmpty(m)) return m; // Error
//----- Good. Re-bind roles to ListView
Bind_Roles_To_Listbox_From_All_Roles();
return string.Empty;
}
//-----------------------------------------------
public string Delete_Role(string rolename)
{
//---- Remove the role if no users in it
try
{
Roles.DeleteRole(rolename, true); // true=do not delete if names in role
}
catch (Exception e)
{
return error_text_delete_role
+ rolename + br_string_Class.text_comma
+ e.GetType().ToString();
}
return string.Empty;
}
//-----------------------------------------------
public void Delete_Roll_OnClick(object sender, EventArgs args)
{
string m = Delete_Roll_Form(Create_Delete_Role_TextBox);
Message_Set(m);
}
//-----------------------------------------------
public string Delete_Roll_Form(TextBox Create_Delete_Role_TextBox)
{
string rolename = Create_Delete_Role_TextBox.Text;
string m = Delete_Role(rolename);
if (!string.IsNullOrEmpty(m)) return m; // Error
//----- Good. Re-bind roles to ListView
Bind_Roles_To_Listbox_From_All_Roles();
return string.Empty;
}
//-----------------------------------------------
public void Message_Set(string m)
{
if (string.IsNullOrEmpty(m)) return; // Message not set
br_webpage_Class wepage = (br_webpage_Class)Page;
Webpage.Message.Append(m);
}
//-----------------------------------------------
public string Remove_User_From_Role(string rolename, string username)
{
//---- Remove the user from the selected role.
try
{
Roles.RemoveUserFromRole(username, rolename);
}
catch (Exception e)
{
return error_text_remove_user_from_role
+ username + br_string_Class.text_comma
+ rolename + br_string_Class.text_comma
+ e.GetType().ToString();
}
return string.Empty;
}
//-----------------------------------------------
public void Remove_User_From_Role_onClickLink(object sender, GridViewCommandEventArgs args)
{
string m = Remove_User_From_Role_Form(RolesListBox, UsersInRoleGrid, args);
Message_Set(m);
}
//-----------------------------------------------
public string Remove_User_From_Role_Form(ListBox RolesListBox
, GridView UsersInRoleGrid, GridViewCommandEventArgs args)
{
//----- Get the selected user name to remove
int index = Convert.ToInt32(args.CommandArgument);
string rolename = RolesListBox.SelectedItem.Value;
string username = ((DataBoundLiteralControl)UsersInRoleGrid.Rows[index].Cells[0].Controls[0]).Text;
//----- Remove the user from the selected role
string m = Remove_User_From_Role(rolename, username);
if (!string.IsNullOrEmpty(m)) return m; // Error
//----- Re-bind users in role to GridView.
Bind_Users_To_Grid_From_Role(rolename);
return string.Empty;
}
//-----------------------------------------------
/// <summary>
/// Remove users from role.
/// </summary>
/// <param name="rolename"></param>
/// <param name="newusers"></param>
/// <returns>Empty string if good; Error message if bad.</returns>
public string Remove_Users_From_Role(string rolename, string[] usernames)
{
//---- Remove the user from the selected role.
try
{
Roles.RemoveUsersFromRole(usernames, rolename);
}
catch (Exception e)
{
return error_text_remove_users_from_role
+ rolename + br_string_Class.text_comma
+ e.GetType().ToString();
}
return string.Empty;
}
//-----------------------------------------------
public void Remove_Users_From_Role_OnClick(object sender, EventArgs args)
{
string m = Remove_Users_From_Role_Form(RolesListBox, UsersListBox);
br_webpage_Class Webpage = (br_webpage_Class)Page;
if (!string.IsNullOrEmpty(m)) Webpage.Message.Append(m);
}
//-----------------------------------------------
public string Remove_Users_From_Role_Form(ListBox RolesListBox, ListBox UsersListBox)
{
if (RolesListBox.SelectedItem == null) // Verify that a role is selected.
return text_select_a_role;
if (UsersListBox.SelectedItem == null) // Verify that at least one user is selected
return text_select_user;
//----- Create list of users to be added to the selected role
string[] newusers = new string[UsersListBox.GetSelectedIndices().Length];
for (int i = 0; i < newusers.Length; i++)
newusers[i] = UsersListBox.Items[UsersListBox.GetSelectedIndices()]i[].Value;
//----- Add the users to the selected role.
string rolename = RolesListBox.SelectedItem.Value;
string m = Remove_Users_From_Role(rolename, newusers);
if (!string.IsNullOrEmpty(m)) return m; // Error
//----- Good. Re-bind users in role to GridView.
Bind_Users_To_Grid_From_Role_Selected();
return string.Empty;
}
//-----------------------------------------------
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="content_body1" Runat="Server" EnableViewState="false">
<table cellpadding="3" cellspacing="0" border="0">
<tr valign="top">
<td>Role:
<asp:TextBox Columns="30" MaxLength="200" ID="Create_Delete_Role_TextBox" runat="server" EnableViewState="false"></asp:TextBox>
<asp:Button Text="Create Role" ID="Create_Role_Button" runat="server" OnClick="Create_Roll_OnClick" EnableViewState="false" />
<asp:Button Text="Delete Role" ID="Delete_Role_Button" runat="server" OnClick="Delete_Roll_OnClick" EnableViewState="false" />
</td>
</tr>
</table>
<hr />
<table cellpadding="3" cellspacing="0" border="0">
<tr valign="top">
<td>
Roles:</td>
<td>
<asp:ListBox ID="RolesListBox" runat="server" Rows="8" AutoPostBack="true" /></td>
<td>
Users:</td>
<td>
<asp:ListBox ID="UsersListBox" DataTextField="Username" Rows="8" SelectionMode="Multiple"
runat="server" /></td>
<td>
<asp:Button Text="Add Users to Role" ID="Add_Users_To_Role_Button" runat="server" OnClick="Add_Users_To_Role_OnClick" EnableViewState="false" />
<br /><asp:Button Text="Remove Users from Role" ID="Remove_Users_From_Role_Button" runat="server" OnClick="Remove_Users_From_Role_OnClick" EnableViewState="false" />
</td>
</tr>
</table>
<hr />
<h4>Users In Role: <%=role_selected %></h4>
<asp:GridView runat="server" ID="UsersInRoleGrid" EnableViewState="false"
AutoGenerateColumns="false"
CellPadding="3" CellSpacing="0"
GridLines="None"
OnRowCommand="Remove_User_From_Role_onClickLink">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# Container.DataItem.ToString() %>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField Text="Remove From Role" ButtonType="Link" />
</Columns>
</asp:GridView>
</asp:Content>
--- end ---