• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

系统角色篇

数据结构

用户管理

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 name="page">当前页</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 name="model">管理员实体类</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 name="model">实体类</param>
        /// <param name="fc"></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 name="id">删除编号</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

复制代码 代码如下:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model SJCRM.Helpers.Pager<SJCRM.Models.SystemUser>
<div class="contrgcon">
    <div class="contrg_list">
        <div class="rgtit">
            <form method="post" action="/@ViewBag.ControllerName/search">
                <dl>
                    <dd>关键词:</dd>
                    <dd>
                        <input name="KeyWord" type="text" class="rgtxt2" /></dd>
                    <dd>
                        <input type="submit" value="查 询" class="rgbut1" /></dd>
                </dl>
            </form>
        </div>
        <div class="rgc">
            <dl class="rgt11">
                <dt>
                    <span class="sple"><a href="/systemuser/add">添加用户</a></span>
                </dt>
            </dl>
            <div class="rg3">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td class="rgtd2">用户编号</td>
                        <td class="rgtd3">登录名</td>
                        <td class="rgtd3">手机</td>
                        <td class="rgtd3">Email</td>
                        <td class="rgtd3">昵称</td>
                        <td class="rgtd3">状态</td>
                        <td class="rgtd4">操作</td>
                    </tr>
                </table>
            </div>
            <div class="rg4">
                @{int classnum = 1;}
                @foreach (var item in Model)
                {
                    <table width="100%" border="0" cellspacing="0" cellpadding="0" @if (classnum % 2 == 0)
                                                                                   {<text>class="rgbg2"</text>}>
                        <tr>
                            <td class="rgtd2">@item.SystemUserID</td>
                            <td class="rgtd3">@(item.LogName)</td>
                            <td class="rgtd3">@item.Mobile</td>
                            <td class="rgtd3">@item.Email</td>
                            <td class="rgtd3">@item.TrueName</td>
                            <td class="rgtd3">@(item.State == 1 ? "有效" : "无效")</td>
                            <td class="rgtd4"><a href="/systemuser/delete/@item.SystemUserID">删除</a>&nbsp;|&nbsp;<a href="/systemuser/edit/@item.SystemUserID">编辑</a>&nbsp; </td>
                        </tr>
                    </table>
                                                                                   classnum = classnum + 1;
                }
            </div>
        </div>
        <div class="rg5">
            @UIHelper.PagesHelper(ViewBag.ControllerName, Model.TotalPages, Model.PageIndex, Model.PageSize, Model.TotalCount, Model.HasPreviousPage, Model.HasNextPage, Model.PageList)
        </div>
    </div>
</div>
<script>
    //菜单显示
    $("#@(ViewBag.ControllerName)sys").addClass("hbg");
    $("#@(ViewBag.ControllerName)").removeClass("navli");
    $("#@(ViewBag.ControllerName)").addClass("navlihg");
    $(".@(ViewBag.ControllerName)sys").each(function (index, item) {
        $(this).show();
    });
</script>

Add.cshtml

复制代码 代码如下:

