/// <summary> /// Execute the request and generate a reply. /// </summary> /// <param name="request">Request to execute</param> /// <returns> /// Task which will contain the reply once completed. /// </returns> public async Task <ClaimsIdentity> ActivateAccount(ClaimsPrincipal user, string activationKey) { var account = await _repository.FindByActivationKeyAsync(activationKey); if (account == null) { throw new ArgumentOutOfRangeException("ActivationKey", activationKey, "Key was not found."); } account.Activate(); await _repository.UpdateAsync(account); if (!user.IsCurrentAccount(account.Id)) { var evt = new AccountActivated(account.Id, account.UserName) { EmailAddress = account.Email }; await _messageBus.SendAsync(user, evt); } var identity = await CreateIdentity(account.Id, account.UserName, account.IsSysAdmin); return(identity); }
/// <summary> /// Execute the request and generate a reply. /// </summary> /// <param name="request">Request to execute</param> /// <returns> /// Task which will contain the reply once completed. /// </returns> public async Task <ActivateAccountReply> ExecuteAsync(ActivateAccount request) { var account = await _repository.FindByActivationKeyAsync(request.ActivationKey); if (account == null) { throw new ArgumentOutOfRangeException("ActivationKey", request.ActivationKey, "Key was not found."); } account.Activate(); await _repository.UpdateAsync(account); var query = new GetApplicationList(); var apps = await _queryBus.QueryAsync(query); var roles = apps.Select(x => "Member_" + x.Id).ToArray(); Thread.CurrentPrincipal = new OneTruePrincipal(account.Id, account.UserName, roles); var evt = new AccountActivated(account.Id, account.UserName) { EmailAddress = account.Email }; await _eventBus.PublishAsync(evt); return(new ActivateAccountReply(account.Id, account.UserName)); }
/// <summary> /// Execute a command asynchronously. /// </summary> /// <param name="command">Command to execute.</param> /// <returns> /// Task which will be completed once the command has been executed. /// </returns> public async Task HandleAsync(IMessageContext context, RegisterAccount command) { if (command == null) { throw new ArgumentNullException("command"); } if (!string.IsNullOrEmpty(command.UserName) && await _repository.IsUserNameTakenAsync(command.UserName)) { _logger.Warn("UserName is taken: " + command.UserName); await SendAccountInfo(context, command.UserName); return; } var account = command.AccountId > 0 ? new Account(command.AccountId, command.UserName, command.Password) : new Account(command.UserName, command.Password); account.SetVerifiedEmail(command.Email); if (command.ActivateDirectly) { _logger.Debug("Activating directly"); account.Activate(); } var accountCount = await _repository.CountAsync(); if (accountCount == 0) { account.IsSysAdmin = true; } await _repository.CreateAsync(account); // accounts can be activated directly. // should not send activation email then. if (account.AccountState == AccountState.VerificationRequired) { await SendVerificationEmail(context, account); } var evt = new AccountRegistered(account.Id, account.UserName) { IsSysAdmin = account.IsSysAdmin }; await context.SendAsync(evt); if (command.ActivateDirectly) { var evt1 = new AccountActivated(account.Id, account.UserName) { EmailAddress = account.Email }; await context.SendAsync(evt1); } }
/// <summary> /// Execute the request and generate a reply. /// </summary> /// <param name="request">Request to execute</param> /// <returns> /// Task which will contain the reply once completed. /// </returns> public async Task <ActivateAccountReply> ExecuteAsync(ActivateAccount request) { var account = await _repository.FindByActivationKeyAsync(request.ActivationKey); if (account == null) { throw new ArgumentOutOfRangeException("ActivationKey", request.ActivationKey, "Key was not found."); } account.Activate(); await _repository.UpdateAsync(account); var query = new GetApplicationList { AccountId = account.Id }; var apps = await _queryBus.QueryAsync(query); var claims = apps.Select(x => new Claim(OneTrueClaims.Application, x.Id.ToString(), ClaimValueTypes.Integer32)) .ToArray(); if (ClaimsPrincipal.Current.IsAccount(account.Id)) { var context = new PrincipalFactoryContext(account.Id, account.UserName, new string[0]) { Claims = claims }; var identity = await PrincipalFactory.CreateAsync(context); identity.AddUpdateCredentialClaim(); Thread.CurrentPrincipal = identity; } var evt = new AccountActivated(account.Id, account.UserName) { EmailAddress = account.Email }; await _eventBus.PublishAsync(evt); return(new ActivateAccountReply(account.Id, account.UserName)); }
public async Task HandleAsync(AccountActivated @event) => await CompleteAsync(@event, @event.UserId);