/// <summary> /// 添加角色 /// </summary> /// <param name="model"></param> /// <returns></returns> public int AddRole(string name, string qxid) { var db = SugerBase.GetInstance(); Role role = new Role(); role.RoleName = name; int i = db.Insertable(role).ExecuteCommand(); if (i > 0) { var newId = db.Queryable <Role>().Where(m => m.RoleName == name).OrderBy("ID desc").First().ID; string[] qxids = qxid.Split(','); int state = 0; foreach (var item in qxids) { int id = Convert.ToInt32(item); RoleAuthority roleAuthority = new RoleAuthority(); roleAuthority.AuthorityId = id; roleAuthority.RoleId = Convert.ToInt32(newId); state += db.Insertable(roleAuthority).ExecuteCommand();//记录成功条数 } return(qxids.Length == state ? state : -1); } return(0); }
public ActionResult Authorize(RoleAuthrizeInfo roleAuthInfo) { var json = new JsonResult(); try { using (var ctx = new GlsunViewEntities()) { var loginUser = (from u in ctx.User where u.ULoginName == HttpContext.User.Identity.Name select u).FirstOrDefault(); //移除未勾选 if (roleAuthInfo.UnCheckedId != null) { foreach (var sId in roleAuthInfo.UnCheckedId) { int maId = Convert.ToInt32(sId); var roleAuth = (from ra in ctx.RoleAuthority where ra.MAID == maId && ra.RID == roleAuthInfo.RoleId select ra).FirstOrDefault(); if (roleAuth != null) { ctx.RoleAuthority.Remove(roleAuth); } } } //添加勾选 if (roleAuthInfo.CheckedId != null) { foreach (var sId in roleAuthInfo.CheckedId) { int maId = Convert.ToInt32(sId); var roleAuth = new RoleAuthority { RID = roleAuthInfo.RoleId, MAID = maId, CreatorID = loginUser.ID, CreationTime = DateTime.Now }; ctx.RoleAuthority.Add(roleAuth); } } ctx.SaveChanges(); } json.Data = new { Code = "", Data = roleAuthInfo, Message = "保存成功" }; } catch (Exception ex) { json.Data = new { Code = "Exception", Data = roleAuthInfo, Message = ex.Message }; } return(json); }
protected void btSave_Click(object sender, EventArgs e) { RoleInfo info = new RoleInfo(); RoleManageBLL bll = null; try { UIBindHelper.BindModelByControls(this.Page, info); bll = BLLFactory.CreateBLL <RoleManageBLL>(); //绑定权限 if (this.HiSelectedPowerList.Value != "") { string[] powers = this.HiSelectedPowerList.Value.Split(",".ToCharArray()); info.Powers = new List <RoleAuthority>(); foreach (string powerID in powers) { RoleAuthority p = new RoleAuthority(); p.AuthorityID = powerID; info.Powers.Add(p); } } if (this.hiID.Value == "") { bll.Insert(info); } else { info.CreateUser = this.HiCREATEUSER.Value; info.CreateTime = DateTime.Parse(this.HiCREATETIME.Value); info.RoleID = this.hiID.Value; bll.Update(info); } ClientScript.RegisterStartupScript(this.GetType(), "myjs", "parent.refreshData();parent.closeAppWindow1();", true); } catch (Exception ex) { throw ex; } }
private void TreetoList(AuthorityTreeModel root) { var num = root.ButtonAuthorityList.Where(m => m.IsChecked).Sum(g => g.ButtonID); if (num > 0) { RoleAuthority.Add(new RoleAuthorityDTO { StructureID = root.StructureID, RoleButtonAuthority = num }); } if (root.children != null && root.children.Count > 0) { foreach (var child in root.children) { TreetoList(child); } } }