Exemplo n.º 1
0
        public void OnGet(string emailAddress = null)
        {
            PrepareModel();

            emailAddress = ProcessEmail(emailAddress);

            if (!string.IsNullOrEmpty(emailAddress))
            {
                Input = new UserSpec()
                {
                    Email = emailAddress
                };
            }
        }
Exemplo n.º 2
0
        public async Task OnGetAsync()
        {
            var userUuid = User.GetUserUuid();

            var user = await _identityServices.GetUserAsync(userUuid).ConfigureAwait(false);

            Input = new UserSpec()
            {
                Email               = user.Email,
                FirstName           = user.FirstName,
                LastName            = user.LastName,
                PhoneNumber         = user.PhoneNumber,
                NotificationCulture = user.NotificationCulture
            };

            Email          = Input.Email;
            EmailConfirmed = user.EmailConfirmed;
        }
Exemplo n.º 3
0
        public IActionResult OnGet(string emailAddress = null, string firstName = null, string lastName = null)
        {
            if (_tenantInfoAccessor != null)
            {
                var tenantInfo = _tenantInfoAccessor.TenantInfo;

                if (tenantInfo.ExternalAuthenticationMethod != null)
                {
                    // do not allow local registration, if users are externally managed

                    return(LocalRedirect("~/"));
                }
            }

            PrepareModel();

            emailAddress = ProcessEmail(emailAddress);

            Input = new UserSpec();

            if (!string.IsNullOrEmpty(emailAddress))
            {
                Input.Email = emailAddress;
            }

            firstName = ProcessFirstName(firstName);

            if (!string.IsNullOrEmpty(firstName))
            {
                Input.FirstName = firstName;
            }

            lastName = ProcessLastName(lastName);

            if (!string.IsNullOrEmpty(lastName))
            {
                Input.LastName = lastName;
            }

            return(Page());
        }
Exemplo n.º 4
0
        public async Task <UserModel> UpdateUserAsync(string userUuid, UserSpec userSpec, bool isAdmin)
        {
            if (!_configurationProvider.ManageName && !_configurationProvider.ManagePhoneNumber)
            {
                throw new RequestFailedApiException(RequestFailedApiException.UserDetailsNotSupported, "This system does not support managing user detail data");
            }

            if (!isAdmin)
            {
                if (!_configurationProvider.SelfManagement)
                {
                    throw new ForbiddenApiException(ForbiddenApiException.SelfServiceNotAllowed, "It is not allowed to modify user data in self-service on this system");
                }
            }

            userUuid = ProcessUserUuid(userUuid);

            try
            {
                using (var transaction = await _identityDbContext.Database.BeginTransactionAsync().ConfigureAwait(false))
                {
                    var oldUser = await _userManager.FindByIdAsync(userUuid).ConfigureAwait(false);

                    if (oldUser == null)
                    {
                        throw new NotFoundApiException(NotFoundApiException.UserNotFound, $"User with UUID {userUuid} was not found", userUuid);
                    }

                    bool changed = false;

                    if (_configurationProvider.ManageName)
                    {
                        if (!string.IsNullOrEmpty(userSpec.FirstName))
                        {
                            userSpec.FirstName = ProcessFirstName(userSpec.FirstName);

                            if (!string.Equals(oldUser.FirstName, userSpec.FirstName))
                            {
                                oldUser.FirstName = userSpec.FirstName;

                                changed = true;
                            }
                        }

                        if (!string.IsNullOrEmpty(userSpec.LastName))
                        {
                            userSpec.LastName = ProcessLastName(userSpec.LastName);

                            if (!string.Equals(oldUser.LastName, userSpec.LastName))
                            {
                                oldUser.LastName = userSpec.LastName;

                                changed = true;
                            }
                        }
                    }

                    if (_configurationProvider.ManagePhoneNumber)
                    {
                        if (!string.IsNullOrEmpty(userSpec.PhoneNumber))
                        {
                            userSpec.PhoneNumber = ProcessPhoneNumber(userSpec.PhoneNumber);

                            if (!string.Equals(oldUser.PhoneNumber, userSpec.PhoneNumber))
                            {
                                oldUser.PhoneNumber = userSpec.PhoneNumber;

                                changed = true;
                            }
                        }
                    }

                    if (!string.IsNullOrEmpty(userSpec.NotificationCulture))
                    {
                        userSpec.NotificationCulture = ProcessNotificationCulture(userSpec.NotificationCulture)?.ToString();

                        if (!string.Equals(oldUser.NotificationCulture, userSpec.NotificationCulture))
                        {
                            oldUser.NotificationCulture = userSpec.NotificationCulture;

                            changed = true;
                        }
                    }

                    if (changed)
                    {
                        var updateResult = await _userManager.UpdateAsync(oldUser);

                        if (!updateResult.Succeeded)
                        {
                            HandleIdentityError(updateResult.Errors);
                        }

                        await _signInManager.RefreshSignInAsync(oldUser).ConfigureAwait(false);

                        await _identityDbContext.SaveChangesAsync().ConfigureAwait(false);

                        transaction.Commit();

                        await SendUserChangeNotificationAsync(oldUser.Id).ConfigureAwait(false);
                    }
                }

                using (var transaction = await _identityDbContext.Database.BeginTransactionAsync().ConfigureAwait(false))
                {
                    var newUser = await _userManager.FindByIdAsync(userUuid).ConfigureAwait(false);

                    return(newUser);
                }
            }
            catch (ApiException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                _logger.LogError($"Error when updating user: {e}");

                throw new InternalServerErrorApiException();
            }
        }
