Exemplo n.º 1
0
        public async Task <IHttpActionResult> CreateTenant(CreateTenantBindingModel model)
        {
            if (ModelState.IsValid)
            {
                var izendaAdminAuthToken = IzendaTokenAuthorization.GetIzendaAdminToken();
                var tenantName           = model.TenantName;

                // check user DB first
                var isTenantExist = IzendaUtilities.GetTenantByName(tenantName);

                if (isTenantExist == null)
                {
                    // try to create a new tenant at izenda config DB
                    var success = await IzendaUtilities.CreateTenant(tenantName, model.TenantId, izendaAdminAuthToken);

                    if (success)
                    {
                        // save a new tenant at user DB
                        var newTenant = new Tenant()
                        {
                            Name = model.TenantId
                        };

                        await IzendaUtilities.SaveTenantAsync(newTenant);

                        return(Ok("success"));
                    }
                    else
                    {
                        // Izenda config DB has the same tenant name.
                        return(BadRequest());
                    }
                }
                else
                {
                    // user DB has the same tenant name.
                    return(BadRequest());
                }
            }

            // If we got this far, something failed
            return(BadRequest(ModelState));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> CreateTenant(CreateTenantViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var izendaAdminAuthToken = IzendaTokenAuthorization.GetIzendaAdminToken();
                var tenantName           = model.TenantName;

                var isTenantExist = IzendaUtilities.GetTenantByName(tenantName); // check user DB first

                if (isTenantExist == null)
                {
                    // try to create a new tenant at izenda DB
                    var success = await IzendaUtilities.CreateTenant(tenantName, model.TenantID, izendaAdminAuthToken);

                    if (success)
                    {
                        // save a new tenant at user DB
                        var newTenant = new Tenant()
                        {
                            Name = model.TenantID
                        };
                        await IzendaUtilities.SaveTenantAsync(newTenant);

                        return(RedirectToAction(returnUrl));
                    }
                    else
                    {
                        // Izenda DB has the same tenant name. Display Message at CreateTenant.cshtml
                        return(FailedTenantCreateAction(_defaultTenantFailureMessage));
                    }
                }
                else
                {
                    // user DB has the same tenant name. Display Message at CreateTenant.cshtml
                    return(FailedTenantCreateAction(_defaultTenantFailureMessage));
                }
            }

            // If we got this far, something failed
            return(FailedTenantCreateAction(_unknownFailureMessage));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");

            if (ModelState.IsValid)
            {
                var izendaAdminAuthToken = IzendaTokenAuthorization.GetIzendaAdminToken();
                var tenantName           = Input.TenantName;
                var isTenantExist        = _tenantManager.GetTenantByName(tenantName); // check user DB first

                if (isTenantExist == null)
                {
                    // try to create a new tenant at izenda DB
                    var success = await IzendaUtilities.CreateTenant(tenantName, Input.TenantID, izendaAdminAuthToken);

                    if (success)
                    {
                        // save a new tenant at user DB
                        var newTenant = new Tenant()
                        {
                            Name = Input.TenantID
                        };
                        await _tenantManager.SaveTenantAsync(newTenant);

                        return(LocalRedirect(returnUrl));
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "Failed to create a tenant. Tenant already exists in Izenda Config DB.");
                        return(Page());
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError(string.Empty, "Failed to create a tenant. Tenant aleady exists in application DB.");
            return(Page());
        }
        protected async void Submit(object sender, EventArgs e)
        {
            if (IsValid)
            {
                var izendaAdminAuthToken = IzendaTokenAuthorization.GetIzendaAdminToken();

                var isTenantExist = IzendaUtilities.GetTenantByName(TenantName.Text);

                if (isTenantExist == null)
                {
                    var success = await IzendaUtilities.CreateTenant(TenantName.Text, TenantID.Text, izendaAdminAuthToken);

                    if (success)
                    {
                        var newTenant = new Tenant()
                        {
                            Name = TenantID.Text
                        };
                        await IzendaUtilities.SaveTenantAsync(newTenant);

                        RedirectResultPage(true);
                    }
                    else // failed at server level
                    {
                        RedirectResultPage(false);
                    }
                }
                else
                {
                    RedirectResultPage(false);
                }
            }
            else
            {
                RedirectResultPage(false);
            }
        }
Exemplo n.º 5
0
        private static bool CreateIzendaTenant(CreateTenantBindingModel model, string adminToken)
        {
            Task <bool> tenantCreated = IzendaUtilities.CreateTenant(model.TenantName, model.TenantId, adminToken);

            return(tenantCreated.Result); //Let it finish and return the results, here we don't worry about the performance.
        }