From 4afb3b0c5471e7022fc7efa9308d2cba52b29421 Mon Sep 17 00:00:00 2001 From: quentin Date: Sat, 13 Jul 2024 13:08:59 -0500 Subject: [PATCH] Added SavedEventAuthentication --- .../GrantNames/SavedEventGrantNames.cs | 14 ++++++ .../Interfaces/ISavedEventAuthentication.cs | 9 ++++ .../SavedEventAuthentication.cs | 43 +++++++++++++++++++ API/Controllers/SavedEventController.cs | 2 +- API/Services/SavedEventService.cs | 4 +- 5 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 API/Authentication/GrantNames/SavedEventGrantNames.cs create mode 100644 API/Authentication/Interfaces/ISavedEventAuthentication.cs create mode 100644 API/Authentication/SavedEventAuthentication.cs diff --git a/API/Authentication/GrantNames/SavedEventGrantNames.cs b/API/Authentication/GrantNames/SavedEventGrantNames.cs new file mode 100644 index 0000000..991c456 --- /dev/null +++ b/API/Authentication/GrantNames/SavedEventGrantNames.cs @@ -0,0 +1,14 @@ +namespace API.Authentication.GrantNames +{ + public static class SavedEventGrantNames + { + public const string CanGetAll = "api.savedEvent.get.all"; + public const string CanGetAny = "api.savedEvent.get.any"; + public const string CanGet = "api.savedEvent.get"; + public const string CanAdd = "api.savedEvent.add"; + public const string CanUpdateAny = "api.savedEvent.update.any"; + public const string CanUpdate = "api.savedEvent.update"; + public const string CanDeleteAny = "api.savedEvent.delete.any"; + public const string CanDelete = "api.savedEvent.delete"; + } +} diff --git a/API/Authentication/Interfaces/ISavedEventAuthentication.cs b/API/Authentication/Interfaces/ISavedEventAuthentication.cs new file mode 100644 index 0000000..4b8cf97 --- /dev/null +++ b/API/Authentication/Interfaces/ISavedEventAuthentication.cs @@ -0,0 +1,9 @@ +using API.DTO.Base; +using DAL.Models; + +namespace API.Authentication.Interfaces +{ + public interface ISavedEventAuthentication : IGenericAuthentication + { + } +} diff --git a/API/Authentication/SavedEventAuthentication.cs b/API/Authentication/SavedEventAuthentication.cs new file mode 100644 index 0000000..fe75588 --- /dev/null +++ b/API/Authentication/SavedEventAuthentication.cs @@ -0,0 +1,43 @@ +using API.Authentication.GrantNames; +using API.Authentication.Interfaces; +using API.DTO.Base; +using API.Services; +using DAL.Models; + +namespace API.Authentication +{ + public class SavedEventAuthentication : ISavedEventAuthentication + { + private readonly GrantService _grantService; + private readonly ILogger _logger; + public SavedEventAuthentication(ILogger logger, GrantService grantService) + { + _logger = logger; + _grantService = grantService; + } + + public bool canGetAll(User user) + { + return _grantService.hasGrant(user.permissionId, SavedEventGrantNames.CanGetAll); + } + public bool canGet(SavedEvent model, User user) + { + return _grantService.hasGrant(user.permissionId, SavedEventGrantNames.CanGetAny) || + _grantService.getULongValues(user.permissionId, SavedEventGrantNames.CanGet).Exists(x => x == model.id); + } + public bool canAdd(SavedEventDTO item, User user) + { + return _grantService.hasGrant(user.permissionId, SavedEventGrantNames.CanAdd); + } + public bool canUpdate(SavedEvent model, User user) + { + return _grantService.hasGrant(user.permissionId, SavedEventGrantNames.CanUpdateAny) || + _grantService.getULongValues(user.permissionId, SavedEventGrantNames.CanUpdate).Exists(x => x == model.id); + } + public bool canDelete(SavedEvent model, User user) + { + return _grantService.hasGrant(user.permissionId, SavedEventGrantNames.CanDeleteAny) || + _grantService.getULongValues(user.permissionId, SavedEventGrantNames.CanDelete).Exists(x => x == model.id); + } + } +} diff --git a/API/Controllers/SavedEventController.cs b/API/Controllers/SavedEventController.cs index d7afc47..dca3558 100644 --- a/API/Controllers/SavedEventController.cs +++ b/API/Controllers/SavedEventController.cs @@ -10,7 +10,7 @@ namespace API.Controllers { [ApiController] [Route("api/v1/[controller]")] - public class SavedEventController : CRUDBase + public class SavedEventController : CRUDBase { public SavedEventController(ILogger logger, UserService userService, SavedEventService service) : base(logger, userService, service) { diff --git a/API/Services/SavedEventService.cs b/API/Services/SavedEventService.cs index 229fa37..cfeba5c 100644 --- a/API/Services/SavedEventService.cs +++ b/API/Services/SavedEventService.cs @@ -6,10 +6,10 @@ using DAL.Models.Audits; namespace API.Services { - public class SavedEventService : ServiceBase + public class SavedEventService : ServiceBase { - public SavedEventService(ILogger logger, SASGContext context, IYesAuthentication auth) : base(logger, context, auth) + public SavedEventService(ILogger logger, SASGContext context, ISavedEventAuthentication auth) : base(logger, context, auth) { } }