Exemplo n.º 1
0
        public async Task <ActionResult <Api.Tenant> > PostTenant([FromBody] Api.CreateTenantRequest tenant)
        {
            try
            {
                if (!await ModelState.TryValidateObjectAsync(tenant))
                {
                    return(BadRequest(ModelState));
                }
                tenant.Name = tenant.Name.ToLower();
                tenant.AdministratorEmail = tenant.AdministratorEmail?.ToLower();

                var mTenant = mapper.Map <Tenant>(tenant);
                await tenantRepository.CreateAsync(mTenant);

                await masterTenantLogic.CreateMasterTrackDocumentAsync(tenant.Name);

                var mLoginUpParty = await masterTenantLogic.CreateMasterLoginDocumentAsync(tenant.Name);

                await masterTenantLogic.CreateFirstAdminUserDocumentAsync(tenant.Name, tenant.AdministratorEmail, tenant.AdministratorPassword, tenant.ConfirmAdministratorAccount);

                await masterTenantLogic.CreateMasterFoxIDsControlApiResourceDocumentAsync(tenant.Name);

                await masterTenantLogic.CreateMasterControlClientDocmentAsync(tenant.Name, tenant.ControlClientBaseUri, mLoginUpParty);

                await CreateTrackDocumentAsync(tenant.Name, "test");
                await CreateTrackDocumentAsync(tenant.Name, "-"); // production

                return(Created(mapper.Map <Api.Tenant>(mTenant)));
            }
            catch (AccountException aex)
            {
                ModelState.TryAddModelError(nameof(tenant.AdministratorPassword), aex.Message);
                return(BadRequest(ModelState, aex));
            }
            catch (CosmosDataException ex)
            {
                if (ex.StatusCode == HttpStatusCode.Conflict)
                {
                    logger.Warning(ex, $"Conflict, Create '{typeof(Api.Tenant).Name}' by name '{tenant.Name}'.");
                    return(Conflict(typeof(Api.Tenant).Name, tenant.Name));
                }
                throw;
            }
        }
        public async Task SeedAsync()
        {
            try
            {
                await CreateAndValidateMasterTenantDocumentAsync();

                await masterTenantLogic.CreateMasterTrackDocumentAsync(Constants.Routes.MasterTenantName);

                var mLoginUpParty = await masterTenantLogic.CreateMasterLoginDocumentAsync(Constants.Routes.MasterTenantName);

                await masterTenantLogic.CreateFirstAdminUserDocumentAsync(Constants.Routes.MasterTenantName, Constants.DefaultAdminAccount.Email, Constants.DefaultAdminAccount.Password, false);

                await masterTenantLogic.CreateMasterFoxIDsControlApiResourceDocumentAsync(Constants.Routes.MasterTenantName, includeMasterTenantScope : true);

                await masterTenantLogic.CreateMasterControlClientDocmentAsync(Constants.Routes.MasterTenantName, settings.FoxIDsControlEndpoint, mLoginUpParty, includeMasterTenantScope : true);
            }
            catch (Exception ex)
            {
                logger.CriticalError(ex, "Error seeding master tenant document.");
                throw;
            }
        }