Skip to content

Commit 25d8075

Browse files
committed
Listing blacklisted usernames that exists
1 parent 066203a commit 25d8075

File tree

4 files changed

+81
-20
lines changed

4 files changed

+81
-20
lines changed

bin/lib/blacklist.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const { loadConfig } = require('./common')
2+
const blacklistService = require('../../lib/services/blacklist-service')
3+
const AccountManager = require('../../lib/models/account-manager')
4+
const LDP = require('../../lib/ldp')
5+
const SolidHost = require('../../lib/models/solid-host')
6+
7+
module.exports = function (program) {
8+
program
9+
.command('blacklist')
10+
.option('--notify', 'Will notify users with usernames that are blacklisted')
11+
.option('--delete', 'Will delete users with usernames that are blacklisted')
12+
.description('Manage usernames that are blacklisted')
13+
.action(async (options) => {
14+
const config = await loadConfig(program, options)
15+
if (!config.multiuser) {
16+
return console.error('You are running a single user server, no need to check for blacklisted users')
17+
}
18+
const host = SolidHost.from({ port: config.port, serverUri: config.serverUri })
19+
20+
const ldp = new LDP(config)
21+
const accountManager = AccountManager.from({
22+
// authMethod: argv.auth,
23+
// emailService: app.locals.emailService,
24+
// tokenService: app.locals.tokenService,
25+
host,
26+
// accountTemplatePath: argv.templates.account,
27+
store: ldp,
28+
multiuser: config.multiuser
29+
})
30+
const blacklistedUsernames = await getBlacklistedUsernames(accountManager)
31+
if (blacklistedUsernames.length === 0) {
32+
console.log('No blacklisted username was found')
33+
}
34+
console.log(`These blacklisted usernames were found:${blacklistedUsernames.map(username => `\n- ${username}`)}`)
35+
})
36+
}
37+
38+
async function getBlacklistedUsernames (accountManager) {
39+
const blacklistedUsernames = []
40+
await Promise.all(blacklistService.list.map(async (word) => {
41+
const accountExists = await accountManager.accountExists(word)
42+
if (accountExists) {
43+
blacklistedUsernames.push(word)
44+
}
45+
}))
46+
return blacklistedUsernames
47+
}

bin/lib/cli.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const program = require('commander')
22
const loadInit = require('./init')
33
const loadStart = require('./start')
4+
const loadBlacklist = require('./blacklist')
45
const { spawnSync } = require('child_process')
56
const path = require('path')
67

@@ -9,6 +10,7 @@ module.exports = function startCli (server) {
910

1011
loadInit(program)
1112
loadStart(program, server)
13+
loadBlacklist(program)
1214

1315
program.parse(process.argv)
1416
if (program.args.length === 0) program.help()

bin/lib/common.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const fs = require('fs')
2+
const extend = require('extend')
3+
const { cyan, bold } = require('colorette')
4+
const util = require('util')
5+
6+
module.exports = {}
7+
module.exports.loadConfig = loadConfig
8+
9+
async function loadConfig (program, options) {
10+
let argv = extend({}, options, { version: program.version() })
11+
let configFile = argv['configFile'] || './config.json'
12+
13+
try {
14+
const file = await util.promisify(fs.readFile)(configFile)
15+
16+
// Use flags with priority over config file
17+
const config = JSON.parse(file)
18+
Object.keys(config).forEach((option) => {
19+
argv[option] = argv[option] || config[option]
20+
})
21+
} catch (err) {
22+
// No file exists, not a problem
23+
console.log(cyan(bold('TIP')), 'create a config.json: `$ solid init`')
24+
}
25+
26+
return argv
27+
}

bin/lib/start.js

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
const options = require('./options')
44
const fs = require('fs')
5-
const extend = require('extend')
6-
const { cyan, red, bold } = require('colorette')
5+
const { loadConfig } = require('./common')
6+
const { red, bold } = require('colorette')
77

88
module.exports = function (program, server) {
99
const start = program
@@ -34,24 +34,9 @@ module.exports = function (program, server) {
3434

3535
start.option('-v, --verbose', 'Print the logs to console')
3636

37-
start.action((opts) => {
38-
let argv = extend({}, opts, { version: program.version() })
39-
let configFile = argv['configFile'] || './config.json'
40-
41-
fs.readFile(configFile, (err, file) => {
42-
// No file exists, not a problem
43-
if (err) {
44-
console.log(cyan(bold('TIP')), 'create a config.json: `$ solid init`')
45-
} else {
46-
// Use flags with priority over config file
47-
const config = JSON.parse(file)
48-
Object.keys(config).forEach((option) => {
49-
argv[option] = argv[option] || config[option]
50-
})
51-
}
52-
53-
bin(argv, server)
54-
})
37+
start.action(async (options) => {
38+
const config = await loadConfig(program, options)
39+
bin(config, server)
5540
})
5641
}
5742

0 commit comments

Comments
 (0)