feat(api): аутентификация и авторизация. Кастомные ParrAuthenticationHandler и BaseSimpleRoleProvider.

This commit is contained in:
Mikhail Trubnikov
2023-12-01 16:41:37 +10:00
parent 78f2310199
commit d0c309e642
23 changed files with 395 additions and 11 deletions

View File

@@ -0,0 +1,48 @@
using PARR.API.Services.Interfaces;
namespace PARR.API.RoleProvider
{
public class BaseSimpleRoleProvider : ISimpleRoleProvider
{
private readonly IServiceProvider serviceProvider;
public BaseSimpleRoleProvider(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}
public async Task<ICollection<string>> GetUserRolesAsync(string ipClient)
{
// Авторизация - проверка на разрешения
// В данном случае, получаем все разрешения (роли)
ICollection<string> defaultResult = new string[0];
using (var scope = serviceProvider.CreateScope())
{
//var clientService = scope.ServiceProvider.GetRequiredService<IClientService>();
//var ipClient = clientService.GetClientIp()?.ToString();
//if (string.IsNullOrEmpty(ipClient))
// return await Task.FromResult(defaultResult);
var authService = scope.ServiceProvider.GetRequiredService<IAuthService>();
// проверяем блокировку пользователя
var userIsBlocked = await authService.UserIsBlockedAsync(ipClient);
if (userIsBlocked)
return await Task.FromResult(defaultResult);
// получаем пользователя из кэша или бд
var user = await authService.GetUserAsync(ipClient);
if (user == null)
return await Task.FromResult(defaultResult);
return user.Roles.Select(t => t.Name).ToList();
}
// return await Task.FromResult(defaultResult);
}
}
}