Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move permissions to its own class to avoid module to module reference #16769

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/OrchardCore.Modules/OrchardCore.Admin/AdminMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected override ValueTask BuildAsync(NavigationBuilder builder)
.Add(S["Admin"], S["Admin"].PrefixPosition(), admin => admin
.AddClass("admin").Id("admin")
.Action("Index", "Admin", _routeValues)
.Permission(PermissionsAdminSettings.ManageAdminSettings)
.Permission(AdminPermissions.ManageAdminSettings)
.LocalNav()
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override async Task<IDisplayResult> EditAsync(ISite site, AdminSettings s
{
var user = _httpContextAccessor.HttpContext?.User;

if (!await _authorizationService.AuthorizeAsync(user, PermissionsAdminSettings.ManageAdminSettings))
if (!await _authorizationService.AuthorizeAsync(user, AdminPermissions.ManageAdminSettings))
{
return null;
}
Expand All @@ -50,7 +50,7 @@ public override async Task<IDisplayResult> UpdateAsync(ISite site, AdminSettings
{
var user = _httpContextAccessor.HttpContext?.User;

if (!await _authorizationService.AuthorizeAsync(user, PermissionsAdminSettings.ManageAdminSettings))
if (!await _authorizationService.AuthorizeAsync(user, AdminPermissions.ManageAdminSettings))
{
return null;
}
Expand Down
3 changes: 0 additions & 3 deletions src/OrchardCore.Modules/OrchardCore.Admin/Permissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ namespace OrchardCore.Admin;

public sealed class Permissions : IPermissionProvider
{
[Obsolete("This will be removed in a future release. Instead use 'AdminPermissions.AccessAdminPanel'.")]
public static readonly Permission AccessAdminPanel = AdminPermissions.AccessAdminPanel;

private readonly IEnumerable<Permission> _allPermissions =
[
AdminPermissions.AccessAdminPanel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ namespace OrchardCore.Admin;

public sealed class PermissionsAdminSettings : IPermissionProvider
{
public static readonly Permission ManageAdminSettings = new("ManageAdminSettings", "Manage Admin Settings");

private readonly IEnumerable<Permission> _allPermissions =
[
ManageAdminSettings,
AdminPermissions.ManageAdminSettings,
];

public IEnumerable<PermissionStereotype> GetDefaultStereotypes() =>
Expand Down
2 changes: 1 addition & 1 deletion src/OrchardCore.Modules/OrchardCore.AdminMenu/AdminMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected override ValueTask BuildAsync(NavigationBuilder builder)
builder
.Add(S["Configuration"], configuration => configuration
.Add(S["Admin Menus"], S["Admin Menus"].PrefixPosition(), adminMenu => adminMenu
.Permission(Permissions.ManageAdminMenu)
.Permission(AdminMenuPermissions.ManageAdminMenu)
.Action("List", "Menu", "OrchardCore.AdminMenu")
.LocalNav()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public MenuController(

public async Task<IActionResult> List(ContentOptions options, PagerParameters pagerParameters)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageAdminMenu))
if (!await _authorizationService.AuthorizeAsync(User, AdminMenuPermissions.ManageAdminMenu))
{
return Forbid();
}
Expand Down Expand Up @@ -121,7 +121,7 @@ public ActionResult IndexFilterPOST(AdminMenuListViewModel model)

public async Task<IActionResult> Create()
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageAdminMenu))
if (!await _authorizationService.AuthorizeAsync(User, AdminMenuPermissions.ManageAdminMenu))
{
return Forbid();
}
Expand All @@ -134,7 +134,7 @@ public async Task<IActionResult> Create()
[HttpPost]
public async Task<IActionResult> Create(AdminMenuCreateViewModel model)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageAdminMenu))
if (!await _authorizationService.AuthorizeAsync(User, AdminMenuPermissions.ManageAdminMenu))
{
return Forbid();
}
Expand All @@ -153,7 +153,7 @@ public async Task<IActionResult> Create(AdminMenuCreateViewModel model)

public async Task<IActionResult> Edit(string id)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageAdminMenu))
if (!await _authorizationService.AuthorizeAsync(User, AdminMenuPermissions.ManageAdminMenu))
{
return Forbid();
}
Expand All @@ -178,7 +178,7 @@ public async Task<IActionResult> Edit(string id)
[HttpPost]
public async Task<IActionResult> Edit(AdminMenuEditViewModel model)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageAdminMenu))
if (!await _authorizationService.AuthorizeAsync(User, AdminMenuPermissions.ManageAdminMenu))
{
return Forbid();
}
Expand Down Expand Up @@ -208,7 +208,7 @@ public async Task<IActionResult> Edit(AdminMenuEditViewModel model)
[HttpPost]
public async Task<IActionResult> Delete(string id)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageAdminMenu))
if (!await _authorizationService.AuthorizeAsync(User, AdminMenuPermissions.ManageAdminMenu))
{
return Forbid();
}
Expand Down Expand Up @@ -240,7 +240,7 @@ public async Task<IActionResult> Delete(string id)
[FormValueRequired("submit.BulkAction")]
public async Task<ActionResult> IndexPost(ContentOptions options, IEnumerable<string> itemIds)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageAdminMenu))
if (!await _authorizationService.AuthorizeAsync(User, AdminMenuPermissions.ManageAdminMenu))
{
return Forbid();
}
Expand Down Expand Up @@ -272,7 +272,7 @@ public async Task<ActionResult> IndexPost(ContentOptions options, IEnumerable<st
[HttpPost]
public async Task<IActionResult> Toggle(string id)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageAdminMenu))
if (!await _authorizationService.AuthorizeAsync(User, AdminMenuPermissions.ManageAdminMenu))
{
return Forbid();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public NodeController(

public async Task<IActionResult> List(string id)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageAdminMenu))
if (!await _authorizationService.AuthorizeAsync(User, AdminMenuPermissions.ManageAdminMenu))
{
return Forbid();
}
Expand Down Expand Up @@ -81,7 +81,7 @@ private async Task<AdminNodeListViewModel> BuildDisplayViewModel(Models.AdminMen

public async Task<IActionResult> Create(string id, string type)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageAdminMenu))
if (!await _authorizationService.AuthorizeAsync(User, AdminMenuPermissions.ManageAdminMenu))
{
return Forbid();
}
Expand Down Expand Up @@ -115,7 +115,7 @@ public async Task<IActionResult> Create(string id, string type)
[HttpPost]
public async Task<IActionResult> Create(AdminNodeEditViewModel model)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageAdminMenu))
if (!await _authorizationService.AuthorizeAsync(User, AdminMenuPermissions.ManageAdminMenu))
{
return Forbid();
}
Expand Down Expand Up @@ -156,7 +156,7 @@ public async Task<IActionResult> Create(AdminNodeEditViewModel model)

