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 @@ -7,5 +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 RemoveLoginInfos = Maintenance + "." + nameof(RemoveLoginInfos);
public const string RemoveUsers = Maintenance + "." + nameof(RemoveUsers);
}

This file was deleted.

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
@@ -1,24 +1,24 @@
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.Collections.Generic;
using System;
using System.Linq;
using System.Threading.Tasks;
using YesSql;

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

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

public RemoveLoginInfosMaintenanceProvider(
IOptions<RemoveLoginInfosMaintenanceOptions> options,
public RemoveUsersMaintenanceProvider(
IOptions<RemoveUsersMaintenanceOptions> options,
ISession session,
UserManager<IUser> userManager)
{
Expand All @@ -29,16 +29,15 @@ public RemoveLoginInfosMaintenanceProvider(

public override Task<bool> ShouldExecuteAsync(MaintenanceTaskExecutionContext context) =>
Task.FromResult(
_options.Value.IsEnabled &&
!context.WasLatestExecutionSuccessful());
DemeSzabolcs marked this conversation as resolved.
Show resolved Hide resolved
_options.Value.IsEnabled);

public override async Task ExecuteAsync(MaintenanceTaskExecutionContext context)
{
var users = await _session.Query<User>().ListAsync();
foreach (var user in users)
foreach (var user in users.Where(user =>
user.Email.EndsWith($"@{_options.Value.EmailDomain}", StringComparison.InvariantCulture)))
{
user.LoginInfos.RemoveAll();
await _userManager.UpdateAsync(user);
await _userManager.DeleteAsync(user);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
using OrchardCore.Environment.Shell.Configuration;
using OrchardCore.Modules;

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

[Feature(FeatureNames.RemoveLoginInfos)]
[Feature(FeatureNames.RemoveUsers)]
public class Startup : StartupBase
{
private readonly IShellConfiguration _shellConfiguration;
Expand All @@ -17,11 +17,11 @@ public Startup(IShellConfiguration shellConfiguration) =>

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

services.AddScoped<IMaintenanceProvider, RemoveLoginInfosMaintenanceProvider>();
services.AddScoped<IMaintenanceProvider, RemoveUsersMaintenanceProvider>();
}
}
6 changes: 3 additions & 3 deletions Lombiq.Hosting.Tenants.Maintenance/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
)]

[assembly: Feature(
Id = RemoveLoginInfos,
Name = "Lombiq Hosting - Tenants Maintenance - Remove Login Infos",
Description = "Removes login infos from every user account, like Azure AD links.",
Id = RemoveUsers,
Name = "Lombiq Hosting - Tenants Maintenance - Remove Users",
Description = "Removes users with the configured email domain.",
Category = "Maintenance",
DefaultTenantOnly = true,
Dependencies = new[] { Maintenance }
Expand Down
9 changes: 5 additions & 4 deletions Lombiq.Hosting.Tenants.Maintenance/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,19 @@ 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.RemoveLoginInfos`
### `Lombiq.Hosting.Tenants.Maintenance.RemoveUsers`

It's a maintenance task that removes the login infos of users from the database. 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 without removing the login infos.
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": {
"RemoveLoginInfos": {
"IsEnabled": true
"RemoveUsers": {
"IsEnabled": true,
"EmailDomain": "example.com"
}
}
}
Expand Down