private async Task<List<UserRegistration>> ImportUsers(OrgUserRegistration orgUserReg) { List<UserRegistration> userErrors = new List<UserRegistration>(); using (var db = new StationCADDb()) { // Loop thru.... foreach (UserRegistration item in orgUserReg.Users) { User user = null; try { // 1. Check to see if the user exists user = db.Users.Include("Profile").Where(x => x.Email == item.Email).FirstOrDefault(); // 1b. if not, CreateAsync() for User if (user == null) { user = new User { Id = Guid.NewGuid().ToString(), UserName = item.Email, Email = item.Email }; DateTime now = DateTime.Now; user.Profile = new UserProfile { FirstName = item.FirstName, LastName = item.LastName, CreateUser = "", CreateDate = now, LastUpdateUser = "", LastUpdateDate = now }; var result = await UserManager.CreateAsync(user, "P@ssword1"); if (!result.Succeeded) { userErrors.Add(item); break; } } // add user-org-affiliation OrganizationUserAffiliation uoa = new OrganizationUserAffiliation(); uoa.CurrentOrganization = orgUserReg.Organization; uoa.Role = OrganizationUserRole.User; uoa.Status = OrganizationUserStatus.Active; user.Profile.OrganizationAffiliations = new List<OrganizationUserAffiliation>(); user.Profile.OrganizationAffiliations.Add(uoa); await db.SaveChangesAsync(); } catch(Exception ex) { string msg = string.Format("", ""); base.LogException("", ex); userErrors.Add(item); break; } // 3. Create Email confirmation // 3a. If !emailConfirmed, create emailConfirmToken and send email if (!user.EmailConfirmed) { string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); string body = string.Format("{1}, {0}{0} You have been added as a user to {2}. Please confirm your account by clicking <a href=\"{3}\">here</a>. You will be required to choose a new password. {0}{0} Thanks, {0}{0} The StationCAD Team", Environment.NewLine, user.Profile.FirstName, orgUserReg.Organization.Name, callbackUrl); await UserManager.SendEmailAsync(user.Id, "StationCAD - Activate your account", body); } // 3b. Just send email letting them know they have been added to the org else { string body = string.Format("{1}, {0}{0} You have been added as a user to {2}. {0}{0} Thanks, {0}{0} The StationCAD Team", Environment.NewLine, user.Profile.FirstName, orgUserReg.Organization.Name); await UserManager.SendEmailAsync(user.Id, "StationCAD - You are part of a new Organization!", body); } } } return userErrors; }
public async Task<ActionResult> Update(EditViewModel model) { try { string id = User.Identity.GetUserId(); using (var db = new StationCADDb()) { User usrUpdate = db.Users.Include("Profile").Where(x => x.Id == id).FirstOrDefault(); usrUpdate.Profile.FirstName = model.FirstName; usrUpdate.Profile.LastName = model.LastName; usrUpdate.Profile.SecurityQuestion = model.SecurityQuestion; usrUpdate.Profile.SecurityAnswer = model.SecurityAnswer; usrUpdate.Profile.AccountEmail = model.AccountEmail; usrUpdate.Profile.IdentificationNumber = model.IdentificationNumber; usrUpdate.Profile.NotificationEmail = model.NotificationEmail; usrUpdate.Profile.NotificationCellPhone = model.NotificationCellPhone; usrUpdate.Profile.NotifcationPushMobile = model.NotifcationPushMobile; usrUpdate.Profile.NotifcationPushBrowser = model.NotifcationPushBrowser; await db.SaveChangesAsync(); } } catch (Exception ex) { string msg = string.Format("An error occured in ManageController.Update(EditViewModel model). Message: {0}", ex.Message); LogException(msg, ex); return View("Error"); } return RedirectToAction("index"); }