feat(AuthController): aktualisiert, um zu prüfen, ob der eingeloggte Benutzer in der erlaubten Gruppe in appsettings ist.

This commit is contained in:
Developer 02 2024-10-31 14:44:39 +01:00
parent 1be71a42e5
commit 671500b3a5
10 changed files with 21 additions and 59 deletions

View File

@ -60,15 +60,15 @@ export interface GroupOfUser {
export interface UserRep {
id?: number,
repUserId?: number,
userId: number,
repGroupId?: number,
groupId?: number,
addedWho: string,
repUserId?: number,
repUser?: User
user?: User,
repGroup?: Group,
group?: Group,
repUser?: User
}
export interface DirGroup {

View File

@ -214,6 +214,10 @@ export const env = {
value: "yyyy-MM-dd",
name: "yyyy-MM-dd",
}
],
languages: [
{ value: "de-DE", name: "de-DE" },
{ value: "en-US", name: "en-US" }
]
}
};

View File

@ -21,14 +21,15 @@ namespace DigitalData.UserManager.API.Controllers
private readonly IDirectorySearchService _dirSearchService;
private readonly IStringLocalizer<Resource> _localizer;
private readonly ILogger<AuthController> _logger;
public AuthController(IUserService userService, IGroupOfUserService gouService, IDirectorySearchService directorySearchService, IStringLocalizer<Resource> localizer, ILogger<AuthController> logger)
private readonly IConfiguration _config;
public AuthController(IUserService userService, IGroupOfUserService gouService, IDirectorySearchService directorySearchService, IStringLocalizer<Resource> localizer, ILogger<AuthController> logger, IConfiguration configuration)
{
_userService = userService;
_gouService = gouService;
_dirSearchService = directorySearchService;
_localizer = localizer;
_logger = logger;
_config = configuration;
}
[AllowAnonymous]
@ -57,7 +58,10 @@ namespace DigitalData.UserManager.API.Controllers
if (!isValid)
return Unauthorized(Result.Fail().Message(_localizer[Key.UserNotFound]));
var gouMsg = await _gouService.HasGroup(login.Username, "PM_USER", caseSensitive: false);
var allowedGroupName = _config.GetSection("AllowedGroupName").Get<string>()
?? throw new InvalidOperationException("Allowed group names configuration is missing.");
var gouMsg = await _gouService.HasGroup(login.Username, allowedGroupName, caseSensitive: false);
if (!gouMsg.IsSuccess)
return Unauthorized(Result.Fail().Message(_localizer[Key.UnauthorizedUser]));

View File

@ -1,27 +0,0 @@
using DigitalData.UserManager.API.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
namespace DigitalData.UserManager.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ConstantsController : ControllerBase
{
private readonly Constants _constants;
public ConstantsController(IOptions<Constants> constantsOptions)
{
_constants = constantsOptions.Value;
}
[HttpGet]
public IActionResult GetConstant(string? name = null)
{
if(name is null)
return Ok(_constants);
return Ok(_constants[name]);
}
}
}

View File

@ -61,6 +61,7 @@
<ItemGroup>
<Folder Include="ClientApp\" />
<Folder Include="Models\" />
</ItemGroup>
<ItemGroup>

View File

@ -1,13 +0,0 @@
using System.Reflection;
namespace DigitalData.UserManager.API.Models
{
public class Constants
{
public IEnumerable<string> UserLanguages { get; init; } = Array.Empty<string>();
public object? this[string propertyName] => GetType()
.GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance)?
.GetValue(this, null);
}
}

View File

@ -9,8 +9,6 @@ using DigitalData.Core.API;
using DigitalData.UserManager.API.Controllers;
using DigitalData.UserManager.Application.Services;
using Microsoft.Data.SqlClient;
using System.Reflection.Metadata;
using DigitalData.UserManager.API.Models;
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
logger.Debug("init main");
@ -80,8 +78,6 @@ try {
builder.Services.AddCookieBasedLocalizer();
builder.ConfigureBySection<Constants>();
var app = builder.Build();
cnn_str = new(() =>

View File

@ -71,13 +71,8 @@
"Key": "JGPwHVD0BQmC7upi5OV11PzzIk47ugTJoqBV/et5w40=",
"IV": "gMuetIjlPvJnSzu+i7I3xg=="
},
"AllowedGroupName": "UM_ADMINS",
// Delete below in production
"UseEncryptor": true,
"UseSwagger": true,
"Constants": {
"UserLanguages": [
"de-DE",
"en-US"
]
}
"UseSwagger": true
}

View File

@ -2,5 +2,6 @@
namespace DigitalData.UserManager.Application.DTOs.Base
{
// TODO: use getter - setter methods for a simple inheritance.
public record BaseReadDto(int Id, string? AddedWho, DateTime? AddedWhen, string? ChangedWho, DateTime? ChangedWhen) : BaseDTO<int>(Id);
}

View File

@ -4,6 +4,7 @@ using DigitalData.EmailProfilerDispatcher.Abstraction.Attributes;
namespace DigitalData.UserManager.Application.DTOs.User
{
//TODO: inherit from base read dto
public record UserReadDto()
{
public int Id { get; set; }