Exemplo n.º 5
0
        public async Task <UserModel> CreateUserAsync(UserSpec userSpec, bool isSelfRegistration, bool emailIsAlreadyConfirmed = false)
        {
            if (isSelfRegistration)
            {
                if (!_configurationProvider.SelfRegistration)
                {
                    throw new ForbiddenApiException(ForbiddenApiException.SelfRegistrationNotAllowed, "It is not allowed to register users in self-service on this system");
                }
            }

            userSpec.Email = ProcessEmail(userSpec.Email);

            if (_configurationProvider.SelfManagement)
            {
                if (_configurationProvider.ManageName && _configurationProvider.RegisterName)
                {
                    userSpec.FirstName = ProcessFirstName(userSpec.FirstName);
                    userSpec.LastName  = ProcessLastName(userSpec.LastName);
                }

                if (_configurationProvider.ManagePhoneNumber && _configurationProvider.RegisterPhoneNumber)
                {
                    userSpec.PhoneNumber = ProcessPhoneNumber(userSpec.PhoneNumber);
                }
            }

            userSpec.Password             = ProcessPassword(userSpec.Password);
            userSpec.PasswordConfirmation = ProcessPasswordConfirmation(userSpec.Password, userSpec.PasswordConfirmation);

            if (!userSpec.AcceptTermsAndConditions)
            {
                throw new RequestFailedApiException(RequestFailedApiException.PleaseAcceptTermsAndConditions, "Please accept the terms and conditions");
            }

            if (!userSpec.AcceptPrivacyPolicy)
            {
                throw new RequestFailedApiException(RequestFailedApiException.PleaseAcceptPrivacyPolicy, "Please accept the privacy policy");
            }

            try
            {
                string newUserUuid = await ReserveUserUuidAsync(userSpec.Email).ConfigureAwait(false);

                using (var transaction = await _identityDbContext.Database.BeginTransactionAsync().ConfigureAwait(false))
                {
                    var existingUser = await _userManager.FindByEmailAsync(userSpec.Email).ConfigureAwait(false);

                    if (existingUser != null)
                    {
                        throw new RequestFailedApiException(RequestFailedApiException.EmailAlreadyExists, "A user account with that email address already exists");
                    }

                    var user = new UserModel {
                        Id = newUserUuid, UserName = userSpec.Email, Email = userSpec.Email
                    };

                    if (_configurationProvider.SelfManagement)
                    {
                        if (_configurationProvider.ManageName && _configurationProvider.RegisterName)
                        {
                            user.FirstName = userSpec.FirstName;
                            user.LastName  = userSpec.LastName;
                        }

                        if (_configurationProvider.ManagePhoneNumber && _configurationProvider.RegisterPhoneNumber)
                        {
                            user.PhoneNumber = userSpec.PhoneNumber;
                        }
                    }

                    if (emailIsAlreadyConfirmed)
                    {
                        user.EmailConfirmed = true;
                    }

                    user.NotificationCulture = CultureInfo.CurrentCulture.TwoLetterISOLanguageName;

                    user.PrivacyPolicyAccepted        = _nowProvider.Now;
                    user.PrivacyPolicyUrl             = _configurationProvider.PrivacyPolicyUrl;
                    user.PrivacyPolicyVersionAccepted = _configurationProvider.PrivacyPolicyVersion;

                    if (userSpec.AcceptCommunication)
                    {
                        user.CommunicationAccepted        = _nowProvider.Now;
                        user.CommunicationUrl             = _configurationProvider.PrivacyPolicyUrl;
                        user.CommunicationVersionAccepted = _configurationProvider.PrivacyPolicyVersion;
                    }

                    user.TermsAndConditionsAccepted        = _nowProvider.Now;
                    user.TermsAndConditionsUrl             = _configurationProvider.TermsAndConditionsUrl;
                    user.TermsAndConditionsVersionAccepted = _configurationProvider.TermsAndConditionsVersion;

                    if (_tenantInfoAccessor != null)
                    {
                        var tenantInfo = _tenantInfoAccessor.TenantInfo;

                        if (tenantInfo != null)
                        {
                            string tenantPrivacyPolicyUrl = tenantInfo.DeveloperPrivacyPolicyUrl;
                            if (!string.IsNullOrEmpty(tenantPrivacyPolicyUrl))
                            {
                                user.PrivacyPolicyUrl = tenantPrivacyPolicyUrl;

                                if (userSpec.AcceptCommunication)
                                {
                                    user.CommunicationUrl = tenantPrivacyPolicyUrl;
                                }
                            }

                            int?tenantPrivacyPolicyVersion = tenantInfo.DeveloperPrivacyPolicyVersion;
                            if (tenantPrivacyPolicyVersion != null && tenantPrivacyPolicyVersion > 0)
                            {
                                user.PrivacyPolicyVersionAccepted = tenantPrivacyPolicyVersion;

                                if (userSpec.AcceptCommunication)
                                {
                                    user.CommunicationVersionAccepted = tenantPrivacyPolicyVersion;
                                }
                            }

                            string tenantTermsAndConditionsUrl = tenantInfo.DeveloperTermsAndConditionsUrl;
                            if (!string.IsNullOrEmpty(tenantTermsAndConditionsUrl))
                            {
                                user.TermsAndConditionsUrl = tenantTermsAndConditionsUrl;
                            }

                            int?tenantTermsAndConditionsVersion = tenantInfo.DeveloperTermsAndConditionsVersion;
                            if (tenantTermsAndConditionsVersion != null && tenantTermsAndConditionsVersion > 0)
                            {
                                user.TermsAndConditionsVersionAccepted = tenantTermsAndConditionsVersion;
                            }
                        }
                    }

                    var result = await _userManager.CreateAsync(user, userSpec.Password).ConfigureAwait(false);

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

                        if (!emailIsAlreadyConfirmed)
                        {
                            var code = await _userManager.GenerateEmailConfirmationTokenAsync(user).ConfigureAwait(false);

                            var currentCultureInfo = CultureInfo.CurrentCulture;

                            var callbackUrl = _urlHelper.Page(
                                "/Account/ConfirmEmail",
                                pageHandler: null,
                                values: new { userUuid = user.Id, code, culture = currentCultureInfo.ToString() },
                                protocol: "https");

                            EmailTemplate emailTemplate = await _emailTemplateProvider.GetConfirmAccountEmailAsync(
                                new ConfirmAccountEmailViewModel(callbackUrl), currentCultureInfo).ConfigureAwait(false);

                            await _emailSender.SendEmailAsync(userSpec.Email, emailTemplate.Subject, emailTemplate.Body).ConfigureAwait(false);
                        }

                        await _identityDbContext.SaveChangesAsync().ConfigureAwait(false);

                        transaction.Commit();

                        await SendUserChangeNotificationAsync(user.Id).ConfigureAwait(false);

                        if (isSelfRegistration)
                        {
                            if (!_configurationProvider.RequireEmailConfirmed || user.EmailConfirmed)
                            {
                                await _signInManager.SignInAsync(user, isPersistent : false).ConfigureAwait(false);
                            }
                        }

                        return(user);
                    }
                    else
                    {
                        HandleIdentityError(result.Errors);

                        // to satisfy the compiler
                        throw new InternalServerErrorApiException();
                    }
                }
            }
            catch (ApiException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                _logger.LogError($"Error when registering user: {e}");

                throw new InternalServerErrorApiException();
            }
        }