Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 20th, 2007, 11:39 PM
Authorized User
 
Join Date: Aug 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Code Alternative - Managing Roles

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();
    }
}
 
Old August 1st, 2007, 06:14 PM
Registered User
 
Join Date: Aug 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Very nice and great working code
You wouldn't happen to have made something for assigning users to roles?






Similar Threads
Thread Thread Starter Forum Replies Last Post
Managing RSS with XmlDataSource rit01 ASP.NET 2.0 Professional 1 June 13th, 2006 09:00 AM
Managing TabPages events MAKO C# 2 March 1st, 2006 07:04 AM
Ch. 7 - Managing Reports Using Program Code MER78 BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 September 27th, 2004 08:15 AM
Code alternative for "View / Zoom / Fit for Report MelP Access VBA 2 December 28th, 2003 07:39 AM
Alternative Ways to Close A Form Using Code wscheiman Access VBA 0 November 18th, 2003 12:14 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.