mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-10-04 11:22:09 +00:00
Derive staff vs technician scope from claims (Assigned for User), map DbUpdateConcurrencyException to a stable 409, and add SQLite competing-write tests for categorize-vs-categorize and categorize-vs-delete.
82 lines
2.5 KiB
C#
82 lines
2.5 KiB
C#
using System.Security.Claims;
|
|
using Data.SeaHavenIndustries;
|
|
using SeaHaven.Services.Exceptions;
|
|
|
|
namespace SeaHaven.Services.Helpers
|
|
{
|
|
/// <summary>
|
|
/// Claims-derived authorization for work-order media mutations.
|
|
/// True multi-tenant CustomerId/TenantId is not modeled on WorkOrder/JWT;
|
|
/// scope is role + Assigned (<see cref="WorkOrder.AssignTo"/>) for Technician.
|
|
/// </summary>
|
|
public static class WorkOrderMediaAuthorization
|
|
{
|
|
private static readonly string[] StaffRoles =
|
|
{
|
|
"Admin",
|
|
"Manager",
|
|
"Dispatcher",
|
|
"Supervisor"
|
|
};
|
|
|
|
public static void EnsureCanMutate(ClaimsPrincipal user, string? actorId)
|
|
{
|
|
EnsureAuthenticated(user, actorId);
|
|
|
|
if (IsStaff(user) || user.IsInRole("User"))
|
|
return;
|
|
|
|
throw Forbidden();
|
|
}
|
|
|
|
public static void EnsureCanDelete(ClaimsPrincipal user, string? actorId)
|
|
{
|
|
EnsureAuthenticated(user, actorId);
|
|
|
|
// Technician (User) may upload/categorize assigned media but not delete.
|
|
if (IsStaff(user))
|
|
return;
|
|
|
|
throw Forbidden();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Staff: any in-scope (non-deleted/non-template) work order.
|
|
/// Technician: only work orders assigned to the caller. Out-of-scope → NotFound (no disclosure).
|
|
/// </summary>
|
|
public static void EnsureWorkOrderInCallerScope(
|
|
ClaimsPrincipal user,
|
|
string actorId,
|
|
WorkOrder workOrder)
|
|
{
|
|
if (IsStaff(user))
|
|
return;
|
|
|
|
if (user.IsInRole("User")
|
|
&& string.Equals(workOrder.AssignTo, actorId, StringComparison.Ordinal))
|
|
{
|
|
return;
|
|
}
|
|
|
|
throw new WorkOrderBoardValidationException("NotFound", "Work order not found.");
|
|
}
|
|
|
|
private static void EnsureAuthenticated(ClaimsPrincipal user, string? actorId)
|
|
{
|
|
if (user is null
|
|
|| !(user.Identity?.IsAuthenticated ?? false)
|
|
|| string.IsNullOrWhiteSpace(actorId))
|
|
{
|
|
throw Forbidden();
|
|
}
|
|
}
|
|
|
|
private static bool IsStaff(ClaimsPrincipal user)
|
|
=> StaffRoles.Any(user.IsInRole);
|
|
|
|
private static WorkOrderBoardValidationException Forbidden()
|
|
=> new(
|
|
"Forbidden",
|
|
"You are not allowed to mutate work order media.");
|
|
}
|
|
}
|