/// <summary> /// 查看/设置工作人员权限 /// </summary> /// <param name="id">工作人员ID</param> /// <returns></returns> public ActionResult Roleinfo(int id) { User user = (from u in CQGJ.User where u.UserID == id && u.IsAdmin == true select u).First(); Org org = (from u in CQGJ.User from o in CQGJ.Org where u.OrgID == o.OrgID && u.UserID == id select o).First(); string orgType = org.OrgType.ToString(); var allroles = from r in CQGJ.Role where r.RoleType.Contains(orgType) select r; var myroles = from ur in CQGJ.UsersInRoles from r in CQGJ.Role where ur.AdminID == id && ur.RoleID == r.RoleID select r; //更新工作人员角色信息 if (Request.HttpMethod == "POST") { string[] keys = Request.Form.AllKeys; //先处理删除 foreach (var r in myroles) { if (!keys.Any(name => String.Equals(name, r.RoleID.ToString()))) { int temp = r.RoleID; UsersInRoles userrole = (from ur in CQGJ.UsersInRoles where ur.AdminID == id && ur.RoleID == temp select ur).First(); CQGJ.DeleteObject(userrole); } } CQGJ.SaveChanges(); //后处理插入 foreach (string k in keys) { int temp = Convert.ToInt16(k); var recored = from ur in CQGJ.UsersInRoles where ur.AdminID == id && ur.RoleID == temp select ur; if (recored.Count() <= 0)//小于0则插入新纪录 { Role role = (from r in CQGJ.Role where r.RoleID == temp select r).First(); UsersInRoles ur = new UsersInRoles(); ur.AdminID = user.UserID; ur.RoleID = role.RoleID; CQGJ.AddToUsersInRoles(ur); CQGJ.SaveChanges(); } } myroles = from ur in CQGJ.UsersInRoles from r in CQGJ.Role where ur.AdminID == id && ur.RoleID == r.RoleID select r; } ViewData["MyRoles"] = myroles; ViewData["User"] = user; return View(allroles); }
/// <summary> /// 修改工作人员资料 /// </summary> /// <param name="id">工作人员ID</param> /// <returns></returns> public ActionResult AdminEdit(int id) { AdminViewData viewData = new AdminViewData(); Admin admin = (from a in CQGJ.Admin where a.AdminID == id select a).First(); var myroles = from r in CQGJ.Role from ur in CQGJ.UsersInRoles where ur.RoleID == r.RoleID && ur.AdminID == admin.AdminID select r; var allroles = from r in CQGJ.Role select r; viewData.Nation = Nation(admin.Nation); viewData.GenderList = GenderList(admin.Gender); viewData.Admin = admin; if (Request.HttpMethod == "POST") { //更新信息 string username = GetString("Username"); if (username != null) { var admins = from a in CQGJ.Admin where a.Username == username select a; if (admins.Count() <= 0) { admin.Username = username; if (GetString("Password") != "") { admin.Password = Core.Security.MD5Encrypt(GetString("Password")); } admin.Gender = GetString("Gender"); admin.Nation = GetString("Nation"); admin.Politics = GetString("Politics"); admin.IDCard = GetString("IDCard"); admin.Birthday = GetDate("Birthday"); if (admin.Birthday < new DateTime(1900, 1, 1)) { admin.Birthday = DateTime.Today; } admin.Telephone = GetString("Telephone"); admin.Cellphone = GetString("Cellphone"); admin.WorkingOrgName = GetString("WorkingOrgName"); admin.Position = GetString("Position"); CQGJ.SaveChanges(); List<string> allkeys = Request.Form.AllKeys.ToList(); List<string> keys = new List<string> { }; for (int k = 0; k < allkeys.Count; k++) { if (allkeys[k].StartsWith("role-")) { keys.Add(allkeys[k].Substring(5, allkeys[k].Length - 5)); } } //先处理删除 foreach (var r in myroles) { if (!keys.Any(name => String.Equals(name, r.RoleID.ToString()))) { int temp = r.RoleID; UsersInRoles userrole = (from ur in CQGJ.UsersInRoles where ur.AdminID == id && ur.RoleID == temp select ur).First(); CQGJ.DeleteObject(userrole); } } CQGJ.SaveChanges(); //后处理插入 foreach (string k in keys) { int temp = Convert.ToInt16(k); var records = from ur in CQGJ.UsersInRoles where ur.AdminID == id && ur.RoleID == temp select ur; if (records.Count() <= 0)//小于0则插入新纪录 { UsersInRoles ur = new UsersInRoles(); ur.AdminID = id; ur.RoleID = temp; CQGJ.AddToUsersInRoles(ur); CQGJ.SaveChanges(); } } myroles = from r in CQGJ.Role from ur in CQGJ.UsersInRoles where ur.RoleID == r.RoleID && ur.AdminID == admin.AdminID select r; } else { ViewData["ErrorInfo"] = "用户名已存在!"; } } else { ViewData["ErrorInfo"] = "用户名不能为空!"; } } viewData.MyRoles = myroles.ToList(); viewData.RoleList = allroles.ToList(); return View(viewData); }