Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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);
        }