Skip to content

Commit

Permalink
VM-1483: default permissions for roles
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-buravlev committed Oct 23, 2024
1 parent 79a1cd4 commit b9f38f5
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="VirtoCommerce.MarketplaceVendorModule.Core" Version="3.825.0-alpha.819" />
<PackageReference Include="VirtoCommerce.CommunicationModule.Core" Version="3.800.0-alpha.3" />
<PackageReference Include="VirtoCommerce.CommunicationModule.Core" Version="3.800.0-alpha.5" />
<PackageReference Include="VirtoCommerce.Platform.Core" Version="3.825.0" />
</ItemGroup>
</Project>
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -35,9 +35,8 @@ public void PostInitialize(IApplicationBuilder appBuilder)
var settingsRegistrar = serviceProvider.GetRequiredService<ISettingsRegistrar>();
settingsRegistrar.RegisterSettings(ModuleConstants.Settings.AllSettings, ModuleInfo.Id);

// Register permissions
var permissionsRegistrar = serviceProvider.GetRequiredService<IPermissionsRegistrar>();
permissionsRegistrar.RegisterPermissions(ModuleInfo.Id, "MarketplaceCommunicationModule", ModuleConstants.Security.Permissions.AllPermissions);
//Register module authorization
appBuilder.UseModuleAuthorization();

// Apply migrations
using var serviceScope = serviceProvider.CreateScope();
Expand Down

0 comments on commit b9f38f5

Please sign in to comment.