public ActionResult AddManageCompany(ManageCompanyModel mc) { var categoryList = dbEnt.ManageCompany.Select(s => s.Category); var nameList = dbEnt.ManageCompany.Select(s => s.Name); var emailList = dbEnt.Users.Select(s => s.Email); if (!categoryList.Contains(mc.Category) && !nameList.Contains(mc.Name) && !emailList.Contains(mc.Email)) { ManageCompany _mcObj = new ManageCompany { Name = mc.Name, Category = mc.Category, Street = mc.Street, HouseNumber = mc.HouseNumber }; dbEnt.ManageCompany.Add(_mcObj); dbEnt.SaveChanges(); Users _usrObj = new Users { FirstName = mc.FirstName, SecondName = mc.SecondName, ThirdName = mc.ThirdName, Email = mc.Email, EmailConfirm = false, manageCompany_id = _mcObj.id }; dbEnt.Users.Add(_usrObj); dbEnt.SaveChanges(); var roleList = dbEnt.Roles.Select(r => r.Name); Roles _rlobj; if (!roleList.Contains("ManageCompany")) { _rlobj = new Roles { Name = "ManageCompany" }; dbEnt.Roles.Add(_rlobj); dbEnt.SaveChanges(); } else { _rlobj = dbEnt.Roles.Where(r => r.Name == "ManageCompany").Select(r => r).FirstOrDefault(); } dbEnt.Accounts.Add(new Accounts { user_id = _usrObj.id, password = ISCityFramework.GetPass(rnd) }); dbEnt.SaveChanges(); dbEnt.UserRoles.Add(new UserRoles { user_id = _usrObj.id, role_id = _rlobj.id }); dbEnt.SaveChanges(); ISCityFramework.SendMail(_usrObj, dbEnt); return(RedirectToAction("Index", "Admin", null)); } else { ModelState.AddModelError("", "Организация с таким Наименованием, Категорией или Email уже существует."); } return(View(mc)); }
public ActionResult AddSubCompany(SubCompanyModel sc) { var emailList = dbEnt.Users.Select(s => s.Email); var nameList = dbEnt.SubCompany.Select(s => s.Name); if (!emailList.Contains(sc.Email) && !nameList.Contains(sc.Name)) { var _acc = (from a in dbEnt.Accounts where a.Users.Email == User.Identity.Name select a.Users).FirstOrDefault(); SubCompany _scObj = new SubCompany { Name = sc.Name, Street = sc.Street, HouseNumber = sc.HouseNumber, mangeCompany_id = (int)_acc.manageCompany_id }; dbEnt.SubCompany.Add(_scObj); dbEnt.SaveChanges(); Users _usrObj = new Users { FirstName = sc.FirstName, SecondName = sc.SecondName, ThirdName = sc.ThirdName, Email = sc.Email, EmailConfirm = false, subCompany_id = _scObj.id }; dbEnt.Users.Add(_usrObj); dbEnt.SaveChanges(); var roleList = dbEnt.Roles.Select(r => r.Name); Roles _rlobj; if (!roleList.Contains("SubCompany")) { _rlobj = new Roles { Name = "SubCompany" }; dbEnt.Roles.Add(_rlobj); dbEnt.SaveChanges(); } else { _rlobj = dbEnt.Roles.Where(r => r.Name == "SubCompany").Select(r => r).FirstOrDefault(); } dbEnt.Accounts.Add(new Accounts { user_id = _usrObj.id, password = ISCityFramework.GetPass(rnd) }); dbEnt.SaveChanges(); dbEnt.UserRoles.Add(new UserRoles { user_id = _usrObj.id, role_id = _rlobj.id }); dbEnt.SaveChanges(); ISCityFramework.SendMail(_usrObj, dbEnt); return(RedirectToAction("SubCompanyList")); } ModelState.AddModelError("", "Управляющая коомпания с таким Наименованием или Email уже существует."); return(View(sc)); }
public ActionResult Register(RegisterModel register) { Users _usr = new Users { FirstName = register.FirstName, SecondName = register.SecondName, ThirdName = register.ThirdName, Street = register.Street, HouseNumber = register.HouseNumber, KorpusNumber = register.KorpusNumber, RoomNumber = register.RoomNumber, Email = register.Email, EmailConfirm = false, Telephone = register.Telephone }; var emails = dbEnt.Users.Select(s => s.Email); if (emails.Contains(_usr.Email)) { ModelState.AddModelError("", "Пользователь с указанным email уже зарегестрирован"); return(View(register)); } try { var _roleUserContains = (from r in dbEnt.Roles where r.Name == "User" select r).FirstOrDefault(); if (_roleUserContains == null) { dbEnt.Roles.Add(new ISCity.Models.Roles { Name = "User" }); dbEnt.SaveChanges(); } var _roleUser = (from r in dbEnt.Roles where r.Name == "User" select r).FirstOrDefault(); dbEnt.Users.Add(_usr); dbEnt.SaveChanges(); dbEnt.UserRoles.Add(new UserRoles { user_id = _usr.id, role_id = _roleUser.id }); dbEnt.SaveChanges(); dbEnt.Accounts.Add(new Accounts { password = ISCityFramework.GetPass(rnd), user_id = _usr.id }); dbEnt.SaveChanges(); ISCityFramework.SendMail(_usr, dbEnt); return(RedirectToAction("Index", "User")); } catch { var _usrRls = from r in dbEnt.UserRoles where r.user_id == _usr.id select r; dbEnt.UserRoles.RemoveRange(_usrRls); dbEnt.SaveChanges(); var _usrAccs = from r in dbEnt.Accounts where r.user_id == _usr.id select r; dbEnt.Accounts.RemoveRange(_usrAccs); dbEnt.SaveChanges(); dbEnt.Users.Remove(_usr); dbEnt.SaveChanges(); ModelState.AddModelError("", "Что-то случилось, Вы не прошли регестрацию. Повторите позже."); } return(View(register)); //нужно добавить подтверждение emaila }