@{
    ViewBag.Title = "Add";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="contrgcon">
    <div class="rgtit3">
        <div class="tit1">
            <h3>添加用户</h3>
        </div>
        <div class="rgtit3con">
            @using (Html.BeginForm())
            {
                <dl>
                    <dd class="dc1">用户名:</dd>
                    <dd>
                        <input name="LogName" type="text" class="rgtxt2" datatype="*" nullmsg="请输入产品名称" />
                    </dd>
                </dl>
                <dl>
                    <dd class="dc1">昵称:</dd>
                    <dd>
                        <input name="TrueName" type="text" class="rgtxt2" datatype="n" nullmsg="请输入产品名称" />
                    </dd>
                </dl>
                <dl>
                    <dd class="dc1">Email:</dd>
                    <dd>
                        <input type="text" name="Email" id="Email" class="rgtxt2" />
                    </dd>
                </dl>
                <dl>
                    <dd class="dc1">手机号:</dd>
                    <dd>
                        <input type="text" name="Mobile" id="Mobile" class="rgtxt2" />
                    </dd>
                </dl>
                <dl>
                    <dd class="dc1">密码:</dd>
                    <dd>
                        <input type="text" name="Password" id="Password" class="rgtxt2" />
                    </dd>
                </dl>
                <dl>
                    <dd class="dc1">确认密码:</dd>
                    <dd>
                        <input type="text" name="PassWordto" id="PassWordto" class="rgtxt2" />
                    </dd>
                </dl>
                <dl>
                    <dd class="dc1">岗位:</dd>
                    <dd>
                        @Html.DropDownList("JobID", ViewBag.JobID as SelectList)
                    </dd>
                </dl>
                <dl>
                    <dd class="dc1">部门:</dd>
                    <dd>
                         @Html.DropDownList("DepartmentID", ViewBag.DepartmentID as SelectList)
                    </dd>
                </dl>
                <dl>
                    <dd class="dc1">角色:</dd>
                    <dd>
                         @Html.DropDownList("RoleID", ViewBag.RoleID as SelectList)
                    </dd>
                </dl>
                <dl>
                    <dd>
                        <input type="submit" value="提 交" class="rgbut4" />
                    </dd>
                </dl>
            }
        </div>
    </div>
</div>
<script>
    //菜单显示
    $("#@(ViewBag.ControllerName)sys").addClass("hbg");
    $("#@(ViewBag.ControllerName)").removeClass("navli");
    $("#@(ViewBag.ControllerName)").addClass("navlihg");
    $(".@(ViewBag.ControllerName)sys").each(function (index, item) {
        $(this).show();
    });
</script>

Edit.cshtml

复制代码 代码如下:

@{
    ViewBag.Title = "Add";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model SJCRM.Models.SystemUser
<div class="contrgcon">
    <div class="rgtit3">
        <div class="tit1">
            <h3>编辑用户</h3>
        </div>
        <div class="rgtit3con">
            @using (Html.BeginForm())
            {
                <dl>
                    <dd class="dc1">用户名:</dd>
                    <dd>
                        <input name="LogName" type="text" class="rgtxt2" datatype="*" nullmsg="请输入产品名称" value="@Model.LogName"/>
                    </dd>
                </dl>
                <dl>
                    <dd class="dc1">昵称:</dd>
                    <dd>
                        <input name="TrueName" type="text" class="rgtxt2" datatype="n" nullmsg="请输入产品名称" value="@Model.TrueName" />
                    </dd>
                </dl>
                <dl>
                    <dd class="dc1">Email:</dd>
                    <dd>
                        <input type="text" name="Email" id="Email" class="rgtxt2" value="@Model.Email" />
                    </dd>
                </dl>
                <dl>
                    <dd class="dc1">手机号:</dd>
                    <dd>
                        <input type="text" name="Mobile" id="Mobile" class="rgtxt2" value="@Model.Mobile" />
                    </dd>
                </dl>
                <dl>
                    <dd class="dc1">修改密码:</dd>
                    <dd>
                        <input type="text" name="Password" id="Password" class="rgtxt2" value="@Model.Password" />
                    </dd>
                </dl>
                <dl>
                    <dd class="dc1">岗位:</dd>
                    <dd>
                        @Html.DropDownList("JobID", ViewBag.JobID as SelectList)
                    </dd>
                </dl>
                <dl>
                    <dd class="dc1">部门:</dd>
                    <dd>
                         @Html.DropDownList("DepartmentID", ViewBag.DepartmentID as SelectList)
                    </dd>
                </dl>
                <dl>
                    <dd class="dc1">角色:</dd>
                    <dd>
                         @Html.DropDownList("RoleID", ViewBag.RoleID as SelectList)
                    </dd>
                </dl>
                <dl>
                    <dd>
                        <input type="submit" value="提 交" class="rgbut4" />
                    </dd>
                </dl>
            }
        </div>
    </div>
</div>
<script>
    //菜单显示
    $("#@(ViewBag.ControllerName)sys").addClass("hbg");
    $("#@(ViewBag.ControllerName)").removeClass("navli");
    $("#@(ViewBag.ControllerName)").addClass("navlihg");
    $(".@(ViewBag.ControllerName)sys").each(function (index, item) {
        $(this).show();
    });
</script>

Search.cshtml

复制代码 代码如下:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model SJCRM.Helpers.Pager<SJCRM.Models.SystemUser>
<div class="contrgcon">
    <div class="contrg_list">
        <div class="rgtit">
            <form method="post" action="/@ViewBag.ControllerName/search">
                <dl>
                    <dd>关键词:</dd>
                    <dd>
                        <input name="KeyWord" type="text" class="rgtxt2" /></dd>
                    <dd>
                        <input type="submit" value="查 询" class="rgbut1" /></dd>
                </dl>
            </form>
        </div>
        <div class="rgc">
            <dl class="rgt11">
                <dt>
                    <span class="sple"><a href="/systemuser/add">添加用户</a></span>
                </dt>
            </dl>
            <div class="rg3">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td class="rgtd2">用户编号</td>
                        <td class="rgtd3">登录名</td>
                        <td class="rgtd3">手机</td>
                        <td class="rgtd3">Email</td>
                        <td class="rgtd3">昵称</td>
                        <td class="rgtd3">状态</td>
                        <td class="rgtd4">操作</td>
                    </tr>
                </table>
            </div>
            <div class="rg4">
                @{int classnum = 1;}
                @foreach (var item in Model)
                {
                    <table width="100%" border="0" cellspacing="0" cellpadding="0" @if (classnum % 2 == 0)
                                                                                   {<text>class="rgbg2"</text>}>
                        <tr>
                            <td class="rgtd2">@item.SystemUserID</td>
                            <td class="rgtd3">@(item.LogName)</td>
                            <td class="rgtd3">@item.Mobile</td>
                            <td class="rgtd3">@item.Email</td>
                            <td class="rgtd3">@item.TrueName</td>
                            <td class="rgtd3">@(item.State == 1 ? "有效" : "无效")</td>
                            <td class="rgtd4"><a href="/systemuser/delete/@item.SystemUserID">删除</a>&nbsp;|&nbsp;<a href="/systemuser/edit/@item.SystemUserID">编辑</a>&nbsp; </td>
                        </tr>
                    </table>
                                                                                   classnum = classnum + 1;
                }
            </div>
        </div>
    </div>
</div>
<script>
    //菜单显示
    $("#@(ViewBag.ControllerName)sys").addClass("hbg");
    $("#@(ViewBag.ControllerName)").removeClass("navli");
    $("#@(ViewBag.ControllerName)").addClass("navlihg");
    $(".@(ViewBag.ControllerName)sys").each(function (index, item) {
        $(this).show();
    });
</script>

 角色管理

Controller代码

复制代码 代码如下:

public class RoleController : Controller
   {
       RoleRepository roleryrepository = new RoleRepository();
       MenuRepository menurepository = new MenuRepository();
       RoleMenuMappingRepository rmmrepositoy = new RoleMenuMappingRepository();
       #region 列表
       /// <summary>
       /// 默认页面
       /// </summary>
       /// <returns></returns>
       ///
       [Authorize]
       public ActionResult Index(int? page)
       {
           ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
           var source = roleryrepository.GetModelList();
           return View(roleryrepository.GetPageModelList(source, 10, page ?? 1));
       }
       #endregion
       #region 添加
       /// <summary>
       /// 添加页
       /// </summary>
       /// <param name="model">实体类</param>
       /// <returns></returns>
       [Authorize]
       public ActionResult Add(Role model)
       {
           ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
           var menu = menurepository.GetModelListByState(1);
           ViewBag.Menu = new SelectList(menu, "MenuID", "MenuName");
           // ViewData["State"] = SelectListHelper.GetIsOrNoSelectList(0);
           return View();
       }
       public string 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 + "|";
           }
           return meg;
       }
       /// <summary>
       /// 添加事件
       /// </summary>
       /// <param name="model">实体类</param>
       /// <param name="fc"></param>
       /// <returns></returns>
       [Authorize, HttpPost, ValidateInput(false)]
       public ActionResult Add(Role model, FormCollection fc, int[] Menu)
       {
           try
           {
               ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
               model.State = 1;
               model.CreateTime = DateTime.Now;
               roleryrepository.SaveOrEditModel(model);
               if (Menu.Length > 0)
               {
                   foreach (int gsi in Menu)
                   {
                       RoleMenuMapping rmm = new RoleMenuMapping();
                       rmm.MenuID = gsi;
                       rmm.RoleID = model.RoleID;
                       rmmrepositoy.SaveOrEditModel(rmm);
                   }
               }
               else
               {
                   return RedirectToAction("Index");
               }
           }
           catch (Exception ex)
           {
  &nbs

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NET.4.5.1+MVC5.0设置系统角色与权限(一)发布时间:2022-02-05
下一篇:
ASP.Net下载大文件的实现方法发布时间:2022-02-05
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap