Exemplo n.º 1
0
        public async Task <Result> SendEmilVerificationMail(SendEmailVerificationMailRequest request, string adminId)
        {
            ValidationResult validationResult = _sendEmailVerificationMailValidator.Validate(request);

            if (!validationResult.IsValid)
            {
                _logger.LogWarning($"Invlid SendEmailVerificationMailRequest. AdminId {adminId}");
                return(Result.Fail(validationResult.Errors));
            }

            BaseSpecification <AppUserEntity> userSpecification = new BaseSpecification <AppUserEntity>();

            userSpecification.AddFilter(x => x.Id == request.UserId);

            AppUserEntity appUser = _userRepository.SingleOrDefault(userSpecification);

            if (appUser == null)
            {
                _logger.LogWarning($"No User. UserId {request.UserId}, AdminId {adminId}");
                return(Result.Fail("no_user", "No user"));
            }

            string code = await _userManager.GenerateEmailConfirmationTokenAsync(appUser);

            await _emailConfirmationService.SendVerificationMail(appUser, code);

            return(Result.Ok());
        }
Exemplo n.º 2
0
        public async Task <Result> Register(RegisterRequest registerRequest)
        {
            ValidationResult validationResult = _registerValidator.Validate(registerRequest);

            if (!validationResult.IsValid)
            {
                _logger.LogError($"Invalid RegisterRequest");
                return(Result.Fail(ResultUtils.ToResultError(validationResult.Errors)));
            }

            AppUserEntity appUser = new AppUserEntity(
                userName: registerRequest.Email,
                email: registerRequest.Email,
                firstName: registerRequest.FirstName,
                lastName: registerRequest.LastName,
                emailConfirmed: false,
                enabled: true);

            IdentityResult identityResult = await _userManager.CreateAsync(appUser, registerRequest.Password);

            if (!identityResult.Succeeded)
            {
                _logger.LogError($"Failed to register user");
                return(Result.Fail(ResultUtils.ToResultError(identityResult.Errors)));
            }

            string code = await _userManager.GenerateEmailConfirmationTokenAsync(appUser);

            await _emailService.SendVerificationMail(appUser, code);

            return(Result.Ok());
        }
Exemplo n.º 3
0
        public async Task <Result> Register(RegisterRequest registerRequest)
        {
            if (!_identityUIEndpoints.RegisterEnabled)
            {
                _logger.LogError($"User tried to register, but registrations are disabled");
                return(Result.Fail("registration_is_not_enabled", "Registration disabled"));
            }

            ValidationResult registerValidationResult      = _registerValidator.Validate(registerRequest);
            ValidationResult userAttributeValidationResult = _userAttributeRequestValidator.Validate(registerRequest);

            if (!registerValidationResult.IsValid || !userAttributeValidationResult.IsValid)
            {
                _logger.LogError($"Invalid {typeof(RegisterRequest).Name} model");

                List <Result.ResultError> errors = ResultUtils.ToResultError(registerValidationResult.Errors);
                errors.AddRange(ResultUtils.ToResultError(userAttributeValidationResult.Errors));

                return(Result.Fail(errors));
            }

            List <UserAttributeEntity> userAttributes = null;

            if (registerRequest.Attributes != null)
            {
                userAttributes = registerRequest.Attributes
                                 .Select(x => new UserAttributeEntity(
                                             key: x.Key,
                                             value: x.Value))
                                 .ToList();
            }

            string username;

            if (_identityUIEndpoints.UseEmailAsUsername)
            {
                username = registerRequest.Email;
            }
            else
            {
                if (string.IsNullOrEmpty(registerRequest.Username))
                {
                    _logger.LogError($"Username cannot be empty");
                    return(Result.Fail("username_cannot_be_empty", "username_cannot_be_empty"));
                }

                username = registerRequest.Username;
            }

            AppUserEntity appUser = new AppUserEntity(
                userName: username,
                email: registerRequest.Email,
                firstName: registerRequest.FirstName,
                lastName: registerRequest.LastName,
                emailConfirmed: false,
                enabled: true,
                phoneNumber: registerRequest.PhoneNumber,
                attributes: userAttributes);

            IdentityResult identityResult = await _userManager.CreateAsync(appUser, registerRequest.Password);

            if (!identityResult.Succeeded)
            {
                _logger.LogError($"Failed to register user");
                return(Result.Fail(ResultUtils.ToResultError(identityResult.Errors)));
            }

            string code = await _userManager.GenerateEmailConfirmationTokenAsync(appUser);

            await _emailService.SendVerificationMail(appUser, code);

            return(Result.Ok());
        }
