| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- // ======================================
- // Author: Ebenezer Monney
- // Email: info@ebenmonney.com
- // Copyright (c) 2017 www.ebenmonney.com
- //
- // ==> Gun4Hire: contact@ebenmonney.com
- // ======================================
- using DAL.Models;
- using Microsoft.AspNetCore.Identity;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Logging;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using DAL.Core;
- using DAL.Core.Interfaces;
- namespace DAL
- {
- public interface IDatabaseInitializer
- {
- Task SeedAsync();
- }
- public class DatabaseInitializer : IDatabaseInitializer
- {
- private readonly ApplicationDbContext _context;
- private readonly IAccountManager _accountManager;
- private readonly ILogger _logger;
- public DatabaseInitializer(ApplicationDbContext context, IAccountManager accountManager, ILogger<DatabaseInitializer> logger)
- {
- _accountManager = accountManager;
- _context = context;
- _logger = logger;
- }
- public async Task SeedAsync()
- {
- await _context.Database.MigrateAsync().ConfigureAwait(false);
- if (!await _context.Users.AnyAsync())
- {
- const string adminRoleName = "管理员";
- const string userRoleName = "客服";
- await ensureRoleAsync(adminRoleName, "Default administrator", ApplicationPermissions.GetAllPermissionValues());
- await ensureRoleAsync(userRoleName, "Default user", new string[] { });
- await createUserAsync("admin", "tempP@ss123", "Inbuilt Administrator", "admin@ebenmonney.com", "+1 (123) 000-0000", new string[] { adminRoleName });
- await createUserAsync("user", "tempP@ss123", "Inbuilt Standard User", "user@ebenmonney.com", "+1 (123) 000-0001", new string[] { userRoleName });
- //await _context.SaveChangesAsync();
- }
- }
- private async Task ensureRoleAsync(string roleName, string description, string[] claims)
- {
- if ((await _accountManager.GetRoleByNameAsync(roleName)) == null)
- {
- ApplicationRole applicationRole = new ApplicationRole(roleName, description);
- var result = await this._accountManager.CreateRoleAsync(applicationRole, claims);
- if (!result.Item1)
- throw new Exception($"Seeding \"{description}\" role failed. Errors: {string.Join(Environment.NewLine, result.Item2)}");
- }
- }
- private async Task<ApplicationUser> createUserAsync(string userName, string password, string fullName, string email, string phoneNumber, string[] roles)
- {
- ApplicationUser applicationUser = new ApplicationUser
- {
- UserName = userName,
- FullName = fullName,
- Email = email,
- PhoneNumber = phoneNumber,
- EmailConfirmed = true,
- IsEnabled = true
- };
- var result = await _accountManager.CreateUserAsync(applicationUser, roles, password);
- if (!result.Item1)
- throw new Exception($"Seeding \"{userName}\" user failed. Errors: {string.Join(Environment.NewLine, result.Item2)}");
- return applicationUser;
- }
- }
- }
|