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

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

            SiteSettings newSite = null;

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

                db.Set <SiteSettings>().Add(newSite);

                rowsAffected = await db.SaveChangesAsync();
            }
            // ensure roles
            count = await db.Roles.CountAsync();

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

                if (site != null)
                {
                    var roleAdminRole = InitialData.BuildAdminRole();
                    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();

            if (count == 0)
            {
                SiteSettings site = await db.Set <SiteSettings>().FirstOrDefaultAsync <SiteSettings>(
                    s => s.Id != 0 && s.IsServerAdminSite == true);

                if (site != null)
                {
                    var role = await db.Roles.FirstOrDefaultAsync(
                        x => x.SiteId == site.Id && x.NormalizedName == "ADMINISTRATORS");

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

                        rowsAffected = await db.SaveChangesAsync();

                        if (rowsAffected > 0 && adminUser.Id != 0)
                        {
                            var ur = new UserRoleEntity();
                            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.NormalizedName == "AuthenticatedUsers".ToUpperInvariant());

                            if (role != null)
                            {
                                ur        = new UserRoleEntity();
                                ur.RoleId = role.Id;
                                ur.UserId = adminUser.Id;
                                db.UserRoles.Add(ur);
                                await db.SaveChangesAsync();
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
 public BaseUnitOfWork(CoreDbContext context, IEnumerable <IInterceptor> interceptors) : base(context, interceptors)
 {
     BaseWorkArea = new BaseWorkArea(context);
 }