The method below called UserCanAccessThisPage is based on the following logic: each user and each page has a list of groups. If any of these match, the user has access to the page.
The code below does what I want, but my solution is very C# 1 (or C# 2, at least I didn't use ArrayList).
Can anyone refactor this so it is more straight-forward, e.g. using lambdas to do away with the two methods? I just can't get the syntax to do it.
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestCompare2343
{
class Program
{
static void Main(string[] args)
{
string anonymousUserAccessGroups = "loggedOutUsers";
string normalUserAccessGroups = "loggedInUsers, members";
string developerUserAccessGroups = "loggedInUsers, members, administrators, developers";
string loginPageAccessGroups = "loggedOutUsers";
string logoutPageAccessGroups = "loggedInUsers";
string memberInfoPageAccessGroups = "members";
string devPageAccessGroups = "developers";
//test anonymousUser
Console.WriteLine(StringHelpers.UserCanAccessThisPage(anonymousUserAccessGroups, loginPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(anonymousUserAccessGroups, logoutPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(anonymousUserAccessGroups, memberInfoPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(anonymousUserAccessGroups, devPageAccessGroups));
Console.WriteLine("---");
//test anonymousUser
Console.WriteLine(StringHelpers.UserCanAccessThisPage(normalUserAccessGroups, loginPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(normalUserAccessGroups, logoutPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(normalUserAccessGroups, memberInfoPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(normalUserAccessGroups, devPageAccessGroups));
Console.WriteLine("---");
//test anonymousUser
Console.WriteLine(StringHelpers.UserCanAccessThisPage(developerUserAccessGroups, loginPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(developerUserAccessGroups, logoutPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(developerUserAccessGroups, memberInfoPageAccessGroups));
Console.WriteLine(StringHelpers.UserCanAccessThisPage(developerUserAccessGroups, devPageAccessGroups));
Console.WriteLine("---");
Console.ReadLine();
}
}
public class StringHelpers
{
public static bool UserCanAccessThisPage(string userAccessGroups, string pageItemAccessGroups)
{
List<string> userAccessGroupsList = StringHelpers.SplitAndTrimCommaDelimitedString(userAccessGroups);
List<string> pageItemAccessGroupList = StringHelpers.SplitAndTrimCommaDelimitedString(pageItemAccessGroups);
foreach (string userAccessGroup in userAccessGroupsList)
{
foreach (string pageItemAccessGroup in pageItemAccessGroupList)
{
if (userAccessGroup == pageItemAccessGroup)
return true;
}
}
return false;
}
public static List<string> SplitAndTrimCommaDelimitedString(string line)
{
List<string> piecesWithSpaces = line.Split(',').ToList<string>();
List<string> piecesWithoutSpaces = new List<string>();
foreach (string pieceWithSpace in piecesWithSpaces)
{
piecesWithoutSpaces.Add(pieceWithSpace.Trim());
}
return piecesWithoutSpaces;
}
}
}
Answer:
Fredrik had the most concise code that solved the original task above:
public static bool UserCanAccessThisPage(string userAccessGroups, string pageItemAccessGroups)
{
return userAccessGroups
.Split(',')
.Select(s => s.Trim())
.Contains(pageItemAccessGroups);
}
The code I used:
But Shaul was correct in assuming that the PageItems can also have multiple entries, e.g. "members,guests", and so I actually used Shaul's code:
public static bool UserCanAccessThisPage(string userAccessGroups, string pageItemAccessGroups) {
List<string> userAccessGroupsList = StringHelpers.SplitAndTrimCommaDelimitedString(userAccessGroups);
List<string> pageItemAccessGroupList = StringHelpers.SplitAndTrimCommaDelimitedString(pageItemAccessGroups);
return userAccessGroupsList.Any(userAccessGroup => pageItemAccessGroupList.Any(pageItemAccessGroup => userAccessGroup == pageItemAccessGroup));
}
public static List<string> SplitAndTrimCommaDelimitedString(string line) {
return line.Split(',').Select(s => s.Trim()).ToList();
}