示例#1
0
        public async Task <IActionResult> Register(RegisterViewModel newUser)
        {
            VolunteerRepository repo = new VolunteerRepository(configModel.ConnectionString);
            VolunteerModel      profile;
            List <string>       errors = new List <string>();
            IdentityResult      passResult;
            bool passwordFailed = false;
            int  id;

            // Validate that the first and last name were provided
            if (String.IsNullOrEmpty(newUser.FirstName))
            {
                return(Utilities.ErrorJson("You must provide a first name"));
            }
            if (String.IsNullOrEmpty(newUser.LastName))
            {
                return(Utilities.ErrorJson("You must provide a last name"));
            }

            // Check if a user with that email address already exists
            var existingUser = await userManager.FindByNameAsync(newUser.Email);

            if (existingUser != null)
            {
                return(Utilities.ErrorJson("That email is already in use."));
            }

            // Validate that the username and password are valid and no account exists with the username.  We do this here to prevent
            // having to create and then delete a volunteer profile in the database in the event that one is invalid
            if (!UserHelpers.IsValidEmail(newUser.Email))
            {
                return(Utilities.ErrorJson("You must use an email address to sign up."));
            }
            foreach (var validator in userManager.PasswordValidators)
            {
                passResult = await validator.ValidateAsync(userManager, null, newUser.Password);

                if (!passResult.Succeeded)
                {
                    passwordFailed = true;
                    foreach (var error in passResult.Errors)
                    {
                        errors.Add(error.Description);
                    }
                }
            }
            if (passwordFailed)
            {
                return(Utilities.ErrorJson(String.Join(" ", errors)));
            }

            // Create the profile in the database
            try
            {
                id = repo.CreateVolunteer(new VolunteerModel
                {
                    Email         = newUser.Email,
                    FirstName     = newUser.FirstName,
                    LastName      = newUser.LastName,
                    PreferredName = newUser.FirstName,
                    Picture       = newUser.Picture
                });

                if (id == 0)
                {
                    throw new Exception("Unable to create profile in database");
                }
            }
            catch (Exception e)
            {
                return(Utilities.ErrorJson(e.Message));
            }

            try
            {
                profile = repo.GetVolunteer(id);
            }
            catch (Exception e)
            {
                repo.DeleteVolunteer(id);
                return(Utilities.ErrorJson(e.Message));
            }

            ApplicationUser user = new ApplicationUser
            {
                UserName    = profile.Email,
                FirstName   = profile.FirstName,
                LastName    = profile.LastName,
                VolunteerId = profile.Id,
                Email       = profile.Email
            };

            if (!await roleManager.RoleExistsAsync(UserHelpers.UserRoles.Volunteer.ToString()))
            {
                await roleManager.CreateAsync(new IdentityRole { Name = UserHelpers.UserRoles.Volunteer.ToString() });
            }

            IdentityResult result = await userManager.CreateAsync(user, newUser.Password);

            if (result.Succeeded)
            {
                user = await userManager.FindByNameAsync(user.UserName);

                await userManager.AddToRoleAsync(user, UserHelpers.UserRoles.Volunteer.ToString());

                try
                {
                    await EmailHelpers.SendEmail(user.Email, "Welcome to Orlando Children's Church!",
                                                 $"Thank you for signing up for Orlando Children's Church, {newUser.FirstName}!\n\n"
                                                 + "Please remember to visit our operation portal's website at https://www.operation-portal.com in order to finish filling out your profile.\n\n"
                                                 + "Thanks,\nThe OCC team", configModel.EmailOptions);
                }
                catch (Exception e)
                {
                    // We don't want to abord the signup if the user isn't able to get the email, so this catch remains empty
                }
            }
            else
            {
                // If an error occurred and the user account could not be created for whatever reason, we want to delete the volunteer profile
                repo.DeleteVolunteer(profile.Id);

                // Then we want to return an error message
                foreach (var error in result.Errors)
                {
                    errors.Add(error.Description);
                }

                return(Utilities.ErrorJson(String.Join(" ", errors)));
            }

            return(Utilities.NoErrorJson());
        }