using System;using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections.Specialized;
using System.Collections;
using System.Web.Configuration;
namespace BusinessComponents
{
/// <summary>
/// This class provides a way to implement the role based security to the application.
/// The screen fields are defined in the xml and this file reads the xml and loads it into
/// cache memory as a strongly typed class to be accessed and used inthe screens.
/// </summary>
public class Role
{
#region Private Fields
private static Role RoleInstance = new Role();
/// <summary>
/// HybridDictionary cachedRoleFields, populated in Role's constructor,
/// is the core of this class, as it holds the collection of RoleField objects.
/// cachedRoleFields can only be accessed through the Field property,
/// which retrieves a particular RoleField object from the collection
/// based on a string key.
/// </summary>
private HybridDictionary cachedRoleFields = new HybridDictionary();
private const string ROLE_SEPRATOR = "#";
private const string ROLE_ASSIGNMENT = "=";
#endregion
#region Properties
public static Role Instance
{
get { return RoleInstance; }
}
public static RoleField Field(string fieldName)
{
return ((RoleField)(RoleInstance.cachedRoleFields[fieldName]));
}
#endregion
/// <summary>
/// Contains the structure for the RoleField
/// </summary>
public class RoleField
{
/// <summary>
/// Name is the key used in the calling code to retrieve the appropriate values.
/// </summary>
public String Name = String.Empty;
/// <summary>
/// Control is the control Name in the respective screen
/// </summary>
public String Control = String.Empty;
/// <summary>
/// Collection to hold the property values either true/false
/// </summary>
public Hashtable Enabled = new Hashtable();
/// <summary>
/// Collection to hold the property values either true/false
/// </summary>
public Hashtable Visible = new Hashtable();
}
/// <summary>
///The constructor is only called once, the first time this class is instantiated.
///Every time an instance of this object is called after the first time,
///the "in memory" copy contained in RoleInstance is used.
///The constructor performs the basics.
///It reads RoleValiations.xml into memory, in a DataSet.
///It reads through each row of the DataSet and populates a new RoleField object.
///Finally, it adds the RoleField object to the cachedRoleFields collection.
///Once the DataSet is populated it is not changed until it is reloaded from the
///XML when the application is restarted.
/// </summary>
private Role()
{
#region Local Variable Declaration
RoleField Field = null;
DataSet ds = new DataSet();
string[] RoleEnabledList;
string[] RoleEnabled;
string[] RoleVisibleList;
string[] RoleVisible;
int Ctr = 0;
#endregion
try
{
#region Reads ScreenRoles.xml into memory
string RoleFilePath = WebConfigurationManager.AppSettings["RoleValidation"].ToString();
ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath(RoleFilePath));
#endregion
foreach (DataRow dr in ds.Tables[0].Rows)
{
#region Reads through each row of the DataSet and populates a new Field object
Field = new RoleField();
Field.Name = dr["Name"].ToString();
Field.Control = dr["Control"].ToString();
RoleEnabledList = dr["Enabled"].ToString().Split(ROLE_SEPRATOR.ToCharArray());
#region Spilting Enabled properties by role
for (Ctr = 0; Ctr < RoleEnabledList.Length; Ctr++)
{
RoleEnabled = RoleEnabledList[Ctr].Split(ROLE_ASSIGNMENT.ToCharArray());
Field.Enabled.Add(RoleEnabled[0], RoleEnabled[1]);
}
#endregion
RoleVisibleList = dr["Visible"].ToString().Split(ROLE_SEPRATOR.ToCharArray());
#region Spilting Enabled properties by role
for (Ctr = 0; Ctr < RoleVisibleList.Length; Ctr++)
{
RoleVisible = RoleVisibleList[Ctr].Split(ROLE_ASSIGNMENT.ToCharArray());
Field.Visible.Add(RoleVisible[0], RoleVisible[1]);
}
#endregion
#endregion
#region Adds the Field object to the cachedRoleFields collection.
cachedRoleFields.Add(Field.Name, Field);
#endregion
}
}
catch (Exception ex)
{
throw;
}
}
/// <summary>
/// The Reset method allows for dynamic reloading of ScreenRoles.xml during runtime.
/// Any screen/page/class could very easily call this method in a event handler to
/// allow administrators to reload ScreenRoles.xml on the fly without restarting
/// the application.
/// </summary>
public static void Reset()
{
RoleInstance = new Role();
}
}
}