153 lines
5.8 KiB
C#
153 lines
5.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using gehGassi.Domain.Common;
|
|
using gehGassi.Domain.Roles;
|
|
using gehGassi.Domain.Shop;
|
|
using gehGassi.Domain.Users;
|
|
using gehGassi.Permissions;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Options;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace gehGassi.Persistence
|
|
{
|
|
/// <summary>
|
|
/// Hilfsklasse für das Initialisieren der DB.
|
|
/// Befüllen mit Standard-Werten
|
|
/// </summary>
|
|
public class DbInitializer
|
|
{
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
|
private readonly RoleManager<ApplicationRole> _roleManager;
|
|
private readonly IOptions<DbInitializerOptions> _options;
|
|
private readonly IWebHostEnvironment _hostingEnvironment;
|
|
private readonly SqlDbContext _context;
|
|
private const string RoleToPermissionsFilename = "rolepermissions.json";
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Isntanz
|
|
/// </summary>
|
|
/// <param name="context">Instanz eines DbContext</param>
|
|
/// <param name="userManager">Instanz eines UserManager</param>
|
|
/// <param name="roleManager">Instanz eines RoleManager</param>
|
|
/// <param name="options">Instanz eines IOptions DbInitializerOptions</param>
|
|
/// <param name="hostingEnvironment">Instanz eines IHostingEnvironment</param>
|
|
public DbInitializer(DbContext context, UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager,
|
|
IOptions<DbInitializerOptions> options, IWebHostEnvironment hostingEnvironment)
|
|
{
|
|
_userManager = userManager;
|
|
_roleManager = roleManager;
|
|
_options = options;
|
|
_hostingEnvironment = hostingEnvironment;
|
|
_context = (SqlDbContext)context;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Befüllen der DB mit Standard-Werten
|
|
/// </summary>
|
|
/// <returns>true wenn erfolgreich, false sonst</returns>
|
|
public async Task<bool> SeedAsync()
|
|
{
|
|
//Sicherstellen, dass DB existiert
|
|
//await _context.Database.EnsureCreatedAsync();
|
|
try
|
|
{
|
|
await _context.Database.MigrateAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var err = ex.Message;
|
|
}
|
|
|
|
//Anlegen der Rollen
|
|
//Laden der Vordefinierten Rollen-Rechte
|
|
var path = Path.Combine(_hostingEnvironment.WebRootPath, _options.Value.Directory);
|
|
var rolesToPermissions = JsonConvert.DeserializeObject<List<RoleToPermissions>>(File.ReadAllText(Path.Combine(path, RoleToPermissionsFilename)));
|
|
|
|
foreach (var role in rolesToPermissions)
|
|
{
|
|
if (!await _roleManager.RoleExistsAsync(role.RoleName))
|
|
{
|
|
await _roleManager.CreateAsync(new ApplicationRole(role.RoleName, role.Permissions.PackPermissionsIntoString()));
|
|
}
|
|
else
|
|
{
|
|
var roleItem = await _roleManager.FindByNameAsync(role.RoleName);
|
|
if (roleItem != null)
|
|
{
|
|
roleItem.Permissions = role.Permissions.PackPermissionsIntoString();
|
|
await _roleManager.UpdateAsync(roleItem);
|
|
}
|
|
}
|
|
}
|
|
|
|
var adminRole = await _roleManager.FindByNameAsync("Administrator");
|
|
var adminUser = new ApplicationUser
|
|
{
|
|
FirstName = "Florian",
|
|
LastName = "Mihalits",
|
|
FullName = "Florian Mihalits",
|
|
Email = "office@creativebits.com",
|
|
NormalizedEmail = "OFFICE@CREATIVEBITS.COM",
|
|
UserName = "office@creativebits.com",
|
|
NormalizedUserName = "OFFICE@CREATIVEBITS.COM",
|
|
PhoneNumber = "+43 676 846024 200",
|
|
EmailConfirmed = true,
|
|
PhoneNumberConfirmed = true,
|
|
SecurityStamp = Guid.NewGuid().ToString("D"),
|
|
TimeZoneId = TimeZoneInfo.Local.Id,
|
|
PreferredLanguage = "de",
|
|
Photo = string.Empty,
|
|
RegistrationDate = DateTimeOffset.UtcNow,
|
|
LastLoginDate = DateTimeOffset.UtcNow,
|
|
Permissions = adminRole.Permissions
|
|
};
|
|
|
|
if (!_context.Users.Any(u => u.UserName == adminUser.UserName))
|
|
{
|
|
await _userManager.CreateAsync(adminUser, "!gehGassi#321");
|
|
await _userManager.AddToRoleAsync(adminUser, "Administrator");
|
|
}
|
|
|
|
//Anderes initialisieren....
|
|
var shopSettings = await _context.ShopSettings.FirstOrDefaultAsync();
|
|
if (shopSettings == null)
|
|
{
|
|
shopSettings = new ShopSettings()
|
|
{
|
|
PaymentOnInvoice = false,
|
|
PayPalEnabled = false,
|
|
PayPalClientId = string.Empty,
|
|
PayPalSecret = string.Empty,
|
|
KlarnaEnabled = false,
|
|
KlarnaClientId = string.Empty,
|
|
KlarnaSecret = string.Empty,
|
|
OrderEmail = "office@gehgassi.com",
|
|
ShipmentCalculationType = ShipmentCalculationType.Weight
|
|
};
|
|
_context.ShopSettings.Add(shopSettings);
|
|
}
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Einstellungen für den DbInitionalizer
|
|
/// </summary>
|
|
public class DbInitializerOptions
|
|
{
|
|
/// <summary>
|
|
/// Basis-Directory in welchem die Rollen-Rechte Datei liegt
|
|
/// </summary>
|
|
public string Directory { get; set; }
|
|
}
|
|
}
|