1

this a newbie question, I am designing a class that is going to hold user details. One part tha thas me scratching my head is "permission"

here is the scenario. each user belong to multiple cities. and in that city that could have various permissions.

for example, John is in NY and DC, in NY he has permissions 1,2,3 and in DC 3,4

i dont know how to design my class to take this into account.

my table, which i did not design) looks like this(with some sample data):

userID  City   permission
John    NY     1
John    NY     2
John    NY     3
John    DC     3
John    DC     4
Jane    DC     1
Jane    DC     2
Jane    NY     6

in my C# class, i have originally written them out separately like so:

     public class User
        {

            public string userid { get; set; }
            public string name{ get; set; } //from main user table
            public string address{ get; set; } //from main user table
            public List<string> Citites{ get; set; } //from usercitypermission table
            public List<string> userRole { get; set; }//from usercitypermission table

public User(string username)
    {
        this.userid = username;

        DataSet ds = new DataSet();
        SqlConnection conn = new SqlConnection(cCon.getConn());

        SqlCommand cmd = new SqlCommand("sp_getUserDetails", conn);

        // 2. set the command object so it knows
        // to execute a stored procedure
        cmd.CommandType = CommandType.StoredProcedure;

        // 3. add parameter to command, which
        // will be passed to the stored procedure
        cmd.Parameters.Add(
            new SqlParameter("@userName", username));

        try
        {
            // Open the connection and execute the Command
            conn.Open();
            SqlDataAdapter sqlDA = new SqlDataAdapter();

            sqlDA.SelectCommand = cmd;
            sqlDA.Fill(ds);
        }
        catch (Exception ex)
        {
            throw (ex);
        }
        finally
        {
            conn.Close();
        }

        this.userRole = new List<string>();
        foreach (DataTable DT in ds.Tables)
        {
            foreach (DataRow dr in DT.Rows)
            {
                this.userRole.Add(dr["permission"].ToString());
            }
        }
    }

when i set these, i just run a distinct on my tblUserCityPermission, so my Cities holds a distinct list of Cities the user is in (i didn't post the code for this, but it's just a stored procedure tha ti run, similar to the one above) and the userRole holds all permissions for that user (1-10) (shown above), but it does not take into account what City that permission applies to.

i hope this makes sense. :-/

i guess my question is, how do i design the class to make sure my permissions are tied to each specific City(or Cities) the user is in. should i be using a type other than List? Am i way off here?

2
  • So a user can have multiple cities, and each city has multiple permissions? Commented Mar 27, 2013 at 19:06
  • that is correct. so in my code, i now display a list of cities the user belogs to, after they pick one, i then need to use that user's cities permissions to show proper items on the form. So why both John and Jane belong to NY, they will be able to see different things, because of their permissions in that city. Commented Mar 27, 2013 at 19:08

3 Answers 3

2

You will need to create a class for the cities and their permissions as well. Something along the lines of:

class CityPermission
{
    public string Name;
    public List<int> Permissions{ get; set; }
}

No you have the permissions as a property of the cities. Which in turn are a property of the user:

class User
{
    .
    .
    .
    public List<CityPermission> Citites{ get; set; }
    .
    .
    .
}
Sign up to request clarification or add additional context in comments.

Comments

1

As an alternative to defining a type, you could use a Dictionary:

public Dictionary<string, List<string>> Permissions { get; set; }

Where the key is the city name, and the value is a list of permissions appropriate to that city.

Comments

0

You can store all this in a multi-level dictionary, like this:

    Dictionary<string, Dictionary<string, Dictionary<int, int>>> UserCityPermissions = new Dictionary<string, Dictionary<string, Dictionary<int, int>>>();
    UserCityPermissions.Add("John", new Dictionary<string, Dictionary<int, int>>());
    UserCityPermissions["John"].Add("NY", new Dictionary<int,int>());
    UserCityPermissions["John"]["NY"].Add(1, 1);
    UserCityPermissions["John"]["NY"].Add(2, 2);
    UserCityPermissions["John"]["NY"].Add(3, 3);
    UserCityPermissions["John"].Add("DC", new Dictionary<int, int>());
    UserCityPermissions["John"]["DC"].Add(3, 3);
    UserCityPermissions["John"]["DC"].Add(4, 4);
    UserCityPermissions.Add("Jane", new Dictionary<string, Dictionary<int, int>>());
    UserCityPermissions["Jane"].Add("DC", new Dictionary<int, int>());
    UserCityPermissions["Jane"]["DC"].Add(1, 1);
    UserCityPermissions["Jane"]["DC"].Add(2, 2);
    UserCityPermissions["Jane"].Add("NY", new Dictionary<int, int>());
    UserCityPermissions["Jane"]["NY"].Add(6, 6);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.