Added GrantAuthentication

This commit is contained in:
quentin 2024-07-12 23:29:28 -05:00
parent 7ae98f4bb4
commit 71cabbd548
5 changed files with 72 additions and 3 deletions

View File

@ -0,0 +1,46 @@
using API.Authentication.GrantNames;
using API.Authentication.Interfaces;
using API.DTO.Base;
using API.Services;
using DAL.Models;
namespace API.Authentication
{
public class GrantAuthentication : IGrantAuthentication
{
private readonly GrantService _grantService;
private readonly ILogger<GrantAuthentication> _logger;
public GrantAuthentication(GrantService grantService, ILogger<GrantAuthentication> logger)
{
_grantService = grantService;
_logger = logger;
}
public bool canGetAll(User user)
{
return _grantService.hasGrant(user.permissionId, GrantGrantNames.CanGetAll);
}
public bool canGet(Grant model, User user)
{
return _grantService.hasGrant(user.permissionId, GrantGrantNames.CanGetAny) ||
_grantService.getULongValues(user.permissionId, GrantGrantNames.CanGet).Exists(x => x == model.id);
}
public bool canAdd(GrantDTO item, User user)
{
return _grantService.hasGrant(user.permissionId, GrantGrantNames.CanAdd) &&
_grantService.hasGrant(user.permissionId, item.name);
}
public bool canUpdate(Grant model, User user)
{
// Doesn't make sense to update the name of a grant. The updater can just delete and remake.
return false;
}
public bool canDelete(Grant model, User user)
{
return (_grantService.hasGrant(user.permissionId, GrantGrantNames.CanDeleteAny) ||
_grantService.getULongValues(user.permissionId, GrantGrantNames.CanDelete).Exists(x => x == model.id))
&& _grantService.hasGrant(user.permissionId, model.name);
}
}
}

View File

@ -0,0 +1,14 @@
namespace API.Authentication.GrantNames
{
public static class GrantGrantNames
{
public const string CanGetAll = "api.grant.get.all";
public const string CanGetAny = "api.grant.get.any";
public const string CanGet = "api.grant.get";
public const string CanAdd = "api.grant.add";
public const string CanUpdateAny = "api.grant.update.any";
public const string CanUpdate = "api.grant.update";
public const string CanDeleteAny = "api.grant.delete.any";
public const string CanDelete = "api.grant.delete";
}
}

View File

@ -0,0 +1,9 @@
using API.DTO.Base;
using DAL.Models;
namespace API.Authentication.Interfaces
{
public interface IGrantAuthentication : IGenericAuthentication<GrantDTO, Grant>
{
}
}

View File

@ -10,7 +10,7 @@ namespace API.Controllers
{
[ApiController]
[Route("api/v1/[controller]")]
public class GrantController : CRUDBase<GrantController, GrantDTO, GrantUpdateDTO, Grant, AuditGrant, IYesAuthentication, GrantService>
public class GrantController : CRUDBase<GrantController, GrantDTO, GrantUpdateDTO, Grant, AuditGrant, IGrantAuthentication, GrantService>
{
public GrantController(ILogger<GrantController> logger, UserService userService, GrantService service) : base(logger, userService, service)
{

View File

@ -6,9 +6,9 @@ using DAL.Models.Audits;
namespace API.Services
{
public class GrantService : ServiceBase<GrantService, GrantDTO, Grant, AuditGrant, IYesAuthentication>
public class GrantService : ServiceBase<GrantService, GrantDTO, Grant, AuditGrant, IGrantAuthentication>
{
public GrantService(ILogger<GrantService> logger, SASGContext context, IYesAuthentication auth) : base(logger, context, auth)
public GrantService(ILogger<GrantService> logger, SASGContext context, IGrantAuthentication auth) : base(logger, context, auth)
{
}