ASP.NET.4.5.1+MVC5.0设置系统角色与权限(二)

ASP.NET.4.5.1+MVC5.0设置系统角色与权限(二)

用户管理

ASP.NET.4.5.1+MVC5.0设置系统角色与权限(二)

Controller代码

复制代码 代码如下:


public class SystemUserController : Controller
    {
        //public void Log()
        //{
        //    string meg = "";
        //    int user = int.Parse(CookieHelper.GetValue("SysUserID"));
        //    string ip = IPHelper.GetUserIp;
        //    foreach (var item in RouteData.Values)
        //    {
        //        meg += item.Key.ToUpper() + ":" + item.Value + "|";
        //    }
        //    AuditLogConfig.InsertAuditLog(meg, user, ip);
        //}
        SystemUserRepository sysuserrepository = new SystemUserRepository();
        RoleRepository roleryrepository = new RoleRepository();
        DepartmentRepository departmentryrepository = new DepartmentRepository();
        JobRepository jobryrepository = new JobRepository();
        #region 列表页
        /// <summary>
        /// 列表页
        /// </summary>
        /// <param>当前页</param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Index(int? page)
        {
            ViewBag.ControllerName=RouteData.Values["controller"].ToString().ToLower();
            CookieHelper.Del("LogName");
            var source = sysuserrepository.GetPageModelList(10, page ?? 1, -1);
            return View(source);
        }
        #endregion
        #region 添加管理员
        /// <summary>
        /// 添加页
        /// </summary>
        /// <param>管理员实体类</param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Add(SystemUser model)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            var rolelist = roleryrepository.GetModelListByState(1);
            ViewBag.RoleID = SelectListExtension.ToSelectList(rolelist, d => d.RoleName, d => d.RoleID.ToString());
            var departmentlist = departmentryrepository.GetModelListByState(1);
            ViewBag.DepartmentID = SelectListExtension.ToSelectList(departmentlist, d => d.DepartmentName, d => d.DepartmentID.ToString());
            var joblist = jobryrepository.GetModelListByState(1);
            ViewBag.JobID = SelectListExtension.ToSelectList(joblist, d => d.JobName, d => d.JobID.ToString());
            // ViewData["State"] = SelectListHelper.GetIsOrNoSelectList(0);
            return View();
        }
        /// <summary>
        /// 添加事件
        /// </summary>
        /// <param>实体类</param>
        /// <param></param>
        /// <returns></returns>
        [Authorize, HttpPost, ValidateInput(false)]
        public ActionResult Add(SystemUser model, FormCollection fc)
        {
            model.State = 1;
            model.CreateTime = DateTime.Now;
            sysuserrepository.SaveOrEditModel(model);
            return RedirectToAction("Index");
        }
        #endregion
        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param>删除编号</param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Delete(int id)
        {
            try
            {
                var model = sysuserrepository.GetModel(id);
                if (model != null)
                {
                    sysuserrepository.DeleteModel(id);
                    return RedirectToAction("Index");
                }
                else
                    return View("404");
                //throw new Exception("方法出错!");
            }
            catch (Exception ex)
            {
                Log4netHelper.InvokeErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "删除方法出错-Author:ryanding IP地址[" + IPHelper.GetUserIp + "]" + "操作人ID:" + CookieHelper.GetValue("SystemUserID"), ex);
                return View("404");
            }
        }
        #endregion
        #region 查询
        [Authorize]
        public ActionResult Search(int? page)
        {
            string logname = "";
            if (page == null)
            {
                logname = Request.Form["KeyWord"];
                CookieHelper.SetObj("KeyWord", 0, logname);
            }
            else
                logname = CookieHelper.GetValue("KeyWord");
            var query = QueryBuilder.Create<SystemUser>().Like(d => d.LogName, logname);
            var source = sysuserrepository.GetModelList().Where(query.Expression);
            return View(new Pager<SystemUser>(source, page ?? 1));
        }
        #endregion
        #region 编辑
        [Authorize]
        public ActionResult Edit(int id)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            var model = sysuserrepository.GetModel(id);
            if (model != null)
            {
                var rolelist = roleryrepository.GetModelListByState(1);
                ViewBag.RoleID = SelectListExtension.ToSelectList(rolelist, d => d.RoleName, d => d.RoleID.ToString(),model.RoleID);
                var departmentlist = departmentryrepository.GetModelListByState(1);
                ViewBag.DepartmentID = SelectListExtension.ToSelectList(departmentlist, d => d.DepartmentName, d => d.DepartmentID.ToString(), model.DepartmentID);
                var joblist = jobryrepository.GetModelListByState(1);
                ViewBag.JobID = SelectListExtension.ToSelectList(joblist, d => d.JobName, d => d.JobID.ToString(), model.JobID);
                ViewData["State"] = SelectListHelper.GetStateSelectList(model.State);
                return View(model);
            }
            else
                return View("404");
        }
        [Authorize, AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection fc)
        {
            var model = sysuserrepository.GetModel(id);
            if (model != null)
            {
                string password = model.Password;
                if (Request.Form["PassWord"] != "")
                {
                    UpdateModel(model);
                    sysuserrepository.SaveOrEditModel(model);
                }
                else
                {
                    UpdateModel(model);
                    model.Password = password;
                    sysuserrepository.SaveOrEditModel(model);
                }
                return RedirectToAction("index");
            }
            else
                return View("404");
        }
        #endregion
        #region 用户名是否可用
        //检测用户名是否可用
        [HttpPost]
        public ActionResult GetUsers(string param, string name)
        {
            if (sysuserrepository.IsExist(param))
                return Content("y");
            else
                return Content("用户名已存在");
        }
        #endregion
        public bool Login()
        {
            try
            {
                //TODO:
                throw new Exception("方法出错!");
            }
            catch (Exception ex)
            {
                Log4netHelper.InvokeErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "登录方法Login出错-Author:ryanding IP地址[" + IPHelper.GetUserIp + "]" + "操作人ID:" + CookieHelper.GetValue("SystemUserID"), ex);
                return false;
            }
            return true;
        }
    }

View代码

Index.cshtml

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjzwdj.html