96 lines
5.1 KiB
C#
96 lines
5.1 KiB
C#
using System.Globalization;
|
|
using NTP.Application.Abstractions;
|
|
using NTP.Application.Access;
|
|
using NTP.Application.Common;
|
|
using NTP.Domain.Grains.Registry;
|
|
|
|
namespace NTP.Application.Grains.Registry;
|
|
|
|
public interface IRegistryAccessService
|
|
{
|
|
/// <summary>Legacy session.companyCodeNO: numeric part of the user's company code (mid(code,4,6)), null when not numeric.</summary>
|
|
long? CompanyCodeNo { get; }
|
|
bool IsBadiyaCompany { get; }
|
|
/// <summary>Company users (session.stkid eq 62) only see / touch their own company's records.</summary>
|
|
bool IsCompanyUser { get; }
|
|
RegistryScope Scope { get; }
|
|
Task<bool> HasActionAsync(int actionId, CancellationToken ct = default);
|
|
/// <summary>Throws <see cref="ForbiddenException"/> unless the user has the action (or <paramref name="orBadiya"/> and the user belongs to 327).</summary>
|
|
Task RequireActionAsync(int actionId, bool orBadiya = false, CancellationToken ct = default);
|
|
/// <summary>Adding / editing a truck, trailer or driver is decided by the permission table only (GRAINS_SCREEN_PERMISSIONS); throws Forbidden otherwise.</summary>
|
|
Task RequireAuthorityAsync(string permissionKey, CancellationToken ct = default);
|
|
/// <summary>Company users may only access records of their own company.</summary>
|
|
void EnsureCompanyAccess(long? recordTcscac);
|
|
Task<RegistryPermissionsDto> GetPermissionsAsync(CancellationToken ct = default);
|
|
}
|
|
|
|
public sealed class RegistryAccessService(
|
|
ICurrentUser user, IRegistryRepository repository, IRegistrySettingsService settings, IPermissionService permissions) : IRegistryAccessService
|
|
{
|
|
public long? CompanyCodeNo => ParseCompanyCodeNo(user.CompanyCode);
|
|
|
|
public bool IsBadiyaCompany => CompanyCodeNo == settings.BadiyaScac;
|
|
|
|
public bool IsCompanyUser => user.StkId == RegistryConstants.CompanyStakeholderId;
|
|
|
|
public RegistryScope Scope => new(IsCompanyUser, CompanyCodeNo, user.IsInRole("GOO"));
|
|
|
|
/// <summary>ColdFusion: <c>isnumeric(mid(code,4,6))</c> then <c>mid(code,4,6)*1</c>.</summary>
|
|
public static long? ParseCompanyCodeNo(string? companyCode)
|
|
{
|
|
if (string.IsNullOrEmpty(companyCode) || companyCode.Length < 4) return null;
|
|
var part = companyCode.Substring(3, Math.Min(6, companyCode.Length - 3));
|
|
return long.TryParse(part, NumberStyles.None, CultureInfo.InvariantCulture, out var n) ? n : null;
|
|
}
|
|
|
|
public Task<bool> HasActionAsync(int actionId, CancellationToken ct = default) =>
|
|
repository.HasActionAsync(user.RoleCode, actionId, ct);
|
|
|
|
public async Task RequireActionAsync(int actionId, bool orBadiya = false, CancellationToken ct = default)
|
|
{
|
|
if (orBadiya && IsBadiyaCompany) return;
|
|
if (!await HasActionAsync(actionId, ct))
|
|
throw new ForbiddenException(RegistryErrors.Forbidden, RegistryMessages.Forbidden);
|
|
}
|
|
|
|
public async Task RequireAuthorityAsync(string permissionKey, CancellationToken ct = default)
|
|
{
|
|
if (!await permissions.IsAllowedAsync(permissionKey, ct))
|
|
throw new ForbiddenException(RegistryErrors.Forbidden, RegistryMessages.Forbidden);
|
|
}
|
|
|
|
public void EnsureCompanyAccess(long? recordTcscac)
|
|
{
|
|
if (IsCompanyUser && recordTcscac != CompanyCodeNo)
|
|
throw new NotFoundException("Registry record", "-");
|
|
}
|
|
|
|
public async Task<RegistryPermissionsDto> GetPermissionsAsync(CancellationToken ct = default)
|
|
{
|
|
var badiya = IsBadiyaCompany;
|
|
return new RegistryPermissionsDto
|
|
{
|
|
IsBadiyaCompany = badiya,
|
|
IsCompanyUser = IsCompanyUser,
|
|
CompanyCodeNo = CompanyCodeNo,
|
|
// database only (GRAINS_SCREEN_PERMISSIONS): no legacy action lookup and no hard-coded company exception
|
|
CanAddTruck = await permissions.IsAllowedAsync(PermissionKeys.RegistryTrucksCreate, ct),
|
|
CanAddTrailer = await permissions.IsAllowedAsync(PermissionKeys.RegistryTrailersCreate, ct),
|
|
CanAddDriver = await permissions.IsAllowedAsync(PermissionKeys.RegistryDriversCreate, ct),
|
|
CanEditTruck = await permissions.IsAllowedAsync(PermissionKeys.RegistryTrucksEdit, ct),
|
|
CanEditTrailer = await permissions.IsAllowedAsync(PermissionKeys.RegistryTrailersEdit, ct),
|
|
// legacy: revoke truck / trailer buttons are hidden for company 327
|
|
CanRevokeTruck = !badiya && await HasActionAsync(RegistryActions.RevokeTruck, ct),
|
|
CanRevokeTrailer = !badiya && await HasActionAsync(RegistryActions.RevokeTrailer, ct),
|
|
CanRevokeDriver = await HasActionAsync(RegistryActions.RevokeDriver, ct),
|
|
CanAcceptTruck = await HasActionAsync(RegistryActions.AcceptTruck, ct),
|
|
CanAcceptTrailer = await HasActionAsync(RegistryActions.AcceptTrailer, ct),
|
|
CanAcceptDriver = await HasActionAsync(RegistryActions.AcceptDriver, ct),
|
|
CanManageDestinations = await HasActionAsync(RegistryActions.Destinations, ct),
|
|
CanExportTruckReport = user.IsInAnyRole("GTO", "GOO", "GNAC"),
|
|
CanExportDriverReport = user.IsInAnyRole("GTO", "GOO"),
|
|
CanCancelBlacklist = user.IsInAnyRole("GNSU", "GNCO", "GTO")
|
|
};
|
|
}
|
|
}
|