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

NEST-403: Maintenance to clear all links of users #72

Merged
merged 14 commits into from
Jun 18, 2023
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 @@ -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 @@ -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,43 @@
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);

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.
wAsnk marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -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>