-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deleting old accounts is now a scheduled job.
- Loading branch information
Showing
4 changed files
with
47 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace SS14.Auth.Shared.Config; | ||
|
||
public sealed class AccountConfiguration | ||
{ | ||
/// <summary> | ||
/// Delete unconfirmed accounts after this many days. | ||
/// </summary> | ||
public int DeleteUnconfirmedAfter { get; set; } = 3; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Quartz; | ||
using SS14.Auth.Shared.Config; | ||
using SS14.Auth.Shared.Data; | ||
|
||
namespace SS14.Auth.Jobs; | ||
|
||
public sealed class DeleteUnconfirmedAccounts(ApplicationDbContext dbContext, | ||
ILogger<DeleteUnconfirmedAccounts> logger, | ||
IOptions<AccountConfiguration> configuration) : IJob | ||
{ | ||
public async Task Execute(IJobExecutionContext context) | ||
{ | ||
var optionsValue = configuration.Value; | ||
|
||
var retainMinimum = DateTime.UtcNow - TimeSpan.FromDays(optionsValue.DeleteUnconfirmedAfter); | ||
|
||
// TODO: Use ExecuteDelete() on newer EF Core version. | ||
var accounts = await dbContext.Users | ||
.Where(user => !user.EmailConfirmed && user.CreatedTime < retainMinimum) | ||
.ToListAsync(context.CancellationToken); | ||
|
||
dbContext.RemoveRange(accounts); | ||
await dbContext.SaveChangesAsync(); | ||
logger.LogInformation("Deleted {Count} unconfirmed accounts from database", accounts.Count); | ||
} | ||
} |
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