From d5a7ffc59647878b0dc16c8a357f0ec1b7c21745 Mon Sep 17 00:00:00 2001 From: quentin Date: Sat, 13 Jul 2024 12:51:45 -0500 Subject: [PATCH] Added ImageAuthentication --- .../GrantNames/ImageGrantNames.cs | 14 ++++++ API/Authentication/ImageAuthentication.cs | 43 +++++++++++++++++++ .../Interfaces/IImageAuthentication.cs | 9 ++++ API/Controllers/ImageController.cs | 2 +- API/Services/ImageService.cs | 4 +- 5 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 API/Authentication/GrantNames/ImageGrantNames.cs create mode 100644 API/Authentication/ImageAuthentication.cs create mode 100644 API/Authentication/Interfaces/IImageAuthentication.cs diff --git a/API/Authentication/GrantNames/ImageGrantNames.cs b/API/Authentication/GrantNames/ImageGrantNames.cs new file mode 100644 index 0000000..c127acb --- /dev/null +++ b/API/Authentication/GrantNames/ImageGrantNames.cs @@ -0,0 +1,14 @@ +namespace API.Authentication.GrantNames +{ + public static class ImageGrantNames + { + public const string CanGetAll = "api.image.get.all"; + public const string CanGetAny = "api.image.get.any"; + public const string CanGet = "api.image.get"; + public const string CanAdd = "api.image.add"; + public const string CanUpdateAny = "api.image.update.any"; + public const string CanUpdate = "api.image.update"; + public const string CanDeleteAny = "api.image.delete.any"; + public const string CanDelete = "api.image.delete"; + } +} diff --git a/API/Authentication/ImageAuthentication.cs b/API/Authentication/ImageAuthentication.cs new file mode 100644 index 0000000..9b23f52 --- /dev/null +++ b/API/Authentication/ImageAuthentication.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 ImageAuthentication : IImageAuthentication + { + private readonly GrantService _grantService; + private readonly ILogger _logger; + public ImageAuthentication(ILogger logger, GrantService grantService) + { + _logger = logger; + _grantService = grantService; + } + + public bool canGetAll(User user) + { + return _grantService.hasGrant(user.permissionId, ImageGrantNames.CanGetAll); + } + public bool canGet(Image model, User user) + { + return _grantService.hasGrant(user.permissionId, ImageGrantNames.CanGetAny) || + _grantService.getULongValues(user.permissionId, ImageGrantNames.CanGet).Exists(x => x == model.id); + } + public bool canAdd(ImageDTO item, User user) + { + return _grantService.hasGrant(user.permissionId, ImageGrantNames.CanAdd); + } + public bool canUpdate(Image model, User user) + { + return _grantService.hasGrant(user.permissionId, ImageGrantNames.CanUpdateAny) || + _grantService.getULongValues(user.permissionId, ImageGrantNames.CanUpdate).Exists(x => x == model.id); + } + public bool canDelete(Image model, User user) + { + return _grantService.hasGrant(user.permissionId, ImageGrantNames.CanDeleteAny) || + _grantService.getULongValues(user.permissionId, ImageGrantNames.CanDelete).Exists(x => x == model.id); + } + } +} diff --git a/API/Authentication/Interfaces/IImageAuthentication.cs b/API/Authentication/Interfaces/IImageAuthentication.cs new file mode 100644 index 0000000..267f805 --- /dev/null +++ b/API/Authentication/Interfaces/IImageAuthentication.cs @@ -0,0 +1,9 @@ +using API.DTO.Base; +using DAL.Models; + +namespace API.Authentication.Interfaces +{ + public interface IImageAuthentication : IGenericAuthentication + { + } +} diff --git a/API/Controllers/ImageController.cs b/API/Controllers/ImageController.cs index 6ad31d2..4b6bfe1 100644 --- a/API/Controllers/ImageController.cs +++ b/API/Controllers/ImageController.cs @@ -10,7 +10,7 @@ namespace API.Controllers { [ApiController] [Route("api/v1/[controller]")] - public class ImageController : CRUDBase + public class ImageController : CRUDBase { public ImageController(ILogger logger, UserService userService, ImageService service) : base(logger, userService, service) { diff --git a/API/Services/ImageService.cs b/API/Services/ImageService.cs index 737af2f..291cab5 100644 --- a/API/Services/ImageService.cs +++ b/API/Services/ImageService.cs @@ -6,9 +6,9 @@ using DAL.Models.Audits; namespace API.Services { - public class ImageService : ServiceBase + public class ImageService : ServiceBase { - public ImageService(ILogger logger, SASGContext context, IYesAuthentication auth) : base(logger, context, auth) + public ImageService(ILogger logger, SASGContext context, IImageAuthentication auth) : base(logger, context, auth) { } }