public async Task <ActionResult <UserViewModel> > Create(CreateUserViewModel model)
        {
            if (model.Password != model.ConfirmPassword)
            {
                return(BadRequest());
            }

            bool userAlreadyExists = await _userRepository.Exists(model.Username);

            if (userAlreadyExists)
            {
                return(Conflict());
            }

            string hashPassword = _passwordHasher.Hash(model.Password);
            var    user         = new User
            {
                Username = model.Username
            };
            await _userRepository.Insert(user, hashPassword);

            var userViewModel = new UserViewModel
            {
                Id       = user.Id,
                Username = user.Username
            };

            return(CreatedAtAction(nameof(Show), new { username = model.Username }, userViewModel));
        }
        public async Task <SaveShopperResult> Handle(CreateShopperCommand command, CancellationToken cancellationToken)
        {
            var result = new SaveShopperResult();

            if (await _userRepository.AnyAsync(x => x.Email.ToLower() == command.Email.ToLower()))
            {
                Notifications.Handle("E-mail em uso");
            }

            if (await _shopperRepository.AnyAsync(x => x.Cpf.Number.Equals(command.Cpf.Number)))
            {
                Notifications.Handle("CPF em uso");
            }

            if (Notifications.HasNotifications())
            {
                return(null);
            }

            var user = User.New(command.Email, _passwordHasherService.Hash(command.Password), command.Name,
                                EUserType.Shopper);

            command.Cpf.Type = EIdentiticationType.Cpf;
            var shopper = Shopper.New(user, command.Cpf);

            await _shopperRepository.AddAsync(shopper);

            if (!await CommitAsync())
            {
                return(null);
            }

            await _shopperAddressRepository.AddAsync(ShopperAddress.New(shopper.Id, EAddressType.Home, command.Address,
                                                                        "registro"));

            if (!await CommitAsync())
            {
                return(null);
            }

            result.Id = shopper.Id;
            return(result);
        }
        public async Task <User> Register(string email, string password)
        {
            var isPasswordValid = _hasNumber.IsMatch(password) && _hasUpperChar.IsMatch(password) && _hasMinimum8Chars.IsMatch(password);

            if (!isPasswordValid)
            {
                throw ExceptionFactory.SoftException(ExceptionEnum.PasswordNotValid, $"Password need to follow:\n\tMinimum 8 char\n\tHave at least one number\n\tHave upper char");
            }

            if (!_emailRegex.IsMatch(email))
            {
                throw ExceptionFactory.SoftException(ExceptionEnum.EmailFormatInvalid, $"Email {email} have wrong format");
            }

            if (_publicContext.User.FirstOrDefault(x => x.Email == email) != null)
            {
                throw ExceptionFactory.SoftException(ExceptionEnum.UserAlreadyExist,
                                                     $"User with email {email} already exist");
            }

            var confirmToken = new UserConfirmationToken()
            {
                ConfirmEmail = email,
                ConfirmId    = Guid.NewGuid(),
                IsActive     = true,
            };

            if (!await SendConfirmEmail(email, confirmToken.ConfirmId.ToString()))
            {
                throw ExceptionFactory.SoftException(ExceptionEnum.CantSendEmail,
                                                     $"Can't send confirmation email. Try later");
            }

            User userModel = new User()
            {
                Email        = email,
                UserSecurity = new UserSecurity()
                {
                    Password               = _passwordHasherService.Hash(password),
                    Role                   = await _publicContext.Role.FirstOrDefaultAsync(x => x.RoleName == "PreMember"),
                    IsConfirmed            = false,
                    UserConfirmationTokens = new List <UserConfirmationToken>()
                    {
                        confirmToken,
                    },
                },
            };

            await _publicContext.User.AddAsync(userModel);

            await _publicContext.SaveChangesAsync();

            return(userModel);
        }
        public async Task <DefaultResult> Handle(CreateUserCommand command, CancellationToken cancellationToken)
        {
            var result = new DefaultResult();

            var user = User.New(command.Email, _passwordHasherService.Hash(command.Password), command.Name,
                                command.UserType);

            await _userRepository.AddAsync(user);

            if (!await CommitAsync())
            {
                return(result);
            }
            return(result);
        }
Exemple #5
0
        public override Task <RegistrationResult> RegisterAsync(User user)
        {
            if (!String.IsNullOrEmpty(user.Password))
            {
                if (!_passwordRegex.Match(user.Password))
                {
                    return(Task.FromResult(RegistrationResult.BadPasswordFormat));
                }

                user.Password = _hasher.Hash(user.Password);
            }

            if (_userRepo.SearchFor(u => u.Email.Equals(user.Email)).Any())
            {
                return(Task.FromResult(RegistrationResult.UserAlreadyExist));
            }

            return(base.RegisterAsync(user));
        }