public async Task<IActionResult> Edit(string id, string treeNodeId)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageAdminMenu))
if (!await _authorizationService.AuthorizeAsync(User, AdminMenuPermissions.ManageAdminMenu))
{
return Forbid();
}
Expand Down Expand Up @@ -195,7 +195,7 @@ public async Task<IActionResult> Edit(string id, string treeNodeId)
[HttpPost]
public async Task<IActionResult> Edit(AdminNodeEditViewModel model)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageAdminMenu))
if (!await _authorizationService.AuthorizeAsync(User, AdminMenuPermissions.ManageAdminMenu))
{
return Forbid();
}
Expand Down Expand Up @@ -238,7 +238,7 @@ public async Task<IActionResult> Edit(AdminNodeEditViewModel model)
[HttpPost]
public async Task<IActionResult> Delete(string id, string treeNodeId)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageAdminMenu))
if (!await _authorizationService.AuthorizeAsync(User, AdminMenuPermissions.ManageAdminMenu))
{
return Forbid();
}
Expand Down Expand Up @@ -273,7 +273,7 @@ public async Task<IActionResult> Delete(string id, string treeNodeId)
[HttpPost]
public async Task<IActionResult> Toggle(string id, string treeNodeId)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageAdminMenu))
if (!await _authorizationService.AuthorizeAsync(User, AdminMenuPermissions.ManageAdminMenu))
{
return Forbid();
}
Expand Down Expand Up @@ -306,7 +306,7 @@ public async Task<IActionResult> Toggle(string id, string treeNodeId)
public async Task<IActionResult> MoveNode(string treeId, string nodeToMoveId,
string destinationNodeId, int position)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageAdminMenu))
if (!await _authorizationService.AuthorizeAsync(User, AdminMenuPermissions.ManageAdminMenu))
{
return Forbid();
}
Expand Down
14 changes: 7 additions & 7 deletions src/OrchardCore.Modules/OrchardCore.AdminMenu/Permissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ namespace OrchardCore.AdminMenu;

