using System; using System.Collections.Generic; using System.Text; using System.Web.SessionState; using System.Web; using ScrewTurn.Wiki.PluginFramework; namespace ScrewTurn.Wiki { /// /// Exposes in a strongly-typed fashion the Session variables. /// public static class SessionFacade { /// /// Gets the current Session object. /// private static HttpSessionState Session { get { return HttpContext.Current.Session; } } /// /// Gets or sets the Login Key. /// public static string LoginKey { get { return Session != null ? (string)Session["LoginKey"] : null; } set { if(Session != null) Session["LoginKey"] = value; } } /// /// Gets or sets the current username, or null. /// public static string CurrentUsername { get { return Session != null ? Session["Username"] as string : null; } set { if(Session != null) Session["Username"] = value; } } /// /// Gets the current user, if any. /// /// The current user, or null. public static UserInfo GetCurrentUser() { if(Session != null) { string sessionId = Session.SessionID; UserInfo current = SessionCache.GetCurrentUser(sessionId); if(current != null) return current; else { string un = CurrentUsername; if(string.IsNullOrEmpty(un)) return null; else if(un == AnonymousUsername) return Users.GetAnonymousAccount(); else { current = Users.FindUser(un); if(current != null) SessionCache.SetCurrentUser(sessionId, current); return current; } } } else return null; } /// /// The username for anonymous users. /// public const string AnonymousUsername = "|"; /// /// Gets the current username for security checks purposes only. /// /// The current username, or AnonymousUsername if anonymous. public static string GetCurrentUsername() { string un = CurrentUsername; if(string.IsNullOrEmpty(un)) return AnonymousUsername; else return un; } /// /// Gets the current user groups. /// public static UserGroup[] GetCurrentGroups() { if(Session != null) { string sessionId = Session.SessionID; UserGroup[] groups = SessionCache.GetCurrentGroups(sessionId); if(groups == null || groups.Length == 0) { UserInfo current = GetCurrentUser(); if(current != null) { groups = new UserGroup[current.Groups.Length]; for(int i = 0; i < groups.Length; i++) { groups[i] = Users.FindUserGroup(current.Groups[i]); } } else { groups = new UserGroup[] { Users.FindUserGroup(Settings.AnonymousGroup) }; } SessionCache.SetCurrentGroups(sessionId, groups); } return groups; } else return new UserGroup[0]; } /// /// Gets the current group names. /// /// The group names. public static string[] GetCurrentGroupNames() { return Array.ConvertAll(GetCurrentGroups(), delegate(UserGroup g) { return g.Name; }); } /// /// Gets the Breadcrumbs Manager. /// public static BreadcrumbsManager Breadcrumbs { get { return new BreadcrumbsManager(); } } } /// /// Implements a session data cache whose lifetime is only limited to one request. /// public static class SessionCache { private static Dictionary currentUsers = new Dictionary(100); private static Dictionary currentGroups = new Dictionary(100); /// /// Gets the current user, if any, of a session. /// /// The session ID. /// The current user, or null. public static UserInfo GetCurrentUser(string sessionId) { lock(currentUsers) { UserInfo result = null; currentUsers.TryGetValue(sessionId, out result); return result; } } /// /// Sets the current user of a session. /// /// The session ID. /// The user. public static void SetCurrentUser(string sessionId, UserInfo user) { lock(currentUsers) { currentUsers[sessionId] = user; } } /// /// Gets the current groups, if any, of a session. /// /// The session ID. /// The groups, or null. public static UserGroup[] GetCurrentGroups(string sessionId) { lock(currentGroups) { UserGroup[] result = null; currentGroups.TryGetValue(sessionId, out result); return result; } } /// /// Sets the current groups of a session. /// /// The session ID. /// The groups. public static void SetCurrentGroups(string sessionId, UserGroup[] groups) { lock(currentGroups) { currentGroups[sessionId] = groups; } } /// /// Clears all cached data of a session. /// /// The session ID. public static void ClearData(string sessionId) { lock(currentUsers) { currentUsers.Remove(sessionId); } lock(currentGroups) { currentGroups.Remove(sessionId); } } } }