public async Task <Guid> HandleCommand(CreateNewUserCommand cmd) { var org = _organizationRepository.Get(cmd.OrganizationId); if (org == null) { throw new Exception("Organization does not exist."); } var user = org.AddNewUser(cmd.FirstName, cmd.LastName, cmd.Email, cmd.PhoneNumber, cmd.IdentityId, active: true); _organizationRepository.Save(); await _messageProducer.ProduceEventAsync <UserCreatedEvent>(new UserCreatedEvent { CorrelationId = (cmd.CommandId == null) ? Guid.NewGuid() : (Guid)cmd.CommandId, Active = user.Active, DateJoined = user.DateJoined, Email = user.Email, EntityId = user.ID, FirstName = user.FirstName, LastName = user.LastName, IdentityId = user.IdentityId, OrganizationId = org.ID, PhoneNumber = user.PhoneNumber, TimeStamp = DateTime.UtcNow }); return(user.ID); }
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); }
public async Task <LoginCommandResponseDto> HandleCommand(LoginCommand cmd) { string loginFailureReason = ""; var user = _applicationUserRepository.GetActiveUserByUsername(cmd.Username); //this username is no good... if (user == null) { loginFailureReason = "User doesn't exist in our records"; return(new LoginCommandResponseDto { LoginSuccess = false, FailureReason = loginFailureReason }); } var response = user.Authenticates(cmd.Password, out loginFailureReason); _applicationUserRepository.Save(); if (!response) { return new LoginCommandResponseDto { LoginSuccess = false, FailureReason = loginFailureReason } } ; //fire event here... await _messageProducer.ProduceEventAsync <UserLoggedInEvent>(new UserLoggedInEvent { CorrelationId = (cmd.CommandId == null) ? Guid.NewGuid() : (Guid)cmd.CommandId, EntityId = user.Id, UserName = user.UserName, TimeStamp = user.LastLogin }); return(new LoginCommandResponseDto { LoginSuccess = true, IdentityUserId = user.Id }); }
private async Task <bool> FireOrgAddedEvent(Organization org, AddOrganizationCommand cmd) { var addressDtos = new List <AddressDto>(); addressDtos.AddRange(org.Addresses.Select(x => new AddressDto { Address1 = x.Address1, Address2 = x.Address2, City = x.City, ID = x.ID, PostalCode = x.PostalCode, StateProvince = x.AddressStateProvince.Name, TypeOfAddress = x.TypeOfAddress })); await _messageProducer.ProduceEventAsync <OrganizationCreatedEvent>(new OrganizationCreatedEvent { CorrelationId = (cmd.CommandId == null) ? Guid.NewGuid() : (Guid)cmd.CommandId, EntityId = org.ID, Active = org.Active, Addresses = addressDtos, DbaName = org.DbaName, EIN = org.EIN, Name = org.Name, OrgType = new OrgTypeDto { CanSell = org.OrgType.CanSell, Description = org.OrgType.Description, ID = org.OrgType.ID, Name = org.OrgType.Name }, ParentId = org.ParentId, TimeStamp = org.Created, Verified = org.Verified }); return(true); }