コード例 #1
0
        public async Task ThenThereIsATenantWithIdAsAChildOfTheRootTenant(string tenantId)
        {
            ITenantProvider tenantProvider = ContainerBindings.GetServiceProvider(this.scenarioContext).GetRequiredService <ITenantProvider>();

            // A TenantNotFound exception will be thrown if the tenant doesn't exist
            ITenant tenant = await tenantProvider.GetTenantAsync(tenantId).ConfigureAwait(false);

            Assert.AreEqual(tenantProvider.Root.Id, tenant.GetRequiredParentId());
        }
コード例 #2
0
        public async Task WhenIUseTheTenantStoreToUnenrollTheTenantCalledFromTheServiceCalled(
            string unenrollingTenantName,
            string serviceTenantName)
        {
            ITenantStore tenantStore =
                ContainerBindings.GetServiceProvider(this.scenarioContext).GetRequiredService <ITenantStore>();
            ITenantProvider tenantProvider = ContainerBindings.GetServiceProvider(this.scenarioContext).GetRequiredService <ITenantProvider>();

            ITenant unenrollingTenant = await tenantProvider.GetTenantAsync(this.scenarioContext.Get <string>(unenrollingTenantName)).ConfigureAwait(false);

            ITenant serviceTenant = await tenantProvider.GetTenantAsync(this.scenarioContext.Get <string>(serviceTenantName)).ConfigureAwait(false);

            await CatchException.AndStoreInScenarioContextAsync(
                this.scenarioContext,
                () => tenantStore.UnenrollFromServiceAsync(
                    unenrollingTenant.Id,
                    serviceTenant.Id)).ConfigureAwait(false);
        }
コード例 #3
0
        private async Task <TenantContext> GetTenantContext(HttpContext context)
        {
            var tenantCode = GetTenantCodeFromContext(context);
            var tenant     = await _tenantProvider.GetTenantAsync(tenantCode);

            if (tenant == null)
            {
                throw new DomainException(ErrorCode.TenantNotFound);
            }

            return(new TenantContext(tenant.Id, tenant.Code, tenant.IsAdminTenant));
        }
コード例 #4
0
        private async Task AddConfiguration(
            string tenantName,
            string configurationName)
        {
            ITenantStore tenantStore =
                ContainerBindings.GetServiceProvider(this.scenarioContext).GetRequiredService <ITenantStore>();
            ITenantProvider tenantProvider = ContainerBindings.GetServiceProvider(this.scenarioContext).GetRequiredService <ITenantProvider>();

            ITenant tenant = await tenantProvider.GetTenantAsync(this.scenarioContext.Get <string>(tenantName)).ConfigureAwait(false);

            List <ConfigurationItem> configuration = this.scenarioContext.Get <List <ConfigurationItem> >(configurationName);

            await CatchException.AndStoreInScenarioContextAsync(
                this.scenarioContext,
                () => tenantStore.AddOrUpdateStorageConfigurationAsync(
                    tenant,
                    configuration.ToArray())).ConfigureAwait(false);
        }
コード例 #5
0
        public async Task ThenThereIsNoTenantWithIdAsAChildOfTheRootTenant(string tenantId)
        {
            ITenantProvider tenantProvider = ContainerBindings.GetServiceProvider(this.scenarioContext).GetRequiredService <ITenantProvider>();

            try
            {
                ITenant tenant = await tenantProvider.GetTenantAsync(tenantId).ConfigureAwait(false);

                // The tenant exists. If it's a child of the root tenant, the test is failed.
                Assert.AreNotEqual(
                    tenantProvider.Root.Id,
                    tenant.GetRequiredParentId(),
                    $"A tenant with Id '{tenantId}' does exist and is a child of the root tenant.");
            }
            catch (TenantNotFoundException)
            {
                // This means the test has passed.
            }
        }
