DatabaseInitializer.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // ======================================
  2. // Author: Ebenezer Monney
  3. // Email: info@ebenmonney.com
  4. // Copyright (c) 2017 www.ebenmonney.com
  5. //
  6. // ==> Gun4Hire: contact@ebenmonney.com
  7. // ======================================
  8. using DAL.Models;
  9. using Microsoft.AspNetCore.Identity;
  10. using Microsoft.EntityFrameworkCore;
  11. using Microsoft.Extensions.Logging;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using DAL.Core;
  18. using DAL.Core.Interfaces;
  19. namespace DAL
  20. {
  21. public interface IDatabaseInitializer
  22. {
  23. Task SeedAsync();
  24. }
  25. public class DatabaseInitializer : IDatabaseInitializer
  26. {
  27. private readonly ApplicationDbContext _context;
  28. private readonly IAccountManager _accountManager;
  29. private readonly ILogger _logger;
  30. public DatabaseInitializer(ApplicationDbContext context, IAccountManager accountManager, ILogger<DatabaseInitializer> logger)
  31. {
  32. _accountManager = accountManager;
  33. _context = context;
  34. _logger = logger;
  35. }
  36. public async Task SeedAsync()
  37. {
  38. await _context.Database.MigrateAsync().ConfigureAwait(false);
  39. if (!await _context.Users.AnyAsync())
  40. {
  41. const string adminRoleName = "管理员";
  42. const string userRoleName = "客服";
  43. await ensureRoleAsync(adminRoleName, "Default administrator", ApplicationPermissions.GetAllPermissionValues());
  44. await ensureRoleAsync(userRoleName, "Default user", new string[] { });
  45. await createUserAsync("admin", "tempP@ss123", "Inbuilt Administrator", "admin@ebenmonney.com", "+1 (123) 000-0000", new string[] { adminRoleName });
  46. await createUserAsync("user", "tempP@ss123", "Inbuilt Standard User", "user@ebenmonney.com", "+1 (123) 000-0001", new string[] { userRoleName });
  47. //await _context.SaveChangesAsync();
  48. }
  49. }
  50. private async Task ensureRoleAsync(string roleName, string description, string[] claims)
  51. {
  52. if ((await _accountManager.GetRoleByNameAsync(roleName)) == null)
  53. {
  54. ApplicationRole applicationRole = new ApplicationRole(roleName, description);
  55. var result = await this._accountManager.CreateRoleAsync(applicationRole, claims);
  56. if (!result.Item1)
  57. throw new Exception($"Seeding \"{description}\" role failed. Errors: {string.Join(Environment.NewLine, result.Item2)}");
  58. }
  59. }
  60. private async Task<ApplicationUser> createUserAsync(string userName, string password, string fullName, string email, string phoneNumber, string[] roles)
  61. {
  62. ApplicationUser applicationUser = new ApplicationUser
  63. {
  64. UserName = userName,
  65. FullName = fullName,
  66. Email = email,
  67. PhoneNumber = phoneNumber,
  68. EmailConfirmed = true,
  69. IsEnabled = true
  70. };
  71. var result = await _accountManager.CreateUserAsync(applicationUser, roles, password);
  72. if (!result.Item1)
  73. throw new Exception($"Seeding \"{userName}\" user failed. Errors: {string.Join(Environment.NewLine, result.Item2)}");
  74. return applicationUser;
  75. }
  76. }
  77. }