public static IEnumerable <AdminSection> GetAdminSectionsForUser(User user) { List <AdminSection> list = new List <AdminSection>(); foreach (AdminSection adminSection in AdminSections) { // Get admin section pages in this section that are accessible by the specified user List <AdminSectionPage> adminSectionPages = GetAdminSectionPages(adminSection, user); // Only go ahead and add the section if the user can access pages inside it. Otherwise, // there's no point in adding it (as the section will be empty to the user). if (adminSectionPages.Count > 0) { // Get a copy of the admin section object // This will only return the basic object without child pages, so we don't need to clear the child pages first. // Getting some strange behaviour with the MemberwiseClone method. See comments in Clone method for more info. AdminSection adminSectionClone = (AdminSection)adminSection.Clone(); // Add back in the pages accessible in this section to the specified user adminSectionClone.AdminSectionPages.AddRange(adminSectionPages); // Add the admin section to the list list.Add(adminSectionClone); } } return(list); }
/// <summary> /// Gets the list of admin section pages for the specified section, that are /// accessible by the specified user /// </summary> private static List <AdminSectionPage> GetAdminSectionPages(AdminSection adminSection, User user) { return((from page in adminSection.AdminSectionPages where SecurityManager.UserHasAccess(user, page) select page).ToList()); }