Use roles to sync Libraries and Age Rating for simplicty

This commit is contained in:
Amelia 2025-06-30 19:08:49 +02:00
parent 9f94abe1be
commit 54fb4c7a8a
No known key found for this signature in database
GPG key ID: D6D0ECE365407EAA
3 changed files with 11 additions and 9 deletions

View file

@ -53,9 +53,9 @@ public static class StringExtensions
return string.IsNullOrEmpty(value) ? defaultValue : double.Parse(value, CultureInfo.InvariantCulture);
}
public static string? TrimPrefix(this string? value, string prefix)
public static string TrimPrefix(this string? value, string prefix)
{
if (string.IsNullOrEmpty(value)) return value;
if (string.IsNullOrEmpty(value)) return string.Empty;
if (!value.StartsWith(prefix)) return value;

View file

@ -44,8 +44,8 @@ public interface IOidcService
public class OidcService(ILogger<OidcService> logger, UserManager<AppUser> userManager,
IUnitOfWork unitOfWork, IMapper mapper, IAccountService accountService): IOidcService
{
private const string LibraryAccessClaim = "library";
private const string AgeRatingClaim = "AgeRating";
private const string LibraryAccessPrefix = "library-";
private const string AgeRatingPrefix = "age-rating-";
public async Task<AppUser?> LoginOrCreate(ClaimsPrincipal principal)
{
@ -175,8 +175,9 @@ public class OidcService(ILogger<OidcService> logger, UserManager<AppUser> userM
private async Task SyncLibraries(ClaimsPrincipal claimsPrincipal, AppUser user)
{
var libraryAccess = claimsPrincipal
.FindAll(LibraryAccessClaim)
.Select(r => r.Value)
.FindAll(ClaimTypes.Role)
.Where(r => r.Value.StartsWith(LibraryAccessPrefix))
.Select(r => r.Value.TrimPrefix(LibraryAccessPrefix))
.ToList();
if (libraryAccess.Count == 0) return;
@ -191,8 +192,9 @@ public class OidcService(ILogger<OidcService> logger, UserManager<AppUser> userM
{
var ageRatings = claimsPrincipal
.FindAll(AgeRatingClaim)
.Select(r => r.Value)
.FindAll(ClaimTypes.Role)
.Where(r => r.Value.StartsWith(AgeRatingPrefix))
.Select(r => r.Value.TrimPrefix(AgeRatingPrefix))
.ToList();
if (ageRatings.Count == 0) return;

View file

@ -373,7 +373,7 @@ public class SettingsService : ISettingsService
}
catch (Exception e)
{
_logger.LogError(e, "OpenIdConfiguration failed: {Reason}", e.Message);
_logger.LogTrace(e, "OpenIdConfiguration failed: {Reason}", e.Message);
return false;
}
}