Example #1
0
        public async Task <CommandResult> Handle(AddUserCommand command)
        {
            if (!command.Validate(out ValidationError error))
            {
                return(CommandResult.Fail(error));
            }

            if (!string.IsNullOrWhiteSpace(command.Email) && userRepo.Get(command.Email) != null)
            {
                return(CommandResult.Fail(ValidationError.New().AddError(nameof(command.Email), "Email Already Exists")));
            }

            if (tenantRepository.Get(command.TenantId) == null)
            {
                return(CommandResult.Fail(UserManagementError.TenanNotFound()));
            }

            if (command.ValidateOnly)
            {
                return(CommandResult.Success());
            }

            var user = GetUserFromInput(command);

            user.Credentials     = GetCredentialsIfEmail(command.Email);
            user.ValidationCodes = SetupValidationCodes(command);
            userRepo.Add(user);

            if (!string.IsNullOrWhiteSpace(command.Email))
            {
                var email = new VerifyEmailEmail()
                {
                    To  = command.Email,
                    Url = string.Format(appSettings.EmailVerificationUrl, user.Id.ToString(), user.ValidationCodes.FirstOrDefault().Code)
                };
                await emailSender.Send(email);
            }

            return(CommandResult <string> .Success(user.Id.ToString()));
        }
Example #2
0
        public async Task <CommandResult> Handle(RegisterUserCommand input)
        {
            if (!input.Validate(out ValidationError error))
            {
                return(CommandResult.Fail(error));
            }

            if (!string.IsNullOrWhiteSpace(input.Email) && userRepo.Get(input.Email) != null)
            {
                return(CommandResult.Fail(ValidationError.New().AddError(nameof(input.Email), "Email Already Exists")));
            }

            if (input.ValidateOnly)
            {
                return(CommandResult.Success());
            }

            var user = new User()
            {
                Registered  = true,
                Credentials = new Credentials()
                {
                    Email         = input.Email,
                    EmailVerified = null,
                    Password      = hasher.HashPassword(input.Password)
                },
                Profile = new UserProfile()
                {
                    FirstName = input.FirstName,
                    LastName  = input.LastName,
                    Address   = new Address()
                    {
                        City    = input.City,
                        State   = input.State,
                        Street  = input.Street,
                        Country = input.Country,
                        Zip     = input.Zip
                    }
                },
                Tenants         = new List <UserTenant>(),
                ValidationCodes = new List <ValidationCode>()
            };

            var emailCode = new ValidationCode()
            {
                Code        = codeGenerator.Generate(30),
                CreatedDate = DateTime.Now,
                Type        = ValidationCodeType.Email
            };

            user.ValidationCodes.Add(emailCode);
            userRepo.Add(user);

            var email = new VerifyEmailEmail()
            {
                To  = input.Email,
                Url = string.Format(appSettings.EmailVerificationUrl, user.Id.ToString(), emailCode.Code)
            };
            await emailSender.Send(email);

            return(CommandResult <string> .Success(user.Id.ToString()));
        }