Пример #1
0
        private async Task <bool> SetupCloudscribeCore(HttpResponse response)
        {
            bool result = true;

            if (!schemaHasBeenCreated)
            {
                if (canAlterSchema)
                {
                    schemaHasBeenCreated = await CreateInitialSchema(response, "cloudscribe-core");

                    if (schemaHasBeenCreated)
                    {
                        //recheck
                        needCoreSchemaUpgrade = setupManager.NeedsUpgrade("cloudscribe-core");
                    }
                }
            }

            if (
                (schemaHasBeenCreated) &&
                (needCoreSchemaUpgrade) &&
                (canAlterSchema)
                )
            {
                needCoreSchemaUpgrade = await UpgradeSchema(response, "cloudscribe-core");
            }

            if (!CoreSystemIsReady())
            {
                return(false);
            }

            existingSiteCount = await siteManager.ExistingSiteCount();

            if (existingSiteCount == 0)
            {
                await WritePageContent(response,
                                       "CreatingSite" //SetupResources.CreatingSiteMessage
                                       , true);

                SiteSettings newSite = await siteManager.CreateNewSite(true);

                await WritePageContent(response,
                                       "CreatingRolesAndAdminUser" //SetupResources.CreatingRolesAndAdminUserMessage
                                       , true);

                result = await siteManager.CreateRequiredRolesAndAdminUser(newSite);
            }
            else
            {
                // check here if count of users is 0
                // if something went wrong with creating admin user
                // setup page should try to correct it on subsequent runs
                // ie create an admin user if no users exist
                if (response.HttpContext.Request.Host.HasValue)
                {
                    ISiteSettings site = await siteManager.Fetch(response.HttpContext.Request.Host.Value);

                    if (site != null)
                    {
                        int roleCount = await siteManager.GetRoleCount(site.SiteId);

                        bool roleResult = true;
                        if (roleCount == 0)
                        {
                            roleResult = await siteManager.EnsureRequiredRoles(site);
                        }

                        if (roleResult)
                        {
                            int userCount = await siteManager.GetUserCount(site.SiteId);

                            if (userCount == 0)
                            {
                                await siteManager.CreateAdminUser(site);
                            }
                        }
                    }
                }
            }



            //TODO dbSiteSettingsEx.EnsureSettings(); add to ISiteRepository


            return(result);
        }
Пример #2
0
        public async Task <IActionResult> Index()
        {
            bool isAllowed = await authorizationService.AuthorizeAsync(User, "SetupSystemPolicy");

            if (!setupOptions.AllowAnonymous && !isAllowed)
            {
                log.LogInformation("returning 404 because allowAnonymous is false and user is either not authenticated or not allowed by policy");
                Response.StatusCode = 404;
                return(new EmptyResult());
            }


            //scriptTimeout = Server.ScriptTimeout;
            //Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            //Response.BufferOutput = true;
            // Server.ScriptTimeout = int.MaxValue;

            startTime = DateTime.UtcNow;

            await WritePageHeader(HttpContext.Response);

            if (!setupOptions.AllowAnonymous && isAllowed)
            {
                await WritePageContent(Response,
                                       "RunningSetupForAllowedUser" //SetupResources.RunningSetupForAdminUser
                                       );
            }

            // SetupManager and ISetupTasks will use this function to write to the response
            Func <string, bool, Task> outputWriter = async(string message, bool showTime) =>
            {
                await WritePageContent(Response, message, showTime);
            };

            // this locking strategy did not work as expected perhaps because we are doing things async
            //int lockTimeoutMilliseconds = config.GetOrDefault("AppSetings:SetupLockTimeoutMilliseconds", 60000); // 1 minute

            //if(!Monitor.TryEnter(Lock, lockTimeoutMilliseconds))
            //{
            //    //throw new Exception("setup is already locked and runnning")

            //    await WritePageContent(Response,
            //            "SetupAlreadyInProgress" //SetupResources.SetupAlreadyInProgress
            //            );
            //}
            //else
            //{
            bool keepGoing;

            try
            {
                await setupManager.ProbeSystem(outputWriter);

                // the setup system must be bootstrapped first
                // to make sure mp_SchemaVersion table exists
                // which is used to keep track of script versions that have already been run
                bool needSetupUpdate = setupManager.NeedsUpgrade("cloudscribe-setup");
                if (needSetupUpdate)
                {
                    keepGoing = await setupManager.SetupCloudscribeSetup(outputWriter);
                }
                else
                {
                    keepGoing = true;
                }


                if (keepGoing)
                {
                    // this runs the scripts for other apps including cloudscribe-core,
                    // cloudscribe-logging, and any custom apps that use the setup system
                    keepGoing = await setupManager.SetupOtherApplications(outputWriter);
                }

                if (setupSteps != null)
                {
                    foreach (ISetupTask step in setupSteps)
                    {
                        try
                        {
                            await step.DoSetupStep(
                                outputWriter,
                                setupManager.NeedsUpgrade,
                                setupManager.GetSchemaVersion,
                                setupManager.GetCodeVersion
                                );
                        }
                        catch (Exception ex)
                        {
                            await WriteException(Response, ex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                await WriteException(Response, ex);
            }
            finally
            {
                //ClearSetupLock();
                //Monitor.Exit(Lock);
            }

            if (setupOptions.ShowSchemaListOnSetupPage)
            {
                await WriteInstalledSchemaSummary(Response);
            }



            await WritePageFooter(Response);

            return(new EmptyResult());
        }