0

What I am trying to do is filter a query based on the role of the logged in user in my MVC3 Controller. I am restricting users to have only one role; no user can have more than one role. I have some code snippets but I am not sure this is the way to go. And also if someone can assist me to achieve my goal

  string[] roles = Roles.GetRolesForUser();

    string color= roles[0];

    string vcolor = color.Substring(0, 4);

    switch (vcolor)
                {
                    case "Rewa":
                        vcolor = "white";
                        break;
                    case "Ukau":
                        vcolor == "black";
                        break;
                    case "Whau":
                        vcolor = "green";
                        break;
                    case "Angi":
                        vcolor = "Blue";
                        break;
                }

public ActionResult _MembersView()
    {
        var pagenew = db.Members

            .Where(u => u.color == vcolor)(I will want to then set this condition to the vcolor dynamically)
            .OrderBy(u => u.Tcd);
            //.Take(12);
        return PartialView(pagenew);

    }
Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
Diin
  • 565
  • 11
  • 41

1 Answers1

0

Well put your logic into a method:

private string GetVColorForCurrentUser()
{
    string[] roles = Roles.GetRolesForUser();
    string color = roles[0];
    string vcolor = color.Substring(0, 4);
    switch (vcolor)
    {
        case "Rewa":
            return "white";
        case "Ukau":
            return "black";
        case "Whau":
            return "green";
        case "Angi":
            return "Blue";
    }
    return vcolor;
} 

and then call this method:

public ActionResult _MembersView()
{
    var pagenew = db
        .Members
        .Where(u => u.color == GetVColorForCurrentUser())
        .OrderBy(u => u.Tcd);
    return PartialView(pagenew);
}

Or directly write it as a filter method:

private bool CurrentUserHasSameVColor(Member member)
{
    string[] roles = Roles.GetRolesForUser();
    string color = roles[0];
    string vcolor = color.Substring(0, 4);
    switch (vcolor)
    {
        case "Rewa":
            vcolor = "white";
            break;
        case "Ukau":
            vcolor = "black";
            break;
        case "Whau":
            vcolor = "green";
            break;
        case "Angi":
            vcolor = "Blue";
            break;
    }
    return member.color == vcolor;
}

and then:

public ActionResult _MembersView()
{
    var pagenew = db
        .Members
        .Where(CurrentUserHasSameVColor)
        .OrderBy(u => u.Tcd);
    return PartialView(pagenew);

}
CAbbott
  • 8,078
  • 4
  • 31
  • 38
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I believe that the OP is calling the `Roles.GetRolesForUser` method incorrectly. They should be passing a `username` to the method, otherwise it's only getting the roles for the currently logged in user. – CAbbott Jan 04 '13 at 16:07
  • @CAbbott, that's exactly what he wants => get the roles of the currently logged in user. And then filter the db.Members collection based on his roles. – Darin Dimitrov Jan 04 '13 at 16:09
  • if so, then why loop through the method `n Member` times? Why not get the current user color once and then use that in the LINQ Where? – CAbbott Jan 04 '13 at 16:12
  • Because he probably want to get all users that are in the same role as the currently authenticated user. As a matter of fact he hasn't clearly explained what he is trying to do. So we could only be playing some *guess who* games. – Darin Dimitrov Jan 04 '13 at 16:14
  • Thanks very much Maybe just to clarify. I have 1 superAdmin and 4 admins each admin is resposible for a color so What I want is when the admin logs in he/she should see only members whose colors have been assigned – Diin Jan 04 '13 at 16:23
  • Hi and thanks @Darin Dimitrov I tried the filter method and I get this error Cannot implicitly convert type 'string' to 'bool' referring to the switch case return values – Diin Jan 04 '13 at 16:26
  • I edited Darin's answer so the filter method always returns bool. – CAbbott Jan 04 '13 at 16:31
  • thanks @CAbbott It works perfectly well now. One more help if a person with superAdmin role logs in I don't want the members table to be filtered – Diin Jan 04 '13 at 16:46