コード例 #1
0
        public ActionResult SaveChoose(Info info)
        {
            try
            {
                if (info.list.Count == 0)
                {
                    return(Json(new { state = 1, msg = "请至少选择一个角色" }));

                    ;
                }
                var usermodel = UserManager.GetInstance().GetItemById(info.userid);
                if (usermodel == null)
                {
                    return(Json(new { state = 1, msg = "未找到该用户信息" }));
                }

                //如果该用户已选择角色后则删除
                var list = UserForRoleManager.GetInstance().SelectList(m => m.userid == info.userid);
                if (list.Count > 0)
                {
                    foreach (var item in list)
                    {
                        item.isdelete   = true;
                        item.updatetime = DateTime.Now;
                        UserForRoleManager.GetInstance().Update(item);
                    }
                    UserForRoleManager.GetInstance().Save();
                }
                //保存
                foreach (var item in info.list.ToList())
                {
                    var model = new bjf_roleforuser()
                    {
                        userid     = info.userid,
                        roleid     = item.roleid,
                        createid   = 1,
                        updateid   = 1,
                        createtime = DateTime.Now,
                        updatetime = DateTime.Now
                    };
                    UserForRoleManager.GetInstance().Add(model);
                }
                UserForRoleManager.GetInstance().Save();
                return(Json(new { state = 0, msg = "保存成功" }));
            }
            catch (Exception ex)
            {
                LogHelper.log.Error(ex.Message);
                return(Json(new { state = 1, msg = ex.Message }));
            }
        }
コード例 #2
0
        public ActionResult ChooseRole(int userid)
        {
            var model = UserManager.GetInstance().GetItemById(userid);
            //获取所有角色
            var listrole = RoleManager.GetInstance().SelectList(m => m.isdelete == false && m.isuse == 0);
            //获取该用户对应的角色信息
            var listroleforuser = UserForRoleManager.GetInstance().SelectList(m => m.isdelete == false && m.userid == userid);
            var roleIdList      = new List <int>();

            if (listroleforuser.Count > 0)
            {
                roleIdList = listroleforuser.Select(m => m.roleid).ToList();
            }
            ViewBag.ListRole   = listrole;
            ViewBag.ListRoleId = roleIdList;
            return(View(model));
        }
コード例 #3
0
        public ActionResult Login(LoginQuery query)
        {
            try
            {
                if (Session["ValidateCode"] == null)
                {
                    return(Json(new { state = 1, msg = "验证码已过期,请重新输入" }));
                }
                if (Session["ValidateCode"].ToString().ToLower() != query.ValidCode.ToLower())  //验证码忽略大小写
                {
                    return(Json(new { state = 1, msg = "验证码错误" }));
                }

                //判断用户信息
                var item = UserManager.GetInstance().GetItemByLoginName(query.LoginName);
                if (item == null)
                {
                    return(Json(new { state = 1, msg = "此用户不存在." }));
                }
                if (query.PassWord != EncryAndDecryptHelper.Decryption(item.password))
                {
                    return(Json(new { state = 1, msg = "用户密码不对." }));
                }
                var roleList = UserForRoleManager.GetInstance().GetListRole(item.id);
                if (roleList.Count == 0)
                {
                    return(Json(new { state = 1, msg = "当前用户暂未分配角色,不能使用系统." }));
                }

                //用户全部角色集合
                string arrRoleid = "";
                roleList.ForEach(m => arrRoleid += m.roleid + ",");

                //用户可查看菜单集合
                var    menulist  = RoleForMenuManager.GetInstance().GetListMenuId(roleList);
                string arrMenuid = "";
                menulist.ForEach(m => arrMenuid += m.ToString() + ",");

                //记住用户名和密码
                if (query.Rember != null)
                {
                    //放进cookie中
                    Response.Cookies.Add(new HttpCookie("cookieLoginname", query.LoginName));
                    Response.Cookies.Add(new HttpCookie("cookiePassword", query.PassWord));
                    Response.Cookies["cookieLoginname"].Expires = DateTime.Now.AddDays(3);
                    Response.Cookies["cookiePassword"].Expires  = DateTime.Now.AddDays(3);
                }
                //验证通过将用户信息记录进session
                SessionHelper.SetSession("userinfo", item);
                SessionHelper.SetSession("userid", item.id);
                SessionHelper.SetSession("username", item.username);
                SessionHelper.SetSession("rolelist", arrRoleid);                //角色集合
                SessionHelper.SetSession("menulist", menulist);                 //菜单集合
                SessionHelper.SetSessionTimeout(20);

                //写登录日志
                LoginLogManager.GetInstance().SaveItem();
                return(Json(new { state = 0, msg = "登录成功." }));
            }
            catch (Exception e)
            {
                LogHelper.log.Error(e.Message);
                return(Json(new { state = 1, msg = e.Message }));
            }
        }