zoukankan      html  css  js  c++  java
  • MVC基于角色权限控制--用户管理

     用户管理模块包括 新增用户、修改用户、展示用户列表、删除用户、用户角色分配、用户角色删除、用户权限分配

    这里只介绍关于权限有关的 用户角色分配、用户角色删除、用户权限分配

    新建控制器 UserInfoController继承 BaseController

     #region 展示用户已经有的角色
             public ActionResult ShowUserRoleInfo()
             {
                 int id = int.Parse(Request["id"]);
                var userInfo= UserInfoService.LoadEntities(u=>u.ID==id).FirstOrDefault();
                ViewBag.UserInfo = userInfo;
                //查询所有的角色.
                  short delFlag = (short)DeleteEnumType.Normarl;
               var allRoleList= RoleInfoService.LoadEntities(r=>r.DelFlag==delFlag).ToList();
                 //查询一下要分配角色的用户以前具有了哪些角色编号。
               var allUserRoleIdList = (from r in userInfo.RoleInfo
                                        select r.ID).ToList();
               ViewBag.AllRoleList = allRoleList;
               ViewBag.AllUserRoleIdList = allUserRoleIdList;
               return View();
             }
    
            #endregion
    
            #region 完成用户角色的分配
             public ActionResult SetUserRoleInfo()
             {
                 int userId = int.Parse(Request["userId"]);
                 string[]allKeys=Request.Form.AllKeys;//获取所有表单元素name属性值。
                 List<int> roleIdList = new List<int>();
                 foreach (string key in allKeys)
                 {
                     if (key.StartsWith("cba_"))
                     {
                         string k = key.Replace("cba_","");
                         roleIdList.Add(Convert.ToInt32(k));
                     }
                 }
                 if (UserInfoService.SetUserRoleInfo(userId, roleIdList))//设置用户的角色
                 {
                     return Content("ok");
                 }
                 else
                 {
                     return Content("no");
                 }
             }
    
            #endregion
            #region 展示用户权限
             public ActionResult ShowUserAction()
             {
                 int userId = int.Parse(Request["userId"]);
                var userInfo=UserInfoService.LoadEntities(u=>u.ID==userId).FirstOrDefault();
                ViewBag.UserInfo = userInfo;
                 //获取所有的权限。
                 short delFlag = (short)DeleteEnumType.Normarl;
                var allActionList= ActionInfoService.LoadEntities(a => a.DelFlag == delFlag).ToList();
                 //获取要分配的用户已经有的权限。
                var allActionIdList = (from a in userInfo.R_UserInfo_ActionInfo
                                       select a).ToList();
                ViewBag.AllActionList = allActionList;
                ViewBag.AllActionIdList = allActionIdList;
                return View();
             }
            #endregion
    
            #region 完成用户权限的分配
             public ActionResult SetUserAction()
             {
                 int actionId = int.Parse(Request["actionId"]);
                 int userId = int.Parse(Request["userId"]);
                 bool isPass = Request["isPass"] == "true" ? true : false;
                 if (UserInfoService.SetUserActionInfo(actionId, userId, isPass))
                 {
                     return Content("ok");
                 }
                 else
                 {
                     return Content("no");
                 }
             }
            #endregion
    
            #region 完成权限删除
             public ActionResult ClearUserAction()
             {
                 int actionId = int.Parse(Request["actionId"]);
                 int userId = int.Parse(Request["userId"]);
               var r_userInfo_actionInfo=R_UserInfo_ActionInfoService.LoadEntities(r=>r.ActionInfoID==actionId&&r.UserInfoID==userId).FirstOrDefault();
               if (r_userInfo_actionInfo != null)
               {
                   if (R_UserInfo_ActionInfoService.DeleteEntity(r_userInfo_actionInfo))
                   {
                       return Content("ok:删除成功!!");
                   }
                   else
                   {
                       return Content("ok:删除失败!!");
                   }
               }
               else
               {
                   return Content("no:数据不存在!!");
               }
    
             }
            #endregion

    UserInfoService中添加分配角色和分配权限的方法

       /// <summary>
            /// 为用户分配角色
            /// </summary>
            /// <param name="userId">用户编号</param>
            /// <param name="roleIdList">要分配的角色的编号</param>
            /// <returns></returns>
            public bool SetUserRoleInfo(int userId, List<int> roleIdList)
            {
                var userInfo = this.CurrentDBSession.UserInfoDal.LoadEntities(u=>u.ID==userId).FirstOrDefault();//根据用户的编号查找用户的信息
                if (userInfo != null)
                {
                    userInfo.RoleInfo.Clear();
                    foreach (int roleId in roleIdList)
                    {
                        var roleInfo = this.CurrentDBSession.RoleInfoDal.LoadEntities(r=>r.ID==roleId).FirstOrDefault();
                        userInfo.RoleInfo.Add(roleInfo);
                    }
                   return this.CurrentDBSession.SaveChanges();
                }
                return false;
    
            }
    
            /// <summary>
            /// 完成用户权限的分配
            /// </summary>
            /// <param name="actionId"></param>
            /// <param name="userId"></param>
            /// <param name="isPass"></param>
            /// <returns></returns>
            public bool SetUserActionInfo(int actionId, int userId, bool isPass)
            {
               //判断userId以前是否有了该actionId,如果有了只需要修改isPass状态,否则插入。
                var r_userInfo_actionInfo = this.CurrentDBSession.R_UserInfo_ActionInfoDal.LoadEntities(a=>a.ActionInfoID==actionId&&a.UserInfoID==userId).FirstOrDefault();
                if (r_userInfo_actionInfo == null)
                {
                    R_UserInfo_ActionInfo userInfoActionInfo = new R_UserInfo_ActionInfo();
                    userInfoActionInfo.ActionInfoID = actionId;
                    userInfoActionInfo.UserInfoID = userId;
                    userInfoActionInfo.IsPass =isPass;
                    this.CurrentDBSession.R_UserInfo_ActionInfoDal.AddEntity(userInfoActionInfo);
                }
                else
                {
                    r_userInfo_actionInfo.IsPass = isPass;
                    this.CurrentDBSession.R_UserInfo_ActionInfoDal.EditEntity(r_userInfo_actionInfo);
                }
               return this.CurrentDBSession.SaveChanges();
    
            }
  • 相关阅读:
    《算法笔记》8.1小节——搜索专题->深度优先搜索(DFS)
    [xdoj]1303jlz的刷题黑科技
    [xdoj]1299&1300朱神的烦恼 朱神的序列
    [洛谷]p1996约瑟夫环 &xdoj1311
    [xdoj] 1310 DSKer的卡牌游戏
    [xdoj] 1301&1302 数字计数 数字计数的复仇
    随手快递app开发第十六天
    随手快递app开发第十五天
    随手快递app开发第十四天
    随手快递app开发第十三天
  • 原文地址:https://www.cnblogs.com/xiaoliwang/p/7866700.html
Copyright © 2011-2022 走看看