private void btnSaveNewLocation_Click(object sender, EventArgs e) { try { Location location = new Location { LocationName = txtNewLocName.Text }; context.Locations.Add(location); LocationAdmin locAdmin = new LocationAdmin { LocationID = location.LocationID, PersonID = Convert.ToInt32(cmbLocationManager.SelectedValue) }; context.LocationAdmins.Add(locAdmin); context.SaveChanges(); //form.FillDataGrid(); form.RefreshDataGrid(); Close(); } catch (Exception ex) { MessageBox.Show(ex.Message + ex.InnerException); } }
public async Task <ActionResult> AddAdmin(AddNewAdminViewModel adminVM) { if (!Request.IsAuthenticated) { return(RedirectToAction("Login", "Account")); } else { if (!User.IsInRole("System_Admin")) { return(RedirectToAction("Index", "Home")); } else { if (adminVM.Name != null && adminVM.LastName != null && adminVM.Email != null && adminVM.UserName != null) { Admin newAdmin = null; if (adminVM.Admin_Type == AdminType.SYSTEM_ADMIN) { newAdmin = new SystemAdmin { Admin_Type = adminVM.Admin_Type, Name = adminVM.Name, LastName = adminVM.LastName, Email = adminVM.Email, UserName = adminVM.UserName, IsMainAdmin = false }; } else if (adminVM.Admin_Type == AdminType.FANZONE_ADMIN) { newAdmin = new FanZoneAdmin { Admin_Type = adminVM.Admin_Type, Name = adminVM.Name, LastName = adminVM.LastName, Email = adminVM.Email, UserName = adminVM.UserName, HasSetPassword = false }; } else if (adminVM.Admin_Type == AdminType.LOCATION_ADMIN) { if (adminVM.MyLocationId != null) { newAdmin = new LocationAdmin { Admin_Type = adminVM.Admin_Type, Name = adminVM.Name, LastName = adminVM.LastName, Email = adminVM.Email, UserName = adminVM.UserName, MyLocationId = adminVM.MyLocationId }; } else { ModelState.AddModelError("", "Error: Admin location is null."); TempData["ModelState"] = ModelState; return(RedirectToAction("AddNewAdmin", "System_Admin")); } } if (newAdmin != null) { using (var um = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext()))) { IdentityResult result; if (um.Users.FirstOrDefault(usr => usr.Email == newAdmin.Email) == null) { //RandomString() string newPassword = "******"; result = await um.CreateAsync(newAdmin, newPassword); if (result.Succeeded) { if (adminVM.Admin_Type == AdminType.SYSTEM_ADMIN) { if (!um.IsInRole(newAdmin.Id, "System_Admin")) { var userResult = um.AddToRole(newAdmin.Id, "System_Admin"); if (!userResult.Succeeded) { ModelState.AddModelError("", "Adding user '" + newAdmin.UserName + "' to '" + "System_Admin" + "' role failed with error(s): " + userResult.Errors); TempData["ModelState"] = ModelState; return(RedirectToAction("AddNewAdmin", "System_Admin")); } } } else if (adminVM.Admin_Type == AdminType.FANZONE_ADMIN) { if (!um.IsInRole(newAdmin.Id, "Fanzone_Admin")) { var userResult = um.AddToRole(newAdmin.Id, "Fanzone_Admin"); if (!userResult.Succeeded) { ModelState.AddModelError("", "Adding user '" + newAdmin.UserName + "' to '" + "Fanzone_Admin" + "' role failed with error(s): " + userResult.Errors); TempData["ModelState"] = ModelState; return(RedirectToAction("AddNewAdmin", "System_Admin")); } else { //email to [email protected] from the same address var fromAddress = new MailAddress("*****@*****.**", "ISA NS"); var toAddress = new MailAddress("*****@*****.**", "ISA NS"); string fromPassword = "******"; string subject = "Welcome to ISA2017 Cinemas"; string body = "Hello new Fanzone admin!" + System.Environment.NewLine + "Your sign-in credentials are:" + System.Environment.NewLine + "Email: " + adminVM.Email + System.Environment.NewLine + "Password: "******"smtp.gmail.com", Port = 587, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(fromAddress.Address, fromPassword) }; using (var message = new MailMessage(fromAddress, toAddress) { Subject = subject, Body = body }) { smtp.Send(message); } } } } else if (adminVM.Admin_Type == AdminType.LOCATION_ADMIN) { if (!um.IsInRole(newAdmin.Id, "Location_Admin")) { var userResult = um.AddToRole(newAdmin.Id, "Location_Admin"); if (!userResult.Succeeded) { ModelState.AddModelError("", "Adding user '" + newAdmin.UserName + "' to '" + "Location_Admin" + "' role failed with error(s): " + userResult.Errors); TempData["ModelState"] = ModelState; return(RedirectToAction("AddNewAdmin", "System_Admin")); } else { ApplicationDbContext ctx = new ApplicationDbContext(); var resLoc = ctx.Locations.FirstOrDefault(x => x.Id.ToString() == adminVM.MyLocationId); if (resLoc != null) { resLoc.MyAdminId = newAdmin.Id; ctx.SaveChanges(); } else { um.RemoveFromRole(newAdmin.Id, "Location_Admin"); ModelState.AddModelError("", "Error: Given admin location is not found! Please try again."); TempData["ModelState"] = ModelState; return(RedirectToAction("AddNewAdmin", "System_Admin")); } } } } } else { ModelState.AddModelError("", "Error while trying to create new admin"); TempData["ModelState"] = ModelState; return(RedirectToAction("AddNewAdmin", "System_Admin")); } } else { ModelState.AddModelError("", "User with this email adress already exists"); TempData["ModelState"] = ModelState; return(RedirectToAction("AddNewAdmin", "System_Admin")); } } } else { ModelState.AddModelError("", "Error while trying to add new admin (newAdmin is null)"); TempData["ModelState"] = ModelState; return(RedirectToAction("AddNewAdmin", "System_Admin")); } } else { ModelState.AddModelError("", "Error while trying to add new admin (some fields are null)"); TempData["ModelState"] = ModelState; return(RedirectToAction("AddNewAdmin", "System_Admin")); } TempData["success"] = "Succesfully added a new: " + adminVM.Admin_Type.ToString(); return(RedirectToAction("Index", "Home")); } } }