|
| 1 | +import fs from 'fs-extra'; |
| 2 | +import Handlebars from 'handlebars'; |
| 3 | +import path from 'path'; |
| 4 | +import { getAccountManager, loadConfig, loadUsernames } from './cli-utils.js'; |
| 5 | +import { isValidUsername } from '../../lib/common/user-utils.js'; |
| 6 | +import blacklistService from '../../lib/services/blacklist-service.js'; |
| 7 | +import { initConfigDir, initTemplateDirs } from '../../lib/server-config.js'; |
| 8 | +import { fromServerConfig } from '../../lib/models/oidc-manager.js'; |
| 9 | +import EmailService from '../../lib/services/email-service.js'; |
| 10 | +import SolidHost from '../../lib/models/solid-host.js'; |
| 11 | + |
| 12 | +export default function (program) { |
| 13 | + program |
| 14 | + .command('invalidusernames') |
| 15 | + .option('--notify', 'Will notify users with usernames that are invalid') |
| 16 | + .option('--delete', 'Will delete users with usernames that are invalid') |
| 17 | + .description('Manage usernames that are invalid') |
| 18 | + .action(async (options) => { |
| 19 | + const config = loadConfig(program, options); |
| 20 | + if (!config.multiuser) { |
| 21 | + return console.error('You are running a single user server, no need to check for invalid usernames'); |
| 22 | + } |
| 23 | + const invalidUsernames = getInvalidUsernames(config); |
| 24 | + const host = SolidHost.from({ port: config.port, serverUri: config.serverUri }); |
| 25 | + const accountManager = getAccountManager(config, { host }); |
| 26 | + if (options.notify) { |
| 27 | + return notifyUsers(invalidUsernames, accountManager, config); |
| 28 | + } |
| 29 | + if (options.delete) { |
| 30 | + return deleteUsers(invalidUsernames, accountManager, config, host); |
| 31 | + } |
| 32 | + listUsernames(invalidUsernames); |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +function backupIndexFile(username, accountManager, invalidUsernameTemplate, dateOfRemoval, supportEmail) { |
| 37 | + const userDirectory = accountManager.accountDirFor(username); |
| 38 | + const currentIndex = path.join(userDirectory, 'index.html'); |
| 39 | + const currentIndexExists = fs.existsSync(currentIndex); |
| 40 | + const backupIndex = path.join(userDirectory, 'index.backup.html'); |
| 41 | + const backupIndexExists = fs.existsSync(backupIndex); |
| 42 | + if (currentIndexExists && !backupIndexExists) { |
| 43 | + fs.renameSync(currentIndex, backupIndex); |
| 44 | + createNewIndexAcl(userDirectory); |
| 45 | + createNewIndex(username, invalidUsernameTemplate, dateOfRemoval, supportEmail, currentIndex); |
| 46 | + console.info(`index.html updated for user ${username}`); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +function createNewIndex(username, invalidUsernameTemplate, dateOfRemoval, supportEmail, currentIndex) { |
| 51 | + const newIndexSource = invalidUsernameTemplate({ |
| 52 | + username, |
| 53 | + dateOfRemoval, |
| 54 | + supportEmail |
| 55 | + }); |
| 56 | + fs.writeFileSync(currentIndex, newIndexSource, 'utf-8'); |
| 57 | +} |
| 58 | + |
| 59 | +function createNewIndexAcl(userDirectory) { |
| 60 | + const currentIndexAcl = path.join(userDirectory, 'index.html.acl'); |
| 61 | + const backupIndexAcl = path.join(userDirectory, 'index.backup.html.acl'); |
| 62 | + const currentIndexSource = fs.readFileSync(currentIndexAcl, 'utf-8'); |
| 63 | + const backupIndexSource = currentIndexSource.replace(/index.html/g, 'index.backup.html'); |
| 64 | + fs.writeFileSync(backupIndexAcl, backupIndexSource, 'utf-8'); |
| 65 | +} |
| 66 | + |
| 67 | +async function deleteUsers(usernames, accountManager, config, host) { |
| 68 | + const oidcManager = fromServerConfig({ ...config, host }); |
| 69 | + const deletingUsers = usernames.map(async username => { |
| 70 | + try { |
| 71 | + const user = accountManager.userAccountFrom({ username }); |
| 72 | + await oidcManager.users.deleteUser(user); |
| 73 | + } catch (error) { |
| 74 | + if (error.message !== 'No email given') { |
| 75 | + throw error; |
| 76 | + } |
| 77 | + } |
| 78 | + const userDirectory = accountManager.accountDirFor(username); |
| 79 | + await fs.remove(userDirectory); |
| 80 | + }); |
| 81 | + await Promise.all(deletingUsers); |
| 82 | + console.info(`Deleted ${deletingUsers.length} users succeeded`); |
| 83 | +} |
| 84 | + |
| 85 | +function getInvalidUsernames(config) { |
| 86 | + const usernames = loadUsernames(config); |
| 87 | + return usernames.filter(username => !isValidUsername(username) || !blacklistService.validate(username)); |
| 88 | +} |
| 89 | + |
| 90 | +function listUsernames(usernames) { |
| 91 | + if (usernames.length === 0) { |
| 92 | + return console.info('No invalid usernames was found'); |
| 93 | + } |
| 94 | + console.info(`${usernames.length} invalid usernames were found:${usernames.map(username => `\n- ${username}`)}`); |
| 95 | +} |
| 96 | + |
| 97 | +async function notifyUsers(usernames, accountManager, config) { |
| 98 | + const twoWeeksFromNow = Date.now() + 14 * 24 * 60 * 60 * 1000; |
| 99 | + const dateOfRemoval = (new Date(twoWeeksFromNow)).toLocaleDateString(); |
| 100 | + const { supportEmail } = config; |
| 101 | + updateIndexFiles(usernames, accountManager, dateOfRemoval, supportEmail); |
| 102 | + await sendEmails(config, usernames, accountManager, dateOfRemoval, supportEmail); |
| 103 | +} |
| 104 | + |
| 105 | +async function sendEmails(config, usernames, accountManager, dateOfRemoval, supportEmail) { |
| 106 | + if (config.email && config.email.host) { |
| 107 | + const configPath = initConfigDir(config); |
| 108 | + const templates = initTemplateDirs(configPath); |
| 109 | + const users = await Promise.all(await usernames.map(async username => { |
| 110 | + const emailAddress = await accountManager.loadAccountRecoveryEmail({ username }); |
| 111 | + const accountUri = accountManager.accountUriFor(username); |
| 112 | + return { username, emailAddress, accountUri }; |
| 113 | + })); |
| 114 | + const emailService = new EmailService(templates.email, config.email); |
| 115 | + const sendingEmails = users |
| 116 | + .filter(user => !!user.emailAddress) |
| 117 | + .map(user => emailService.sendWithTemplate('invalid-username', { |
| 118 | + to: user.emailAddress, |
| 119 | + accountUri: user.accountUri, |
| 120 | + dateOfRemoval, |
| 121 | + supportEmail |
| 122 | + })); |
| 123 | + const emailsSent = await Promise.all(sendingEmails); |
| 124 | + console.info(`${emailsSent.length} emails sent to users with invalid usernames`); |
| 125 | + return; |
| 126 | + } |
| 127 | + console.info('You have not configured an email service.'); |
| 128 | + console.info('Please set it up to send users email about their accounts'); |
| 129 | +} |
| 130 | + |
| 131 | +function updateIndexFiles(usernames, accountManager, dateOfRemoval, supportEmail) { |
| 132 | + const invalidUsernameFilePath = path.join(process.cwd(), 'default-views', 'account', 'invalid-username.hbs'); |
| 133 | + const source = fs.readFileSync(invalidUsernameFilePath, 'utf-8'); |
| 134 | + const invalidUsernameTemplate = Handlebars.compile(source); |
| 135 | + usernames.forEach(username => backupIndexFile(username, accountManager, invalidUsernameTemplate, dateOfRemoval, supportEmail)); |
| 136 | +} |
0 commit comments