The ASP.NET Core solution (mws.api/mws.application/mws.domain/ mws.infrastructure) existed only as untracked working files. Adding it to version control, plus a .gitignore for build output and local tooling directories, so the CQRS/mediator refactor plan has a real git history to branch and diff against.
48 lines
1.6 KiB
C#
48 lines
1.6 KiB
C#
using Mws.Application.Common;
|
|
|
|
namespace Mws.Application.Permissions;
|
|
|
|
public class PermissionService(IUnitOfWork uow) : IPermissionService
|
|
{
|
|
public async Task<List<MenuItemDto>> GetMenuAsync(Guid userId, CancellationToken ct = default)
|
|
{
|
|
var roleId = await uow.Users.GetRoleIdAsync(userId, ct);
|
|
var permissions = await uow.Roles.GetPermissionsAsync(roleId, ct);
|
|
|
|
return ScreenCatalog.Screens.Select(s =>
|
|
{
|
|
permissions.TryGetValue(s.Key, out var p);
|
|
return new MenuItemDto
|
|
{
|
|
Key = s.Key,
|
|
Label = s.Label,
|
|
Path = s.Path,
|
|
CanView = p?.CanView ?? false,
|
|
CanCreate = p?.CanCreate ?? false,
|
|
CanEdit = p?.CanEdit ?? false,
|
|
CanDelete = p?.CanDelete ?? false,
|
|
};
|
|
}).ToList();
|
|
}
|
|
|
|
public async Task EnsureAsync(Guid userId, string screen, PermissionAction action, CancellationToken ct = default)
|
|
{
|
|
var roleId = await uow.Users.GetRoleIdAsync(userId, ct);
|
|
var permission = await uow.Roles.GetPermissionAsync(roleId, screen, ct);
|
|
|
|
var allowed = action switch
|
|
{
|
|
PermissionAction.View => permission?.CanView ?? false,
|
|
PermissionAction.Create => permission?.CanCreate ?? false,
|
|
PermissionAction.Edit => permission?.CanEdit ?? false,
|
|
PermissionAction.Delete => permission?.CanDelete ?? false,
|
|
_ => false,
|
|
};
|
|
|
|
if (!allowed)
|
|
{
|
|
throw new ForbiddenException($"Not permitted to {action} on {screen}");
|
|
}
|
|
}
|
|
}
|