Exemplo n.º 1
0
        public ActionResult Authorize(int id)
        {
            Session["CurrentRoleId"] = id;

            //Lấy danh sách action đã được gán cho Role
            var listActionGranted = (from a in _actionService.GetListActionByRoleId(id)
                                     select new RightAction {
                Id = a.Id, ParentId = a.ParentId ?? 0, Name = a.Name, IsGranted = true
            }).ToList();

            //Lấy tất cả danh sách action
            var listAction = (from b in _actionService.GetAll()
                              select new RightAction {
                Id = b.Id, ParentId = b.ParentId ?? 0, Name = b.Name, IsGranted = false
            }).ToList();

            //Lấy tất cả danh sách function được gán cho Role
            var listFunctionGranted = (from c in _functionService.GetListFunctionByRoleId(id)
                                       select new RightFunction {
                Id = c.Id, Code = c.Code, Description = c.Description, IsGranted = true
            }).ToList();

            //Lấy tất cả danh sách function
            var listFunction = (from d in _functionService.GetAll()
                                select new RightFunction {
                Id = d.Id, Code = d.Code, Description = d.Description, IsGranted = false
            }).ToList();

            foreach (var function in listFunction)
            {
                if (!listFunctionGranted.Select(x => x.Id).Contains(function.Id))
                {
                    listFunctionGranted.Add(function);
                }
            }


            foreach (var action in listAction)
            {
                if (!listActionGranted.Select(x => x.Id).Contains(action.Id))
                {
                    listActionGranted.Add(action);
                }
            }

            var listActionTreeView = new List <ActionTreeViewModel>();

            var menuParent = _actionService.GetMenuParent();

            foreach (var parent in menuParent)
            {
                var actionTreeView = new ActionTreeViewModel
                {
                    Id      = parent.Id,
                    Name    = parent.Name,
                    Actions = listActionGranted.Where(x => x.ParentId == parent.Id).ToList()
                };
                listActionTreeView.Add(actionTreeView);
            }

            ViewBag.ListFunctionGranted = listFunctionGranted;
            return(View(listActionTreeView));
        }