139 lines
5.4 KiB
C#
139 lines
5.4 KiB
C#
using Marco.Pms.DataAccess.Data;
|
|
using Marco.Pms.Model.Employees;
|
|
using Marco.Pms.Model.Entitlements;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Marco.Pms.Services.Service
|
|
{
|
|
public class StartupUserSeeder : IHostedService
|
|
{
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
public StartupUserSeeder(IServiceProvider serviceProvider)
|
|
{
|
|
_serviceProvider = serviceProvider;
|
|
}
|
|
|
|
public async Task StartAsync(CancellationToken cancellationToken)
|
|
{
|
|
using var scope = _serviceProvider.CreateScope();
|
|
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
|
|
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
|
|
var userEmail = "admin@marcoaiot.com";
|
|
|
|
var user = await userManager.FindByEmailAsync(userEmail);
|
|
var newUser = new ApplicationUser
|
|
{
|
|
UserName = userEmail,
|
|
Email = userEmail,
|
|
EmailConfirmed = true,
|
|
IsRootUser = true,
|
|
TenantId = Guid.Parse("b3466e83-7e11-464c-b93a-daf047838b26")
|
|
};
|
|
var result = new IdentityResult();
|
|
|
|
if (user == null)
|
|
{
|
|
result = await userManager.CreateAsync(newUser, "User@123");
|
|
}
|
|
else
|
|
{
|
|
newUser = user;
|
|
}
|
|
|
|
var jobRole = new JobRole
|
|
{
|
|
Id = Guid.Empty,
|
|
Name = "Admin",
|
|
Description = "Admin",
|
|
TenantId = Guid.Parse("b3466e83-7e11-464c-b93a-daf047838b26"),
|
|
};
|
|
|
|
if (!await dbContext.JobRoles.Where(j => j.Name == "Admin").AnyAsync())
|
|
{
|
|
await dbContext.JobRoles.AddAsync(jobRole);
|
|
}
|
|
else
|
|
{
|
|
jobRole = await dbContext.JobRoles.Where(j => j.Name == "Admin").FirstOrDefaultAsync();
|
|
}
|
|
var role = new ApplicationRole
|
|
{
|
|
Role = "Super User",
|
|
Description = "Super User",
|
|
IsSystem = true,
|
|
TenantId = Guid.Parse("b3466e83-7e11-464c-b93a-daf047838b26")
|
|
};
|
|
if (!await dbContext.ApplicationRoles.Where(a => a.Role == "Super User").AnyAsync())
|
|
{
|
|
await dbContext.ApplicationRoles.AddAsync(role);
|
|
}
|
|
else
|
|
{
|
|
role = await dbContext.ApplicationRoles.Where(a => a.Role == "Super User").FirstOrDefaultAsync();
|
|
}
|
|
await dbContext.SaveChangesAsync();
|
|
var employee = new Employee
|
|
{
|
|
ApplicationUserId = newUser.Id,
|
|
FirstName = "Admin",
|
|
LastName = "",
|
|
Email = userEmail,
|
|
TenantId = Guid.Parse("b3466e83-7e11-464c-b93a-daf047838b26"),
|
|
CurrentAddress = "",
|
|
BirthDate = Convert.ToDateTime("1965-04-20 10:11:17.588000"),
|
|
EmergencyPhoneNumber = "",
|
|
EmergencyContactPerson = "",
|
|
AadharNumber = "",
|
|
Gender = "",
|
|
MiddleName = "",
|
|
PanNumber = "",
|
|
PermanentAddress = "",
|
|
PhoneNumber = "9876543210",
|
|
Photo = null, // GetFileDetails(model.Photo).Result.FileData,
|
|
JobRoleId = jobRole != null ? jobRole.Id : Guid.Empty,
|
|
IsSystem = true,
|
|
JoiningDate = Convert.ToDateTime("2000-04-20 10:11:17.588000"),
|
|
};
|
|
if ((!await dbContext.Employees.Where(e => e.FirstName == "Admin").AnyAsync()) && (jobRole != null ? jobRole.Id : Guid.Empty) != Guid.Empty)
|
|
{
|
|
await dbContext.Employees.AddAsync(employee);
|
|
}
|
|
else
|
|
{
|
|
employee = await dbContext.Employees.Where(e => e.FirstName == "Admin").FirstOrDefaultAsync();
|
|
}
|
|
await dbContext.SaveChangesAsync();
|
|
if (!await dbContext.EmployeeRoleMappings.AnyAsync())
|
|
{
|
|
await dbContext.EmployeeRoleMappings.AddAsync(new EmployeeRoleMapping
|
|
{
|
|
EmployeeId = employee != null ? employee.Id : Guid.Empty,
|
|
IsEnabled = true,
|
|
TenantId = Guid.Parse("b3466e83-7e11-464c-b93a-daf047838b26"),
|
|
RoleId = role != null ? role.Id : Guid.Empty
|
|
});
|
|
}
|
|
if (!await dbContext.RolePermissionMappings.AnyAsync())
|
|
{
|
|
List<FeaturePermission> permissions = await dbContext.FeaturePermissions.ToListAsync();
|
|
List<RolePermissionMappings> rolesMapping = new List<RolePermissionMappings>();
|
|
foreach (var permission in permissions)
|
|
{
|
|
rolesMapping.Add(new RolePermissionMappings
|
|
{
|
|
ApplicationRoleId = role != null ? role.Id : Guid.Empty,
|
|
FeaturePermissionId = permission.Id
|
|
});
|
|
}
|
|
await dbContext.RolePermissionMappings.AddRangeAsync(rolesMapping);
|
|
}
|
|
await dbContext.SaveChangesAsync();
|
|
}
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
|
}
|
|
}
|