Esempio n. 1
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var managerInfo = filterContext.RequestContext.HttpContext.Request.Cookies["managerInfo"]?.Value;

            if (managerInfo == "null" || managerInfo == null)
            {
                return;
            }

            var controller = (filterContext.RouteData.Values["controller"] as string).FirstToUpper();
            var action     = (filterContext.RouteData.Values["action"] as string).FirstToUpper();

            if (controller == "zero" || controller == "error" || controller == "login")
            {
                return;
            }
            var url = filterContext.HttpContext.Request.Url.ToString();
            //..\Company\Index

            var actionUrl = string.Format("..\\{0}\\{1}", controller, action);

            var materialActions = _actionBll.GetEntities(m => m.Action_Url == actionUrl);

            if (materialActions == null || materialActions.Count == 0)
            {
                return;
            }
            var materialAction = _actionBll.GetEntity(m => m.Action_Url == actionUrl);

            if (materialAction == null)
            {
                return;
            }

            Material_Teacher materialTeacher = _teacherBll.GetEntity(m => m.Teacher_Name == managerInfo || m.Teacher_Id == managerInfo);

            var roleIds = _roleTeacherBll.GetEntities(m => m.Teacher_Id == materialTeacher.Teacher_Id).Select(m => m.Role_Id).ToList();

            var actionIds = _roleActionBll.GetEntities(m => roleIds.Contains(m.Role_Id)).Select(m => m.Action_Id).ToList();

            if (actionIds.Contains(materialAction.Id))
            {
                return;
            }
            actionIds.AddRange(_teacherActionBll.GetEntities(m => m.Teacher_Id == materialTeacher.Teacher_Id && m.Has_Permission).Select(m => m.Action_Id).ToList());
            if (!actionIds.Contains(materialAction.Id))
            {
                filterContext.Result = new RedirectResult("..\\Error\\NoAuthority");
            }
        }
Esempio n. 2
0
        public string GetActionTree(List <int> id, string teacherId)
        {
            List <Material_Action> materialActions = _actionBll.GetEntities(m => m.Del_Flag == false);
            List <ActionTree>      trees           = new List <ActionTree>();

            foreach (Material_Action materialAction in materialActions)
            {
                //父节点
                if (materialAction.Menu_Id == 0)
                {
                    if (trees.FirstOrDefault(m => m.Value == materialAction.Id) == null)
                    {
                        trees.Add(new ActionTree
                        {
                            Title    = materialAction.Action_Name,
                            Value    = materialAction.Id,
                            Checked  = false,
                            Disabled = false
                        });
                    }
                }
                else//子节点
                {
                    //查找父节点
                    ActionTree tree = trees.FirstOrDefault(m => m.Value == materialAction.Menu_Id);
                    //没有找到父节点 添加父节点
                    if (tree == null)
                    {
                        Material_Action action = materialActions.Find(m => m.Id == materialAction.Menu_Id);
                        tree = new ActionTree
                        {
                            Title    = action.Action_Name,
                            Value    = action.Id,
                            Checked  = false,
                            Disabled = false
                        };
                        trees.Add(tree);
                    }

                    if (tree.Data == null)
                    {
                        tree.Data = new List <ActionTree>
                        {
                            new ActionTree
                            {
                                Title    = materialAction.Action_Name,
                                Value    = materialAction.Id,
                                Checked  = false,
                                Disabled = false,
                                Data     = new List <ActionTree>()
                            }
                        };
                    }
                    else
                    {
                        tree.Data.Add(new ActionTree
                        {
                            Title    = materialAction.Action_Name,
                            Value    = materialAction.Id,
                            Checked  = false,
                            Disabled = false,
                            Data     = new List <ActionTree>()
                        });
                    }
                }
            }
            //如果id不是null 选中该角色id的权限页面
            if (id != null)
            {
                List <int> actionIds = _roleActionBll.GetEntities(m => id.Contains(m.Role_Id)).Select(m => m.Action_Id).ToList();
                SetRoleActionChecked(actionIds, trees);
            }

            //如果teacherId 设置该用户的权限
            if (teacherId != null)
            {
                List <Material_Teacher_Action> materialTeacherActions = _teacherActionBll.GetEntities(m => m.Teacher_Id == teacherId);
                SetTeacherActionChecked(materialTeacherActions, trees);
            }

            var dataTrees = new
            {
                code = 0,
                data = trees,
                msg  = "获取成功"
            };

            return(JsonConvert.SerializeObject(dataTrees));
        }