Exemplo n.º 4
0
        public virtual async Task <CommonUtils.Result.Result <AppUserEntity> > AddUser(
            BaseRegisterRequest baseRegisterRequest,
            bool setPassword          = true,
            bool sendConfirmationMail = true,
            bool emailConfirmed       = false)
        {
            ValidationResult validationResult;

            if (_identityUIEndpoints.UseEmailAsUsername)
            {
                validationResult = _baseRegisterValidator.Validate(baseRegisterRequest, ruleSet: BaseRegisterRequestValidator.USE_EMAIL_AS_USERNAME);
            }
            else
            {
                validationResult = _baseRegisterValidator.Validate(baseRegisterRequest, ruleSet: BaseRegisterRequestValidator.REQUIRE_EMAIL_USERNAME);
            }

            if (setPassword)
            {
                ValidationResult setPasswordValidationResult = _baseRegisterValidator.Validate(baseRegisterRequest, ruleSet: BaseRegisterRequestValidator.REQUIRE_PASSWORD);
                if (!setPasswordValidationResult.IsValid)
                {
                    foreach (ValidationFailure setPasswordError in setPasswordValidationResult.Errors)
                    {
                        validationResult.Errors.Add(setPasswordError);
                    }
                }
            }

            if (!validationResult.IsValid)
            {
                _logger.LogError($"Invalid {typeof(BaseRegisterRequest).Name} model");
                return(Result.Fail <AppUserEntity>(validationResult.ToResultError()));
            }

            ValidationResult userAttributeValidationResult = _userAttributeRequestValidator.Validate(baseRegisterRequest);

            if (!userAttributeValidationResult.IsValid)
            {
                _logger.LogError($"Invalid {typeof(IUserAttributeRequest).Name} type");
                return(Result.Fail <AppUserEntity>(validationResult.ToResultError()));
            }

            List <UserAttributeEntity> userAttributes = null;

            if (baseRegisterRequest.Attributes != null)
            {
                userAttributes = baseRegisterRequest.Attributes
                                 .Select(x => new UserAttributeEntity(
                                             key: x.Key?.Trim(),
                                             value: x.Value?.Trim()))
                                 .ToList();
            }

            if (_identityUIEndpoints.UseEmailAsUsername)
            {
                baseRegisterRequest.Username = baseRegisterRequest.Email;
            }

            baseRegisterRequest.Username.Trim();

            Result beforeUserAddResult = await _addUserFilter.BeforeAdd(baseRegisterRequest);

            if (beforeUserAddResult.Failure)
            {
                return(Result.Fail <AppUserEntity>(beforeUserAddResult));
            }

            AppUserEntity appUser = new AppUserEntity(
                userName: baseRegisterRequest.Username,
                email: baseRegisterRequest.Email,
                firstName: baseRegisterRequest.FirstName,
                lastName: baseRegisterRequest.LastName,
                emailConfirmed: emailConfirmed,
                enabled: true,
                phoneNumber: baseRegisterRequest.PhoneNumber,
                attributes: userAttributes);

            IdentityResult identityResult;

            if (setPassword)
            {
                identityResult = await _userManager.CreateAsync(appUser, baseRegisterRequest.Password);
            }
            else
            {
                identityResult = await _userManager.CreateAsync(appUser);
            }

            if (!identityResult.Succeeded)
            {
                _logger.LogError($"Failed to register user");
                return(Result.Fail <AppUserEntity>(identityResult.ToResultError()));
            }

            Result afterUserAddedResult = await _addUserFilter.AfterAdded(appUser);

            if (afterUserAddedResult.Failure)
            {
                return(Result.Fail <AppUserEntity>(afterUserAddedResult));
            }

            if (sendConfirmationMail)
            {
                string code = await _userManager.GenerateEmailConfirmationTokenAsync(appUser);

                await _emailService.SendVerificationMail(appUser, code);
            }

            return(Result.Ok(appUser));
        }