Ejemplo n.º 1
0
        /// <summary>
        /// 添加菜单权限
        /// </summary>
        /// <param name="roleIds"></param>
        /// <returns></returns>
        public ActionResult AddMenuPermission(string roleIdsAndpermValues, long funcId)
        {
            bool        addSuccess = true;
            List <long> roleList   = new List <long>();
            List <int>  permList   = new List <int>();

            if (string.IsNullOrEmpty(roleIdsAndpermValues))
            {
                return(OperateContext.Current.RedirectAjax(HolidaysWebConst.HolidaysWebConst.FAIL, "请先添加角色", null, null));
            }
            //获取角色Id和对应的权限值
            foreach (var data in roleIdsAndpermValues.Split(';'))
            {
                roleList.Add(Convert.ToInt64(data.Split(',')[0]));
                permList.Add(Convert.ToInt32(data.Split(',')[1]));
            }
            //获取选中菜单信息
            var func = OperateContext.Current.BLLSession.IFunctionBLL.GetListBy(h => h.IsDeleted == false && h.Id == funcId).FirstOrDefault();
            //获取所有的角色
            var allRoles = OperateContext.Current.BLLSession.IRoleBLL.GetListBy(s => s.IsDeleted == false);

            //遍历所有角色
            foreach (var role in allRoles)
            {
                string menuPermName = $"{func.MenuName}|{role.Name}|权限";
                if (roleList.Contains(role.Id))//如果角色被选中,则添加权限,并且记录角色权限关系表中
                {
                    //查看当前权限以及中间表中记录是否已经存在
                    bool permIsExist = OperateContext.Current.BLLSession.IPermissionBLL.GetListBy(s => s.IsDeleted == false && s.Name == menuPermName).Count > 0;
                    Guid permGUID    = Guid.NewGuid();
                    if (permIsExist)
                    {
                        permGUID = OperateContext.Current.BLLSession.IPermissionBLL.GetListBy(s => s.IsDeleted == false && s.Name == menuPermName).FirstOrDefault().GUID;
                    }
                    bool middleTableIsExist = OperateContext.Current.BLLSession.IFuncPermissionBLL.GetListBy(s => s.MemberId == role.GUID && s.FuncId == permGUID).Count > 0;
                    int  permValue          = (int)permList.FirstOrDefault();
                    bool changeIsPermValue  = OperateContext.Current.BLLSession.IFuncPermissionBLL.GetListBy(s => s.MemberId == role.GUID && s.FuncId == permGUID && s.PermValue != permValue).Count > 0;

                    if (permIsExist == true && middleTableIsExist == false)//中间表中记录不存在
                    {
                        var currentPerm = OperateContext.Current.BLLSession.IPermissionBLL.GetListBy(s => s.Name == menuPermName).FirstOrDefault();
                        //给当前角色加上当前权限
                        FuncPermission fp = new FuncPermission();
                        fp.FuncId     = currentPerm.GUID;
                        fp.MemberId   = role.GUID;
                        fp.MemberType = (int)MemberEnum.MemberEnum.Role;
                        fp.PermValue  = permList.FirstOrDefault();
                        var result = OperateContext.Current.BLLSession.IFuncPermissionBLL.Add(fp);
                        if (result != 1)
                        {
                            addSuccess = false;
                        }
                    }
                    else if (permIsExist && middleTableIsExist && changeIsPermValue)//改变的是权限值
                    {
                        var currentPerm     = OperateContext.Current.BLLSession.IPermissionBLL.GetListBy(s => s.IsDeleted == false && s.Name == menuPermName).FirstOrDefault();
                        var currentFuncPerm = OperateContext.Current.BLLSession.IFuncPermissionBLL.GetListBy(s => s.MemberId == role.GUID && s.FuncId == currentPerm.GUID).FirstOrDefault();
                        if (currentFuncPerm == null)
                        {
                            //向中间表插入数据
                            FuncPermission fp = new FuncPermission();
                            fp.FuncId       = currentPerm.GUID;
                            fp.MemberId     = role.GUID;
                            fp.MemberType   = (int)MemberEnum.MemberEnum.Role;
                            fp.PermValue    = permList.FirstOrDefault();
                            addSuccess      = OperateContext.Current.BLLSession.IFuncPermissionBLL.Add(fp) == 1;
                            currentFuncPerm = fp;
                        }
                        currentFuncPerm.PermValue = permList.FirstOrDefault();
                        var result = OperateContext.Current.BLLSession.IFuncPermissionBLL.Modify(currentFuncPerm, "PermValue");
                        if (result != 1)
                        {
                            addSuccess = false;
                        }
                    }
                    else if (permIsExist && middleTableIsExist && !changeIsPermValue)
                    {
                        continue;
                    }
                    else
                    {
                        Permission perm = new Permission();
                        perm.GUID        = Guid.NewGuid();
                        perm.Name        = menuPermName;
                        perm.ParentId    = 0;
                        perm.Type        = 1;//菜单权限
                        perm.Url         = func.Url;
                        perm.CreateTime  = DateTime.Now;
                        perm.Description = string.Empty;
                        perm.PermValue   = permList.FirstOrDefault();
                        perm.IsDeleted   = false;
                        //添加权限
                        int addPermResult = OperateContext.Current.BLLSession.IPermissionBLL.Add(perm);
                        //向中间表插入数据
                        FuncPermission fp = new FuncPermission();
                        fp.FuncId     = perm.GUID;
                        fp.MemberId   = role.GUID;
                        fp.MemberType = (int)MemberEnum.MemberEnum.Role;
                        fp.PermValue  = permList.FirstOrDefault();
                        int addMiddleTableResult = OperateContext.Current.BLLSession.IFuncPermissionBLL.Add(fp);
                        if (addPermResult != 1 || addMiddleTableResult != 1)
                        {
                            addSuccess = false;
                        }
                    }
                }
                else//否则直接删除关于这些角色的中间表中的记录
                {
                    OperateContext.Current.BLLSession.IFuncPermissionBLL.DelBy(s => s.MemberId == role.GUID);
                }
            }

            if (!addSuccess)
            {
                return(OperateContext.Current.RedirectAjax(HolidaysWebConst.HolidaysWebConst.FAIL, "添加失败", null, null));
            }
            return(OperateContext.Current.RedirectAjax(HolidaysWebConst.HolidaysWebConst.SUCCESS, "添加成功", null, null));
        }
