过滤记录列表 User.IsInRole MVC

filtering a recordlist User.IsInRole MVC

我有 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");
    }

在 Post 之后它会转到一个索引页面,我试图在其中过滤那些列表,但我认为我的做法完全错误了:/。这是我正在加载记录列表的索引页面。我还过滤了一个分配给一个人的案例,它工作正常。但是我如何过滤这些权限以显示某人拥有权限的记录?

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

如有任何帮助,我们将不胜感激。
谢谢亚当

您可以在字典中记录角色与组的关联,并使用 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 以记录 collection 个组和关联的组。

collection 的 GroupIDs

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