public async Task <IActionResult> Update(Guid id, [FromBody] Dictionary <string, string> data) { IActionResult result = null; bool locked = default; try { if (result == null && data == null) { result = BadRequest("Missing patch data"); } if (result == null && (!data.ContainsKey("Locked") || string.IsNullOrEmpty(data["Locked"]))) { result = BadRequest("Missing Locked value"); } if (result == null && Guid.Empty.Equals(id)) { result = BadRequest("Invalid account id"); } if (result == null && !UserCanAccessAccount(id)) { result = StatusCode(StatusCodes.Status401Unauthorized); } if (result == null && !bool.TryParse(data["Locked"], out locked)) { result = BadRequest("Invalid locked value. Expecting 'True' or 'False'"); } if (result == null) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IAccountFactory accountFactory = scope.Resolve <IAccountFactory>(); IAccount innerAccount = await accountFactory.Get(settings, id); if (innerAccount == null) { result = NotFound(); } else { IAccountSaver saver = scope.Resolve <IAccountSaver>(); await saver.UpdateLocked(settings, innerAccount.AccountId, locked); result = Ok(); } } } } catch (Exception ex) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value); } result = StatusCode(StatusCodes.Status500InternalServerError); } return(result); }
public async Task <IActionResult> GetByEmailAddress(string emailAddress) { IActionResult result = null; using (ILifetimeScope scope = _container.BeginLifetimeScope()) { SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IUserFactory userFactory = scope.Resolve <IUserFactory>(); IEnumerable <IUser> users = await userFactory.GetByEmailAddress(settings, emailAddress); IAccountFactory accountFactory = scope.Resolve <IAccountFactory>(); ConcurrentBag <Task <IEnumerable <IAccount> > > accounts = new ConcurrentBag <Task <IEnumerable <IAccount> > >(); users.AsParallel().ForAll(user => accounts.Add(accountFactory.GetByUserId(settings, user.UserId))); IMapper mapper = MapperConfigurationFactory.CreateMapper(); result = Ok( (await Task.WhenAll <IEnumerable <IAccount> >(accounts)) .SelectMany(results => results) .Where(a => UserCanAccessAccount(a.AccountId)) .Select <IAccount, Account>(innerAccount => mapper.Map <Account>(innerAccount)) .ToList() ); } return(result); }
public async Task <IActionResult> Update(Guid id, [FromBody] Account account) { IActionResult result = null; try { if (result == null && account == null) { result = BadRequest("Missing account data"); } if (result == null && string.IsNullOrEmpty(account.Name)) { result = BadRequest("Missing account name"); } if (result == null && Guid.Empty.Equals(id)) { result = BadRequest("Invalid account id"); } if (result == null && !UserCanAccessAccount(id)) { result = StatusCode(StatusCodes.Status401Unauthorized); } if (result == null) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IAccountFactory accountFactory = scope.Resolve <IAccountFactory>(); IAccount innerAccount = await accountFactory.Get(settings, id); if (innerAccount == null) { result = NotFound(); } else { IMapper mapper = MapperConfigurationFactory.CreateMapper(); mapper.Map <Account, IAccount>(account, innerAccount); IAccountSaver saver = scope.Resolve <IAccountSaver>(); await saver.Update(settings, innerAccount); result = Ok( mapper.Map <Account>(innerAccount) ); } } } } catch (Exception ex) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value); } result = StatusCode(StatusCodes.Status500InternalServerError); } return(result); }
public async Task <IActionResult> Update([FromRoute] Guid?id, [FromBody] ClientCredentialRequest client) { IActionResult result = null; try { if (result == null && !id.HasValue || id.Value.Equals(Guid.Empty)) { result = BadRequest("Missing client id value"); } if (result == null && client == null) { result = BadRequest("Missing client data"); } if (result == null && string.IsNullOrEmpty(client.Name)) { result = BadRequest("Missing client name value"); } if (result == null && !string.IsNullOrEmpty(client.Secret) && client.Secret.Trim().Length < 16) { result = BadRequest("Client secret must be at least 16 characters in lenth"); } if (result == null) { using ILifetimeScope scope = _container.BeginLifetimeScope(); IClientFactory clientFactory = scope.Resolve <IClientFactory>(); SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IClient innerClient = await clientFactory.Get(settings, id.Value); if (innerClient == null) { result = NotFound(); } if (result == null && !UserCanAccessAccount(innerClient.AccountId)) { result = StatusCode(StatusCodes.Status401Unauthorized); } if (result == null) { IMapper mapper = MapperConfigurationFactory.CreateMapper(); mapper.Map <Client, IClient>(client, innerClient); IClientSaver saver = scope.Resolve <IClientSaver>(); await saver.Update(settings, innerClient, client.Secret); result = Ok(mapper.Map <Client>(innerClient)); } } } catch (Exception ex) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value); } result = StatusCode(StatusCodes.Status500InternalServerError); } return(result); }
private async Task <string> GetAccountIdClaim(IAccountFactory accountFactory, SettingsFactory settingsFactory, Guid userId) { return(string.Join( ' ', (await accountFactory.GetAccountIdsByUserId(settingsFactory.CreateAccount(_settings.Value), userId)) .Select <Guid, string>(g => g.ToString("N")) )); }
public async Task <IActionResult> Update([FromRoute] Guid?id, [FromBody] Domain domain) { IActionResult result = null; try { if (result == null && (!id.HasValue || id.Value.Equals(Guid.Empty))) { result = BadRequest("Missing domain id value"); } if (result == null && domain == null) { result = BadRequest("Missing domain data"); } if (result == null && string.IsNullOrEmpty(domain.Name)) { result = BadRequest("Missing domain name value"); } if (result == null) { using ILifetimeScope scope = _container.BeginLifetimeScope(); SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IDomainFactory domainFactory = scope.Resolve <IDomainFactory>(); IDomain innerDomain = await domainFactory.Get(settings, id.Value); if (result == null && innerDomain == null) { result = NotFound(); } if (result == null && !UserCanAccessAccount(innerDomain.AccountId)) { result = StatusCode(StatusCodes.Status401Unauthorized); } if (result == null) { IMapper mapper = MapperConfigurationFactory.CreateMapper(); mapper.Map <Domain, IDomain>(domain, innerDomain); IDomainSaver saver = scope.Resolve <IDomainSaver>(); await saver.Update(settings, innerDomain); result = Ok(mapper.Map <Domain>(innerDomain)); } } } catch (Exception ex) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value); } result = StatusCode(StatusCodes.Status500InternalServerError); } return(result); }
private async Task <IUser> GetUser() { IUser user; IEmailAddress emailAddress = null; using (ILifetimeScope scope = _container.BeginLifetimeScope()) { string subscriber = User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value; IUserFactory userFactory = scope.Resolve <IUserFactory>(); IEmailAddressFactory emailAddressFactory = scope.Resolve <IEmailAddressFactory>(); SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); user = await userFactory.GetByReferenceId(settingsFactory.CreateAccount(_settings.Value), subscriber); if (user == null) { string email = User.Claims.First(c => c.Type == ClaimTypes.Email).Value; emailAddress = await emailAddressFactory.GetByAddress(settingsFactory.CreateAccount(_settings.Value), email); if (emailAddress == null) { emailAddress = emailAddressFactory.Create(email); IEmailAddressSaver emailAddressSaver = scope.Resolve <IEmailAddressSaver>(); await emailAddressSaver.Create(settingsFactory.CreateAccount(_settings.Value), emailAddress); } user = userFactory.Create(subscriber, emailAddress); user.Name = User.Claims.First(c => string.Equals(c.Type, "name", StringComparison.OrdinalIgnoreCase)).Value; SetSuperUser(user); IUserSaver userSaver = scope.Resolve <IUserSaver>(); await userSaver.Create(settingsFactory.CreateAccount(_settings.Value), user); } else { user.Name = User.Claims.First(c => string.Equals(c.Type, "name", StringComparison.OrdinalIgnoreCase)).Value; SetSuperUser(user); IUserSaver userSaver = scope.Resolve <IUserSaver>(); await userSaver.Update(settingsFactory.CreateAccount(_settings.Value), user); } } return(user); }
public async Task <IActionResult> CreateClientCredential([FromBody] ClientCredential clientCredential) { IActionResult result = null; try { if (result == null && clientCredential == null) { result = BadRequest("Missing request data"); } if (result == null && (!clientCredential.ClientId.HasValue || clientCredential.ClientId.Value.Equals(Guid.Empty))) { result = BadRequest("Missing client id value"); } if (result == null && string.IsNullOrEmpty(clientCredential.Secret)) { result = BadRequest("Missing secret value"); } if (result == null) { using ILifetimeScope scope = _container.BeginLifetimeScope(); SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IClientFactory clientFactory = scope.Resolve <IClientFactory>(); IClient client = await clientFactory.Get(settings, clientCredential.ClientId.Value); if (client == null) { result = StatusCode(StatusCodes.Status401Unauthorized); } if (result == null) { ISecretProcessor secretProcessor = scope.Resolve <ISecretProcessor>(); if (!secretProcessor.Verify(clientCredential.Secret, await client.GetSecretHash(settings))) { result = StatusCode(StatusCodes.Status401Unauthorized); } } if (result == null) { result = Content(await CreateToken(client), "text/plain"); } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); result = StatusCode(StatusCodes.Status500InternalServerError); } return(result); }
public async Task <IActionResult> GetAccountDomain([FromRoute] Guid?id) { IActionResult result = null; try { if (result == null && !id.HasValue || id.Value.Equals(Guid.Empty)) { result = BadRequest("Missing domain id value"); } if (result == null) { using ILifetimeScope scope = _container.BeginLifetimeScope(); SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IDomainFactory domainFactory = scope.Resolve <IDomainFactory>(); IDomain innerDomain = await domainFactory.Get(settings, id.Value); if (innerDomain != null && !UserCanAccessAccount(innerDomain.AccountId)) { innerDomain = null; } if (innerDomain == null) { result = NotFound(); } else { IMapper mapper = MapperConfigurationFactory.CreateMapper(); AccountDomain accountDomain = mapper.Map <AccountDomain>(innerDomain); IAccountFactory accountFactory = scope.Resolve <IAccountFactory>(); accountDomain.Account = mapper.Map <Account>(await accountFactory.Get(settings, innerDomain.AccountId)); result = Ok(accountDomain); } } } catch (Exception ex) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value); } result = StatusCode(StatusCodes.Status500InternalServerError); } return(result); }
public async Task <IActionResult> Get([FromRoute] Guid?id) { IActionResult result = null; try { if (result == null && !id.HasValue || id.Value.Equals(Guid.Empty)) { result = BadRequest("Missing client id value"); } if (result == null) { using ILifetimeScope scope = _container.BeginLifetimeScope(); SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IClientFactory clientFactory = scope.Resolve <IClientFactory>(); IClient client = await clientFactory.Get(settings, id.Value); if (client == null) { result = NotFound(); } if (result == null && !UserCanAccessAccount(client.AccountId)) { result = StatusCode(StatusCodes.Status401Unauthorized); } if (result == null) { IMapper mapper = MapperConfigurationFactory.CreateMapper(); result = Ok( mapper.Map <Client>(client) ); } } } catch (Exception ex) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value); } result = StatusCode(StatusCodes.Status500InternalServerError); } return(result); }
public async Task <IActionResult> Create([FromBody] Account account) { IActionResult result = null; try { if (result == null && account == null) { result = BadRequest("Missing account data"); } if (result == null && string.IsNullOrEmpty(account.Name)) { result = BadRequest("Missing account name"); } if (result == null) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IUser user = await GetUser(scope.Resolve <IUserFactory>(), settings); IAccountFactory accountFactory = scope.Resolve <IAccountFactory>(); IAccount innerAccount = accountFactory.Create(); IMapper mapper = MapperConfigurationFactory.CreateMapper(); mapper.Map <Account, IAccount>(account, innerAccount); IAccountSaver saver = scope.Resolve <IAccountSaver>(); await saver.Create(settings, user.UserId, innerAccount); result = Ok( mapper.Map <Account>(innerAccount) ); } } } catch (Exception ex) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value); } result = StatusCode(StatusCodes.Status500InternalServerError); } return(result); }
public async Task <IActionResult> GetByAccountId([FromRoute] Guid?id, [FromQuery] bool deleted = false) { IActionResult result = null; try { if (result == null && !id.HasValue || id.Value.Equals(Guid.Empty)) { result = BadRequest("Missing account id value"); } if (result == null && !UserCanAccessAccount(id.Value)) { result = StatusCode(StatusCodes.Status401Unauthorized); } if (result == null) { using ILifetimeScope scope = _container.BeginLifetimeScope(); SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IDomainFactory domainFactory = scope.Resolve <IDomainFactory>(); IEnumerable <IDomain> domains; if (deleted) { domains = await domainFactory.GetDeletedByAccountId(settings, id.Value); } else { domains = await domainFactory.GetByAccountId(settings, id.Value); } IMapper mapper = MapperConfigurationFactory.CreateMapper(); result = Ok( domains.Select <IDomain, Domain>(d => mapper.Map <Domain>(d)) ); } } catch (Exception ex) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value); } result = StatusCode(StatusCodes.Status500InternalServerError); } return(result); }
public async Task <IActionResult> Get([FromRoute] Guid?id) { IActionResult result = null; try { if (result == null && (!id.HasValue || id.Value.Equals(Guid.Empty))) { result = BadRequest("Missing invitation id parameter value"); } if (result == null) { using ILifetimeScope scope = _container.BeginLifetimeScope(); SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IUserInvitationFactory factory = scope.Resolve <IUserInvitationFactory>(); IUserInvitation innerInvitation = await factory.Get(settings, id.Value); if (innerInvitation != null && !(await UserCanAccessInvitation(settings, innerInvitation))) { innerInvitation = null; } if (innerInvitation == null) { result = NotFound(); } else { IMapper mapper = MapperConfigurationFactory.CreateMapper(); result = Ok( await Map(mapper, settings, innerInvitation) ); } } } catch (Exception ex) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value); } result = StatusCode(StatusCodes.Status500InternalServerError); } return(result); }
public async Task <IActionResult> GetAll() { IActionResult result = null; using (ILifetimeScope scope = _container.BeginLifetimeScope()) { SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IUser user = await GetUser(scope.Resolve <IUserFactory>(), settings); IAccountFactory accountFactory = scope.Resolve <IAccountFactory>(); IEnumerable <IAccount> accounts = await accountFactory.GetByUserId(settings, user.UserId); IMapper mapper = MapperConfigurationFactory.CreateMapper(); result = Ok( accounts.Select <IAccount, Account>(innerAccount => mapper.Map <Account>(innerAccount)) ); } return(result); }
public async Task <IActionResult> DeleteUser([FromRoute] Guid?accountId, [FromRoute] Guid?userId) { IActionResult result = null; try { if (result == null && (!accountId.HasValue || Guid.Empty.Equals(accountId.Value))) { result = BadRequest("Missing account id parameter value"); } if (result == null && (!userId.HasValue || Guid.Empty.Equals(userId.Value))) { result = BadRequest("Missing user id parameter value"); } if (result == null && !UserCanAccessAccount(accountId.Value)) { result = StatusCode(StatusCodes.Status401Unauthorized); } if (result == null) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IAccountSaver accountSaver = scope.Resolve <IAccountSaver>(); await accountSaver.RemoveUser(settings, userId.Value, accountId.Value); result = Ok(); } } } catch (Exception ex) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value); } result = StatusCode(StatusCodes.Status500InternalServerError); } return(result); }
public async Task <IActionResult> GetUsers([FromRoute] Guid?accountId) { IActionResult result = null; try { if (result == null && (!accountId.HasValue || Guid.Empty.Equals(accountId.Value))) { result = BadRequest("Missing account id parameter value"); } if (result == null && !UserCanAccessAccount(accountId.Value)) { result = StatusCode(StatusCodes.Status401Unauthorized); } if (result == null) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IUserFactory userFactory = scope.Resolve <IUserFactory>(); IEnumerable <IUser> innerUsers = await userFactory.GetByAccountId(settings, accountId.Value); IMapper mapper = MapperConfigurationFactory.CreateMapper(); result = Ok( innerUsers.Select <IUser, User>(innerUser => mapper.Map <User>(innerUser)) ); } } } catch (Exception ex) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value); } result = StatusCode(StatusCodes.Status500InternalServerError); } return(result); }
private async Task <string> CreateToken(IUser user) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); RSAParameters rsaParameters = CreateRSAParameter(); //RSA rsa = new RSACryptoServiceProvider(2048); //Debug.WriteLine(Convert.ToBase64String(rsa.ExportRSAPublicKey())); RsaSecurityKey securityKey = new RsaSecurityKey(rsaParameters); //JsonWebKey jsonWebKey = JsonWebKeyConverter.ConvertFromRSASecurityKey(securityKey); //Debug.WriteLine(JsonConvert.SerializeObject(jsonWebKey)); SigningCredentials credentials = new SigningCredentials(securityKey, SecurityAlgorithms.RsaSha512); List <Claim> claims = new List <Claim> { new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString("N")) }; Claim claim = User.Claims.FirstOrDefault(c => string.Equals(_settings.Value.IdIssuer, c.Issuer, StringComparison.OrdinalIgnoreCase) && string.Equals(ClaimTypes.NameIdentifier, c.Type, StringComparison.OrdinalIgnoreCase)); if (claim != null) { claims.Add(new Claim(JwtRegisteredClaimNames.Sub, claim.Value)); } claims.Add(new Claim(JwtRegisteredClaimNames.Email, (await user.GetEmailAddress(settingsFactory.CreateAccount(_settings.Value))).Address)); claims.Add(new Claim("accounts", await GetAccountIdClaim(scope.Resolve <IAccountFactory>(), settingsFactory, user.UserId))); if ((user.Roles & UserRole.SystemAdministrator) == UserRole.SystemAdministrator) { claims.Add(new Claim("role", "sysadmin")); } if ((user.Roles & UserRole.AccountAdministrator) == UserRole.AccountAdministrator) { claims.Add(new Claim("role", "actadmin")); } JwtSecurityToken token = new JwtSecurityToken( "urn:brassloon", "urn:brassloon", claims, expires: DateTime.Now.AddHours(6), signingCredentials: credentials ); return(new JwtSecurityTokenHandler().WriteToken(token)); } }
public async Task <IActionResult> UpdateDeleted([FromRoute] Guid?id, [FromBody] Dictionary <string, string> patch) { bool deleted = default; IActionResult result = null; try { if (result == null && (!id.HasValue || id.Value.Equals(Guid.Empty))) { result = BadRequest("Missing domain id value"); } if (result == null && patch == null) { result = BadRequest("Missing patch data"); } if (result == null && !patch.ContainsKey("Deleted")) { result = BadRequest("Missing deleted patch value"); } if (result == null && !bool.TryParse(patch["Deleted"], out deleted)) { result = BadRequest("Invalid deleted patch value"); } if (result == null) { using ILifetimeScope scope = _container.BeginLifetimeScope(); SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IDomainFactory domainFactory = scope.Resolve <IDomainFactory>(); IDomain innerDomain; if (!deleted) { innerDomain = await domainFactory.GetDeleted(settings, id.Value); } else { innerDomain = await domainFactory.Get(settings, id.Value); } if (result == null && innerDomain == null) { result = NotFound(); } if (result == null && !UserCanAccessAccount(innerDomain.AccountId)) { result = StatusCode(StatusCodes.Status401Unauthorized); } if (result == null) { innerDomain.Deleted = deleted; IDomainSaver saver = scope.Resolve <IDomainSaver>(); await saver.Update(settings, innerDomain); IMapper mapper = MapperConfigurationFactory.CreateMapper(); result = Ok(mapper.Map <Domain>(innerDomain)); } } } catch (Exception ex) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value); } result = StatusCode(StatusCodes.Status500InternalServerError); } return(result); }
public async Task <IActionResult> Update([FromRoute] Guid?id, [FromBody] UserInvitation userInvitation) { IActionResult result = null; try { if (result == null && (!id.HasValue || id.Value.Equals(Guid.Empty))) { result = BadRequest("Missing id parameter value"); } if (result == null && userInvitation == null) { result = BadRequest("Missing user invitation body"); } if (result == null && !userInvitation.ExpirationTimestamp.HasValue) { result = BadRequest("Missing expiration timestamp value"); } if (result == null && !userInvitation.Status.HasValue) { result = BadRequest("Missing status value"); } if (result == null) { using ILifetimeScope scope = _container.BeginLifetimeScope(); SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IAccountFactory accountFactory = scope.Resolve <IAccountFactory>(); IUserInvitationFactory invitationFactory = scope.Resolve <IUserInvitationFactory>(); IUserInvitation innerInvitation = await invitationFactory.Get(settings, id.Value); if (innerInvitation != null && !(await UserCanAccessInvitation(settings, innerInvitation))) { innerInvitation = null; } if (innerInvitation == null) { result = NotFound(); } else { if (DateTime.UtcNow < innerInvitation.ExpirationTimestamp && innerInvitation.Status != UserInvitationStatus.Cancelled && userInvitation.Status == (short)UserInvitationStatus.Completed && !UserTokenHasAccount(innerInvitation.AccountId)) { await AddAccountUser(scope.Resolve <IUserFactory>(), scope.Resolve <IAccountSaver>(), settings, innerInvitation.AccountId); } IMapper mapper = MapperConfigurationFactory.CreateMapper(); mapper.Map <UserInvitation, IUserInvitation>(userInvitation, innerInvitation); IUserInvitationSaver saver = scope.Resolve <IUserInvitationSaver>(); await saver.Update(settings, innerInvitation); result = Ok(await Map(mapper, settings, innerInvitation)); } } } catch (Exception ex) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value); } result = StatusCode(StatusCodes.Status500InternalServerError); } return(result); }
public async Task <IActionResult> Create([FromRoute] Guid?accountId, [FromBody] UserInvitation userInvitation) { IActionResult result = null; try { if (result == null && (!accountId.HasValue || accountId.Value.Equals(Guid.Empty))) { result = BadRequest("Missing account id parameter value"); } if (result == null && !UserCanAccessAccount(accountId.Value)) { result = StatusCode(StatusCodes.Status401Unauthorized); } if (result == null && userInvitation == null) { result = BadRequest("Missing user invitation body"); } if (result == null && string.IsNullOrEmpty(userInvitation.EmailAddress)) { result = BadRequest("Missing user email address value"); } if (result == null && !userInvitation.ExpirationTimestamp.HasValue) { userInvitation.ExpirationTimestamp = DateTime.UtcNow.AddDays(7); } if (result == null && userInvitation.ExpirationTimestamp.HasValue && userInvitation.ExpirationTimestamp.Value.ToUniversalTime() <= DateTime.UtcNow) { result = BadRequest("Invalid expiration timestamp in the past"); } if (result == null) { using ILifetimeScope scope = _container.BeginLifetimeScope(); SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IAccountFactory accountFactory = scope.Resolve <IAccountFactory>(); IAccount account = await accountFactory.Get(settings, accountId.Value); if (account == null) { result = NotFound(); } else { IEmailAddress emailAddress = await GetEmailAddress( settings, scope.Resolve <IEmailAddressFactory>(), scope.Resolve <IEmailAddressSaver>(), userInvitation.EmailAddress ); IUserInvitationFactory invitationFactory = scope.Resolve <IUserInvitationFactory>(); IUserInvitation innerInvitation = invitationFactory.Create(account, emailAddress); IMapper mapper = MapperConfigurationFactory.CreateMapper(); mapper.Map <UserInvitation, IUserInvitation>(userInvitation, innerInvitation); innerInvitation.Status = UserInvitationStatus.Created; IUserInvitationSaver saver = scope.Resolve <IUserInvitationSaver>(); await saver.Create(settings, innerInvitation); result = Ok(await Map(mapper, settings, innerInvitation)); } } } catch (Exception ex) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value); } result = StatusCode(StatusCodes.Status500InternalServerError); } return(result); }