9cad9ed0fc
v2-ci / build (push) Failing after 41s
v2-ci / unit-tests (tests/Core/ZB.MOM.WW.OtOpcUa.Cluster.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.ControlPlane.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.Security.Tests) (push) Has been skipped
v2-ci / integration (tests/Server/ZB.MOM.WW.OtOpcUa.Host.IntegrationTests) (push) Has been skipped
v2-ci / integration (tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests) (push) Has been skipped
Adds <summary>/<param>/<returns>/<inheritdoc> where missing and removes project bookkeeping IDs (task/tracking refs) from shipped code comments, so the docs read cleanly and CommentChecker is quiet except for known false positives (PLC/protocol terms, event/IEqualityComparer inheritdoc). Doc/comment-only; no logic changed; solution builds clean.
74 lines
3.0 KiB
C#
74 lines
3.0 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Configuration.Services;
|
|
|
|
/// <summary>
|
|
/// EF Core implementation of <see cref="ILdapGroupRoleMappingService"/>. Enforces the
|
|
/// "exactly one of (ClusterId, IsSystemWide)" invariant at the write surface so a
|
|
/// malformed row can't land in the DB.
|
|
/// </summary>
|
|
public sealed class LdapGroupRoleMappingService(OtOpcUaConfigDbContext db) : ILdapGroupRoleMappingService
|
|
{
|
|
/// <inheritdoc />
|
|
public async Task<IReadOnlyList<LdapGroupRoleMapping>> GetByGroupsAsync(
|
|
IEnumerable<string> ldapGroups, CancellationToken cancellationToken)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(ldapGroups);
|
|
var groupSet = ldapGroups.ToList();
|
|
if (groupSet.Count == 0) return [];
|
|
|
|
return await db.LdapGroupRoleMappings
|
|
.AsNoTracking()
|
|
.Where(m => groupSet.Contains(m.LdapGroup))
|
|
.ToListAsync(cancellationToken)
|
|
.ConfigureAwait(false);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IReadOnlyList<LdapGroupRoleMapping>> ListAllAsync(CancellationToken cancellationToken)
|
|
=> await db.LdapGroupRoleMappings
|
|
.AsNoTracking()
|
|
.OrderBy(m => m.LdapGroup)
|
|
.ThenBy(m => m.ClusterId)
|
|
.ToListAsync(cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
/// <inheritdoc />
|
|
public async Task<LdapGroupRoleMapping> CreateAsync(LdapGroupRoleMapping row, CancellationToken cancellationToken)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(row);
|
|
ValidateInvariants(row);
|
|
|
|
if (row.Id == Guid.Empty) row.Id = Guid.NewGuid();
|
|
if (row.CreatedAtUtc == default) row.CreatedAtUtc = DateTime.UtcNow;
|
|
|
|
db.LdapGroupRoleMappings.Add(row);
|
|
await db.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
|
return row;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken)
|
|
{
|
|
var existing = await db.LdapGroupRoleMappings.FindAsync([id], cancellationToken).ConfigureAwait(false);
|
|
if (existing is null) return;
|
|
db.LdapGroupRoleMappings.Remove(existing);
|
|
await db.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
|
}
|
|
|
|
private static void ValidateInvariants(LdapGroupRoleMapping row)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(row.LdapGroup))
|
|
throw new InvalidLdapGroupRoleMappingException("LdapGroup must not be empty.");
|
|
|
|
if (row.IsSystemWide && !string.IsNullOrEmpty(row.ClusterId))
|
|
throw new InvalidLdapGroupRoleMappingException(
|
|
"IsSystemWide=true requires ClusterId to be null. A fleet-wide grant cannot also be cluster-scoped.");
|
|
|
|
if (!row.IsSystemWide && string.IsNullOrEmpty(row.ClusterId))
|
|
throw new InvalidLdapGroupRoleMappingException(
|
|
"IsSystemWide=false requires a populated ClusterId. A cluster-scoped grant needs its target cluster.");
|
|
}
|
|
}
|