Esempio n. 3
0
        //1. 首先删除原有权限,然后重新分配权限
        //2. 如果该页面是子页面,在给自己分配权限的同时需要为父菜单项分配权限
        //3. 如果该页面是父菜单项,但是没有分配权限,需要将子页面中的所有权限删除
        //4. 如果该页面是父菜单项,并且分配了权限,在给自己分配权限的同时,需要判断子页面的权限,如果子页面拥有的权限多余父菜单项,这需要将多余的权限删除。
        public bool SetRoleByAction(List <int> roleIds, int actionId)
        {
            IActionBll actionBll = UnityContainerHelper.Server <IActionBll>();
            //删除该页面原有的角色权限
            List <Material_Role_Action> roleActions = GetEntities(m => m.Action_Id == actionId);

            if (!DeleteEntity(roleActions))
            {
                return(false);
            }

            //重新分配角色权限
            List <Material_Role_Action> addRoleActions = new List <Material_Role_Action>();

            if (roleIds != null)
            {
                //获取该页面的父菜单Id
                Material_Action action = actionBll.Find(actionId);
                if (action == null)
                {
                    return(false);
                }

                int menuId = action.Menu_Id;
                foreach (int roleId in roleIds)
                {
                    //为该页面设置权限
                    addRoleActions.Add(new Material_Role_Action
                    {
                        Role_Id   = roleId,
                        Action_Id = actionId
                    });
                    Material_Role_Action roleAction = GetEntity(m => m.Action_Id == menuId && m.Role_Id == roleId);
                    //menuId != 0即拥有父菜单,并且该角色没有父菜单的权限则为该角色分配权限
                    if (menuId != 0 && roleAction == null)
                    {
                        addRoleActions.Add(new Material_Role_Action
                        {
                            Role_Id   = roleId,
                            Action_Id = menuId
                        });
                    }
                }
                //如果是父菜单,判断子页面拥有的角色是否多于父菜单 将多出的角色权限删除
                if (menuId == 0)
                {
                    List <int> ids = actionBll.GetEntities(m => m.Menu_Id == actionId).Select(m => m.Id).ToList();
                    List <Material_Role_Action> deleteRoleActions = new List <Material_Role_Action>();
                    foreach (int id in ids)
                    {
                        List <int> roleIdList = GetEntities(m => m.Action_Id == id).Select(m => m.Role_Id).ToList();
                        foreach (int roleId in roleIdList)
                        {
                            if (!roleIds.Contains(roleId))
                            {
                                deleteRoleActions.Add(GetEntity(m => m.Action_Id == id && m.Role_Id == roleId));
                            }
                        }
                    }

                    DeleteEntity(deleteRoleActions);
                }
            }
            else
            {
                //如果该页面是菜单并且没有为任何角色分配权限,则将该菜单项的子页面的所有角色权限删除
                if (actionBll.Find(actionId).Is_Menu)
                {
                    List <int> ids = actionBll.GetEntities(m => m.Menu_Id == actionId).Select(m => m.Id).ToList();
                    if (!DeleteEntity(GetEntities(m => ids.Contains(m.Action_Id))))
                    {
                        return(false);
                    }
                }
                else//如果该页面不是菜单且没有分配权限则结束操作
                {
                    return(true);
                }
            }

            //添加权限
            if (AddEntities(addRoleActions))
            {
                return(true);
            }

            return(false);
        }