-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
NEST-391: Maintenance to make a role site owner
- Loading branch information
Showing
12 changed files
with
145 additions
and
7 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
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
7 changes: 7 additions & 0 deletions
7
...aintenance/AddSiteOwnerPermissionToRole/AddSiteOwnerPermissionToRoleMaintenanceOptions.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,7 @@ | ||
namespace Lombiq.Hosting.Tenants.Maintenance.Maintenance.AddSiteOwnerPermissionToRole; | ||
|
||
public class AddSiteOwnerPermissionToRoleMaintenanceOptions | ||
{ | ||
public bool IsEnabled { get; set; } | ||
public string Role { get; set; } | ||
} |
42 changes: 42 additions & 0 deletions
42
...intenance/AddSiteOwnerPermissionToRole/AddSiteOwnerPermissionToRoleMaintenanceProvider.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,42 @@ | ||
using Lombiq.Hosting.Tenants.Maintenance.Extensions; | ||
using Lombiq.Hosting.Tenants.Maintenance.Models; | ||
using Lombiq.Hosting.Tenants.Maintenance.Services; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.Extensions.Options; | ||
using OrchardCore.Security; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using static OrchardCore.Security.Permissions.Permission; | ||
using static OrchardCore.Security.StandardPermissions; | ||
|
||
namespace Lombiq.Hosting.Tenants.Maintenance.Maintenance.AddSiteOwnerPermissionToRole; | ||
|
||
public class AddSiteOwnerPermissionToRoleMaintenanceProvider : MaintenanceProviderBase | ||
{ | ||
private readonly IOptions<AddSiteOwnerPermissionToRoleMaintenanceOptions> _options; | ||
private readonly RoleManager<IRole> _roleManager; | ||
|
||
public AddSiteOwnerPermissionToRoleMaintenanceProvider( | ||
IOptions<AddSiteOwnerPermissionToRoleMaintenanceOptions> options, | ||
RoleManager<IRole> roleManager) | ||
{ | ||
_options = options; | ||
_roleManager = roleManager; | ||
} | ||
|
||
public override Task<bool> ShouldExecuteAsync(MaintenanceTaskExecutionContext context) => | ||
Task.FromResult( | ||
_options.Value.IsEnabled && | ||
!context.WasLatestExecutionSuccessful()); | ||
|
||
public override async Task ExecuteAsync(MaintenanceTaskExecutionContext context) | ||
{ | ||
if (await _roleManager.FindByNameAsync(_options.Value.Role) is not Role role) return; | ||
|
||
if (role.RoleClaims.Any(claim => claim.ClaimType == ClaimType && claim.ClaimValue == SiteOwner.Name)) return; | ||
|
||
role.RoleClaims.Add(new RoleClaim { ClaimType = ClaimType, ClaimValue = SiteOwner.Name }); | ||
|
||
await _roleManager.UpdateAsync(role); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
Lombiq.Hosting.Tenants.Maintenance/Maintenance/AddSiteOwnerPermissionToRole/Startup.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,27 @@ | ||
using Lombiq.Hosting.Tenants.Maintenance.Constants; | ||
using Lombiq.Hosting.Tenants.Maintenance.Services; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OrchardCore.Environment.Shell.Configuration; | ||
using OrchardCore.Modules; | ||
|
||
namespace Lombiq.Hosting.Tenants.Maintenance.Maintenance.AddSiteOwnerPermissionToRole; | ||
|
||
[Feature(FeatureNames.AddSiteOwnerPermissionToRole)] | ||
public class Startup : StartupBase | ||
{ | ||
private readonly IShellConfiguration _shellConfiguration; | ||
|
||
public Startup(IShellConfiguration shellConfiguration) => | ||
_shellConfiguration = shellConfiguration; | ||
|
||
public override void ConfigureServices(IServiceCollection services) | ||
{ | ||
var options = new AddSiteOwnerPermissionToRoleMaintenanceOptions(); | ||
var configSection = _shellConfiguration.GetSection("Lombiq_Hosting_Tenants_Maintenance:AddSiteOwnerPermissionToRole"); | ||
configSection.Bind(options); | ||
services.Configure<AddSiteOwnerPermissionToRoleMaintenanceOptions>(configSection); | ||
|
||
services.AddScoped<IMaintenanceProvider, AddSiteOwnerPermissionToRoleMaintenanceProvider>(); | ||
} | ||
} |
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
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
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