public static void RunSeed() { BusinessModelSeedDataManager.CheckMultiTenantData(); using (var context = BusinessDbContext.Create()) { context.DisableAllFilters(); ResourceBuilder.Build(context); RoleBuilder.Build(context, null); PermissionBuilder.Build(context, null); UserBuilder.Build(context, null); UserRoleBuilder.Build(context, null); TenantBuilder.BuildTenantRoles(context); TenantBuilder.BuildTenantUserRoles(context); TenantBuilder.BuildTenantPermissions(context); context.EnableAllFilters(); } }
public static void Build(BusinessDbContext db, string tenantId) { BusinessDbContext businessDbContext = new BusinessDbContext(); Branch branch; Company company; Tenant tenant; if (string.IsNullOrWhiteSpace(tenantId)) { branch = businessDbContext.Branches.FirstOrDefault(x => x.Type == BranchType.HeadOffice); company = businessDbContext.Companies.FirstOrDefault(x => x.Name == StaticCompany.Host); tenant = businessDbContext.Tenants.FirstOrDefault(x => x.Name == StaticTenant.Host); } else { branch = businessDbContext.Branches.FirstOrDefault(x => x.Type == BranchType.HeadOffice && x.TenantId == tenantId); company = businessDbContext.Companies.FirstOrDefault(x => x.TenantId == tenantId); tenant = businessDbContext.Tenants.FirstOrDefault(x => x.Id == tenantId); } var roles = db.Roles.ToList(); var users = new List <User>(); #region Create System Admin User if (string.IsNullOrWhiteSpace(tenantId)) { var systemRole = roles.FirstOrDefault(x => x.Name == StaticRoles.SystemAdmin.Name); var systemAdminUserId = Guid.NewGuid().ToString(); var systemAdmin = new User() { Id = systemAdminUserId, FirstName = "System", LastName = "Admin", Email = "*****@*****.**", UserName = "******", PhoneNumber = "+8801911831907", PasswordHash = new PasswordHasher().HashPassword("123qwe"), Roles = { new UserRole() { UserId = systemAdminUserId, RoleId = systemRole?.Id, TenantId = tenant?.Id, CompanyId = company?.Id } }, BranchId = branch?.Id, TenantId = tenant?.Id, TenantName = tenant?.TenancyName, CompanyId = company?.Id, IsActive = true, Created = DateTime.Now, CreatedBy = null, }; systemAdmin.EmployeeId = BusinessModelSeedDataManager.AddEmployee(db, systemAdmin).Id; users.Add(systemAdmin); } #endregion #region Create Admin User var adminRole = roles.FirstOrDefault(x => x.Name == StaticRoles.Admin.Name); var adminUserId = Guid.NewGuid().ToString(); var admin = new User() { Id = adminUserId, FirstName = "Admin", LastName = "Admin", Email = "*****@*****.**", UserName = "******", PhoneNumber = "", PasswordHash = new PasswordHasher().HashPassword("123qwe"), Roles = { new UserRole() { UserId = adminUserId, RoleId = adminRole?.Id, TenantId = tenant?.Id, CompanyId = company?.Id } }, BranchId = branch?.Id, TenantId = tenant?.Id, TenantName = tenant?.TenancyName, CompanyId = company?.Id, IsActive = true, }; admin.EmployeeId = BusinessModelSeedDataManager.AddEmployee(db, admin).Id; users.Add(admin); #endregion if (!db.Users.Any()) { foreach (var user in users) { user.SecurityStamp = Guid.NewGuid().ToString(); db.Users.Add(user); } } else { foreach (var user in users.Where(user => db.Users.FirstOrDefault(x => x.UserName == user.UserName) == null)) { user.SecurityStamp = Guid.NewGuid().ToString(); db.Users.Add(user); } } db.SaveChanges(); }