Exemplo n.º 1
0
        public async Task <ApplicationUser> Handle(CreateNewApplicationUserCommand cmd, bool useStrongPassword)
        {
            if (cmd.CommandId == null)
            {
                cmd.CommandId = Guid.NewGuid();
            }

            var applicationUser = new ApplicationUser(cmd.UserName, cmd.Password, cmd.ConfirmPassword, useStrongPassword, true);

            foreach (var role in cmd.Roles)
            {
                applicationUser.AddClaim(Constants.CLAIM_ROLE, role);
            }

            _applicationUserRepository.Add(applicationUser);
            _applicationUserRepository.Save();

            //fire event here...
            await _messageProducer.ProduceEventAsync <ApplicationUserCreatedEvent>(new ApplicationUserCreatedEvent
            {
                CorrelationId = (cmd.CommandId == null) ? Guid.NewGuid() : (Guid)cmd.CommandId,
                EntityId      = applicationUser.Id,
                Active        = applicationUser.Active,
                Claims        = cmd.Roles,
                DateCreated   = applicationUser.DateCreated,
                UserName      = applicationUser.UserName
            });

            return(applicationUser);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> CreateNewUser([FromBody] CreateNewApplicationUserCommand cmd)
        {
            try
            {
                var applicationUser = await _createNewApplicationUserCommandHandler.Handle(cmd, _appSettings.UseStrongPassword);

                return(Ok(new NewUserCreatedResponseDto
                {
                    Active = applicationUser.Active,
                    ApplicationUserId = applicationUser.Id
                }));
            }
            catch (Exception ex)
            {
                if (ex is DbUpdateException)
                {
                    return(BadRequest("That username is already in use."));
                }
                return(BadRequest(ex.Message));
            }
        }