user list cleanup
This commit is contained in:
17
src/Yavsc.Server/Settings/UserPolicies.cs
Normal file
17
src/Yavsc.Server/Settings/UserPolicies.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Yavsc.Models;
|
||||
|
||||
namespace Yavsc.Server.Settings
|
||||
{
|
||||
public class UserPolicies
|
||||
{
|
||||
public static readonly Dictionary<string, Func<ApplicationUser, bool>> Criterias =
|
||||
new Dictionary<string, Func<ApplicationUser, bool>>
|
||||
{
|
||||
{ "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) }
|
||||
};
|
||||
}
|
||||
}
|
@ -5,16 +5,6 @@ using Yavsc.Models;
|
||||
|
||||
namespace Yavsc.Templates
|
||||
{
|
||||
public static class TemplateConstants
|
||||
{
|
||||
public static readonly Dictionary<string, Func<ApplicationUser, bool>> Criterias =
|
||||
new Dictionary<string, Func<ApplicationUser, bool>>
|
||||
{
|
||||
{ "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; }
|
||||
|
@ -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<ILoggerFactory>();
|
||||
var logger = loggerFactory.CreateLogger<Program>();
|
||||
logger.LogInformation("Starting emailling");
|
||||
mailer.SendEmailFromCriteria(critCommandArg.Value, TemplateConstants.Criterias[critCommandArg.Value]);
|
||||
mailer.SendEmailFromCriteria(critCommandArg.Value);
|
||||
logger.LogInformation("Finished emailling");
|
||||
}
|
||||
else
|
||||
|
71
src/cli/Commands/UserListCleanUp.cs
Normal file
71
src/cli/Commands/UserListCleanUp.cs
Normal file
@ -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<Startup>()
|
||||
.Build();
|
||||
var app = hostengnine.Start();
|
||||
|
||||
var mailer = app.Services.GetService<EMailer>();
|
||||
var loggerFactory = app.Services.GetService<ILoggerFactory>();
|
||||
var logger = loggerFactory.CreateLogger<Program>();
|
||||
var userManager = app.Services.GetService<UserManager<ApplicationUser>>();
|
||||
ApplicationDbContext dbContext = app.Services.GetService<ApplicationDbContext>();
|
||||
Func<ApplicationUser, bool> 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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<ApplicationUser,bool> criteria)
|
||||
public void SendEmailFromCriteria(string templateCode)
|
||||
{
|
||||
string className = "GeneratedTemplate";
|
||||
|
||||
string subtemp = stringLocalizer["MonthlySubjectTemplate"].Value;
|
||||
|
||||
Func<ApplicationUser,bool> criteria = UserPolicies.Criterias[templateCode];
|
||||
|
||||
logger.LogInformation($"Generating {subtemp}[{className}]");
|
||||
|
||||
var templateInfo = dbContext.MailingTemplate.FirstOrDefault(t => t.Id == templateCode);
|
||||
|
Reference in New Issue
Block a user