コード例 #6
0
        public static async Task SetupTransientTenant(FeatureContext featureContext, IObjectContainer objectContainer)
        {
            ITenantProvider tenantProvider         = ContainerBindings.GetServiceProvider(featureContext).GetRequiredService <ITenantProvider>();
            var             transientTenantManager = TransientTenantManager.GetInstance(featureContext);
            await transientTenantManager.EnsureInitialised().ConfigureAwait(false);

            // Create a transient service tenant for testing purposes.
            ITenant transientServiceTenant = await transientTenantManager.CreateTransientServiceTenantFromEmbeddedResourceAsync(
                typeof(TransientTenantBindings).Assembly,
                "Marain.Claims.OpenApi.Specs.ServiceManifests.ClaimsServiceManifest.jsonc").ConfigureAwait(false);

            // TODO: This only works for the direct invocation mode. We have to do different things for the other
            // modes. Consider creating a separate DI container for the direct mode instance.
            IConfiguration configuration = ContainerBindings
                                           .GetServiceProvider(featureContext)
                                           .GetRequiredService <IConfiguration>();

            configuration["MarainServiceConfiguration:ServiceTenantId"] = transientServiceTenant.Id;

            // Now we need to construct a transient client tenant for the test, and enroll it in the new
            // transient service.
            ITenant transientClientTenant = await transientTenantManager.CreateTransientClientTenantAsync().ConfigureAwait(false);

            await transientTenantManager.AddEnrollmentAsync(
                transientClientTenant.Id,
                transientServiceTenant.Id,
                GetClaimsConfig(featureContext)).ConfigureAwait(false);

            // TODO: Temporary hack to work around the fact that the transient tenant manager no longer holds the latest
            // version of the tenants it's tracking; see https://github.com/marain-dotnet/Marain.TenantManagement/issues/28
            transientTenantManager.PrimaryTransientClient = await tenantProvider.GetTenantAsync(transientClientTenant.Id).ConfigureAwait(false);

            var testContext = new ClaimsServiceTestTenants(
                transientServiceTenant.Id,
                transientClientTenant.Id);

            objectContainer.RegisterInstanceAs(testContext);
        }
コード例 #7
0
        public async Task <IHttpActionResult> CreateUser(UserCreateRequestModel model)
        {
            if (string.IsNullOrWhiteSpace(model.Id))
            {
                model.Id = Guid.NewGuid().ToString();
            }
            model.UserName = model.Email;

            if (!string.IsNullOrWhiteSpace(model.UserName))
            {
                ModelState.Remove("model.UserName");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (_service.IsEmailExist(model.Email))
            {
                return(BadRequest($"{model.Email} email address already exist."));
            }


            if (string.IsNullOrWhiteSpace(model.TenantId))
            {
                model.TenantId   = User.Identity.GetTenantId();
                model.TenantName = User.Identity.GetTenantName();
                model.CompanyId  = User.Identity.GetCompanyId();
            }
            else
            {
                var tenant = await _tenantProvider.GetTenantAsync(model.TenantId);

                var company = await _tenantProvider.GetTenantCompanyAsync(model.TenantId);

                model.TenantName = tenant.TenancyName;
                model.CompanyId  = company?.Id;
            }

            model.CreatedBy      = User.Identity.GetUserId();
            model.IsActive       = !model.SendActivationEmailToUser;
            model.EmailConfirmed = !model.SendActivationEmailToUser;

            var  featureUsers = _featureProvider.GetEditionFeatureValue(model.TenantId, StaticFeature.Users.Name);
            bool isReachedMaximumUsersCount =
                _service.IsReachedMaximumUsersCount(model.TenantId, Convert.ToInt32(featureUsers));

            if (isReachedMaximumUsersCount)
            {
                return(BadRequest("You already have added " + featureUsers + " users. You can not add more user with your current subscription."));
            }

            var identityResult = await _service.CreateUserAsync(model);

            if (model.SendActivationEmailToUser)
            {
                var user = await _service.GetUserAsync(model.Id);

                user.EmailConfirmationCode = await UserManager.GenerateEmailConfirmationTokenAsync(model.Id);

                user.EmailConfirmed = false;
                user.EmailConfirmationCodeExpireTime = DateTime.Now.AddMinutes(30);
                user.PhoneConfirmationCode           = await UserManager.GenerateChangePhoneNumberTokenAsync(model.Id, model.PhoneNumber);

                user.PhoneNumberConfirmed            = false;
                user.PhoneConfirmationCodeExpireTime = DateTime.Now.AddMinutes(30);

                await _service.UpdateUserAsync(user);

                await _service.SendEmailConfirmationLinkAsync(model.Id, model.FullName(), model.Email, user.EmailConfirmationCode);
            }

            return(Ok(identityResult));
        }
コード例 #8
0
        /// <summary>
        /// Gets the tenants for a given set of tenant IDs.
        /// </summary>
        /// <param name="tenantProvider">The underlying tenant provider to use.</param>
        /// <param name="tenantIds">The ids of the tenants to retrieve.</param>
        /// <returns>A task that produces the specified tenants.</returns>
        public static async Task <ITenant[]> GetTenantsAsync(this ITenantProvider tenantProvider, IEnumerable <string> tenantIds)
        {
            ArgumentNullException.ThrowIfNull(tenantIds);

            IEnumerable <Task <ITenant> > getTenantTasks = tenantIds.Select(tenantId => tenantProvider.GetTenantAsync(tenantId));

            return(await Task.WhenAll(getTenantTasks).ConfigureAwait(false));
        }
コード例 #9
0
        /// <summary>
        /// Get the current tenant
        /// </summary>
        /// <returns></returns>
        public async Task <T> GetTenantAsync()
        {
            var tenantIdentifier = await _tenantResolutionStrategy.GetHostAsync();

            return(await _tenantProvider.GetTenantAsync(tenantIdentifier, cancellationToken : CancellationToken.None));
        }