过滤记录列表User.IsInRole MVC

时间:2015-11-11 16:28:59

标签: linq asp.net-mvc-4 permissions isinrole

我有16个不同的角色,我试图根据用户角色过滤列表。我希望有一种方法可以使用LINQ过滤这个,但我不确定最好的方法。

 [HttpPost]
    public ActionResult Login(int? Group, int? ListDept, int DivisionID)
    {

        LoginViewModel login = new LoginViewModel();

        login.GroupID = Group;
        login.ListDeptID = ListDept;
        login.DivisionID = DivisionID;

        bool rolecheck1 = false;
        bool rolecheck2 = false;
        bool rolecheck3 = false;

        rolecheck1 = User.IsInRole("Role1");
        rolecheck2 = User.IsInRole("Role2");
        rolecheck3 = User.IsInRole("Role3");

        if (rolecheck1 == true && login.GroupID == 1)
        {
            login.GroupID = 1;
        }
        if (rolecheck2 == true && login.GroupID == 2)
        {
            login.GroupID = 2;
        }
        if (rolecheck3 == true && login.GroupID == 3)
        {
            login.GroupID = 3;
        }
        Session["Login"] = login;
        return RedirectToAction("Index", "Case");
    }

发布后,它进入索引页面,我试图过滤这些列表,我认为我完全错了:/。这是我加载记录列表的索引页面。我还过滤了一个分配给一个人的案例,它工作正常。但是,如何过滤这些权限以显示某人拥有该权限的记录?

public ActionResult Index()
    {
        LoginViewModel login = new LoginViewModel();
        login = (LoginViewModel)Session["Login"];

        // uses the DirectorySearcher to find a users email address to use the "assignedTo" for filtering
        var assignedto = User.Identity.Name.Split('\\')[1];
        string emailAddr = null;
        try
        {
            DirectorySearcher searcher = new DirectorySearcher();
            searcher.Filter = string.Format("sAMAccountName={0}", assignedto);
            SearchResult user = searcher.FindOne();
            emailAddr = user.Properties["mail"][0].ToString();
        }
        catch
        {
            TempData["loginErrorMessage"] = String.Format("Directory Services is down or you have an invalid account");
            return RedirectToAction("Login");
        }

        CaseListViewModel cases = new CaseListViewModel();
        cases.OpenCases = db.Cases.Where(c => c.StatusID == 1 && c.AssignedTo == emailAddr || login.DivisionID == c.DivisionID && login.GroupID == c.GroupID);
        cases.ClosedCases = db.Cases.Where(c => c.StatusID == 2);


        // Before added email filtering for assigned to
        //cases.OpenCases = db.Cases.Where(c => c.StatusID == 1);

        // Loop that checks each case and compares DateTime.Now to Lockout Stamp to unlock cases

        foreach (var cased in cases.OpenCases)
        {
            DateTime LockoutDate = new DateTime();

            if (cased.Lockout_TS.HasValue)
            {
                LockoutDate = (DateTime)cased.Lockout_TS;
            }

            if (cased.LockCase == true)
            {
                bool shouldUnlock = (DateTime.Now - LockoutDate).TotalMinutes > 15;

                //if yes, then unlock it
                if (shouldUnlock)
                {
                    cased.LockCase = false;
                    cased.Lockout_TS = null;
                }


            }
        }
        // End Loop
        db.SaveChanges();
        return View(cases);
    }

非常感谢任何帮助 谢谢亚当

1 个答案:

答案 0 :(得分:0)

您可以在字典中记录角色与组的关联,并使用Linq根据用户的角色确定用户组。

  var roleGroupMap = new Dictionary<string, int> {
    ["Role1"] = 1,
    ["Role2"] = 2,
    ["Role3"] = 3
  };

 ...

  var userRole = roleGroupMap.Keys.FirstOrDefault(x => User.IsInRole(x));
  if (userRole != null) {
    login.GroupID = roleGroupMap[userRole];
  }

此模型将容纳具有单一角色的用户。如果用户可以拥有多个角色,则需要修改LoginViewModel以记录组和关联组的集合。

收集GroupIDs

roleGroupMap.Keys
  .Where(x => User.IsInRole(x))
  .ToList()
  .ForEach(x => login.GroupIDs.Add(x => User.IsInRole(x)))