public async Task ExecuteAsync(InviteUser command) { var inviter = await _userRepository.GetUserAsync(command.UserId); var invitedUser = await _userRepository.FindByEmailAsync(command.EmailAddress); if (invitedUser != null) { var member = new ApplicationTeamMember(command.ApplicationId, invitedUser.AccountId) { AddedByName = inviter.UserName, Roles = new[] { ApplicationRole.Member } }; await _applicationRepository.CreateAsync(member); await _eventBus.PublishAsync(new UserAddedToApplication(command.ApplicationId, member.AccountId)); return; } var invitedMember = new ApplicationTeamMember(command.ApplicationId, command.EmailAddress) { AddedByName = inviter.UserName, Roles = new[] { ApplicationRole.Member } }; await _applicationRepository.CreateAsync(invitedMember); var invitation = await _invitationRepository.FindByEmailAsync(command.EmailAddress); if (invitation == null) { invitation = new Invitation(command.EmailAddress, inviter.UserName); await _invitationRepository.CreateAsync(invitation); await SendInvitationEmailAsync(invitation, command.Text); } invitation.Add(command.ApplicationId, inviter.UserName); await _invitationRepository.UpdateAsync(invitation); var app = await _applicationRepository.GetByIdAsync(command.ApplicationId); var evt = new UserInvitedToApplication( invitation.InvitationKey, command.ApplicationId, app.Name, command.EmailAddress, inviter.UserName); await _eventBus.PublishAsync(evt); }
public async Task <AcceptInvitationReply> ExecuteAsync(AcceptInvitation request) { var account = new Account(request.UserName, request.Password); account.SetVerifiedEmail(request.Email); var invitation = await _repository.FindByEmailAsync(request.Email); if (invitation == null) { return(null); } await _repository.DeleteAsync(request.InvitationKey); var inviter = await _accountRepository.FindByUserNameAsync(invitation.InvitedBy); account.Activate(); account.Login(request.Password); var roles = invitation.Invitations.Select(x => "Member_" + x.ApplicationId).ToArray(); await _accountRepository.CreateAsync(account); Thread.CurrentPrincipal = new OneTruePrincipal(account.Id, account.UserName, roles); await _eventBus.PublishAsync(new AccountActivated(account.Id, account.UserName) { EmailAddress = account.Email }); var e = new InvitationAccepted(account.Id, invitation.InvitedBy, account.UserName) { EmailAddress = account.Email, ApplicationIds = invitation.Invitations.Select(x => x.ApplicationId).ToArray() }; await _eventBus.PublishAsync(e); return(new AcceptInvitationReply(account.Id, account.UserName)); }
public async Task HandleAsync(IMessageContext context, DeleteInvitation message) { var invite = await _invitationRepository.FindByEmailAsync(message.InvitedEmailAddress); await _applicationRepository.RemoveTeamMemberAsync(message.ApplicationId, message.InvitedEmailAddress); invite.Remove(message.ApplicationId); if (!invite.Invitations.Any()) { await _invitationRepository.DeleteAsync(invite.InvitationKey); await context.SendAsync(new InvitationDeleted { ApplicationIds = new[] { message.ApplicationId }, InvitedEmailAddress = message.InvitedEmailAddress, InvitationId = invite.Id }); } else { await _invitationRepository.UpdateAsync(invite); } }
/// <inheritdoc /> public async Task ExecuteAsync(InviteUser command) { var inviter = await _userRepository.GetUserAsync(command.UserId); if (!ClaimsPrincipal.Current.IsSysAdmin() && !ClaimsPrincipal.Current.IsApplicationAdmin(command.ApplicationId)) { _logger.Warn($"User {command.UserId} attempted to do an invite for an application: {command.ApplicationId}."); throw new SecurityException("You are not an admin of that application."); } var invitedUser = await _userRepository.FindByEmailAsync(command.EmailAddress); if (invitedUser != null) { //correction of issue #21, verify that the person isn't already a member. var members = await _applicationRepository.GetTeamMembersAsync(command.ApplicationId); if (members.Any(x => x.AccountId == invitedUser.AccountId)) { _logger.Warn("User " + invitedUser.AccountId + " is already a member."); return; } var member = new ApplicationTeamMember(command.ApplicationId, invitedUser.AccountId) { AddedByName = inviter.UserName, Roles = new[] { ApplicationRole.Member } }; await _applicationRepository.CreateAsync(member); await _eventBus.PublishAsync(new UserAddedToApplication(command.ApplicationId, member.AccountId)); return; } else { //correction of issue #21, verify that the person isn't already a member. var members = await _applicationRepository.GetTeamMembersAsync(command.ApplicationId); if (members.Any(x => x.EmailAddress == command.EmailAddress)) { _logger.Warn("User " + command.EmailAddress + " is already invited."); return; } } var invitedMember = new ApplicationTeamMember(command.ApplicationId, command.EmailAddress) { AddedByName = inviter.UserName, Roles = new[] { ApplicationRole.Member } }; await _applicationRepository.CreateAsync(invitedMember); var invitation = await _invitationRepository.FindByEmailAsync(command.EmailAddress); if (invitation == null) { invitation = new Invitation(command.EmailAddress, inviter.UserName); await _invitationRepository.CreateAsync(invitation); await SendInvitationEmailAsync(invitation, command.Text); } invitation.Add(command.ApplicationId, inviter.UserName); await _invitationRepository.UpdateAsync(invitation); var app = await _applicationRepository.GetByIdAsync(command.ApplicationId); var evt = new UserInvitedToApplication( invitation.InvitationKey, command.ApplicationId, app.Name, command.EmailAddress, inviter.UserName); await _eventBus.PublishAsync(evt); }