Search code examples
asp.net-mvc-3linq-to-entitiesasp.net-roles

filter linq query based on role of logged in user MVC3 Controller


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);

    }

Solution

  • 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);
    
    }