public sealed class Permissions : IPermissionProvider
{
public static readonly Permission ManageAdminMenu = new("ManageAdminMenu", "Manage the admin menu");
public static readonly Permission ViewAdminMenuAll = new("ViewAdminMenuAll", "View Admin Menu - View All", new[] { ManageAdminMenu });

private static readonly Permission _viewAdminMenu = new("ViewAdminMenu_{0}", "View Admin Menu - {0}", new[] { ManageAdminMenu, ViewAdminMenuAll });
private static readonly Permission _viewAdminMenu = new("ViewAdminMenu_{0}", "View Admin Menu - {0}", new[] {
AdminMenuPermissions.ManageAdminMenu,
AdminMenuPermissions.ViewAdminMenuAll
});

private readonly IEnumerable<Permission> _generalPermissions =
[
ManageAdminMenu,
AdminMenuPermissions.ManageAdminMenu,
];

private readonly IAdminMenuService _adminMenuService;
Expand All @@ -28,8 +28,8 @@ public async Task<IEnumerable<Permission>> GetPermissionsAsync()

var permissions = new List<Permission>(adminMenuItems.Count + 2)
{
ViewAdminMenuAll,
ManageAdminMenu,
AdminMenuPermissions.ViewAdminMenuAll,
AdminMenuPermissions.ManageAdminMenu,
};

foreach (var adminMenu in adminMenuItems)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected override ValueTask BuildAsync(NavigationBuilder builder)
.Add(S["Configuration"], configuration => configuration
.Add(S["GraphiQL"], S["GraphiQL"].PrefixPosition(), graphiQL => graphiQL
.Action("Index", "Admin", "OrchardCore.Apis.GraphQL")
.Permission(CommonPermissions.ExecuteGraphQL)
.Permission(GraphQLPermissions.ExecuteGraphQL)
.LocalNav()
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next)
context.User = authenticateResult.Principal;
}
var authorizationService = context.RequestServices.GetService<IAuthorizationService>();
var authorized = await authorizationService.AuthorizeAsync(context.User, CommonPermissions.ExecuteGraphQL);
var authorized = await authorizationService.AuthorizeAsync(context.User, GraphQLPermissions.ExecuteGraphQL);

if (authorized)
{
Expand Down
12 changes: 3 additions & 9 deletions src/OrchardCore.Modules/OrchardCore.Apis.GraphQL/Permissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@ namespace OrchardCore.Apis.GraphQL;

public sealed class Permissions : IPermissionProvider
{
[Obsolete("This will be removed in a future release. Instead use 'OrchardCore.Apis.GraphQL.CommonPermissions.ExecuteGraphQLMutations'.")]
public static readonly Permission ExecuteGraphQLMutations = CommonPermissions.ExecuteGraphQLMutations;

[Obsolete("This will be removed in a future release. Instead use 'OrchardCore.Apis.GraphQL.CommonPermissions.ExecuteGraphQL'.")]
public static readonly Permission ExecuteGraphQL = CommonPermissions.ExecuteGraphQL;

private readonly IEnumerable<Permission> _allPermissions =
[
CommonPermissions.ExecuteGraphQL,
CommonPermissions.ExecuteGraphQLMutations,
GraphQLPermissions.ExecuteGraphQL,
GraphQLPermissions.ExecuteGraphQLMutations,
];

public Task<IEnumerable<Permission>> GetPermissionsAsync()
Expand All @@ -26,7 +20,7 @@ public IEnumerable<PermissionStereotype> GetDefaultStereotypes() =>
Name = OrchardCoreConstants.Roles.Administrator,
Permissions =
[
CommonPermissions.ExecuteGraphQLMutations,
GraphQLPermissions.ExecuteGraphQLMutations,
],
},
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public async ValueTask<INodeVisitor> GetPreNodeVisitorAsync(ValidationContext va

private async Task AuthorizeOperationAsync(ASTNode node, ValidationContext validationContext, GraphQLUserContext userContext, OperationType? operationType, string operationName)
{
if (operationType == OperationType.Mutation && !(await _authorizationService.AuthorizeAsync(userContext.User, CommonPermissions.ExecuteGraphQLMutations)))
if (operationType == OperationType.Mutation && !(await _authorizationService.AuthorizeAsync(userContext.User, GraphQLPermissions.ExecuteGraphQLMutations)))
{
validationContext.ReportError(new ValidationError(
validationContext.Document.Source,
Expand Down
6 changes: 0 additions & 6 deletions src/OrchardCore.Modules/OrchardCore.AuditTrail/Permissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ namespace OrchardCore.AuditTrail;

public sealed class Permissions : IPermissionProvider
{
[Obsolete("This will be removed in a future release. Instead use 'OrchardCore.AuditTrail.AuditTrailPermissions.ViewAuditTrail'.")]
public static readonly Permission ViewAuditTrail = AuditTrailPermissions.ViewAuditTrail;

[Obsolete("This will be removed in a future release. Instead use 'OrchardCore.AuditTrail.AuditTrailPermissions.ManageAuditTrailSettings'.")]
public static readonly Permission ManageAuditTrailSettings = AuditTrailPermissions.ManageAuditTrailSettings;

private readonly IEnumerable<Permission> _allPermissions =
[
AuditTrailPermissions.ViewAuditTrail,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ await context.Updater.TryUpdateModelAsync(viewModel, Prefix,

var httpContext = _httpContextAccessor.HttpContext;

if (httpContext != null && await _authorizationService.AuthorizeAsync(httpContext.User, Permissions.SetHomepage))
if (httpContext != null && await _authorizationService.AuthorizeAsync(httpContext.User, AutoroutePermissions.SetHomepage))
{
await context.Updater.TryUpdateModelAsync(model, Prefix, t => t.SetHomepage);
}
Expand Down
4 changes: 1 addition & 3 deletions src/OrchardCore.Modules/OrchardCore.Autoroute/Permissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ namespace OrchardCore.Autoroute;

public sealed class Permissions : IPermissionProvider
{
public static readonly Permission SetHomepage = new("SetHomepage", "Set homepage.");

private readonly IEnumerable<Permission> _allPermissions =
[
SetHomepage,
AutoroutePermissions.SetHomepage,
];

public Task<IEnumerable<Permission>> GetPermissionsAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
</div>
</div>
@{
var authorized = await AuthorizationService.AuthorizeAsync(User, Permissions.SetHomepage);
var authorized = await AuthorizationService.AuthorizeAsync(User, AutoroutePermissions.SetHomepage);
var showHomepageOption = Model.Settings.ShowHomepageOption && authorized;

if (Model.IsHomepage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ protected override ValueTask BuildAsync(NavigationBuilder builder)
.AddClass("contentrequestcultureprovider")
.Id("contentrequestcultureprovider")
.Action("Index", "Admin", _providersRouteValues)
.Permission(Permissions.ManageContentCulturePicker)
.Permission(ContentLocalizationPermissions.ManageContentCulturePicker)
.LocalNav()
)
.Add(S["Content Culture Picker"], S["Content Culture Picker"].PrefixPosition(), picker => picker
.AddClass("contentculturepicker")
.Id("contentculturepicker")
.Action("Index", "Admin", _pickerRouteValues)
.Permission(Permissions.ManageContentCulturePicker)
.Permission(ContentLocalizationPermissions.ManageContentCulturePicker)
.LocalNav()
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task<IActionResult> Localize(string contentItemId, string targetCul
return NotFound();
}

if (!await _authorizationService.AuthorizeAsync(User, Permissions.LocalizeContent, contentItem))
if (!await _authorizationService.AuthorizeAsync(User, ContentLocalizationPermissions.LocalizeContent, contentItem))
{
return Forbid();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override async Task<IDisplayResult> EditAsync(ISite site, ContentCultureP
{
var user = _httpContextAccessor.HttpContext?.User;

if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageContentCulturePicker))
if (!await _authorizationService.AuthorizeAsync(user, ContentLocalizationPermissions.ManageContentCulturePicker))
{
return null;
}
Expand All @@ -47,7 +47,7 @@ public override async Task<IDisplayResult> UpdateAsync(ISite site, ContentCultur
{
var user = _httpContextAccessor.HttpContext?.User;

if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageContentCulturePicker))
if (!await _authorizationService.AuthorizeAsync(user, ContentLocalizationPermissions.ManageContentCulturePicker))
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override async Task<IDisplayResult> EditAsync(ISite site, ContentRequestC
{
var user = _httpContextAccessor.HttpContext?.User;

if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageContentCulturePicker))
if (!await _authorizationService.AuthorizeAsync(user, ContentLocalizationPermissions.ManageContentCulturePicker))
{
return null;
}
Expand All @@ -46,7 +46,7 @@ public override async Task<IDisplayResult> UpdateAsync(ISite site, ContentReques
{
var user = _httpContextAccessor.HttpContext?.User;

if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageContentCulturePicker))
if (!await _authorizationService.AuthorizeAsync(user, ContentLocalizationPermissions.ManageContentCulturePicker))
{
return null;
}
Expand Down
Loading