public ActionResult Details(string id) { string userId = id; bool ownAccount = false; if (string.IsNullOrEmpty(id)) { userId = User.Identity.GetUserId(); //View your own account ownAccount = true; } else if (userId == User.Identity.GetUserId()) { ownAccount = true; } //if (string.IsNullOrEmpty(userId)) //{ // userId = User.Identity.GetUserId(); //} ApplicationUser user = db.Users.Find(userId); if (user == null) { return(HttpNotFound()); } string countryName = ""; if (!string.IsNullOrEmpty(user.CountryName)) { Models.Shared.Country country = db.Countries.Where(c => c.CountryAbbreviation == user.CountryName).FirstOrDefault(); countryName = country.CountryName + " (" + country.CountryAbbreviation + ")"; } DisplayUserViewModel model = new DisplayUserViewModel { Id = /*user.Id == User.Identity.GetUserId() ? "" : */ user.Id, //no need to pass id when viewing own account Role = UserManager.GetRoles(user.Id).FirstOrDefault(), StartDate = user.UserStartDate, LastLoginDate = user.LastLoginDate, UserFullName = user.UserFullName, Email = user.Email, UserName = user.UserName, CountryName = countryName, EmailConfirmed = user.EmailConfirmed, ExpiryDate = user.ExpiryDate, LockoutEndDate = user.LockoutEndDateUtc, LockoutEnabled = user.LockoutEnabled, NumberOfBlogs = user.Blogs.ToList().Count(), NumberOfBlogComments = user.BlogComments.Count() }; ViewBag.OwnAccount = ownAccount; return(View(model)); }
public async Task <ActionResult> Register(RegisterViewModel model) { bool newAdminAccount = model.Role == null; //&& User.IsInRole(Role.Admin.ToString()); //<----------!! string countryName = ""; if (ModelState.IsValid) { var user = new ApplicationUser(); if (newAdminAccount) //AccountType == Role.Admin.ToString() { if (model.UserFirstName != "anonymous") { user.UserFirstName = model.UserFirstName; } if (model.UserLastName != "anonymous") { user.UserLastName = model.UserLastName; } //if (model.Organisation != "anonymous") user.Organisation = model.Organisation; user.LockoutEnabled = false; } else if (model.Role == RegisterRole.IdeaCarrier) { if (model.UserFirstName == "anonymous" || model.UserLastName == "anonymous" || model.Organisation == "anonymous") { ViewBag.CountryId = new SelectList(db.Countries, "CountryId", "CountryName"); return(View(model)); } Models.Shared.Country country = db.Countries.Where(c => c.CountryID == model.CountryId).FirstOrDefault(); user.CountryName = country.CountryAbbreviation; countryName = country.CountryName + " (" + country.CountryAbbreviation + ")"; //for email if (model.ExpiryDate.HasValue) { user.ExpiryDate = model.ExpiryDate; } user.LockoutEndDateUtc = model.LockoutEndDate; //Only for IdeaCarriers } else if (model.Role == RegisterRole.Investor) { Models.Shared.Country country = db.Countries.Where(c => c.CountryID == model.CountryId).FirstOrDefault(); user.CountryName = country.CountryAbbreviation; string investorRandomId = ""; do { investorRandomId = "INV" + user.CountryName + HelpFunctions.GetShortCode(); } while (db.Users.Any(u => u.ExternalId == investorRandomId)); user.ExternalId = investorRandomId; //countryName = country.CountryName + " (" + country.CountryAbbreviation + ")"; //for email //user.ActiveInvestor = true; //<-------to be implemented user.LockoutEnabled = false; } user.UserName = model.Email; user.Email = model.Email; user.UserStartDate = DateTime.Now; //<---move to ApplicationUser var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { if (newAdminAccount) { UserManager.AddToRole(user.Id, Role.Admin.ToString()); // AccountType == Role.Admin.ToString() Register another Admin account return(RedirectToAction("Index", "Account", new { accountType = Role.Admin.ToString() })); } else { UserManager.AddToRole(user.Id, model.Role.ToString()); await SendEmail(model, user); if (!User.Identity.IsAuthenticated && !User.IsInRole(Role.Admin.ToString())) //<----------------!! { user.LastLoginDate = DateTime.Now; //if (model.Role == RegisterRole.IdeaCarrier) user.LockoutEnabled = false; db.Entry(user).State = EntityState.Modified; db.SaveChanges(); await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); //await SendEmail(model, user.Id); } if (model.Role == RegisterRole.IdeaCarrier) { //await SendEmail(model, user.Id, user.CountryName); return(RedirectToAction("AddNewProject", "Startups")); } else if (model.Role == RegisterRole.Investor) { //await SendEmail(model, user.Id, user.CountryName); return(RedirectToAction("AddNewProfile", "Investments")); } } // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); // await sendEmail(model); <------------------- //if (User.Identity.IsAuthenticated) { //if (newAdminAccount) //AccountType == Role.Admin.ToString() User.IsInRole(Role.Admin.ToString()) <-----------------!! //{ // //if (AccountType == Role.Admin.ToString()) // return RedirectToAction("Index", "Account", new { newAdminAccount = true }); //Another Admin account registered // //else return RedirectToAction("Index", "Manage"); //<-- maybe never //} //else //if (model.Role == RegisterRole.IdeaCarrier) //{ // await SendEmail(model, user.Id); // return RedirectToAction("Index", "Account"); //<--------------------------------!! // //return RedirectToAction("Create", "StartUps"); //} //else if (model.Role == RegisterRole.Investor) //{ // await SendEmail(model, user.Id); // return RedirectToAction("Index", "Account"); //<--------------------------------!! // //return RedirectToAction("Create", "Investments"); //} //else if (/*User.Identity.IsAuthenticated && */model.Role == RegisterRole.IdeaCarrier) //User.IsInRole(Role.IdeaCarrier.ToString()) //{ // //await SendEmail(model, user.Id); // return RedirectToAction("Index", "StartUps"); //} //else if (/*User.Identity.IsAuthenticated && */model.Role == RegisterRole.IdeaCarrier) //User.IsInRole(Role.Investor.ToString()) //{ // //await SendEmail(model, user.Id); // return RedirectToAction("Index", "Investments"); //} return(RedirectToAction("Index", "Home")); } AddErrors(result); //<-----add error page if account already registered ViewBag.Error = "Error: <br />" + result.Succeeded + "<br />" + result.ToString(); ModelState.AddModelError("", "Account could not be created: < br />" + result.ToString()); ViewBag.AccountType = (newAdminAccount) ? Role.Admin.ToString() : ""; if (!User.IsInRole(Role.Admin.ToString())) { ViewBag.CountryId = new SelectList(db.Countries, "CountryId", "CountryName"); } return(View(model)); } // If we got this far, something failed, redisplay form //ViewBag.AccountType = AccountType; ViewBag.AccountType = (newAdminAccount) ? Role.Admin.ToString() : ""; if (!User.IsInRole(Role.Admin.ToString())) { ViewBag.CountryId = new SelectList(db.Countries, "CountryId", "CountryName"); } return(View(model)); }