using System; using System.Data; using System.Configuration; 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; /// /// Use members of this class instead of the Membership class to access /// membership information of the current user, or of a user account that is being impersonated. /// namespace MB.eQuest { public static class AppMembership { const string USER_SESSION_KEY = "objMembershipUser"; const string IMPERSONATOR_SESSION_KEY = "objMembershipUser_Impersonator"; static AppMembership() { } /// /// Returns boolean value indicating whether a user is currently being impersonated. /// If so, members of this class will return objects related to the impersonated user. /// Ex., AppMembership.User will return the MembershipUser object of the impersonated user, /// not the currently logged in user. /// public static bool IsUserBeingImpersonated { get { return (HttpContext.Current.Session[IMPERSONATOR_SESSION_KEY] != null) ? true : false; } } /// /// Returns a MembershipUser for either: /// a) the logged in user /// b) the user currently being impersonated, regardless of who is logged in. /// public static MembershipUser User { get { if (HttpContext.Current.User.Identity.IsAuthenticated) { if (HttpContext.Current.Session[USER_SESSION_KEY] == null) HttpContext.Current.Session[USER_SESSION_KEY] = Membership.GetUser(); return (MembershipUser)HttpContext.Current.Session[USER_SESSION_KEY]; } else { //Get user's anonymous record return Membership.GetUser(HttpContext.Current.Profile.UserName); } } } /// Returns: /// a) If an account is beign impersonated, the MembershipUser object of the impersonator /// b) null otherwise public static MembershipUser Impersonator { get { //Return impersonator account if an admin is currently impersonating another user. if (HttpContext.Current.Session[IMPERSONATOR_SESSION_KEY] != null) return (MembershipUser)HttpContext.Current.Session[IMPERSONATOR_SESSION_KEY]; else return null; } } /// /// Returns boolean value to determine: /// a) Whether the logged in user is in the specified role /// b) Whether the user currently being impersonated is in the specified role /// /// public static bool IsUserInRole(string roleName) { return (Roles.IsUserInRole(AppMembership.User.UserName, roleName)); } /// /// Returns array of role names for: /// a) Roles the currently logged in user is a member of. /// b) If admin is emulating a user, returns roles the emulated user is a member of. /// /// public static string[] GetRolesForUser(string roleName) { return (Roles.GetRolesForUser(AppMembership.User.UserName)); } /// /// Start impersonating the specified user /// /// public static void StartImpersonation(string UserName) { //The impersonator will always be the currently logged-in user HttpContext.Current.Session[IMPERSONATOR_SESSION_KEY] = Membership.GetUser(); //Get the impersonated account from the supplied UserName. HttpContext.Current.Session[USER_SESSION_KEY] = Membership.GetUser(UserName); //Clear cached profile AppProfile.Clear(); } /// /// Stop impersonating /// public static void EndImpersonation() { //End impersonation by resetting all of the session variables. HttpContext.Current.Session[IMPERSONATOR_SESSION_KEY] = null; HttpContext.Current.Session[USER_SESSION_KEY] = null; //Clear cached profile AppProfile.Clear(); } } }