Skip to content

Commit

Permalink
Merge pull request #72 from Lombiq/issue/NEST-403
Browse files Browse the repository at this point in the history
NEST-403: Maintenance to clear all links of users
  • Loading branch information
DemeSzabolcs authored Jun 18, 2023
2 parents a6b761e + 6cfaba9 commit 0627875
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' == 'true'">
<PackageReference Include="Lombiq.HelpfulLibraries.OrchardCore" Version="5.1.1" />
<PackageReference Include="Lombiq.HelpfulLibraries.OrchardCore" Version="5.2.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' == 'true'">
<PackageReference Include="Lombiq.Tests.UI" Version="6.0.0" />
<PackageReference Include="Lombiq.Tests.UI" Version="6.0.2-alpha.2.nest-403" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' == 'true'">
<PackageReference Include="Lombiq.Tests.UI" Version="6.0.0" />
<PackageReference Include="Lombiq.Tests.UI" Version="6.0.2-alpha.2.nest-403" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' == 'true'">
<PackageReference Include="Lombiq.HelpfulLibraries.OrchardCore" Version="5.1.1" />
<PackageReference Include="Lombiq.HelpfulLibraries.Common" Version="5.1.1" />
<PackageReference Include="Lombiq.HelpfulLibraries.OrchardCore" Version="5.2.0" />
<PackageReference Include="Lombiq.HelpfulLibraries.Common" Version="5.2.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' == 'true'">
<PackageReference Include="Lombiq.Tests.UI" Version="6.0.0" />
<PackageReference Include="Lombiq.Tests.UI" Version="6.0.2-alpha.2.nest-403" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' == 'true'">
<PackageReference Include="Lombiq.Tests.UI" Version="6.0.0" />
<PackageReference Include="Lombiq.Tests.UI" Version="6.0.2-alpha.2.nest-403" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public static class FeatureNames
public const string UpdateSiteUrl = Maintenance + "." + nameof(UpdateSiteUrl);
public const string UpdateShellRequestUrls = Maintenance + "." + nameof(UpdateShellRequestUrls);
public const string AddSiteOwnerPermissionToRole = Maintenance + "." + nameof(AddSiteOwnerPermissionToRole);
public const string RemoveUsers = Maintenance + "." + nameof(RemoveUsers);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
<PackageReference Include="OrchardCore.ContentTypes.Abstractions" Version="1.5.0" />
<PackageReference Include="OrchardCore.DisplayManagement" Version="1.5.0" />
<PackageReference Include="OrchardCore.Settings" Version="1.5.0" />
<PackageReference Include="OrchardCore.Users.Abstractions" Version="1.5.0" />
<PackageReference Include="OrchardCore.Users.Core" Version="1.5.0" />
</ItemGroup>

<ItemGroup>
Expand All @@ -46,7 +48,7 @@
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' == 'true'">
<PackageReference Include="Lombiq.HelpfulLibraries.Common" Version="5.1.1" />
<PackageReference Include="Lombiq.HelpfulLibraries.Common" Version="5.2.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Lombiq.Hosting.Tenants.Maintenance.Maintenance.RemoveUsers;

public class RemoveUsersMaintenanceOptions
{
public bool IsEnabled { get; set; }
public string EmailDomain { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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.Users;
using OrchardCore.Users.Models;
using System;
using System.Linq;
using System.Threading.Tasks;
using YesSql;

namespace Lombiq.Hosting.Tenants.Maintenance.Maintenance.RemoveUsers;

public class RemoveUsersMaintenanceProvider : MaintenanceProviderBase
{
private readonly IOptions<RemoveUsersMaintenanceOptions> _options;
private readonly ISession _session;
private readonly UserManager<IUser> _userManager;

public RemoveUsersMaintenanceProvider(
IOptions<RemoveUsersMaintenanceOptions> options,
ISession session,
UserManager<IUser> userManager)
{
_options = options;
_session = session;
_userManager = userManager;
}

public override Task<bool> ShouldExecuteAsync(MaintenanceTaskExecutionContext context) =>
Task.FromResult(
_options.Value.IsEnabled &&
!context.WasLatestExecutionSuccessful());

public override async Task ExecuteAsync(MaintenanceTaskExecutionContext context)
{
var users = await _session.Query<User>().ListAsync();
foreach (var user in users.Where(user =>
user.Email.EndsWith($"@{_options.Value.EmailDomain}", StringComparison.InvariantCulture)))
{
await _userManager.DeleteAsync(user);
}
}
}
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.RemoveUsers;

[Feature(FeatureNames.RemoveUsers)]
public class Startup : StartupBase
{
private readonly IShellConfiguration _shellConfiguration;

public Startup(IShellConfiguration shellConfiguration) =>
_shellConfiguration = shellConfiguration;

public override void ConfigureServices(IServiceCollection services)
{
var options = new RemoveUsersMaintenanceOptions();
var configSection = _shellConfiguration.GetSection("Lombiq_Hosting_Tenants_Maintenance:RemoveUsers");
configSection.Bind(options);
services.Configure<RemoveUsersMaintenanceOptions>(configSection);

services.AddScoped<IMaintenanceProvider, RemoveUsersMaintenanceProvider>();
}
}
9 changes: 9 additions & 0 deletions Lombiq.Hosting.Tenants.Maintenance/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,12 @@
DefaultTenantOnly = true,
Dependencies = new[] { Maintenance }
)]

[assembly: Feature(
Id = RemoveUsers,
Name = "Lombiq Hosting - Tenants Maintenance - Remove Users",
Description = "Removes users with the configured email domain.",
Category = "Maintenance",
DefaultTenantOnly = true,
Dependencies = new[] { Maintenance }
)]
19 changes: 19 additions & 0 deletions Lombiq.Hosting.Tenants.Maintenance/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,22 @@ The following configuration options are available to set the shell request URLs:
```

**NOTE**: The `{TenantName}` placeholder will be replaced with the actual tenant name automatically.

### `Lombiq.Hosting.Tenants.Maintenance.RemoveUsers`

It's a maintenance task that removes users from the database with the given email domain. It is available only for the default tenant. Useful if you have Azure AD enabled in your production environment and you want to reset staging to the production database. Then you would get "System.InvalidOperationException: Provider AzureAD is already linked for userName" error, so deleting those users will solve the error.

The following configuration should be used to allow the maintenance to run:

```json
{
"OrchardCore": {
"Lombiq_Hosting_Tenants_Maintenance": {
"RemoveUsers": {
"IsEnabled": true,
"EmailDomain": "example.com"
}
}
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' == 'true'">
<PackageReference Include="Lombiq.HelpfulLibraries.OrchardCore" Version="5.1.1" />
<PackageReference Include="Lombiq.HelpfulLibraries.OrchardCore" Version="5.2.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' == 'true'">
<PackageReference Include="Lombiq.Tests.UI" Version="6.0.0" />
<PackageReference Include="Lombiq.Tests.UI" Version="6.0.2-alpha.2.nest-403" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' == 'true'">
<PackageReference Include="Lombiq.HelpfulLibraries.OrchardCore" Version="5.1.1" />
<PackageReference Include="Lombiq.HelpfulLibraries.OrchardCore" Version="5.2.0" />
</ItemGroup>

</Project>

0 comments on commit 0627875

Please sign in to comment.