public ActionResult Create([Bind(Include = "name,realName,certificateType,certificateNo,mobile,email,password,password2,state,gender,postId,officePhone,picture,deptId,deptChild,roleId")] UserEditModel model) { if (!User.Identity.IsAuthenticated) { return(RedirectToRoute(new { controller = "Login", action = "LogOut" })); } int user = PageValidate.FilterParam(User.Identity.Name); if (!RoleCheck.CheckHasAuthority(user, db, "用户管理")) { return(RedirectToRoute(new { controller = "Error", action = "Index", err = "没有权限。" })); } setSelect(); if (ModelState.IsValid) { User_Info info = new User_Info(); model.toUserInfoDB(info); if (db.User_Info.Where(x => x.user_name == info.user_name).Count() > 0) { ViewBag.msg = "该用户名已注册。"; goto next; } var salt = Guid.NewGuid().ToString("N").Substring(0, 10).ToUpper(); info.user_password = PasswordUnit.getPassword(model.password.ToUpper(), salt); info.user_salt = salt; info.ToEncrypt(); if (db.User_Info.Where(x => x.user_certificate_type == info.user_certificate_type && x.user_certificate_no == info.user_certificate_no).Count() > 0) { ViewBag.msg = "该证件号已注册。"; goto next; } if (db.User_Info.Where(x => x.user_email == info.user_email).Count() > 0) { ViewBag.msg = "该邮箱已注册。"; goto next; } if (db.User_Info.Where(x => x.user_mobile == info.user_mobile).Count() > 0) { ViewBag.msg = "该手机号已注册。"; goto next; } if (model.password != model.password2) { ViewBag.msg = "两次输入密码不一致,请重新输入。"; goto next; } db.User_Info.Add(info); try { db.SaveChanges(); }catch (Exception ex) { ViewBag.msg = "信息录入失败,请重新录入。"; ErrorUnit.WriteErrorLog(ex.ToString(), this.GetType().Name); goto next; } User_Extend extend = new User_Extend(); model.toUserExtendDB(extend); extend.user_id = info.user_id; extend.user_add_user = PageValidate.FilterParam(User.Identity.Name); extend.user_add_time = DateTime.Now; db.User_Extend.Add(extend); string photoDir = ConfigurationManager.AppSettings["photoPath"]; if (!Directory.Exists(photoDir)) { Directory.CreateDirectory(photoDir); } string photoTempDir = ConfigurationManager.AppSettings["tempPhotoPath"]; string file_name = string.Format("{0}{1}", photoDir, extend.user_picture).Replace("_temp", ""); string temp_file_name = string.Format("{0}{1}", photoTempDir, extend.user_picture); if (System.IO.File.Exists(temp_file_name)) { FileInfo fi = new FileInfo(temp_file_name); fi.CopyTo(file_name, true); } else { ViewBag.msg = "图片保存失败。"; } if (model.roleId != null) { User_vs_Role uvr = new User_vs_Role(); uvr.uvr_user_id = info.user_id; uvr.uvr_role_id = (int)model.roleId; db.User_vs_Role.Add(uvr); } db.SaveChanges(); SysLog.WriteLog(user, string.Format("添加用户[{0}]", model.realName), IpHelper.GetIP(), info.user_id.ToString(), 2, "", db); ViewBag.msg = " 用户创建成功。"; } else { StringBuilder sbmsg = new StringBuilder(); foreach (var value in ModelState.Values) { if (value.Errors.Count() > 0) { foreach (var err in value.Errors) { sbmsg.Append(err.ErrorMessage); } ViewBag.msg = sbmsg.ToString();; } } } next: return(View(model)); }
public ActionResult Edit([Bind(Include = "id,name,realName,certificateType,certificateNo,mobile,email,password,password2,state,gender,postId,officePhone,picture,deptId,deptChild,roleId")] UserEditModel model) { if (!User.Identity.IsAuthenticated) { return(RedirectToRoute(new { controller = "Login", action = "LogOut" })); } int user = PageValidate.FilterParam(User.Identity.Name); if (!RoleCheck.CheckHasAuthority(user, db, "用户管理") && user != model.id) { return(RedirectToRoute(new { controller = "Error", action = "Index", err = "没有权限!" })); } setSelect(); if (ModelState.IsValid) { if (model.deptChild != null && model.deptId != null) { List <SelectOption> options = DropDownList.getDepartment((int)model.deptId); ViewBag.DeptChild = DropDownList.SetDropDownList(options); } User_Info info = db.User_Info.Find(model.id); info.ToDecrypt(); if (info == null) { ViewBag.msg = "该用户可能已被删除,无法更改。"; goto next; } model.toUserInfoDB(info); if (db.User_Info.Where(x => x.user_name == info.user_name && x.user_id != info.user_id).Count() > 0) { ViewBag.msg = "该用户名已注册。"; goto next; } if (!string.IsNullOrEmpty(model.password)) { if (model.password != model.password2) { ViewBag.msg = "两次输入密码不一致,请重新输入。"; goto next; } var salt = Guid.NewGuid().ToString("N").Substring(0, 10).ToUpper(); info.user_password = PasswordUnit.getPassword(model.password.ToUpper(), salt); info.user_salt = salt; } info.ToEncrypt(); if (db.User_Info.Where(x => (x.user_certificate_type == info.user_certificate_type && x.user_certificate_no == info.user_certificate_no) && x.user_id != info.user_id).Count() > 0) { ViewBag.msg = "该证件号已注册。"; goto next; } if (db.User_Info.Where(x => x.user_email == info.user_email && x.user_id != info.user_id).Count() > 0) { ViewBag.msg = "该邮箱已注册。"; goto next; } if (db.User_Info.Where(x => x.user_mobile == info.user_mobile && x.user_id != info.user_id).Count() > 0) { ViewBag.msg = "该手机号已注册。"; goto next; } db.Entry <User_Info>(info).State = EntityState.Modified; bool edit = true; User_Extend extend = db.User_Extend.Find(info.user_id); if (extend == null) { edit = false; extend = new User_Extend(); extend.user_id = info.user_id; } if (!string.IsNullOrEmpty(model.picture) && model.picture != extend.user_picture) { string photoDir = ConfigurationManager.AppSettings["photoPath"]; if (!Directory.Exists(photoDir)) { Directory.CreateDirectory(photoDir); } string photoTempDir = ConfigurationManager.AppSettings["tempPhotoPath"]; string file_name = string.Format("{0}{1}", photoDir, model.picture).Replace("_temp", ""); string temp_file_name = string.Format("{0}{1}", photoTempDir, model.picture); if (System.IO.File.Exists(temp_file_name)) { FileInfo fi = new FileInfo(temp_file_name); fi.CopyTo(file_name, true); model.picture = Path.GetFileName(file_name); } else { ViewBag.msg = "图片保存失败。"; } } model.toUserExtendDB(extend); extend.user_edit_time = DateTime.Now; extend.user_edit_user = PageValidate.FilterParam(User.Identity.Name); if (edit) { db.Entry <User_Extend>(extend).State = EntityState.Modified; } else { db.User_Extend.Add(extend); } edit = true; if (model.roleId != null) { User_vs_Role uvr = db.User_vs_Role.Find(info.user_id); if (uvr == null) { edit = false; uvr = new User_vs_Role(); } uvr.uvr_user_id = info.user_id; uvr.uvr_role_id = (int)model.roleId; if (edit) { db.Entry <User_vs_Role>(uvr).State = EntityState.Modified; } else { db.User_vs_Role.Add(uvr); } } try { db.SaveChanges(); ViewBag.msg = " 更新成功。"; SysLog.WriteLog(user, string.Format("修改用户[{0}]信息", model.realName), IpHelper.GetIP(), info.user_id.ToString(), 2, "", db); } catch (DbEntityValidationException ex) { StringBuilder errors = new StringBuilder(); IEnumerable <DbEntityValidationResult> validationResult = ex.EntityValidationErrors; foreach (DbEntityValidationResult result in validationResult) { ICollection <DbValidationError> validationError = result.ValidationErrors; foreach (DbValidationError err in validationError) { errors.Append(err.PropertyName + ":" + err.ErrorMessage + "\r\n"); } } ErrorUnit.WriteErrorLog(errors.ToString(), this.GetType().Name); ViewBag.msg = " 更新失败。"; } } next: return(View(model)); }
public ActionResult Edit([Bind(Include = "user_id,user_name,real_name,gender,user_phone,user_info,user_email,user_password,user_password2,user_home_address,user_photo_path,role_id,state")] TeacherEditModel model) { setSelect(); if (!User.Identity.IsAuthenticated) { return(RedirectToRoute(new { controller = "Login", action = "LogOut" })); } if (ModelState.IsValid) { //if (Session["token"] == null || Session["token"].ToString() != model.token) //{ // ViewBag.msg = "异常操作,请退出当前页面后重新进入操作。"; // return View(model); //} int userid = PageValidate.FilterParam(User.Identity.Name); if (!RoleCheck.CheckHasAuthority(userid, db, "用户管理") && model.user_id != userid) { return(RedirectToRoute(new { controller = "Error", action = "Index", err = "没有权限。" })); } User_Info user_Info = db.User_Infos.Find(model.user_id); if (user_Info == null) { ViewBag.msg = "没有找到相关信息,资料可能被删除。"; return(View(model)); } if (db.User_Infos.Where(x => x.user_id != model.user_id && x.user_phone == model.user_phone).Count() > 0) { ViewBag.msg = "该手机号码已存在。"; return(View(model)); } if (!string.IsNullOrEmpty(model.user_password)) { if (model.user_password != model.user_password2) { ViewBag.msg = "两次输入的密码不匹配。"; return(View(model)); } var salt = Guid.NewGuid().ToString("N").Substring(0, 10).ToUpper(); user_Info.user_password = AESEncrypt.Encrypt(PasswordUnit.getPassword(model.user_password.ToUpper(), salt)); user_Info.user_salt = salt; } string err = ""; if (!string.IsNullOrEmpty(model.user_photo_path) && model.user_photo_path != user_Info.user_photo_path) { string photoDir = MyConfiguration.GetPhotoPath(); if (!Directory.Exists(photoDir)) { Directory.CreateDirectory(photoDir); } string photoTempDir = MyConfiguration.GetTempPhotoPath(); string file_name = string.Format("{0}{1}", photoDir, model.user_photo_path).Replace("_temp", ""); string temp_file_name = string.Format("{0}{1}", photoTempDir, model.user_photo_path); if (System.IO.File.Exists(temp_file_name)) { FileInfo fi = new FileInfo(temp_file_name); fi.CopyTo(file_name, true); model.user_photo_path = Path.GetFileName(file_name); user_Info.user_photo_path = model.user_photo_path; } else { err = "图片保存失败。"; } } user_Info.user_name = model.user_name; user_Info.user_phone = model.user_phone; user_Info.user_info = model.user_info; user_Info.user_email = model.user_email; user_Info.user_home_address = model.user_home_address; user_Info.user_update_time = DateTime.Now; user_Info.user_update_user = userid; user_Info.user_gender = model.gender; user_Info.real_name = model.real_name; user_Info.user_is_teacher = true; if (string.IsNullOrEmpty(user_Info.user_bindCode)) { user_Info.user_bindCode = Guid.NewGuid().ToString("N").Substring(0, 8); } db.Entry(user_Info).State = EntityState.Modified; try { db.SaveChanges(); }catch (Exception e) { err = "资料保存失败。"; ErrorUnit.WriteErrorLog(e.ToString(), this.GetType().ToString()); } //权限设置 if (RoleCheck.CheckIsSuperAdmin(model.user_id, db)) { if (model.role_id != 1) { err = "系统管理员权限不允许更改。"; } goto next; } if (model.role_id == 1 && !RoleCheck.CheckIsSuperAdmin(userid, db))//添加系统管理员权限 { err = "只有系统管理员才可以添加系统管理员权限。"; } else { var uvr = db.User_vs_Roles.Where(x => x.uvr_user_id == model.user_id); db.User_vs_Roles.RemoveRange(uvr); User_vs_Role Nuvr = new User_vs_Role { uvr_user_id = model.user_id, uvr_role_id = model.role_id }; db.User_vs_Roles.Add(Nuvr); try { db.SaveChanges(); }catch (Exception e) { err = "角色添加失败。"; } } next: if (err == "") { ViewBag.msg = "修改成功。"; } else { ViewBag.msg = err; } } return(View(model)); }