diff --git a/src/VirtoCommerce.MarketplaceCommunicationModule.Core/ModuleConstants.cs b/src/VirtoCommerce.MarketplaceCommunicationModule.Core/ModuleConstants.cs index 57e670b..77dc03d 100644 --- a/src/VirtoCommerce.MarketplaceCommunicationModule.Core/ModuleConstants.cs +++ b/src/VirtoCommerce.MarketplaceCommunicationModule.Core/ModuleConstants.cs @@ -1,4 +1,6 @@ using System.Collections.Generic; +using System.Linq; +using VirtoCommerce.Platform.Core.Security; using VirtoCommerce.Platform.Core.Settings; namespace VirtoCommerce.MarketplaceCommunicationModule.Core; @@ -22,6 +24,65 @@ public static class Permissions Delete }; } + + public static class Roles + { + public static readonly Role Operator = new() + { + Id = "vcmp-operator-role", + Permissions = new[] + { + Permissions.Read, + Permissions.Send, + Permissions.Edit, + Permissions.Delete + } + .Select(x => new Permission { GroupName = "Marketplace", Name = x }) + .ToList() + }; + + public static readonly Role VendorOwner = new() + { + Id = "vcmp-owner-role", + Permissions = new[] + { + Permissions.Read, + Permissions.Send, + Permissions.Edit, + Permissions.Delete + } + .Select(x => new Permission { GroupName = "Marketplace", Name = x }) + .ToList() + }; + + public static readonly Role VendorAdmin = new() + { + Id = "vcmp-admin-role", + Permissions = new[] + { + Permissions.Read, + Permissions.Send + } + .Select(x => new Permission { GroupName = "Marketplace", Name = x }) + .ToList() + }; + + public static readonly Role VendorAgent = new() + { + Id = "vcmp-agent-role", + Permissions = new[] + { + Permissions.Read, + Permissions.Send + } + .Select(x => new Permission { GroupName = "Marketplace", Name = x }) + .ToList() + }; + + public static Role[] AllRoles = { Operator, VendorOwner, VendorAdmin, VendorAgent }; + + } + } public static class Settings diff --git a/src/VirtoCommerce.MarketplaceCommunicationModule.Core/VirtoCommerce.MarketplaceCommunicationModule.Core.csproj b/src/VirtoCommerce.MarketplaceCommunicationModule.Core/VirtoCommerce.MarketplaceCommunicationModule.Core.csproj index c45d373..0fd2100 100644 --- a/src/VirtoCommerce.MarketplaceCommunicationModule.Core/VirtoCommerce.MarketplaceCommunicationModule.Core.csproj +++ b/src/VirtoCommerce.MarketplaceCommunicationModule.Core/VirtoCommerce.MarketplaceCommunicationModule.Core.csproj @@ -9,7 +9,7 @@ - + diff --git a/src/VirtoCommerce.MarketplaceCommunicationModule.Web/Authorization/ApplicationBuilderExtensions.cs b/src/VirtoCommerce.MarketplaceCommunicationModule.Web/Authorization/ApplicationBuilderExtensions.cs new file mode 100644 index 0000000..6d63143 --- /dev/null +++ b/src/VirtoCommerce.MarketplaceCommunicationModule.Web/Authorization/ApplicationBuilderExtensions.cs @@ -0,0 +1,53 @@ +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.DependencyInjection; +using VirtoCommerce.Platform.Core.Security; +using VcmpCommunicationModule = VirtoCommerce.MarketplaceCommunicationModule.Core; +using VendorModule = VirtoCommerce.MarketplaceVendorModule.Core; + +namespace VirtoCommerce.MarketplaceCommunicationModule.Web.Authorization; + +public static class ApplicationBuilderExtensions +{ + public static IApplicationBuilder UseModuleAuthorization(this IApplicationBuilder appBuilder) + { + using var serviceScope = appBuilder.ApplicationServices.CreateScope(); + + var permissionsProvider = appBuilder.ApplicationServices.GetRequiredService(); + permissionsProvider.RegisterPermissions(VcmpCommunicationModule.ModuleConstants.Security.Permissions.AllPermissions.Select(x => new Permission { GroupName = "Marketplace", Name = x }).ToArray()); + + var roleManager = serviceScope.ServiceProvider.GetRequiredService>(); + SavePredefinedRolesAsync(roleManager).GetAwaiter().GetResult(); + + return appBuilder; + } + + private static async Task SavePredefinedRolesAsync(RoleManager roleManager) + { + foreach (var vendorModuleRole in VendorModule.ModuleConstants.Security.Roles.AllRoles) + { + var existingVendorModuleRole = await roleManager.FindByIdAsync(vendorModuleRole.Id); + var communicationModuleRole = VcmpCommunicationModule.ModuleConstants.Security.Roles.AllRoles.Where(x => x.Id == vendorModuleRole.Id).FirstOrDefault(); + + if (existingVendorModuleRole != null) + { + vendorModuleRole.Permissions = existingVendorModuleRole.Permissions.Concat(vendorModuleRole.Permissions).Distinct().ToList(); + if (communicationModuleRole != null) + { + vendorModuleRole.Permissions = vendorModuleRole.Permissions.Concat(communicationModuleRole.Permissions).Distinct().ToList(); + } + await roleManager.UpdateAsync(vendorModuleRole); + } + else + { + if (communicationModuleRole != null) + { + vendorModuleRole.Permissions = vendorModuleRole.Permissions.Concat(communicationModuleRole.Permissions).Distinct().ToList(); + } + await roleManager.CreateAsync(vendorModuleRole); + } + } + } +} diff --git a/src/VirtoCommerce.MarketplaceCommunicationModule.Web/Module.cs b/src/VirtoCommerce.MarketplaceCommunicationModule.Web/Module.cs index 465b12a..bef0d30 100644 --- a/src/VirtoCommerce.MarketplaceCommunicationModule.Web/Module.cs +++ b/src/VirtoCommerce.MarketplaceCommunicationModule.Web/Module.cs @@ -5,8 +5,8 @@ using Microsoft.Extensions.DependencyInjection; using VirtoCommerce.MarketplaceCommunicationModule.Core; using VirtoCommerce.MarketplaceCommunicationModule.Data.Repositories; +using VirtoCommerce.MarketplaceCommunicationModule.Web.Authorization; using VirtoCommerce.Platform.Core.Modularity; -using VirtoCommerce.Platform.Core.Security; using VirtoCommerce.Platform.Core.Settings; namespace VirtoCommerce.MarketplaceCommunicationModule.Web; @@ -35,9 +35,8 @@ public void PostInitialize(IApplicationBuilder appBuilder) var settingsRegistrar = serviceProvider.GetRequiredService(); settingsRegistrar.RegisterSettings(ModuleConstants.Settings.AllSettings, ModuleInfo.Id); - // Register permissions - var permissionsRegistrar = serviceProvider.GetRequiredService(); - permissionsRegistrar.RegisterPermissions(ModuleInfo.Id, "MarketplaceCommunicationModule", ModuleConstants.Security.Permissions.AllPermissions); + //Register module authorization + appBuilder.UseModuleAuthorization(); // Apply migrations using var serviceScope = serviceProvider.CreateScope();