public ActionResult CreateEditAdmin(AdminsCreateAccountVM model) { if (ModelState.IsValid) { Administrator admin; if (model.Id == 0) { admin = new Administrator(); } else { admin = unitOfWork.AdminRepository.GetById(model.Id); } admin.FirstName = model.FirstName; admin.LastName = model.LastName; admin.Email = model.Email; admin.IsActive = true; if (model.Id == 0) { string password = Path.GetRandomFileName().Replace(".", "").Substring(0, 8); var passPhrase = PasswordHasher.Hash(password); admin.Hash = passPhrase.Hash; admin.Salt = passPhrase.Salt; admin.IsConfirmed = false; unitOfWork.AdminRepository.Insert(admin); unitOfWork.Save(); #region Send password to mail MailMessage message = new MailMessage(); message.IsBodyHtml = true; message.Sender = new MailAddress("*****@*****.**"); message.To.Add(model.Email); message.Subject = "Welcome to the University System"; message.From = new MailAddress("*****@*****.**"); StringBuilder msgBody = new StringBuilder(); msgBody.AppendLine(String.Format("<h3>Hello, {0} {1}</h3>", admin.FirstName, admin.LastName)); msgBody.AppendLine("<h4>Welcome to our University System!</h4>"); msgBody.AppendLine(String.Format("<p>You must confirm your account: <a href='{0}'>Confirm</a></p>", Url.Action("ConfirmAccount", "Admin", new { id = admin.Id }, Request.Url.Scheme))); msgBody.AppendLine(String.Format("<p>Use this password to confirm: <strong>{0}</string></p>", password)); message.Body = msgBody.ToString(); SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587); smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; #region Private smtp.Credentials = new System.Net.NetworkCredential("*****@*****.**", "programistaphonebook"); #endregion smtp.Send(message); #endregion } else { unitOfWork.AdminRepository.Update(admin); unitOfWork.Save(); } return RedirectToAction("Index", "Home"); } return View(model); }
public ActionResult EditAdmin(int? id) { if (!id.HasValue) { return RedirectToAction("ManageAdmins"); } Administrator admin = unitOfWork.AdminRepository.GetById(id.Value); if (admin == null) { return RedirectToAction("ManageAdmins"); } if (admin.Id != UniversitySystemMVC.Models.AuthenticationManager.LoggedUser.Id) { return RedirectToAction("ManageAdmins"); } AdminsCreateAccountVM model = new AdminsCreateAccountVM(); model.Id = admin.Id; model.FirstName = admin.FirstName; model.LastName = admin.LastName; model.Email = admin.Email; return View("CreateEditAdmin", model); }
public ActionResult CreateAdmin() { AdminsCreateAccountVM model = new AdminsCreateAccountVM(); model.UserType = UserTypeEnum.Administrator; return View("CreateEditAdmin", model); }