diff --git a/src/LocalTest.csproj b/src/LocalTest.csproj index e7537a6a..0932a0e4 100644 --- a/src/LocalTest.csproj +++ b/src/LocalTest.csproj @@ -4,6 +4,9 @@ net6.0 56f36ce2-b44b-415e-a8a5-f399a76e35b9 enable + + + $(DefineConstants);LOCALTEST diff --git a/src/Notifications/API/Controllers/EmailNotificationOrdersController.cs b/src/Notifications/API/Controllers/EmailNotificationOrdersController.cs index 88fe4cd5..aa055931 100644 --- a/src/Notifications/API/Controllers/EmailNotificationOrdersController.cs +++ b/src/Notifications/API/Controllers/EmailNotificationOrdersController.cs @@ -1,4 +1,8 @@ -using Altinn.Notifications.Core.Models; +#nullable enable +#if !LOCALTEST +using Altinn.Notifications.Configuration; +#endif +using Altinn.Notifications.Core.Models; using Altinn.Notifications.Core.Models.Orders; using Altinn.Notifications.Core.Services.Interfaces; using Altinn.Notifications.Extensions; @@ -8,8 +12,16 @@ using FluentValidation; +#if !LOCALTEST +using Microsoft.AspNetCore.Authorization; +#endif using Microsoft.AspNetCore.Mvc; +#if !LOCALTEST +using Swashbuckle.AspNetCore.Annotations; +using Swashbuckle.AspNetCore.Filters; +#endif + namespace Altinn.Notifications.Controllers; /// @@ -17,6 +29,11 @@ namespace Altinn.Notifications.Controllers; /// [Route("notifications/api/v1/orders/email")] [ApiController] +#if !LOCALTEST +[Authorize(Policy = AuthorizationConstants.POLICY_CREATE_SCOPE_OR_PLATFORM_ACCESS)] +[SwaggerResponse(401, "Caller is unauthorized")] +[SwaggerResponse(403, "Caller is not authorized to access the requested resource")] +# endif public class EmailNotificationOrdersController : ControllerBase { private readonly IValidator _validator; @@ -42,6 +59,11 @@ public EmailNotificationOrdersController(IValidator> Post(EmailNotificationOrderRequestExt emailNotificationOrderRequest) { var validationResult = _validator.Validate(emailNotificationOrderRequest); @@ -51,10 +73,19 @@ public async Task> Post(EmailNotificationOrderRequestEx return ValidationProblem(ModelState); } +#if LOCALTEST string creator = "localtest"; +#else + string? creator = HttpContext.GetOrg(); + + if (creator == null) + { + return Forbid(); + } +#endif var orderRequest = emailNotificationOrderRequest.MapToOrderRequest(creator); - (NotificationOrder registeredOrder, ServiceError error) = await _orderService.RegisterEmailNotificationOrder(orderRequest); + (NotificationOrder? registeredOrder, ServiceError? error) = await _orderService.RegisterEmailNotificationOrder(orderRequest); if (error != null) { diff --git a/src/Notifications/API/Extensions/HttpContextExtensions.cs b/src/Notifications/API/Extensions/HttpContextExtensions.cs deleted file mode 100644 index eb8e8386..00000000 --- a/src/Notifications/API/Extensions/HttpContextExtensions.cs +++ /dev/null @@ -1,19 +0,0 @@ -#nullable enable -namespace Altinn.Notifications.Extensions; - -/// -/// Extensions for HTTP Context -/// -public static class HttpContextExtensions -{ - /// - /// Get the org string from the context items or null if it is not defined - /// - /// - /// The org item is populated to the http context by the - /// - public static string? GetOrg(this HttpContext context) - { - return context.Items["Org"] as string; - } -} diff --git a/src/Notifications/Core/Configuration/NotificationOrderConfig.cs b/src/Notifications/Core/Configuration/NotificationOrderConfig.cs new file mode 100644 index 00000000..6ce8b285 --- /dev/null +++ b/src/Notifications/Core/Configuration/NotificationOrderConfig.cs @@ -0,0 +1,12 @@ +namespace Altinn.Notifications.Core.Configuration; + +/// +/// Configuration class for notification orders +/// +public class NotificationOrderConfig +{ + /// + /// Default from address for email notifications + /// + public string DefaultEmailFromAddress { get; set; } = string.Empty; +} diff --git a/src/Notifications/Core/Models/Notification/SendOperationResult.cs b/src/Notifications/Core/Models/Notification/SendOperationResult.cs index 57e3dfce..2031dcaa 100644 --- a/src/Notifications/Core/Models/Notification/SendOperationResult.cs +++ b/src/Notifications/Core/Models/Notification/SendOperationResult.cs @@ -1,8 +1,8 @@ #nullable enable using System.Text.Json; using System.Text.Json.Serialization; - using Altinn.Notifications.Core.Enums; +using Altinn.Notifications.Core.Models.Orders; namespace Altinn.Notifications.Core.Models.Notification; diff --git a/src/Notifications/Core/Services/EmailNotificationOrderService.cs b/src/Notifications/Core/Services/EmailNotificationOrderService.cs index 849a97b9..0535e4a0 100644 --- a/src/Notifications/Core/Services/EmailNotificationOrderService.cs +++ b/src/Notifications/Core/Services/EmailNotificationOrderService.cs @@ -1,10 +1,13 @@ #nullable enable +using Altinn.Notifications.Core.Configuration; using Altinn.Notifications.Core.Models; using Altinn.Notifications.Core.Models.NotificationTemplate; using Altinn.Notifications.Core.Models.Orders; using Altinn.Notifications.Core.Repository.Interfaces; using Altinn.Notifications.Core.Services.Interfaces; +using Microsoft.Extensions.Options; + namespace Altinn.Notifications.Core.Services; /// @@ -20,12 +23,12 @@ public class EmailNotificationOrderService : IEmailNotificationOrderService /// /// Initializes a new instance of the class. /// - public EmailNotificationOrderService(IOrderRepository repository, IGuidService guid, IDateTimeService dateTime) + public EmailNotificationOrderService(IOrderRepository repository, IGuidService guid, IDateTimeService dateTime, IOptions config) { _repository = repository; _guid = guid; _dateTime = dateTime; - _defaultFromAddress = "localtest@altinn.no"; + _defaultFromAddress = config.Value.DefaultEmailFromAddress; } /// diff --git a/src/Notifications/Persistence/Repository/OrderRepository.cs b/src/Notifications/LocalTestNotifications/LocalOrderRepository.cs similarity index 87% rename from src/Notifications/Persistence/Repository/OrderRepository.cs rename to src/Notifications/LocalTestNotifications/LocalOrderRepository.cs index 2f64ae9d..4b3cab55 100644 --- a/src/Notifications/Persistence/Repository/OrderRepository.cs +++ b/src/Notifications/LocalTestNotifications/LocalOrderRepository.cs @@ -11,12 +11,12 @@ namespace LocalTest.Notifications.Persistence.Repository { - public class OrderRepository : IOrderRepository + public class LocalOrderRepository : IOrderRepository { private readonly LocalPlatformSettings _localPlatformSettings; private readonly JsonSerializerOptions _serializerOptions; - public OrderRepository( + public LocalOrderRepository( IOptions localPlatformSettings) { _localPlatformSettings = localPlatformSettings.Value; @@ -35,13 +35,13 @@ public Task Create(NotificationOrder order) string serializedOrder = JsonSerializer.Serialize(order, _serializerOptions); FileInfo file = new FileInfo(path); - file.Directory.Create(); + file.Directory?.Create(); File.WriteAllText(file.FullName, serializedOrder); return Task.FromResult(order); } - public Task GetOrderById(Guid id, string creator) + public Task GetOrderById(Guid id, string creator) { throw new NotImplementedException(); } @@ -51,7 +51,7 @@ public Task> GetOrdersBySendersReference(string sendersR throw new NotImplementedException(); } - public Task GetOrderWithStatusById(Guid id, string creator) + public Task GetOrderWithStatusById(Guid id, string creator) { throw new NotImplementedException(); } diff --git a/src/Notifications/LocalTestNotifications/NotificationsServiceExtentions.cs b/src/Notifications/LocalTestNotifications/NotificationsServiceExtentions.cs new file mode 100644 index 00000000..f9f101e5 --- /dev/null +++ b/src/Notifications/LocalTestNotifications/NotificationsServiceExtentions.cs @@ -0,0 +1,30 @@ +using Altinn.Notifications.Core.Configuration; +using Altinn.Notifications.Core.Repository.Interfaces; +using Altinn.Notifications.Core.Services; +using Altinn.Notifications.Core.Services.Interfaces; +using Altinn.Notifications.Extensions; +using Altinn.Notifications.Models; +using Altinn.Notifications.Validators; +using FluentValidation; +using LocalTest.Notifications.Persistence.Repository; + +namespace LocalTest.Notifications.LocalTestNotifications; + +public static class NotificationsServiceExtentions +{ + public static void AddNotificationServices(this IServiceCollection services, string baseUrl) + { + // Notifications services + ValidatorOptions.Global.LanguageManager.Enabled = false; + ResourceLinkExtensions.Initialize(baseUrl); + + services.Configure((c) => c.DefaultEmailFromAddress = "localtest@altinn.no"); + + services + .AddSingleton, EmailNotificationOrderRequestValidator>() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton(); + } +} \ No newline at end of file diff --git a/src/Startup.cs b/src/Startup.cs index fe1db556..ef45057f 100644 --- a/src/Startup.cs +++ b/src/Startup.cs @@ -6,12 +6,6 @@ using Altinn.Common.PEP.Clients; using Altinn.Common.PEP.Implementation; using Altinn.Common.PEP.Interfaces; -using Altinn.Notifications.Core.Repository.Interfaces; -using Altinn.Notifications.Core.Services; -using Altinn.Notifications.Core.Services.Interfaces; -using Altinn.Notifications.Extensions; -using Altinn.Notifications.Models; -using Altinn.Notifications.Validators; using Altinn.Platform.Authorization.ModelBinding; using Altinn.Platform.Authorization.Repositories; using Altinn.Platform.Authorization.Repositories.Interface; @@ -31,12 +25,10 @@ using AltinnCore.Authentication.Constants; using AltinnCore.Authentication.JwtCookie; -using FluentValidation; - using LocalTest.Clients.CdnAltinnOrgs; using LocalTest.Configuration; using LocalTest.Helpers; -using LocalTest.Notifications.Persistence.Repository; +using LocalTest.Notifications.LocalTestNotifications; using LocalTest.Services.Authentication.Implementation; using LocalTest.Services.Authentication.Interface; using LocalTest.Services.Authorization.Implementation; @@ -129,18 +121,11 @@ public void ConfigureServices(IServiceCollection services) services.AddTransient(); services.AddTransient(); - // Notifications services - ValidatorOptions.Global.LanguageManager.Enabled = false; + // Notifications services + GeneralSettings generalSettings = Configuration.GetSection("GeneralSettings").Get(); - ResourceLinkExtensions.Initialize(generalSettings.BaseUrl); - - services - .AddSingleton, EmailNotificationOrderRequestValidator>() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton(); - + services.AddNotificationServices(generalSettings.BaseUrl); + // Storage services services.AddSingleton(); services.AddTransient();