Esempio n. 1
0
        private static async Task EnsureData(
            CoreDbContext db
            )
        {
            int rowsAffected = 0;


            int count = await db.Countries.CountAsync <GeoCountry>();

            if (count == 0)
            {
                foreach (GeoCountry c in InitialData.BuildCountryList())
                {
                    db.Countries.Add(c);
                }

                rowsAffected = await db.SaveChangesAsync();
            }

            count = await db.States.CountAsync <GeoZone>();

            if (count == 0)
            {
                foreach (GeoZone c in InitialData.BuildStateList())
                {
                    db.States.Add(c);
                }

                rowsAffected = await db.SaveChangesAsync();
            }



            count = await db.Languages.CountAsync <Language>();

            if (count == 0)
            {
                foreach (Language c in InitialData.BuildLanguageList())
                {
                    db.Languages.Add(c);
                }

                rowsAffected = await db.SaveChangesAsync();
            }

            count = await db.Currencies.CountAsync <Currency>();

            if (count == 0)
            {
                foreach (Currency c in InitialData.BuildCurrencyList())
                {
                    db.Currencies.Add(c);
                }

                rowsAffected = await db.SaveChangesAsync();
            }


            count = await db.Sites.CountAsync <SiteSettings>();

            SiteSettings newSite = null;

            if (count == 0)
            {
                // create first site
                newSite = InitialData.BuildInitialSite();

                db.Sites.Add(newSite);

                rowsAffected = await db.SaveChangesAsync();
            }

            // ensure roles
            count = await db.Roles.CountAsync <SiteRole>();

            if (count == 0)
            {
                var site = newSite;
                if (site == null)
                {
                    site = await db.Sites.SingleOrDefaultAsync <SiteSettings>(
                        s => s.Id != Guid.Empty && s.IsServerAdminSite == true);
                }


                if (site != null)
                {
                    var adminRole = InitialData.BuildAdminRole();
                    adminRole.SiteId = site.Id;
                    db.Roles.Add(adminRole);

                    var roleAdminRole = InitialData.BuildRoleAdminRole();
                    roleAdminRole.SiteId = site.Id;
                    db.Roles.Add(roleAdminRole);

                    var contentAdminRole = InitialData.BuildContentAdminsRole();
                    contentAdminRole.SiteId = site.Id;
                    db.Roles.Add(contentAdminRole);

                    var authenticatedUserRole = InitialData.BuildAuthenticatedRole();
                    authenticatedUserRole.SiteId = site.Id;
                    db.Roles.Add(authenticatedUserRole);

                    rowsAffected = await db.SaveChangesAsync();
                }
            }

            // ensure admin user
            count = await db.Users.CountAsync <SiteUser>();

            if (count == 0)
            {
                SiteSettings site = await db.Sites.SingleOrDefaultAsync <SiteSettings>(
                    s => s.Id != Guid.Empty && s.IsServerAdminSite == true);

                if (site != null)
                {
                    var role = await db.Roles.SingleOrDefaultAsync(
                        x => x.SiteId == site.Id && x.NormalizedRoleName == "Administrators".ToUpperInvariant());

                    if (role != null)
                    {
                        var adminUser = InitialData.BuildInitialAdmin();
                        adminUser.SiteId = site.Id;
                        db.Users.Add(adminUser);

                        rowsAffected = await db.SaveChangesAsync();

                        if (rowsAffected > 0 && adminUser.Id != Guid.Empty)
                        {
                            var ur = new UserRole();
                            ur.RoleId = role.Id;
                            ur.UserId = adminUser.Id;
                            db.UserRoles.Add(ur);
                            await db.SaveChangesAsync();

                            role = await db.Roles.SingleOrDefaultAsync(
                                x => x.SiteId == site.Id && x.NormalizedRoleName == "Authenticated Users".ToUpperInvariant());

                            if (role != null)
                            {
                                ur        = new UserRole();
                                ur.RoleId = role.Id;
                                ur.UserId = adminUser.Id;
                                db.UserRoles.Add(ur);
                                await db.SaveChangesAsync();
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        private static async Task EnsureData(
            ISiteQueries siteQueries,
            ISiteCommands siteCommands,
            IUserQueries userQueries,
            IUserCommands userCommands,
            IGeoQueries geoQueries,
            IGeoCommands geoCommands,
            IBasicQueries <SiteRole> roleQueries,
            IBasicQueries <SiteUser> userBasic,
            IProjectResolver projectResolver
            )
        {
            int count = await geoQueries.GetCountryCount();

            if (count == 0)
            {
                foreach (GeoCountry c in InitialData.BuildCountryList())
                {
                    await geoCommands.Add(c);
                }

                foreach (GeoZone c in InitialData.BuildStateList())
                {
                    await geoCommands.Add(c);
                }
            }

            count = await geoQueries.GetLanguageCount();

            if (count == 0)
            {
                foreach (Language c in InitialData.BuildLanguageList())
                {
                    await geoCommands.Add(c);
                }
            }

            var all = await geoQueries.GetAllCurrencies();

            count = all.Count;
            if (count == 0)
            {
                foreach (Currency c in InitialData.BuildCurrencyList())
                {
                    await geoCommands.Add(c);
                }
            }

            count = await siteQueries.GetCount();

            SiteSettings newSite = null;

            if (count == 0)
            {
                // create first site
                newSite = InitialData.BuildInitialSite();
                await siteCommands.Create(newSite);
            }

            // ensure roles
            var projectId = await projectResolver.ResolveProjectId();

            count = await roleQueries.GetCountAsync(projectId);

            if (count == 0)
            {
                var adminRole = InitialData.BuildAdminRole();
                adminRole.SiteId = newSite.Id;
                await userCommands.CreateRole(adminRole);

                var roleAdminRole = InitialData.BuildRoleAdminRole();
                roleAdminRole.SiteId = newSite.Id;
                await userCommands.CreateRole(roleAdminRole);

                var contentAdminRole = InitialData.BuildContentAdminsRole();
                contentAdminRole.SiteId = newSite.Id;
                await userCommands.CreateRole(contentAdminRole);

                var authenticatedUserRole = InitialData.BuildAuthenticatedRole();
                authenticatedUserRole.SiteId = newSite.Id;
                await userCommands.CreateRole(authenticatedUserRole);
            }

            // ensure admin user
            count = await userBasic.GetCountAsync(projectId);

            if (count == 0)
            {
                var role = await userQueries.FetchRole(newSite.Id, "Administrators");

                if (role != null)
                {
                    var adminUser = InitialData.BuildInitialAdmin();
                    adminUser.SiteId = newSite.Id;
                    await userCommands.Create(adminUser);

                    await userCommands.AddUserToRole(role.Id, adminUser.Id);

                    role = await userQueries.FetchRole(newSite.Id, "Authenticated Users");

                    if (role != null)
                    {
                        await userCommands.AddUserToRole(role.Id, adminUser.Id);
                    }
                }
            }
        }
        public async Task DoSetupStep(
            Func <string, bool, Task> output,
            Func <string, bool> appNeedsUpgrade,
            Func <string, Version> schemaVersionLookup,
            Func <string, Version> codeVersionLookup
            )
        {
            bool needsUpgrade = appNeedsUpgrade("cloudscribe-core");

            if (needsUpgrade)
            {
                await output("cloudscribe-core needs schema upgrade", false);

                return;
            }

            Version codeVersion   = codeVersionLookup("cloudscribe-core");
            Version schemaVersion = schemaVersionLookup("cloudscribe-core");

            if (codeVersion == null)
            {
                await output("cloudscribe-core code version not found", false);
            }
            if (schemaVersion == null)
            {
                await output("cloudscribe-core schema version not found", false);
            }

            if ((codeVersion != null) && (schemaVersion != null))
            {
                if (codeVersion == schemaVersion)
                {
                    var message = string.Format(
                        "cloudscribe-core schema is up to date at version: {0}",
                        codeVersion
                        );

                    await output(message, false);
                }
                else
                {
                    var message = string.Format(
                        "cloudscribe-core code version: {0} is out of sync with schema version: {1}",
                        codeVersion,
                        schemaVersion
                        );

                    await output(message, false);
                }
            }

            int countryCount = await geoDataManager.GetCountryCount();

            if (countryCount == 0)
            {
                await output("creating initial country data", true);

                List <GeoCountry> countries = InitialData.BuildCountryList();
                foreach (GeoCountry c in countries)
                {
                    await geoDataManager.Add(c);
                }

                await output("creating initial state/region data", true);

                List <GeoZone> states = InitialData.BuildStateList();
                foreach (GeoZone s in states)
                {
                    await geoDataManager.Add(s);
                }
            }
            List <ICurrency> dbCurrencies = await geoDataManager.GetAllCurrencies();

            if (dbCurrencies.Count == 0)
            {
                await output("creating initial currency data", true);

                List <Currency> currencies = InitialData.BuildCurrencyList();
                foreach (Currency c in currencies)
                {
                    await geoDataManager.Add(c);
                }
            }
            int languageCount = await geoDataManager.GetLanguageCount();

            if (languageCount == 0)
            {
                await output("creating initial language data", true);

                List <Language> langs = InitialData.BuildLanguageList();
                foreach (Language l in langs)
                {
                    await geoDataManager.Add(l);
                }
            }


            int existingSiteCount = await siteManager.ExistingSiteCount();

            await output(
                string.Format(
                    "ExistingSiteCount {0}",     //SetupResources.ExistingSiteCountMessage,
                    existingSiteCount.ToString()),
                false);

            if (existingSiteCount == 0)
            {
                await output("CreatingSite", true);

                SiteSettings newSite = await siteManager.CreateNewSite(true);

                await output("CreatingRolesAndAdminUser", true);

                bool result = await siteManager.CreateRequiredRolesAndAdminUser(newSite);

                existingSiteCount = await siteManager.ExistingSiteCount();

                if (existingSiteCount > 0)
                {
                    await output(BuildHomeLink(), false);
                }
            }
            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 (contextAccessor.HttpContext.Request.Host.HasValue)
                {
                    ISiteSettings site = await siteManager.Fetch(contextAccessor.HttpContext.Request.Host.Value);

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

                        bool roleResult = true;
                        if (roleCount == 0)
                        {
                            await output("CreatingRoles", true);

                            roleResult = await siteManager.EnsureRequiredRoles(site);
                        }

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

                            if (userCount == 0)
                            {
                                await output("CreatingAdminUser", true);

                                await siteManager.CreateAdminUser(site);
                            }
                        }
                    }
                }

                await output(BuildHomeLink(), false);
            }
        }