2017-09-13 14:53:06 +02:00
|
|
|
|
using IdentityServer4.EntityFramework.DbContexts;
|
2018-04-25 12:58:55 +01:00
|
|
|
|
using IdentityServer4.EntityFramework.Entities;
|
2017-09-13 14:53:06 +02:00
|
|
|
|
using IdentityServer4.EntityFramework.Mappers;
|
2018-04-25 12:58:55 +01:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2017-09-13 14:53:06 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.Services.Identity.API.Configuration;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2018-04-25 12:58:55 +01:00
|
|
|
|
using System;
|
2017-09-13 14:53:06 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Identity.API.Data
|
|
|
|
|
{
|
|
|
|
|
public class ConfigurationDbContextSeed
|
|
|
|
|
{
|
|
|
|
|
public async Task SeedAsync(ConfigurationDbContext context,IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
//callbacks urls from config:
|
|
|
|
|
var clientUrls = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
clientUrls.Add("Mvc", configuration.GetValue<string>("MvcClient"));
|
|
|
|
|
clientUrls.Add("Spa", configuration.GetValue<string>("SpaClient"));
|
|
|
|
|
clientUrls.Add("Xamarin", configuration.GetValue<string>("XamarinCallback"));
|
|
|
|
|
clientUrls.Add("LocationsApi", configuration.GetValue<string>("LocationApiClient"));
|
|
|
|
|
clientUrls.Add("MarketingApi", configuration.GetValue<string>("MarketingApiClient"));
|
|
|
|
|
clientUrls.Add("BasketApi", configuration.GetValue<string>("BasketApiClient"));
|
|
|
|
|
clientUrls.Add("OrderingApi", configuration.GetValue<string>("OrderingApiClient"));
|
2018-02-15 17:30:39 +01:00
|
|
|
|
clientUrls.Add("MobileShoppingAgg", configuration.GetValue<string>("MobileShoppingAggClient"));
|
2018-02-27 14:32:25 +01:00
|
|
|
|
clientUrls.Add("WebShoppingAgg", configuration.GetValue<string>("WebShoppingAggClient"));
|
2017-09-13 14:53:06 +02:00
|
|
|
|
|
|
|
|
|
if (!context.Clients.Any())
|
|
|
|
|
{
|
|
|
|
|
foreach (var client in Config.GetClients(clientUrls))
|
|
|
|
|
{
|
2018-04-25 12:58:55 +01:00
|
|
|
|
context.Clients.Add(client.ToEntity());
|
2017-09-13 14:53:06 +02:00
|
|
|
|
}
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
}
|
2018-04-25 12:58:55 +01:00
|
|
|
|
// Checking always for old redirects to fix existing deployments
|
|
|
|
|
// to use new swagger-ui redirect uri as of v3.0.0
|
|
|
|
|
// There should be no problem for new ones
|
|
|
|
|
// ref: https://github.com/dotnet-architecture/eShopOnContainers/issues/586
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
List<ClientRedirectUri> oldRedirects = (await context.Clients.Include(c => c.RedirectUris).ToListAsync())
|
|
|
|
|
.SelectMany(c => c.RedirectUris)
|
|
|
|
|
.Where(ru => ru.RedirectUri.EndsWith("/o2c.html"))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
if (oldRedirects.Any())
|
|
|
|
|
{
|
|
|
|
|
foreach (var ru in oldRedirects)
|
|
|
|
|
{
|
|
|
|
|
ru.RedirectUri = ru.RedirectUri.Replace("/o2c.html", "/oauth2-redirect.html");
|
|
|
|
|
context.Update(ru.Client);
|
|
|
|
|
}
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-13 14:53:06 +02:00
|
|
|
|
|
|
|
|
|
if (!context.IdentityResources.Any())
|
|
|
|
|
{
|
|
|
|
|
foreach (var resource in Config.GetResources())
|
|
|
|
|
{
|
2018-04-25 12:58:55 +01:00
|
|
|
|
context.IdentityResources.Add(resource.ToEntity());
|
2017-09-13 14:53:06 +02:00
|
|
|
|
}
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!context.ApiResources.Any())
|
|
|
|
|
{
|
|
|
|
|
foreach (var api in Config.GetApis())
|
|
|
|
|
{
|
2018-04-25 12:58:55 +01:00
|
|
|
|
context.ApiResources.Add(api.ToEntity());
|
2017-09-13 14:53:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|