Added roles (may or may not be used)
This commit is contained in:
26
dotnet/AipsCore/Domain/Models/User/External/UserRole.cs
vendored
Normal file
26
dotnet/AipsCore/Domain/Models/User/External/UserRole.cs
vendored
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
using AipsCore.Domain.Common.Validation;
|
||||||
|
using AipsCore.Domain.Models.User.Validation;
|
||||||
|
|
||||||
|
namespace AipsCore.Domain.Models.User.External;
|
||||||
|
|
||||||
|
public record UserRole
|
||||||
|
{
|
||||||
|
public string Name { get; init; }
|
||||||
|
|
||||||
|
private UserRole(string Name)
|
||||||
|
{
|
||||||
|
this.Name = Name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UserRole User => new("User");
|
||||||
|
public static UserRole Admin => new("Admin");
|
||||||
|
|
||||||
|
public static IEnumerable<UserRole> All() => [User, Admin];
|
||||||
|
|
||||||
|
public static UserRole FromString(string name)
|
||||||
|
{
|
||||||
|
var role = All().FirstOrDefault(r => r.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
|
||||||
|
|
||||||
|
return role ?? throw new ValidationException(UserErrors.RoleDoesNotExist(name));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using AipsCore.Domain.Abstract.Validation;
|
using AipsCore.Domain.Abstract.Validation;
|
||||||
using AipsCore.Domain.Common.Validation;
|
using AipsCore.Domain.Common.Validation;
|
||||||
|
using AipsCore.Domain.Models.User.External;
|
||||||
using AipsCore.Domain.Models.User.ValueObjects;
|
using AipsCore.Domain.Models.User.ValueObjects;
|
||||||
|
|
||||||
namespace AipsCore.Domain.Models.User.Validation;
|
namespace AipsCore.Domain.Models.User.Validation;
|
||||||
@@ -21,4 +22,12 @@ public class UserErrors : AbstractErrors<User, UserId>
|
|||||||
|
|
||||||
return CreateValidationError(code, message);
|
return CreateValidationError(code, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ValidationError RoleDoesNotExist(string name)
|
||||||
|
{
|
||||||
|
string code = "user_role_does_not_exist";
|
||||||
|
string message = $"Role '{name}' does not exist";
|
||||||
|
|
||||||
|
return CreateValidationError(code, message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
using AipsCore.Domain.Models.User.External;
|
||||||
|
using Microsoft.AspNetCore.Identity;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace AipsCore.Infrastructure.Persistence.Db;
|
||||||
|
|
||||||
|
public static class DbInitializer
|
||||||
|
{
|
||||||
|
public static async Task SeedRolesAsync(IServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
using var scope = serviceProvider.CreateScope();
|
||||||
|
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole<Guid>>>();
|
||||||
|
|
||||||
|
var roleNames = UserRole.All();
|
||||||
|
|
||||||
|
foreach (var roleName in roleNames)
|
||||||
|
{
|
||||||
|
var roleExist = await roleManager.RoleExistsAsync(roleName.Name);
|
||||||
|
if (!roleExist)
|
||||||
|
{
|
||||||
|
await roleManager.CreateAsync(new IdentityRole<Guid>
|
||||||
|
{
|
||||||
|
Name = roleName.Name,
|
||||||
|
NormalizedName = roleName.Name.ToUpperInvariant()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -65,8 +65,7 @@ public class UserRepository : AbstractRepository<Domain.Models.User.User, UserId
|
|||||||
throw new Exception($"User registration failed: {errors}");
|
throw new Exception($"User registration failed: {errors}");
|
||||||
}
|
}
|
||||||
|
|
||||||
//Realno nebitno, ali postoji infrastruktura ako treba da se koristi kasnije
|
await _userManager.AddToRoleAsync(entity, UserRole.User.Name);
|
||||||
//await _userManager.AddToRoleAsync(entity, "User");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<LoginResult> LoginWithEmailAndPasswordAsync(string email, string password, CancellationToken cancellationToken = default)
|
public async Task<LoginResult> LoginWithEmailAndPasswordAsync(string email, string password, CancellationToken cancellationToken = default)
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using AipsCore.Infrastructure.DI;
|
using AipsCore.Infrastructure.DI;
|
||||||
|
using AipsCore.Infrastructure.Persistence.Db;
|
||||||
using AipsWebApi.Middleware;
|
using AipsWebApi.Middleware;
|
||||||
using DotNetEnv;
|
using DotNetEnv;
|
||||||
|
|
||||||
@@ -15,13 +16,17 @@ builder.Services.AddAips(builder.Configuration);
|
|||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
using (var scope = app.Services.CreateScope())
|
||||||
|
{
|
||||||
|
await DbInitializer.SeedRolesAsync(scope.ServiceProvider);
|
||||||
|
}
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
if (app.Environment.IsDevelopment())
|
if (app.Environment.IsDevelopment())
|
||||||
{
|
{
|
||||||
app.MapOpenApi();
|
app.MapOpenApi();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
app.UseMiddleware<ExceptionHandlingMiddleware>();
|
app.UseMiddleware<ExceptionHandlingMiddleware>();
|
||||||
|
|
||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
|
|||||||
Reference in New Issue
Block a user