Пример #1
0
        private async Task EnsureTestVolunteerExists(string email,
                                                     string phoneNumber,
                                                     string password)
        {
            using var scope = _serviceProvider.CreateScope();
            var userManager = scope.ServiceProvider.GetRequiredService <UserManager <VollyV3User> >();

            var testUser = new VollyV3User
            {
                UserName             = email,
                Email                = email,
                EmailConfirmed       = true,
                PhoneNumber          = phoneNumber,
                PhoneNumberConfirmed = true,
                LockoutEnabled       = false
            };
            var _user = await userManager.FindByEmailAsync(email);

            if (_user == null)
            {
                var createTestUser = await userManager.CreateAsync(testUser, password);

                if (createTestUser.Succeeded)
                {
                    await userManager.AddToRoleAsync(testUser, Enum.GetName(typeof(Role), Role.Volunteer));
                }
            }
        }
Пример #2
0
        private async Task LoadAsync(VollyV3User user)
        {
            Id = user.Id;

            var userName = await _userManager.GetUserNameAsync(user);

            var phoneNumber = await _userManager.GetPhoneNumberAsync(user);

            Username = userName;
            Input    = new InputModel
            {
                FullName    = user.FullName,
                PhoneNumber = phoneNumber
            };

            Applications = _context.Applications
                           .Include(x => x.Opportunity)
                           .ThenInclude(x => x.Organization)
                           .Include(x => x.Occurrence)
                           .Where(x => x.User == user)
                           .OrderBy(x => x.Opportunity.Organization.Id)
                           .ThenBy(x => x.Opportunity.Id)
                           .ThenBy(x => x.SubmittedDateTime)
                           .ToList();

            VolunteerHours = _context.VolunteerHours
                             .Where(x => x.User == user)
                             .OrderBy(x => x.OrganizationId)
                             .ThenBy(x => x.OpportunityId)
                             .ThenBy(x => x.DateTime)
                             .ToList();
        }
Пример #3
0
        public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            // Get the information about the user from the external login provider
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ErrorMessage = "Error loading external login information during confirmation.";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }

            if (ModelState.IsValid)
            {
                var user = new VollyV3User {
                    UserName = Input.Email, Email = Input.Email
                };
                var result = await _userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await _userManager.AddLoginAsync(user, info);

                    if (result.Succeeded)
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);

                        var userId = await _userManager.GetUserIdAsync(user);

                        var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                        var callbackUrl = Url.Page(
                            "/Account/ConfirmEmail",
                            pageHandler: null,
                            values: new { area = "Identity", userId = userId, code = code },
                            protocol: Request.Scheme);

                        await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                          $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            LoginProvider = info.LoginProvider;
            ReturnUrl     = returnUrl;
            return(Page());
        }
Пример #4
0
        private async Task LoadAsync(VollyV3User user)
        {
            var email = await _userManager.GetEmailAsync(user);

            Email = email;

            Input = new InputModel
            {
                NewEmail = email,
            };

            IsEmailConfirmed = await _userManager.IsEmailConfirmedAsync(user);
        }
Пример #5
0
        private async Task EnsureTestOrganizationAdministratorExists(string email,
                                                                     string phoneNumber,
                                                                     string password)
        {
            using var scope = _serviceProvider.CreateScope();
            var userManager = scope.ServiceProvider.GetRequiredService <UserManager <VollyV3User> >();
            var dbContext   = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>();

            var testUser = new VollyV3User
            {
                UserName             = email,
                Email                = email,
                EmailConfirmed       = true,
                PhoneNumber          = phoneNumber,
                PhoneNumberConfirmed = true,
                LockoutEnabled       = false
            };
            var _user = await userManager.FindByEmailAsync(email);

            if (_user == null)
            {
                var createTestUser = await userManager.CreateAsync(testUser, password);

                if (createTestUser.Succeeded)
                {
                    var newOrganization = new Organization()
                    {
                        Name            = email + "_testorg",
                        ContactEmail    = email,
                        PhoneNumber     = phoneNumber,
                        FullDescription = "Test organization",
                    };
                    await userManager.AddToRoleAsync(testUser, Enum.GetName(typeof(Role), Role.OrganizationAdministrator));

                    await dbContext.Organizations.AddAsync(newOrganization);

                    await dbContext.SaveChangesAsync();

                    var createdUser = await userManager.FindByEmailAsync(email);

                    dbContext.OrganizationAdministratorUsers.Add(new OrganizationAdministratorUser()
                    {
                        User         = createdUser,
                        Organization = newOrganization
                    });
                    await dbContext.SaveChangesAsync();
                }
            }
        }
Пример #6
0
 private static Application GetBaseApplication(ApplyApplicationModel application,
                                               Opportunity opportunity,
                                               VollyV3User user)
 {
     return(new Application()
     {
         Opportunity = opportunity,
         Name = application.Name,
         Email = application.Email,
         PhoneNumber = application.PhoneNumber,
         Message = application.Message,
         User = user,
         SubmittedDateTime = DateTime.Now
     });
 }
Пример #7
0
        private async Task LoadSharedKeyAndQrCodeUriAsync(VollyV3User user)
        {
            // Load the authenticator key & QR code URI to display on the form
            var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);

            if (string.IsNullOrEmpty(unformattedKey))
            {
                await _userManager.ResetAuthenticatorKeyAsync(user);

                unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);
            }

            SharedKey = FormatKey(unformattedKey);

            var email = await _userManager.GetEmailAsync(user);

            AuthenticatorUri = GenerateQrCodeUri(email, unformattedKey);
        }
Пример #8
0
 public async Task <HttpResponseMessage> AddUserToSegmentAsync(VollyV3User user, string segment)
 {
     return(await AddUsersToSegmentsAsync(
                new List <VollyV3User>() { user },
                new List <string>() { segment }));
 }
Пример #9
0
 /// <summary>
 /// Add User(s) to segment(s)
 /// </summary>
 /// <param name="user"></param>
 /// <returns></returns>
 public async Task <HttpResponseMessage> AddUserToNewsletterAsync(VollyV3User user)
 {
     return(await AddUsersToSegmentsAsync(
                new List <VollyV3User>() { user },
                new List <string>() { Environment.GetEnvironmentVariable("sendgrid_newsletter_custom_field") }));
 }
Пример #10
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            GoogleRecaptchaSiteKey = Environment.GetEnvironmentVariable("google_recaptcha_site_key");
            ExternalLogins         = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();

            if (!RecaptchaValidator.IsRecaptchaValid(Request.Form["g-recaptcha-response"]) || Input.TripCheck)
            {
                ModelState.AddModelError(string.Empty, "Captcha invalid");
                return(Page());
            }

            if (ModelState.IsValid)
            {
                var user = new VollyV3User
                {
                    UserName        = Input.Email,
                    Email           = Input.Email,
                    CreatedDateTime = DateTime.Now
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { area = "Identity", userId = user.Id, code = code },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", ComposeEmailMessage(callbackUrl));

                    await _userManager.AddToRoleAsync(user, Enum.GetName(typeof(Role), Role.Volunteer));

                    if (Input.IsOrganizationAdministrator)
                    {
                        await _userManager.AddToRoleAsync(user, Enum.GetName(typeof(Role), Role.OrganizationAdministrator));
                    }

                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToPage("RegisterConfirmation", new { email = Input.Email }));
                    }
                    else
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            return(Page());
        }