Beispiel #1
0
        public ActionResult UserRole(int userId, int roleId)
        {
            var userModel = SystemUserRoleMappingHelper.GetUserRoleBy(userId);

            if (userModel == null)
            {
                return(new JsonResult(ResponseHelper.Error("用户不存在")));
            }
            if (roleId == userModel.RoleId)
            {
                return(new JsonResult(ResponseHelper.Success("ok")));
            }
            var roleModel = SystemRoleHelper.GetModel(p => p.Id == roleId);

            if (roleModel == null)
            {
                return(new JsonResult(ResponseHelper.Error("该角色不存在!")));
            }
            var userRoleMapping = SystemUserRoleMappingHelper.GetModel(p => p.SystemUserId == userId);

            if (userRoleMapping == null)
            {
                userRoleMapping = new SystemUserRoleMapping {
                    CreatedTime = DateTime.Now, RoleId = roleId, SystemUserId = userId
                };
                SystemUserRoleMappingHelper.Insert(userRoleMapping);
                return(new JsonResult(ResponseHelper.Success("ok")));
            }
            if (SystemUserRoleMappingHelper.Update(userRoleMapping, p => p.SystemUserId == userId, SystemUserRoleMappingHelper.Columns.RoleId))
            {
                return(new JsonResult(ResponseHelper.Success("ok")));
            }
            return(new JsonResult(ResponseHelper.Error("修改失败!")));
        }
        public ActionResult RoleMenu([FromForm] AuthSaveMenu model)
        {
            var role = SystemRoleHelper.GetModel(p => p.Id == model.RoleId);

            if (role == null)
            {
                return(Json(ResponseHelper.Error("不存在的角色!")));
            }
            var menuList = RoleAuthMappingHelper.GetList(p => p.RoleId == model.RoleId);

            if (menuList.Count > 0)
            {
                var menuStr = string.Join(",", menuList.Select(p => p.MenuId).ToArray());
                if (menuStr.Equals(model.Menus))
                {
                    return(Json(ResponseHelper.Success("OK")));
                }
            }
            RoleAuthMappingHelper.Delete(p => p.RoleId == model.RoleId);
            var menuArr = model.Menus.Split(",").ToList();
            List <RoleAuthMapping> insertList = new List <RoleAuthMapping>();

            foreach (var i in menuArr)
            {
                insertList.Add(new RoleAuthMapping
                {
                    CreatedTime = DateTime.Now,
                    MenuId      = Convert.ToInt32(i),
                    RoleId      = role.Id
                });
            }
            RoleAuthMappingHelper.InsertMany(insertList);
            return(Json(ResponseHelper.Success("ok")));
        }
        // GET: SystemRole/Edit/5
        public ActionResult Edit(int id)
        {
            if (id == 0)
            {
                return(new RedirectResult("/home/errormsg?msg=" + WebUtility.UrlEncode("错误的请求方式!")));
            }
            var role = SystemRoleHelper.GetModel(p => p.Id == id);

            if (role == null)
            {
                return(new RedirectResult("/home/errormsg?msg=未找到该角色"));
            }
            return(View(role));
        }
        public ActionResult RoleMenu(int id)
        {
            if (id == 0)
            {
                return(new RedirectResult("/home/errormsg?msg=" + WebUtility.UrlEncode("错误的请求方式!")));
            }
            var roleModel = SystemRoleHelper.GetModel(p => p.Id == id);

            if (roleModel == null)
            {
                new RedirectResult("/home/errormsg?msg=" + WebUtility.UrlEncode("不存在的角色!"));
            }
            return(View(roleModel));
        }
        public ActionResult Edit([FromForm] SystemRoleEdit ReqModel)
        {
            var role = SystemRoleHelper.GetModel(p => p.Id == ReqModel.Id);

            if (role == null)
            {
                return(new RedirectResult("/home/errormsg?msg=" + WebUtility.UrlEncode("未找到该角色")));
            }
            if (SystemRoleHelper.Exists(p => p.Id != role.Id && p.Name.Equals(ReqModel.RoleName)))
            {
                return(Json(ResponseHelper.Error("该角色名已经存在!")));
            }
            role.Name   = ReqModel.RoleName;
            role.Remark = ReqModel.Remark;
            SystemRoleHelper.Update(role, SystemRoleHelper.Columns.Name, SystemRoleHelper.Columns.Remark);
            return(Json(ResponseHelper.Success("ok")));
        }
        public ActionResult Menu(int id)
        {
            if (id == 0)
            {
                return(new RedirectResult("/home/errormsg?msg=" + WebUtility.UrlEncode("错误的请求方式!")));
            }
            //先查角色
            var roleModel = SystemRoleHelper.GetModel(p => p.Id == id);

            if (roleModel == null)
            {
                return(new RedirectResult("/home/errormsg?msg=" + WebUtility.UrlEncode("不存在的角色!")));
            }
            //在查角色对应的菜单
            var menuList = RoleAuthMappingHelper.GetList(p => p.RoleId == id);

            //在查当前用户拥有的菜单
            SystemMenuHelper.Columns.CreatedTime.SetOrderByAsc();
            var allMenu = SystemMenuHelper.GetList(p => p.IsUse == true && CacheUser.AuthMenu.Contains(p.Id), orderBy: SystemMenuHelper.Columns.CreatedTime);

            //递归菜单做成树
            return(Json(ResponseHelper.Success(CreateTree(allMenu, menuList, 0))));
        }