| @@ -138,7 +138,8 @@ namespace Diligent.WebAPI.Business.Services | |||
| FirstName = user.FirstName, | |||
| LastName = user.LastName, | |||
| Token = token, | |||
| RefreshToken = token | |||
| RefreshToken = token, | |||
| Role = (await _userManager.GetRolesAsync(user))[0] | |||
| }; | |||
| return new ServiceResponseDTO<AuthenticateResponseDto> | |||
| @@ -244,7 +245,8 @@ namespace Diligent.WebAPI.Business.Services | |||
| LastName = userk.LastName, | |||
| Username = userk.UserName, | |||
| Token = model.Token, | |||
| RefreshToken = model.RefreshToken | |||
| RefreshToken = model.RefreshToken, | |||
| Role = (await _userManager.GetRolesAsync(userk))[0] | |||
| } | |||
| }; | |||
| } | |||
| @@ -282,7 +284,8 @@ namespace Diligent.WebAPI.Business.Services | |||
| LastName = userk.LastName, | |||
| Username = userk.UserName, | |||
| Token = token, | |||
| RefreshToken = token | |||
| RefreshToken = token, | |||
| Role = (await _userManager.GetRolesAsync(userk))[0] | |||
| } | |||
| }; | |||
| } | |||
| @@ -24,7 +24,7 @@ | |||
| { | |||
| _logger.LogInformation("Start getting all users"); | |||
| _logger.LogInformation("Getting data from DB"); | |||
| var fromDb = await _userManager.Users.ToListAsync(); | |||
| var fromDb = await _userManager.GetUsersInRoleAsync("Admin"); | |||
| _logger.LogInformation($"Received {fromDb.Count} ads from db."); | |||
| return fromDb; | |||
| } | |||
| @@ -110,6 +110,7 @@ | |||
| }; | |||
| await _userManager.CreateAsync(user, StringGenerator.GenerateRandomPassword()); | |||
| await _userManager.AddToRoleAsync(user, "Admin"); | |||
| // generate invitation token for user | |||
| // encoded for URLs | |||
| @@ -8,5 +8,6 @@ | |||
| public string Username { get; set; } | |||
| public string Token { get; set; } | |||
| public string RefreshToken { get; set; } | |||
| public string Role { get; set; } | |||
| } | |||
| } | |||
| @@ -0,0 +1,35 @@ | |||
| using Microsoft.AspNetCore.Identity; | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| using System.Text; | |||
| using System.Threading.Tasks; | |||
| namespace Diligent.WebAPI.Data | |||
| { | |||
| public class DataSeeder | |||
| { | |||
| public static async void Seed(IServiceProvider serviceProvider) | |||
| { | |||
| using var scope = serviceProvider.CreateScope(); | |||
| var databaseContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>(); | |||
| var userManager = scope.ServiceProvider.GetRequiredService<UserManager<User>>(); | |||
| if (!databaseContext.Users.Any()) | |||
| { | |||
| var superAdmin = new User | |||
| { | |||
| AccessFailedCount = 0, | |||
| Email = "admin@dilig.net", | |||
| FirstName = "SuperAdmin", | |||
| LastName = "SuperAdmin", | |||
| UserName = "superAdmin", | |||
| EmailConfirmed = true | |||
| }; | |||
| await userManager.CreateAsync(superAdmin, "Nekasifra123!"); | |||
| await userManager.AddToRoleAsync(superAdmin, "SuperAdmin"); | |||
| await databaseContext.SaveChangesAsync(); | |||
| } | |||
| } | |||
| } | |||
| } | |||
| @@ -2,7 +2,7 @@ | |||
| public static class ServiceCollection | |||
| { | |||
| public static void ConfigureData(this IServiceCollection services, IConfiguration configuration) | |||
| public static async void ConfigureData(this IServiceCollection services, IConfiguration configuration) | |||
| { | |||
| services.AddDbContext<DatabaseContext>(options => | |||
| { | |||
| @@ -6,7 +6,7 @@ public static class DataConfigurationExtension | |||
| /// <summary> | |||
| /// Services configuration | |||
| /// </summary> | |||
| public static void ConfigureData(this WebApplicationBuilder builder) | |||
| public static async void ConfigureData(this WebApplicationBuilder builder) | |||
| { | |||
| IServiceCollection services = builder.Services; | |||
| services.ConfigureData(builder.Configuration); | |||
| @@ -15,8 +15,24 @@ public static class DataConfigurationExtension | |||
| /// <summary> | |||
| /// App configuration | |||
| /// </summary> | |||
| public static void ConfigureData(this WebApplication app) | |||
| public async static void ConfigureData(this WebApplication app) | |||
| { | |||
| // | |||
| using (var serviceScope = app.Services.CreateScope()) | |||
| { | |||
| var roleManager = serviceScope.ServiceProvider.GetRequiredService<RoleManager<AppRole>>(); | |||
| if (!await roleManager.RoleExistsAsync("SuperAdmin")) | |||
| { | |||
| var managerRole = new AppRole("SuperAdmin"); | |||
| await roleManager.CreateAsync(managerRole); | |||
| } | |||
| if (!await roleManager.RoleExistsAsync("Admin")) | |||
| { | |||
| var managerRole = new AppRole("Admin"); | |||
| await roleManager.CreateAsync(managerRole); | |||
| } | |||
| } | |||
| DataSeeder.Seed(app.Services); | |||
| } | |||
| } | |||