Ejemplo n.º 2
0
        public ActionResult AddRole(AddRoleViewModel addRoleView)
        {
            var    CurrentRoleId = addRoleView.Id;
            string state         = HolidaysWebConst.HolidaysWebConst.SUCCESS;
            //验证传入的数据
            string msg = Validate.ValidateString(
                new CustomValidate()
            {
                FieldName = "角色名", FieldValue = addRoleView.Name, MinLength = 1, MaxLength = 25, IsRequired = true
            });

            if (msg == null)
            {
                int addSuccess;
                if (ExistRepeatNameRole(addRoleView.Name, addRoleView.Id))
                {
                    msg   = "已经存在同名角色!";
                    state = HolidaysWebConst.HolidaysWebConst.FAIL;
                    return(OperateContext.Current.RedirectAjax(state, msg, null, null));
                }

                else if (addRoleView.Id > 0)//编辑角色
                {
                    Role role = OperateContext.Current.BLLSession.IRoleBLL.GetListBy(s => s.IsDeleted == false && s.Id == addRoleView.Id).FirstOrDefault();
                    role.Name        = addRoleView.Name;
                    role.Description = addRoleView.Description;
                    addSuccess       = OperateContext.Current.BLLSession.IRoleBLL.Modify(role, "Name", "Description");
                    if (addSuccess != 1)//添加失败
                    {
                        state = HolidaysWebConst.HolidaysWebConst.FAIL;
                        msg   = "添加失败";
                        return(OperateContext.Current.RedirectAjax(msg, state, null, null));
                    }
                }
                else//添加角色
                {
                    Role role = new Role()
                    {
                        GUID        = Guid.NewGuid(),
                        Name        = addRoleView.Name,
                        Description = addRoleView.Description,
                        CreateTime  = DateTime.Now,
                        IsDeleted   = false
                    };
                    addSuccess = OperateContext.Current.BLLSession.IRoleBLL.Add(role);
                    if (addSuccess != 1)//添加失败
                    {
                        state = HolidaysWebConst.HolidaysWebConst.FAIL;
                        msg   = "添加失败";
                        return(OperateContext.Current.RedirectAjax(msg, state, null, null));
                    }
                    else
                    {
                        CurrentRoleId = role.Id;
                    }
                }
                if (!string.IsNullOrEmpty(addRoleView.MenuIds))
                {
                    //当前角色
                    var currentRole = OperateContext.Current.BLLSession.IRoleBLL.GetListBy(s => s.IsDeleted == false && s.Id == CurrentRoleId).FirstOrDefault();
                    //找到当前角色的所有角色菜单关系表中的所有记录并删除
                    var         RolePermWithCurrentRole = OperateContext.Current.BLLSession.IFuncPermissionBLL.GetListBy(s => s.MemberId == currentRole.GUID);
                    List <Guid> funcIds = (from a in RolePermWithCurrentRole
                                           select a.FuncId).ToList();
                    int delResult = OperateContext.Current.BLLSession.IFuncPermissionBLL.DelBy(s => s.MemberId == currentRole.GUID && funcIds.Contains(s.FuncId));


                    //添加当前角色菜单权限关系
                    foreach (var menuId in addRoleView.MenuIds.Split(','))
                    {
                        long tmpMenuId = Convert.ToInt64(menuId);
                        //当前菜单
                        var    currentMenu  = OperateContext.Current.BLLSession.IFunctionBLL.GetListBy(s => s.IsDeleted == false && s.Id == tmpMenuId).FirstOrDefault();
                        string menuPermName = $"{currentMenu.MenuName}|{currentRole.Name}|权限";
                        //当前菜单权限
                        var currentMenuPerm = OperateContext.Current.BLLSession.IPermissionBLL.GetListBy(s => s.Name == menuPermName).FirstOrDefault();

                        //如果对于这个菜单的菜单权限还不存在,添加一个菜单权限
                        if (currentMenuPerm == null)
                        {
                            Permission pm = new Permission();
                            pm.Name        = $"{currentMenu.MenuName}|{currentRole.Name}|权限";
                            pm.GUID        = Guid.NewGuid();
                            pm.ParentId    = currentMenu.ParentId;
                            pm.PermValue   = 1;
                            pm.Type        = 1;//菜单权限
                            pm.Url         = currentMenu.Url;
                            pm.CreateTime  = DateTime.Now;
                            pm.Description = string.Empty;
                            pm.IsDeleted   = false;
                            int addPermResult = OperateContext.Current.BLLSession.IPermissionBLL.Add(pm);
                            currentMenuPerm = addPermResult == 1 ? pm : currentMenuPerm;
                            state           = addPermResult != 1 ? HolidaysWebConst.HolidaysWebConst.FAIL : HolidaysWebConst.HolidaysWebConst.SUCCESS;
                            msg             = addPermResult != 1 ? "添加失败" : "添加成功";
                        }
                        //给当前用户添加角色
                        //todo:这里没有菜单权限,必须要先添加菜单权限,然后在添加到关系表中
                        //查看菜单权限关系表中是否有该记录
                        bool menuPermIsExist = OperateContext.Current.BLLSession.IFuncPermissionBLL.GetListBy(s => s.MemberId == currentRole.GUID && s.FuncId == currentMenuPerm.GUID).Count > 0;
                        if (!menuPermIsExist)//如果不存在
                        {
                            FuncPermission fp = new FuncPermission();
                            fp.MemberId   = currentRole.GUID;
                            fp.FuncId     = currentMenuPerm.GUID;
                            fp.PermValue  = 1;
                            fp.MemberType = (int)MemberEnum.MemberEnum.Role;
                            var addMiddleTableResult = OperateContext.Current.BLLSession.IFuncPermissionBLL.Add(fp);
                            if (addMiddleTableResult != 1)
                            {
                                state = HolidaysWebConst.HolidaysWebConst.FAIL;
                                msg   = "添加失败";
                            }
                        }
                    }
                }
            }
            return(OperateContext.Current.RedirectAjax(state, msg, null, null));
        }