1- import fs from 'fs-extra' ;
2- import Handlebars from 'handlebars' ;
3- import path from 'path' ;
4- import { getAccountManager , loadConfig , loadUsernames } from './cli-utils.mjs' ;
5- import { isValidUsername } from '../../lib/common/user-utils.mjs' ;
6- import blacklistService from '../../lib/services/blacklist-service.mjs' ;
7- import { initConfigDir , initTemplateDirs } from '../../lib/server-config.mjs' ;
8- import { fromServerConfig } from '../../lib/models/oidc-manager.mjs' ;
9- import EmailService from '../../lib/services/email-service.mjs' ;
10- import SolidHost from '../../lib/models/solid-host.mjs' ;
1+ import fs from 'fs-extra'
2+ import Handlebars from 'handlebars'
3+ import path from 'path'
4+ import { getAccountManager , loadConfig , loadUsernames } from './cli-utils.mjs'
5+ import { isValidUsername } from '../../lib/common/user-utils.mjs'
6+ import blacklistService from '../../lib/services/blacklist-service.mjs'
7+ import { initConfigDir , initTemplateDirs } from '../../lib/server-config.mjs'
8+ import { fromServerConfig } from '../../lib/models/oidc-manager.mjs'
9+ import EmailService from '../../lib/services/email-service.mjs'
10+ import SolidHost from '../../lib/models/solid-host.mjs'
1111
1212export default function ( program ) {
1313 program
@@ -16,121 +16,121 @@ export default function (program) {
1616 . option ( '--delete' , 'Will delete users with usernames that are invalid' )
1717 . description ( 'Manage usernames that are invalid' )
1818 . action ( async ( options ) => {
19- const config = loadConfig ( program , options ) ;
19+ const config = loadConfig ( program , options )
2020 if ( ! config . multiuser ) {
21- return console . error ( 'You are running a single user server, no need to check for invalid usernames' ) ;
21+ return console . error ( 'You are running a single user server, no need to check for invalid usernames' )
2222 }
23- const invalidUsernames = getInvalidUsernames ( config ) ;
24- const host = SolidHost . from ( { port : config . port , serverUri : config . serverUri } ) ;
25- const accountManager = getAccountManager ( config , { host } ) ;
23+ const invalidUsernames = getInvalidUsernames ( config )
24+ const host = SolidHost . from ( { port : config . port , serverUri : config . serverUri } )
25+ const accountManager = getAccountManager ( config , { host } )
2626 if ( options . notify ) {
27- return notifyUsers ( invalidUsernames , accountManager , config ) ;
27+ return notifyUsers ( invalidUsernames , accountManager , config )
2828 }
2929 if ( options . delete ) {
30- return deleteUsers ( invalidUsernames , accountManager , config , host ) ;
30+ return deleteUsers ( invalidUsernames , accountManager , config , host )
3131 }
32- listUsernames ( invalidUsernames ) ;
33- } ) ;
32+ listUsernames ( invalidUsernames )
33+ } )
3434}
3535
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 ) ;
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 )
4242 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 } ` ) ;
43+ fs . renameSync ( currentIndex , backupIndex )
44+ createNewIndexAcl ( userDirectory )
45+ createNewIndex ( username , invalidUsernameTemplate , dateOfRemoval , supportEmail , currentIndex )
46+ console . info ( `index.html updated for user ${ username } ` )
4747 }
4848}
4949
50- function createNewIndex ( username , invalidUsernameTemplate , dateOfRemoval , supportEmail , currentIndex ) {
50+ function createNewIndex ( username , invalidUsernameTemplate , dateOfRemoval , supportEmail , currentIndex ) {
5151 const newIndexSource = invalidUsernameTemplate ( {
5252 username,
5353 dateOfRemoval,
5454 supportEmail
55- } ) ;
56- fs . writeFileSync ( currentIndex , newIndexSource , 'utf-8' ) ;
55+ } )
56+ fs . writeFileSync ( currentIndex , newIndexSource , 'utf-8' )
5757}
5858
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 ( / i n d e x .h t m l / g, 'index.backup.html' ) ;
64- fs . writeFileSync ( backupIndexAcl , backupIndexSource , 'utf-8' ) ;
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 ( / i n d e x .h t m l / g, 'index.backup.html' )
64+ fs . writeFileSync ( backupIndexAcl , backupIndexSource , 'utf-8' )
6565}
6666
67- async function deleteUsers ( usernames , accountManager , config , host ) {
68- const oidcManager = fromServerConfig ( { ...config , host } ) ;
67+ async function deleteUsers ( usernames , accountManager , config , host ) {
68+ const oidcManager = fromServerConfig ( { ...config , host } )
6969 const deletingUsers = usernames . map ( async username => {
7070 try {
71- const user = accountManager . userAccountFrom ( { username } ) ;
72- await oidcManager . users . deleteUser ( user ) ;
71+ const user = accountManager . userAccountFrom ( { username } )
72+ await oidcManager . users . deleteUser ( user )
7373 } catch ( error ) {
7474 if ( error . message !== 'No email given' ) {
75- throw error ;
75+ throw error
7676 }
7777 }
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` ) ;
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` )
8383}
8484
85- function getInvalidUsernames ( config ) {
86- const usernames = loadUsernames ( config ) ;
87- return usernames . filter ( username => ! isValidUsername ( username ) || ! blacklistService . validate ( username ) ) ;
85+ function getInvalidUsernames ( config ) {
86+ const usernames = loadUsernames ( config )
87+ return usernames . filter ( username => ! isValidUsername ( username ) || ! blacklistService . validate ( username ) )
8888}
8989
90- function listUsernames ( usernames ) {
90+ function listUsernames ( usernames ) {
9191 if ( usernames . length === 0 ) {
92- return console . info ( 'No invalid usernames was found' ) ;
92+ return console . info ( 'No invalid usernames was found' )
9393 }
94- console . info ( `${ usernames . length } invalid usernames were found:${ usernames . map ( username => `\n- ${ username } ` ) } ` ) ;
94+ console . info ( `${ usernames . length } invalid usernames were found:${ usernames . map ( username => `\n- ${ username } ` ) } ` )
9595}
9696
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 ) ;
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 )
103103}
104104
105- async function sendEmails ( config , usernames , accountManager , dateOfRemoval , supportEmail ) {
105+ async function sendEmails ( config , usernames , accountManager , dateOfRemoval , supportEmail ) {
106106 if ( config . email && config . email . host ) {
107- const configPath = initConfigDir ( config ) ;
108- const templates = initTemplateDirs ( configPath ) ;
107+ const configPath = initConfigDir ( config )
108+ const templates = initTemplateDirs ( configPath )
109109 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 ) ;
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 )
115115 const sendingEmails = users
116116 . filter ( user => ! ! user . emailAddress )
117117 . map ( user => emailService . sendWithTemplate ( 'invalid-username.mjs' , {
118118 to : user . emailAddress ,
119119 accountUri : user . accountUri ,
120120 dateOfRemoval,
121121 supportEmail
122- } ) ) ;
123- const emailsSent = await Promise . all ( sendingEmails ) ;
124- console . info ( `${ emailsSent . length } emails sent to users with invalid usernames` ) ;
125- return ;
122+ } ) )
123+ const emailsSent = await Promise . all ( sendingEmails )
124+ console . info ( `${ emailsSent . length } emails sent to users with invalid usernames` )
125+ return
126126 }
127- console . info ( 'You have not configured an email service.' ) ;
128- console . info ( 'Please set it up to send users email about their accounts' ) ;
127+ console . info ( 'You have not configured an email service.' )
128+ console . info ( 'Please set it up to send users email about their accounts' )
129129}
130130
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 ) ) ;
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 ) )
136136}
0 commit comments