From f3d3a7e575e9332921d538b43ef62508c18d843f Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Thu, 3 Jun 2021 16:24:33 +0100 Subject: [PATCH] user list cleanup --- src/Yavsc.Server/Settings/UserPolicies.cs | 17 +++++ .../Templates/UserOrientedTemplate.cs | 10 --- src/cli/Commands/SendMailCommand.cs | 16 ++--- src/cli/Commands/UserListCleanUp.cs | 71 +++++++++++++++++++ src/cli/Services/EMailer.cs | 5 +- 5 files changed, 96 insertions(+), 23 deletions(-) create mode 100644 src/Yavsc.Server/Settings/UserPolicies.cs create mode 100644 src/cli/Commands/UserListCleanUp.cs diff --git a/src/Yavsc.Server/Settings/UserPolicies.cs b/src/Yavsc.Server/Settings/UserPolicies.cs new file mode 100644 index 00000000..2aef87ff --- /dev/null +++ b/src/Yavsc.Server/Settings/UserPolicies.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using Yavsc.Models; + +namespace Yavsc.Server.Settings +{ + public class UserPolicies + { + public static readonly Dictionary> Criterias = + new Dictionary> + { + { "allow-monthly", u => u.AllowMonthlyEmail }, + { "email-not-confirmed", u => !u.EmailConfirmed && u.DateCreated < DateTime.Now.AddDays(-7) }, + { "user-to-remove", u => !u.EmailConfirmed && u.DateCreated < DateTime.Now.AddDays(-14) } + }; + } +} diff --git a/src/Yavsc.Server/Templates/UserOrientedTemplate.cs b/src/Yavsc.Server/Templates/UserOrientedTemplate.cs index bcc4a784..6d3f50d4 100644 --- a/src/Yavsc.Server/Templates/UserOrientedTemplate.cs +++ b/src/Yavsc.Server/Templates/UserOrientedTemplate.cs @@ -5,16 +5,6 @@ using Yavsc.Models; namespace Yavsc.Templates { - public static class TemplateConstants - { - public static readonly Dictionary> Criterias = - new Dictionary> - { - { "allow-monthly", u => u.AllowMonthlyEmail }, - { "email-not-confirmed", u => !u.EmailConfirmed && u.DateCreated < DateTime.Now.AddDays(-7) } - }; - } - public abstract class UserOrientedTemplate: Template { public ApplicationUser User { get; set; } diff --git a/src/cli/Commands/SendMailCommand.cs b/src/cli/Commands/SendMailCommand.cs index d1c8b4eb..e6d38cb1 100644 --- a/src/cli/Commands/SendMailCommand.cs +++ b/src/cli/Commands/SendMailCommand.cs @@ -5,11 +5,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using cli.Services; using cli.Model; -using System.Linq; -using Yavsc.Models; -using System.Collections.Generic; -using System; -using Yavsc.Templates; +using Yavsc.Server.Settings; namespace cli { @@ -17,8 +13,6 @@ namespace cli { public CommandLineApplication Integrate(CommandLineApplication rootApp) { - - CommandArgument codeCommandArg = null; CommandArgument critCommandArg = null; CommandOption sendHelpOption = null; CommandLineApplication sendMailCommandApp @@ -28,9 +22,6 @@ namespace cli target.FullName = "Send email"; target.Description = "Sends monthly emails using given template from code"; sendHelpOption = target.HelpOption("-? | -h | --help"); - codeCommandArg = target.Argument( - "code", - "template code of mailling to execute."); critCommandArg = target.Argument( "criteria", "user selection criteria : 'allow-monthly' or 'email-not-confirmed'"); @@ -38,7 +29,8 @@ namespace cli sendMailCommandApp.OnExecute(() => { - bool showhelp = TemplateConstants.Criterias.ContainsKey(critCommandArg.Value); + bool showhelp = !UserPolicies.Criterias.ContainsKey(critCommandArg.Value) + || sendHelpOption.HasValue(); if (!showhelp) { @@ -53,7 +45,7 @@ namespace cli var loggerFactory = app.Services.GetService(); var logger = loggerFactory.CreateLogger(); logger.LogInformation("Starting emailling"); - mailer.SendEmailFromCriteria(critCommandArg.Value, TemplateConstants.Criterias[critCommandArg.Value]); + mailer.SendEmailFromCriteria(critCommandArg.Value); logger.LogInformation("Finished emailling"); } else diff --git a/src/cli/Commands/UserListCleanUp.cs b/src/cli/Commands/UserListCleanUp.cs new file mode 100644 index 00000000..9b3efba2 --- /dev/null +++ b/src/cli/Commands/UserListCleanUp.cs @@ -0,0 +1,71 @@ +using cli.Model; +using Microsoft.AspNet.Hosting; +using Microsoft.Extensions.CommandLineUtils; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using cli.Services; +using Yavsc.Server.Settings; +using Yavsc.Models; +using Microsoft.AspNet.Identity; +using System.Linq; +using System; + +namespace cli.Commands +{ + public class UserListCleanUp : ICommander + { + public CommandLineApplication Integrate(CommandLineApplication rootApp) + { + CommandOption sendHelpOption = null; + CommandLineApplication userCleanupCommandApp + = rootApp.Command("user-cleanup", + (target) => + { + target.FullName = "Remove invalid users"; + target.Description = "Remove who didn't confirmed their e-mail in 14 days"; + sendHelpOption = target.HelpOption("-? | -h | --help"); + }, false); + + userCleanupCommandApp.OnExecute(async () => + { + bool showhelp = sendHelpOption.HasValue(); + + if (!showhelp) + { + var host = new WebHostBuilder(); + + var hostengnine = host.UseEnvironment(Program.HostingEnvironment.EnvironmentName) + .UseServer("cli") + .UseStartup() + .Build(); + var app = hostengnine.Start(); + + var mailer = app.Services.GetService(); + var loggerFactory = app.Services.GetService(); + var logger = loggerFactory.CreateLogger(); + var userManager = app.Services.GetService>(); + ApplicationDbContext dbContext = app.Services.GetService(); + Func criteria = UserPolicies.Criterias["user-to-remove"]; + + logger.LogInformation("Starting emailling"); + mailer.SendEmailFromCriteria("user-to-remove"); + foreach (ApplicationUser user in dbContext.ApplicationUser.Where( + u => criteria(u) + )) + { + dbContext.DeviceDeclaration.RemoveRange(dbContext.DeviceDeclaration.Where(g => g.DeviceOwnerId == user.Id)); + await userManager.DeleteAsync(user); + } + logger.LogInformation("Finished user cleanup"); + } + else + { + userCleanupCommandApp.ShowHelp(); + return 1; + } + return 0; + }); + return userCleanupCommandApp; + } + } +} diff --git a/src/cli/Services/EMailer.cs b/src/cli/Services/EMailer.cs index d3b1033c..c9ef9028 100644 --- a/src/cli/Services/EMailer.cs +++ b/src/cli/Services/EMailer.cs @@ -17,6 +17,7 @@ using Yavsc.Templates; using Yavsc.Abstract.Templates; using Yavsc.Services; using Yavsc.Abstract.Manage; +using Yavsc.Server.Settings; namespace cli.Services { @@ -61,12 +62,14 @@ namespace cli.Services } - public void SendEmailFromCriteria(string templateCode, Func criteria) + public void SendEmailFromCriteria(string templateCode) { string className = "GeneratedTemplate"; string subtemp = stringLocalizer["MonthlySubjectTemplate"].Value; + Func criteria = UserPolicies.Criterias[templateCode]; + logger.LogInformation($"Generating {subtemp}[{className}]"); var templateInfo = dbContext.MailingTemplate.FirstOrDefault(t => t.Id == templateCode);