public ActionResult RegisterDean(RegDean FormRegUser, HttpPostedFileBase Avatar) { using (DiplomEntities5 dc = new DiplomEntities5()) { var anyUserEmail = dc.Users.Any(a => a.Email.Equals(FormRegUser.Email)); if (anyUserEmail) { ModelState.AddModelError("Email", "Пользователь с таким email уже зарегистрирован"); } var anyUserLogin = dc.Users.Any(a => a.Login.Equals(FormRegUser.Login)); if (anyUserLogin) { ModelState.AddModelError("Login", "Пользователь с таким логином уже зарегистрирован"); } if (FormRegUser.Captcha != (string)Session[CaptchaImage.CaptchaValueKey]) { ModelState.AddModelError("Captcha", "Текст с картинки введён не верно"); return View(FormRegUser); } if (ModelState.IsValid == true) { MemoryStream memory = new MemoryStream(); if (Avatar == null) { Bitmap avatar = new Bitmap(Server.MapPath("~/Content/Avatars/default_avatar.gif")); avatar.Save(memory, ImageFormat.Gif); } else { string fileName = System.IO.Path.GetFileName(Avatar.FileName); Avatar.SaveAs(Server.MapPath("~/Content/Avatars/" + fileName)); Avatar.InputStream.CopyTo(memory); } Users UserToSave = new Users(); UserToSave.Name = FormRegUser.Name; UserToSave.Sirname = FormRegUser.Sirname; UserToSave.Patername = FormRegUser.Patername; UserToSave.Password = FormRegUser.Password; UserToSave.RoleId = FormRegUser.RoleId; UserToSave.Group = FormRegUser.Group; UserToSave.Login = FormRegUser.Login; UserToSave.Course = FormRegUser.Course; UserToSave.Email = FormRegUser.Email; UserToSave.Avatar = memory.GetBuffer(); UserToSave.Birthday = FormRegUser.Birthday; dc.Users.Add(UserToSave); dc.SaveChanges(); ModelState.Clear(); FormRegUser = null; ViewBag.Message = "Регистрация прошла успешно"; } } return View(FormRegUser); }
public ActionResult Register(RegUser FormRegUser, HttpPostedFileBase Avatar) { using (DiplomEntities5 dc = new DiplomEntities5()) { var anyUserEmail = dc.Users.Any(a => a.Email.Equals(FormRegUser.Email)); if (anyUserEmail) { ModelState.AddModelError("Email", "Пользователь с таким email уже зарегистрирован"); } var anyUserLogin = dc.Users.Any(a => a.Login.Equals(FormRegUser.Login)); if (anyUserLogin) { ModelState.AddModelError("Login", "Пользователь с таким логином уже зарегистрирован"); } if (FormRegUser.Captcha != (string)Session[CaptchaImage.CaptchaValueKey]) { ModelState.AddModelError("Captcha", "Текст с картинки введён не верно"); return View(FormRegUser); } DateTime now = DateTime.Now; int RegUserCourse = now.Year - (Convert.ToInt32(FormRegUser.Login.ToString().Substring(0, 2)) + 2000); foreach (Users student in dc.Users) { int yearFromLogin = Convert.ToInt32(student.Login.ToString().Substring(0, 2)); if ((yearFromLogin != RegUserCourse) && (FormRegUser.Course == student.Course)) { ModelState.AddModelError("Login", "Ошибка системы. Для решения проблемы обратитесь к методисту"); return View(FormRegUser); } } if(ModelState.IsValid == true) { MemoryStream memory = new MemoryStream(); if (Avatar == null) { Bitmap avatar = new Bitmap(Server.MapPath("~/Content/Avatars/default_avatar.gif")); avatar.Save(memory, ImageFormat.Gif); } else { string fileName = System.IO.Path.GetFileName(Avatar.FileName); Avatar.SaveAs(Server.MapPath("~/Content/Avatars/" + fileName)); Avatar.InputStream.CopyTo(memory); } Users UserToSave = new Users(); UserToSave.Name = FormRegUser.Name; UserToSave.Sirname = FormRegUser.Sirname; UserToSave.Patername = FormRegUser.Patername; UserToSave.Password = FormRegUser.Password; UserToSave.RoleId = FormRegUser.RoleId; UserToSave.Group = FormRegUser.Group; UserToSave.Login = FormRegUser.Login; UserToSave.Course = FormRegUser.Course; UserToSave.Email = FormRegUser.Email; UserToSave.Avatar = memory.GetBuffer(); UserToSave.Birthday = FormRegUser.Birthday; dc.Users.Add(UserToSave); dc.SaveChanges(); ModelState.Clear(); FormRegUser = null; ViewBag.Message = "Регистрация прошла успешно"; } } return View(FormRegUser); }