-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VM-1483: default permissions for roles
- Loading branch information
1 parent
79a1cd4
commit b9f38f5
Showing
4 changed files
with
118 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...Commerce.MarketplaceCommunicationModule.Web/Authorization/ApplicationBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<IPermissionsRegistrar>(); | ||
permissionsProvider.RegisterPermissions(VcmpCommunicationModule.ModuleConstants.Security.Permissions.AllPermissions.Select(x => new Permission { GroupName = "Marketplace", Name = x }).ToArray()); | ||
|
||
var roleManager = serviceScope.ServiceProvider.GetRequiredService<RoleManager<Role>>(); | ||
SavePredefinedRolesAsync(roleManager).GetAwaiter().GetResult(); | ||
|
||
return appBuilder; | ||
} | ||
|
||
private static async Task SavePredefinedRolesAsync(RoleManager<Role> 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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters