Listing 4 The MayaSecurity class
public class MayaSecurity { // the list of validated application roles for the user internal ArrayList _roles = new ArrayList(); public MayaSecurity(IPrincipal user) { CheckRoles(user); } public void CheckRoles(IPrincipal user) { //loop through all roles defined in the MayaRole enum foreach( FieldInfo enumFieldInfo in typeof(MayaRole).GetFields() ) { // get the current enum value from the FieldInfo object MayaRole currentRole = enumFieldInfo.GetValue(); // get the list of windows groups for this enum from the web.config file string key = "Role" + currentRole.ToString(); string roleList = ConfigurationSettings.AppSettings[key]; if ( roleList != null ) { // indicates that the user is a member of a specified group bool hasRequiredGroup = false; foreach(string role in roleList.Split(',')) // uses IPrincipal to check for the Windows Group if ( _user.IsInRole(role.Trim()) ) hasRequiredGroup = true; // if the user is in a required group, add this role to our list if ( hasRequiredGroup ) _roles.Add( currentRole ); } } } public bool IsMemberOf(MayaRole roleToCheck) { return _roles.Contains(roleToCheck); } public bool IsAdmin { get { return IsMemberOf(MayaRole.Admin); } } public bool IsUser { get { return _roles.Count > 0; } } }