public async Task <bool> Save(IGeoCountry geoCountry, CancellationToken cancellationToken = default(CancellationToken)) { if (geoCountry == null) { return(false); } GeoCountry country = GeoCountry.FromIGeoCountry(geoCountry); // convert from IGeoCountry if (country.Guid == Guid.Empty) { country.Guid = Guid.NewGuid(); dbContext.Countries.Add(country); } else { bool tracking = dbContext.ChangeTracker.Entries <GeoCountry>().Any(x => x.Entity.Guid == country.Guid); if (!tracking) { dbContext.Countries.Update(country); } } int rowsAffected = await dbContext.SaveChangesAsync(cancellationToken); return(rowsAffected > 0); }
public async Task <bool> Save( ISiteSettings site, CancellationToken cancellationToken = default(CancellationToken)) { if (site == null) { return(false); } SiteSettings siteSettings = SiteSettings.FromISiteSettings(site); if (siteSettings.SiteId == -1) // a newly created item { siteSettings.SiteId = 0; //EF needs this to be 0 in order to generate it from the db identity if (siteSettings.SiteGuid == Guid.Empty) { siteSettings.SiteGuid = Guid.NewGuid(); } dbContext.Sites.Add(siteSettings); } else { bool tracking = dbContext.ChangeTracker.Entries <SiteSettings>().Any(x => x.Entity.SiteId == siteSettings.SiteId); if (!tracking) { dbContext.Sites.Update(siteSettings); } } int rowsAffected = await dbContext.SaveChangesAsync(cancellationToken) .ConfigureAwait(false); // update the original with the new keys after insert if ((site.SiteId == -1) && (siteSettings != null)) { site.SiteId = siteSettings.SiteId; site.SiteGuid = siteSettings.SiteGuid; } return(rowsAffected > 0); }
public async Task <bool> Add(IGeoCountry geoCountry, CancellationToken cancellationToken = default(CancellationToken)) { if (geoCountry == null) { return(false); } GeoCountry country = GeoCountry.FromIGeoCountry(geoCountry); // convert from IGeoCountry if (country.Guid == Guid.Empty) { country.Guid = Guid.NewGuid(); } dbContext.Countries.Add(country); int rowsAffected = await dbContext.SaveChangesAsync(cancellationToken); return(rowsAffected > 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 cloudscribe.Core.Models.InitialData.BuildCountryList()) { db.Countries.Add(c); } rowsAffected = await db.SaveChangesAsync(); } count = await db.States.CountAsync <GeoZone>(); if (count == 0) { foreach (GeoZone c in cloudscribe.Core.Models.InitialData.BuildStateList()) { db.States.Add(c); } rowsAffected = await db.SaveChangesAsync(); } count = await db.Languages.CountAsync <Language>(); if (count == 0) { foreach (Language c in cloudscribe.Core.Models.InitialData.BuildLanguageList()) { db.Languages.Add(c); } rowsAffected = await db.SaveChangesAsync(); } count = await db.Currencies.CountAsync <Currency>(); if (count == 0) { foreach (Currency c in cloudscribe.Core.Models.InitialData.BuildCurrencyList()) { db.Currencies.Add(c); } rowsAffected = await db.SaveChangesAsync(); } count = await db.Sites.CountAsync <SiteSettings>(); if (count == 0) { // create first site SiteSettings newSite = new SiteSettings(); newSite.SiteId = 0; newSite.SiteGuid = Guid.NewGuid(); newSite.SiteName = "Sample Site"; newSite.IsServerAdminSite = true; newSite.Layout = "Default_Layout.cshtml"; newSite.AllowNewRegistration = true; newSite.AllowUserFullNameChange = false; newSite.AutoCreateLdapUserOnFirstLogin = true; newSite.ReallyDeleteUsers = true; newSite.LdapPort = 389; newSite.LdapRootDN = string.Empty; newSite.LdapServer = string.Empty; newSite.UseEmailForLogin = true; newSite.UseLdapAuth = false; newSite.UseSecureRegistration = false; newSite.UseSslOnAllPages = false; //0 = clear, 1= hashed, 2= encrypted //newSite.PasswordFormat = 1; newSite.RequiresQuestionAndAnswer = false; newSite.MaxInvalidPasswordAttempts = 10; newSite.PasswordAttemptWindowMinutes = 5; newSite.MinReqNonAlphaChars = 0; newSite.MinRequiredPasswordLength = 7; db.Sites.Add(newSite); rowsAffected = await db.SaveChangesAsync(); } // ensure roles count = await db.Roles.CountAsync <SiteRole>(); if (count == 0) { SiteSettings site = await db.Sites.SingleOrDefaultAsync <SiteSettings>( s => s.SiteId > 0 && s.IsServerAdminSite == true); if (site != null) { SiteRole adminRole = new SiteRole(); adminRole.RoleId = 0; adminRole.RoleGuid = Guid.NewGuid(); adminRole.RoleName = "Admins"; adminRole.DisplayName = "Administrators"; adminRole.SiteId = site.SiteId; adminRole.SiteGuid = site.SiteGuid; db.Roles.Add(adminRole); //rowsAffected = await db.SaveChangesAsync(); SiteRole roleAdminRole = new SiteRole(); roleAdminRole.RoleId = 0; roleAdminRole.RoleGuid = Guid.NewGuid(); roleAdminRole.RoleName = "Role Admins"; roleAdminRole.DisplayName = "Role Administrators"; roleAdminRole.SiteId = site.SiteId; roleAdminRole.SiteGuid = site.SiteGuid; db.Roles.Add(roleAdminRole); //rowsAffected = await db.SaveChangesAsync(); SiteRole contentAdminRole = new SiteRole(); contentAdminRole.RoleId = 0; contentAdminRole.RoleGuid = Guid.NewGuid(); contentAdminRole.RoleName = "Content Administrators"; contentAdminRole.DisplayName = "Content Administrators"; contentAdminRole.SiteId = site.SiteId; contentAdminRole.SiteGuid = site.SiteGuid; db.Roles.Add(contentAdminRole); SiteRole authenticatedUserRole = new SiteRole(); authenticatedUserRole.RoleId = 0; authenticatedUserRole.RoleGuid = Guid.NewGuid(); authenticatedUserRole.RoleName = "Authenticated Users"; authenticatedUserRole.DisplayName = "Authenticated Users"; authenticatedUserRole.SiteId = site.SiteId; authenticatedUserRole.SiteGuid = site.SiteGuid; 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.SiteId > 0 && s.IsServerAdminSite == true); if (site != null) { SiteRole role = await db.Roles.SingleOrDefaultAsync( x => x.SiteId == site.SiteId && x.RoleName == "Admins"); if (role != null) { SiteUser adminUser = new SiteUser(); adminUser.SiteId = site.SiteId; adminUser.SiteGuid = site.SiteGuid; adminUser.Email = "*****@*****.**"; adminUser.LoweredEmail = adminUser.Email; adminUser.DisplayName = "Admin"; adminUser.UserName = "******"; adminUser.UserId = 0; adminUser.UserGuid = Guid.NewGuid(); adminUser.EmailConfirmed = true; adminUser.AccountApproved = true; // clear text password will be hashed upon login // this format allows migrating from mojoportal adminUser.PasswordHash = "admin||0"; //pwd/salt/format db.Users.Add(adminUser); rowsAffected = await db.SaveChangesAsync(); if (rowsAffected > 0 && adminUser.UserId > -1) { UserRole ur = new UserRole(); ur.Id = 0; ur.RoleGuid = role.RoleGuid; ur.RoleId = role.RoleId; ur.UserGuid = adminUser.UserGuid; ur.UserId = adminUser.UserId; db.UserRoles.Add(ur); rowsAffected = await db.SaveChangesAsync(); } } } } }
private static async Task EnsureData( CoreDbContext db ) { int rowsAffected = 0; int count = await db.Countries.CountAsync<GeoCountry>(); if(count == 0) { foreach(GeoCountry c in cloudscribe.Core.Models.InitialData.BuildCountryList()) { db.Countries.Add(c); } rowsAffected = await db.SaveChangesAsync(); } count = await db.States.CountAsync<GeoZone>(); if (count == 0) { foreach (GeoZone c in cloudscribe.Core.Models.InitialData.BuildStateList()) { db.States.Add(c); } rowsAffected = await db.SaveChangesAsync(); } count = await db.Languages.CountAsync<Language>(); if (count == 0) { foreach (Language c in cloudscribe.Core.Models.InitialData.BuildLanguageList()) { db.Languages.Add(c); } rowsAffected = await db.SaveChangesAsync(); } count = await db.Currencies.CountAsync<Currency>(); if (count == 0) { foreach (Currency c in cloudscribe.Core.Models.InitialData.BuildCurrencyList()) { db.Currencies.Add(c); } rowsAffected = await db.SaveChangesAsync(); } count = await db.Sites.CountAsync<SiteSettings>(); if (count == 0) { // create first site SiteSettings newSite = new SiteSettings(); newSite.SiteId = 0; newSite.SiteGuid = Guid.NewGuid(); newSite.SiteName = "Sample Site"; newSite.IsServerAdminSite = true; newSite.Layout = "Default_Layout.cshtml"; newSite.AllowNewRegistration = true; newSite.AllowUserFullNameChange = false; newSite.AutoCreateLdapUserOnFirstLogin = true; newSite.ReallyDeleteUsers = true; newSite.LdapPort = 389; newSite.LdapRootDN = string.Empty; newSite.LdapServer = string.Empty; newSite.UseEmailForLogin = true; newSite.UseLdapAuth = false; newSite.UseSecureRegistration = false; newSite.UseSslOnAllPages = false; //0 = clear, 1= hashed, 2= encrypted //newSite.PasswordFormat = 1; newSite.RequiresQuestionAndAnswer = false; newSite.MaxInvalidPasswordAttempts = 10; newSite.PasswordAttemptWindowMinutes = 5; newSite.MinReqNonAlphaChars = 0; newSite.MinRequiredPasswordLength = 7; db.Sites.Add(newSite); rowsAffected = await db.SaveChangesAsync(); } // ensure roles count = await db.Roles.CountAsync<SiteRole>(); if (count == 0) { SiteSettings site = await db.Sites.SingleOrDefaultAsync<SiteSettings>( s => s.SiteId > 0 && s.IsServerAdminSite == true); if(site != null) { SiteRole adminRole = new SiteRole(); adminRole.RoleId = 0; adminRole.RoleGuid = Guid.NewGuid(); adminRole.RoleName = "Admins"; adminRole.DisplayName = "Administrators"; adminRole.SiteId = site.SiteId; adminRole.SiteGuid = site.SiteGuid; db.Roles.Add(adminRole); //rowsAffected = await db.SaveChangesAsync(); SiteRole roleAdminRole = new SiteRole(); roleAdminRole.RoleId = 0; roleAdminRole.RoleGuid = Guid.NewGuid(); roleAdminRole.RoleName = "Role Admins"; roleAdminRole.DisplayName = "Role Administrators"; roleAdminRole.SiteId = site.SiteId; roleAdminRole.SiteGuid = site.SiteGuid; db.Roles.Add(roleAdminRole); //rowsAffected = await db.SaveChangesAsync(); SiteRole contentAdminRole = new SiteRole(); contentAdminRole.RoleId = 0; contentAdminRole.RoleGuid = Guid.NewGuid(); contentAdminRole.RoleName = "Content Administrators"; contentAdminRole.DisplayName = "Content Administrators"; contentAdminRole.SiteId = site.SiteId; contentAdminRole.SiteGuid = site.SiteGuid; db.Roles.Add(contentAdminRole); SiteRole authenticatedUserRole = new SiteRole(); authenticatedUserRole.RoleId = 0; authenticatedUserRole.RoleGuid = Guid.NewGuid(); authenticatedUserRole.RoleName = "Authenticated Users"; authenticatedUserRole.DisplayName = "Authenticated Users"; authenticatedUserRole.SiteId = site.SiteId; authenticatedUserRole.SiteGuid = site.SiteGuid; 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.SiteId > 0 && s.IsServerAdminSite == true); if (site != null) { SiteRole role = await db.Roles.SingleOrDefaultAsync( x => x.SiteId == site.SiteId && x.RoleName == "Admins"); if(role != null) { SiteUser adminUser = new SiteUser(); adminUser.SiteId = site.SiteId; adminUser.SiteGuid = site.SiteGuid; adminUser.Email = "*****@*****.**"; adminUser.LoweredEmail = adminUser.Email; adminUser.DisplayName = "Admin"; adminUser.UserName = "******"; adminUser.UserId = 0; adminUser.UserGuid = Guid.NewGuid(); adminUser.EmailConfirmed = true; adminUser.AccountApproved = true; // clear text password will be hashed upon login // this format allows migrating from mojoportal adminUser.PasswordHash = "admin||0"; //pwd/salt/format db.Users.Add(adminUser); rowsAffected = await db.SaveChangesAsync(); if(rowsAffected > 0 && adminUser.UserId > -1) { UserRole ur = new UserRole(); ur.Id = 0; ur.RoleGuid = role.RoleGuid; ur.RoleId = role.RoleId; ur.UserGuid = adminUser.UserGuid; ur.UserId = adminUser.UserId; db.UserRoles.Add(ur); rowsAffected = await db.SaveChangesAsync(); } } } } }