-
Notifications
You must be signed in to change notification settings - Fork 848
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VCST-1392: App with Permission Not Displayed in App List After Sign-In (
#2808) fix: App with Permission Not Displayed in App List After Sign-In.
- Loading branch information
Showing
1 changed file
with
31 additions
and
60 deletions.
There are no files selected for viewing
91 changes: 31 additions & 60 deletions
91
src/VirtoCommerce.Platform.Web/Controllers/Api/AppsController.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 |
---|---|---|
@@ -1,76 +1,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using VirtoCommerce.Platform.Core.Modularity; | ||
using VirtoCommerce.Platform.Security.Authorization; | ||
using VirtoCommerce.Platform.Web.Model.Modularity; | ||
|
||
|
||
namespace VirtoCommerce.Platform.Web.Controllers.Api | ||
{ | ||
[Route("api/platform/apps")] | ||
public class AppsController : Controller | ||
{ | ||
private readonly ILocalModuleCatalog _localModuleCatalog; | ||
private readonly IAuthorizationService _authorizationService; | ||
|
||
public AppsController(ILocalModuleCatalog localModuleCatalog, IAuthorizationService authorizationService) | ||
{ | ||
_localModuleCatalog = localModuleCatalog ?? throw new ArgumentNullException(nameof(localModuleCatalog)); | ||
_authorizationService = authorizationService ?? throw new ArgumentNullException(nameof(authorizationService)); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the list of available apps, filtered by user permissions. | ||
/// </summary> | ||
/// <returns>The list of available apps</returns> | ||
[HttpGet] | ||
public async Task<ActionResult<IEnumerable<AppDescriptor>>> GetApps() | ||
{ | ||
|
||
var authorizedApps = new List<AppDescriptor> | ||
{ | ||
// Add Commerce Manager by Default | ||
new AppDescriptor | ||
{ | ||
Id = "platform", | ||
Title = "Commerce Manager", | ||
Description = "Virto Commerce Platform", | ||
RelativeUrl = "/", | ||
IconUrl = "/images/platform_app.svg" | ||
} | ||
}; | ||
|
||
var applicationList = _localModuleCatalog.Modules.OfType<ManifestModuleInfo>() | ||
.SelectMany(x => x.Apps) | ||
.OrderBy(x => x.Title); | ||
namespace VirtoCommerce.Platform.Web.Controllers.Api; | ||
|
||
foreach (var moduleAppInfo in applicationList) | ||
{ | ||
if (await AuthorizeAppAsync(moduleAppInfo.Permission)) | ||
{ | ||
authorizedApps.Add(new AppDescriptor(moduleAppInfo)); | ||
} | ||
} | ||
|
||
return authorizedApps; | ||
[Route("api/platform/apps")] | ||
public class AppsController : Controller | ||
{ | ||
private readonly ILocalModuleCatalog _localModuleCatalog; | ||
|
||
} | ||
public AppsController(ILocalModuleCatalog localModuleCatalog) | ||
{ | ||
_localModuleCatalog = localModuleCatalog ?? throw new ArgumentNullException(nameof(localModuleCatalog)); | ||
} | ||
|
||
private async Task<bool> AuthorizeAppAsync(string permission) | ||
{ | ||
if (string.IsNullOrEmpty(permission)) | ||
/// <summary> | ||
/// Gets the list of available apps, filtered by user permissions. | ||
/// </summary> | ||
/// <returns>The list of available apps</returns> | ||
[HttpGet] | ||
public IEnumerable<AppDescriptor> GetApps() | ||
{ | ||
var apps = _localModuleCatalog.Modules.OfType<ManifestModuleInfo>() | ||
.SelectMany(x => x.Apps) | ||
.Select(x => new AppDescriptor(x)) | ||
.OrderBy(x => x.Title) | ||
.ToList(); | ||
|
||
apps.Insert(0, // Add Commerce Manager by Default | ||
new AppDescriptor | ||
{ | ||
return true; | ||
} | ||
Id = "platform", | ||
Title = "Commerce Manager", | ||
Description = "Virto Commerce Platform", | ||
RelativeUrl = "/", | ||
IconUrl = "/images/platform_app.svg" | ||
}); | ||
|
||
var result = await _authorizationService.AuthorizeAsync(User, null, | ||
new PermissionAuthorizationRequirement(permission)); | ||
return apps; | ||
|
||
return result.Succeeded; | ||
} | ||
} | ||
} |