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")));
        }
Example #2
0
        public ActionResult Create([FromForm] SaveMenuModel model)
        {
            // TODO: Add insert logic here
            //查询是否存在该菜单
            if (SystemMenuHelper.Exists(p => p.Name == model.ControllName && p.ActionRoute == model.ControllUrl))
            {
                return(Json(ResponseHelper.Error("该菜单已经存在了!")));
            }
            var MenuModel = new SystemMenu
            {
                ActionRoute = model.ControllUrl.ToLower(),
                CreatedTime = DateTime.Now,
                Icon        = model.Icon,
                IsUse       = Convert.ToBoolean(model.Status),
                Name        = model.ControllName,
                ParentId    = Convert.ToInt32(model.ParentCode),
                Remark      = model.Remark,
                Sort        = model.Sort,
                Type        = model.ControllType
            };
            var menuId = SystemMenuHelper.Insert(MenuModel);

            //给系统预留角色把权限绑上
            RoleAuthMappingHelper.Insert(new RoleAuthMapping {
                CreatedTime = DateTime.Now, MenuId = menuId, RoleId = 1
            });
            if (CacheUser.UId == "1")
            {
                CacheUser.AuthMenu.Add(menuId);
                HttpContext.Session.SetString("UserInfo", JsonConvert.SerializeObject(CacheUser));
            }
            return(Json(ResponseHelper.Success("ok")));
        }
        public ActionResult SignIn([FromForm] SignInModel model)
        {
            try
            {
                var UserString = HttpContext.Session.GetString("UserInfo");

                if (!string.IsNullOrEmpty(UserString))
                {
                    return(Json(ResponseHelper.Success(JsonConvert.DeserializeObject <SessionUser>(UserString))));
                }
                // TODO: Add login logic here
                var password = AESUtil.Md5(model.Password);
                var user     = SystemUserHelper.GetModel(p => p.Password == password && (p.Email == model.Account || p.Account == model.Account || p.Phone == model.Account));
                if (user == null)
                {
                    return(Json(ResponseHelper.Error("账户或密码错误,请确认后再试!")));
                }
                if (user.Status != SystemUser_Status_Enum.正常)
                {
                    return(Json(ResponseHelper.Error("账户已被冻结!")));
                }
                //获取用户角色
                var role = SystemUserRoleMappingHelper.GetRoleBy(user.Id);
                if (role == null)
                {
                    return(Json(ResponseHelper.Error("该账户还未分配角色请联系管理员!")));
                }
                var roleAuthList = RoleAuthMappingHelper.GetList(p => p.RoleId == role.Id);
                if (roleAuthList.Count < 1)
                {
                    return(Json(ResponseHelper.Error("角色未拥有权限,请联系下管理员处理")));
                }
                var token = Guid.NewGuid().ToString("N");
                //存session
                var UserCache = new SessionUser
                {
                    HeadImg  = user.HeadImage,
                    Token    = token,
                    UId      = user.Id.ToString(),
                    UserName = user.NickName,
                    RoleId   = role.Id,
                    Email    = user.Email,
                    RoleName = role.Name,
                    AuthMenu = roleAuthList.Select(p => p.MenuId).ToList()
                };
                HttpContext.Session.SetString("UserInfo", JsonConvert.SerializeObject(UserCache));
                //返回用户信息
                return(Json(ResponseHelper.Success(UserCache)));
            }
            catch
            {
                return(View());
            }
        }
        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))));
        }