@@ -2,7 +2,6 @@ const fs = require('fs-extra')
22const Handlebars = require ( 'handlebars' )
33const path = require ( 'path' )
44const { URL } = require ( 'url' )
5- const util = require ( 'util' )
65
76const { loadConfig } = require ( './common' )
87const { isValidUsername } = require ( '../../lib/common/user-utils' )
@@ -15,22 +14,19 @@ const EmailService = require('../../lib/services/email-service')
1514const LDP = require ( '../../lib/ldp' )
1615const SolidHost = require ( '../../lib/models/solid-host' )
1716
18- const fileExists = util . promisify ( fs . exists )
19- const fileRename = util . promisify ( fs . rename )
20-
2117module . exports = function ( program ) {
2218 program
2319 . command ( 'invalidusernames' )
2420 . option ( '--notify' , 'Will notify users with usernames that are invalid' )
2521 . option ( '--delete' , 'Will delete users with usernames that are invalid' )
2622 . description ( 'Manage usernames that are invalid' )
2723 . action ( async ( options ) => {
28- const config = await loadConfig ( program , options )
24+ const config = loadConfig ( program , options )
2925 if ( ! config . multiuser ) {
3026 return console . error ( 'You are running a single user server, no need to check for invalid usernames' )
3127 }
3228
33- const invalidUsernames = await getInvalidUsernames ( config )
29+ const invalidUsernames = getInvalidUsernames ( config )
3430 const host = SolidHost . from ( { port : config . port , serverUri : config . serverUri } )
3531 const accountManager = getAccountManager ( config , host )
3632
@@ -42,28 +38,41 @@ module.exports = function (program) {
4238 return deleteUsers ( invalidUsernames , accountManager , config , host )
4339 }
4440
45- listUsernames ( listUsernames )
41+ listUsernames ( invalidUsernames )
4642 } )
4743}
4844
49- async function createNewIndexFile ( username , accountManager , invalidUsernameTemplate , dateOfRemoval , supportEmail , fileOptions ) {
45+ function backupIndexFile ( username , accountManager , invalidUsernameTemplate , dateOfRemoval , supportEmail ) {
5046 const userDirectory = accountManager . accountDirFor ( username )
5147 const currentIndex = path . join ( userDirectory , 'index.html' )
52- const currentIndexExists = await fileExists ( currentIndex )
48+ const currentIndexExists = fs . existsSync ( currentIndex )
5349 const backupIndex = path . join ( userDirectory , 'index.backup.html' )
54- const backupIndexExists = await fileExists ( backupIndex )
50+ const backupIndexExists = fs . existsSync ( backupIndex )
5551 if ( currentIndexExists && ! backupIndexExists ) {
56- await fileRename ( currentIndex , backupIndex )
57- const newIndexSource = invalidUsernameTemplate ( {
58- username,
59- dateOfRemoval,
60- supportEmail
61- } )
62- fs . writeFileSync ( currentIndex , newIndexSource , fileOptions )
52+ fs . renameSync ( currentIndex , backupIndex )
53+ createNewIndexAcl ( userDirectory )
54+ createNewIndex ( username , invalidUsernameTemplate , dateOfRemoval , supportEmail , currentIndex )
6355 console . info ( `index.html updated for user ${ username } ` )
6456 }
6557}
6658
59+ function createNewIndex ( username , invalidUsernameTemplate , dateOfRemoval , supportEmail , currentIndex ) {
60+ const newIndexSource = invalidUsernameTemplate ( {
61+ username,
62+ dateOfRemoval,
63+ supportEmail
64+ } )
65+ fs . writeFileSync ( currentIndex , newIndexSource , 'utf-8' )
66+ }
67+
68+ function createNewIndexAcl ( userDirectory ) {
69+ const currentIndexAcl = path . join ( userDirectory , 'index.html.acl' )
70+ const backupIndexAcl = path . join ( userDirectory , 'index.backup.html.acl' )
71+ const currentIndexSource = fs . readFileSync ( currentIndexAcl , 'utf-8' )
72+ const backupIndexSource = currentIndexSource . replace ( / i n d e x .h t m l / g, 'index.backup.html' )
73+ fs . writeFileSync ( backupIndexAcl , backupIndexSource , 'utf-8' )
74+ }
75+
6776async function deleteUsers ( usernames , accountManager , config , host ) {
6877 const oidcManager = fromServerConfig ( {
6978 ...config ,
@@ -96,8 +105,8 @@ function getAccountManager (config, host) {
96105 } )
97106}
98107
99- async function getInvalidUsernames ( config ) {
100- const files = await util . promisify ( fs . readdir ) ( config . root )
108+ function getInvalidUsernames ( config ) {
109+ const files = fs . readdirSync ( config . root )
101110 const hostname = new URL ( config . serverUri ) . hostname
102111 const isUserDirectory = new RegExp ( `.${ hostname } $` )
103112 return files
@@ -108,7 +117,7 @@ async function getInvalidUsernames (config) {
108117
109118function listUsernames ( usernames ) {
110119 if ( usernames . length === 0 ) {
111- console . info ( 'No invalid usernames was found' )
120+ return console . info ( 'No invalid usernames was found' )
112121 }
113122 console . info ( `${ usernames . length } invalid usernames were found:${ usernames . map ( username => `\n- ${ username } ` ) } ` )
114123}
@@ -118,7 +127,7 @@ async function notifyUsers (usernames, accountManager, config) {
118127 const dateOfRemoval = ( new Date ( twoWeeksFromNow ) ) . toLocaleDateString ( )
119128 const { supportEmail } = config
120129
121- await updateIndexFiles ( usernames , accountManager , dateOfRemoval , supportEmail )
130+ updateIndexFiles ( usernames , accountManager , dateOfRemoval , supportEmail )
122131 await sendEmails ( config , usernames , accountManager , dateOfRemoval , supportEmail )
123132}
124133
@@ -132,9 +141,9 @@ async function sendEmails (config, usernames, accountManager, dateOfRemoval, sup
132141 return { username, emailAddress, accountUri }
133142 } ) )
134143 const emailService = new EmailService ( templates . email , config . email )
135- const sendingEmails = await users
144+ const sendingEmails = users
136145 . filter ( user => ! ! user . emailAddress )
137- . map ( async user => await emailService . sendWithTemplate ( 'invalid-username' , {
146+ . map ( user => emailService . sendWithTemplate ( 'invalid-username' , {
138147 to : user . emailAddress ,
139148 accountUri : user . accountUri ,
140149 dateOfRemoval,
@@ -148,13 +157,9 @@ async function sendEmails (config, usernames, accountManager, dateOfRemoval, sup
148157 console . info ( 'Please set it up to send users email about their accounts' )
149158}
150159
151- async function updateIndexFiles ( usernames , accountManager , dateOfRemoval , supportEmail ) {
152- const invalidUsernameFilePath = path . join ( process . cwd ( ) , 'default-views/account/invalid-username.hbs' )
153- const fileOptions = {
154- encoding : 'utf-8'
155- }
156- const source = fs . readFileSync ( invalidUsernameFilePath , fileOptions )
160+ function updateIndexFiles ( usernames , accountManager , dateOfRemoval , supportEmail ) {
161+ const invalidUsernameFilePath = path . join ( process . cwd ( ) , 'default-views' , 'account' , 'invalid-username.hbs' )
162+ const source = fs . readFileSync ( invalidUsernameFilePath , 'utf-8' )
157163 const invalidUsernameTemplate = Handlebars . compile ( source )
158- const updatingFiles = usernames . map ( username => createNewIndexFile ( username , accountManager , invalidUsernameTemplate , dateOfRemoval , supportEmail , fileOptions ) )
159- return Promise . all ( updatingFiles )
164+ usernames . forEach ( username => backupIndexFile ( username , accountManager , invalidUsernameTemplate , dateOfRemoval , supportEmail ) )
160165}
0 commit comments