using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; using ScrewTurn.Wiki.PluginFramework; namespace ScrewTurn.Wiki { public partial class AdminMaster : System.Web.UI.MasterPage { protected void Page_Load(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(100); sb.Append(""); lblStrings.Text = sb.ToString(); Page.Title = Properties.Messages.AdminTitle + " - " + Settings.WikiTitle; if(!string.IsNullOrEmpty(Request.UserAgent) && Request.UserAgent.ToLowerInvariant().Contains("konqueror") || Request.UserAgent.ToLowerInvariant().Contains("safari")) { lblBrowserSupport.Visible = true; } lblJS.Text = Tools.GetJavaScriptIncludes(); SetupButtons(); SetupButtonsVisibility(); } /// /// Redirects to the login page if needed. /// public static void RedirectToLoginIfNeeded() { if(SessionFacade.LoginKey == null) { UrlTools.Redirect("Login.aspx?Redirect=" + Tools.UrlEncode(HttpContext.Current.Request.Url.ToString())); } } private readonly Dictionary HyperlinkMap = new Dictionary() { { "admingroups", "lnkSelectGroups" }, { "adminusers", "lnkSelectAccounts" }, { "adminnamespaces", "lnkSelectNamespaces" }, { "adminpages", "lnkSelectPages" }, { "admincontent", "lnkSelectContent" }, { "adminlog", "lnkSelectLog" }, { "adminconfig", "lnkSelectConfig" }, { "adminsnippets", "lnkSelectSnippets" }, { "admincategories", "lnkSelectCategories" }, { "adminhome", "lnkSelectAdminHome" }, { "adminnavpaths", "lnkSelectNavPaths" }, { "adminproviders", "lnkSelectProviders" } }; /// /// Sets up the buttons state. /// private void SetupButtons() { string selectedPage = System.IO.Path.GetFileNameWithoutExtension(Request.PhysicalPath).ToLowerInvariant(); HyperLink hyperLink = (HyperLink)FindControl(HyperlinkMap[selectedPage]); lnkSelectGroups.CssClass = "tab"; lnkSelectAccounts.CssClass = "tab"; lnkSelectNamespaces.CssClass = "tab"; lnkSelectPages.CssClass = "tab"; lnkSelectContent.CssClass = "tab"; lnkSelectLog.CssClass = "tab"; lnkSelectConfig.CssClass = "tab"; lnkSelectSnippets.CssClass = "tab"; lnkSelectCategories.CssClass = "tab"; lnkSelectAdminHome.CssClass = "tab"; lnkSelectNavPaths.CssClass = "tab"; lnkSelectProviders.CssClass = "tab"; hyperLink.CssClass = "tabselected"; } /// /// Sets up the buttons visibility based on the current user's permissions. /// private void SetupButtonsVisibility() { string currentUser = SessionFacade.GetCurrentUsername(); string[] currentGroups = SessionFacade.GetCurrentGroupNames(); // Categories (can manage categories in at least one NS) lnkSelectCategories.Visible = CanManageCategories(currentUser, currentGroups); // Configuration (can manage config) lnkSelectConfig.Visible = CanManageConfiguration(currentUser, currentGroups); // Content (can manage config) lnkSelectContent.Visible = CanManageConfiguration(currentUser, currentGroups); // Groups (can manage groups) lnkSelectGroups.Visible = CanManageGroups(currentUser, currentGroups); // Home (can manage config) lnkSelectAdminHome.Visible = CanManageConfiguration(currentUser, currentGroups); // Log (can manage config) lnkSelectLog.Visible = CanManageConfiguration(currentUser, currentGroups); // Namespaces (can manage namespaces) lnkSelectNamespaces.Visible = CanManageNamespaces(currentUser, currentGroups); // Nav. Paths (can manage pages in at least one NS) lnkSelectNavPaths.Visible = CanManagePages(currentUser, currentGroups); // Pages // Always displayed because checking every page can take too much time // Providers (can manage providers) lnkSelectProviders.Visible = CanManageProviders(currentUser, currentGroups); // Snippets (can manage snippets) lnkSelectSnippets.Visible = CanManageSnippetsAndTemplates(currentUser, currentGroups); // Accounts (can manage user accounts) lnkSelectAccounts.Visible = CanManageUsers(currentUser, currentGroups); } /// /// Determines whether a user can manage the configuration. /// /// The username. /// The groups. /// true if the user can manage the configuration, false otherwise. public static bool CanManageConfiguration(string username, string[] groups) { bool canManageConfiguration = AuthChecker.CheckActionForGlobals(Actions.ForGlobals.ManageConfiguration, username, groups); return canManageConfiguration; } /// /// Determines whether a user can manage categories in at least one namespace. /// /// The username. /// The groups. /// true if the user can manage categories in at least one namespace, false otherwise. public static bool CanManageCategories(string username, string[] groups) { if(AuthChecker.CheckActionForNamespace(null, Actions.ForNamespaces.ManageCategories, username, groups)) return true; foreach(NamespaceInfo ns in Pages.GetNamespaces()) { if(AuthChecker.CheckActionForNamespace(ns, Actions.ForNamespaces.ManageCategories, username, groups)) return true; } return false; } /// /// Determines whether a user can manage user groups. /// /// The username. /// The groups. /// true if the user can manage groups, false otherwise. public static bool CanManageGroups(string username, string[] groups) { bool canManageGroups = AuthChecker.CheckActionForGlobals(Actions.ForGlobals.ManageGroups, username, groups); return canManageGroups; } /// /// Determines whether a user can manage permissions. /// /// The username. /// The groups. /// true if the user can manage permissions, false otherwise. public static bool CanManagePermissions(string username, string[] groups) { bool canManagePermissions = AuthChecker.CheckActionForGlobals(Actions.ForGlobals.ManagePermissions, username, groups); return canManagePermissions; } /// /// Determines whether a user can manage namespaces. /// /// The username. /// The groups. /// true if the user can manage namespace, false otherwise. public static bool CanManageNamespaces(string username, string[] groups) { bool canManageNamespaces = AuthChecker.CheckActionForGlobals(Actions.ForGlobals.ManageNamespaces, username, groups); return canManageNamespaces; } /// /// Determines whether a user can manage pages in at least one namespace. /// /// The username. /// The groups. /// true if the the user can manage pages in at least one namespace, false otherwise. public static bool CanManagePages(string username, string[] groups) { if(AuthChecker.CheckActionForNamespace(null, Actions.ForNamespaces.ManagePages, username, groups)) return true; foreach(NamespaceInfo ns in Pages.GetNamespaces()) { if(AuthChecker.CheckActionForNamespace(ns, Actions.ForNamespaces.ManagePages, username, groups)) return true; } return false; } /// /// Determines whether a user can manage providers. /// /// The username. /// The groups. /// true if the user can manage providers, false otherwise. public static bool CanManageProviders(string username, string[] groups) { bool canManageProviders = AuthChecker.CheckActionForGlobals(Actions.ForGlobals.ManageProviders, username, groups); return canManageProviders; } /// /// Determines whether a user can manage snippets and templates. /// /// The username. /// The groups. /// true if the user can manage snippets and templates, false otherwise. public static bool CanManageSnippetsAndTemplates(string username, string[] groups) { bool canManageSnippets = AuthChecker.CheckActionForGlobals(Actions.ForGlobals.ManageSnippetsAndTemplates, username, groups); return canManageSnippets; } /// /// Determines whether a user can manager user accounts. /// /// The username. /// The groups. /// true if the user can manage user accounts, false otherwise. public static bool CanManageUsers(string username, string[] groups) { bool canManageUsers = AuthChecker.CheckActionForGlobals(Actions.ForGlobals.ManageAccounts, username, groups); return canManageUsers; } /// /// Determines whether a user can approve/reject a draft of a page. /// /// The page. /// The username. /// The groups. /// true if the user can approve/reject a draft of the page, false otherwise. public static bool CanApproveDraft(PageInfo page, string username, string[] groups) { return Pages.CanApproveDraft(page, username, groups); } } }