標籤:-- uname text ret orm efault 處理 clear user
1.IBLL層
添加方法聲明
public partial interface IUserInfoService:IBaseService<UserInfo>{//oh ,my baby,oh ,my babyIQueryable<UserInfo> LoagPageData(Model.Param.UserQueryParam userQueryParam);bool SetRole(int userId, List<int> roleIds);}
BLL層 方法實現 :現將原來的角色全部刪除,在重新添加角色(存放在集合中)
public bool SetRole(int userId, List<int> roleIds) { var user = DbSession.UserInfoDal.GetEntities(u => u.ID == userId).FirstOrDefault(); user.RoleInfo.Clear();//全剁掉。 var allRoles = DbSession.RoleInfoDal.GetEntities(r => roleIds.Contains(r.ID)); foreach (var roleInfo in allRoles) { user.RoleInfo.Add(roleInfo);//加最新的角色。 } DbSession.SaveChanges(); return true; }
2.控制器 UserInfoController
public ActionResult SetRole(int id) { //當前要設定角色的使用者 int userId = id; UserInfo user= UserInfoService.GetEntities(u => u.ID == id).FirstOrDefault(); //把所有的角色發送 到前台 ViewBag.AllRoles = RoleInfoService.GetEntities(u => u.DelFlag == delflagNormal).ToList(); //使用者已經關聯的角色發送到前台。 ViewBag.ExitsRoles = (from r in user.RoleInfo select r.ID).ToList(); return View(user); }
3.view層中 設定選擇給使用者選擇角色的介面
<div> <h2>當前給使用者: @Model.UName 設定角色</h2> <hr /> @{ //所有的角色 List<LTeasyOA.Model.RoleInfo> allRoles = ViewBag.AllRoles; //所有目前使用者已經存在的角色 List<int> existRoleIdList = ViewBag.ExitsRoles; using (Ajax.BeginForm("ProcessSetRole", "UserInfo", new AjaxOptions() { OnSuccess = "afterSetRole" })) { <input type="hidden" name="UId" value="@Model.ID"/> foreach (var roleInfo in allRoles) { string str = "ckb_" +roleInfo.ID; if (existRoleIdList.Contains(roleInfo.ID)) { <input type="checkbox" checked="checked" value="@str" name="@str" id="@str"/> } else { <input type="checkbox" value="@str" name="@str" id="@str"/> } <label for="@str">@roleInfo.Name</label> <br /> } } } </div>
4.提交選擇的角色到資料庫中
public ActionResult ProcessSetRole(int UId) { //第一:目前使用者id --uid //第二:所有打上對勾的 角色。 ---> list List<int> setRoleIdList =new List<int>(); foreach (var key in Request.Form.AllKeys) { if (key.StartsWith("ckb_")) { int roleId = int.Parse(key.Replace("ckb_", "")); setRoleIdList.Add(roleId); } } UserInfoService.SetRole(UId, setRoleIdList); return Content("ok"); }
許可權設計資料庫設計 關聯表會在資料庫中自動產生(RoleInfo,R_UserInfo_ActionInfo)
DbSession是資料庫上下文,即聯絡資料庫datebase和.net的中間變數,用已操作資料庫的最底層
類似 Dbcontext 直接可以可取表變數
在mvc中只是簡單的處理一下
BaseService中CurrentDal只能擷取當前Services的類型的Dal
dbSession在這裡理論是取所有的_Dal層表 ,但是在這裡 只聲明,未賦值,要想取到其他_Dal在這裡只能在Spring.net中註冊,給DbSession註冊所有_DAl或者在_Service中註冊只用得到的_Dal,,,,,或者直接在代碼中賦值(比較好)
mvc給使用者佈建角色主要步驟