aboutsummaryrefslogblamecommitdiffhomepage
path: root/admin
blob: 755680ff910b65f6f34d6647daa943b253e9f9c8 (plain) (tree)








































































































                                                                                                               
#!/usr/bin/env node

'use strict';

var program = require('commander'),
    safe = require('safetydance'),
    bcrypt = require('bcryptjs'),
    path = require('path');

function exit(error) {
    if (error) console.error(error);
    process.exit(error ? 1 : 0);
}

var LOCAL_AUTH_FILE = path.resolve(process.env.LOCAL_AUTH_FILE || './.users.json');

console.log('Using local auth file: ', LOCAL_AUTH_FILE);

function addUser(options) {
    if (!options.username) exit('missing --username');
    if (!options.password) exit('missing --password');

    var users = safe.require(LOCAL_AUTH_FILE);
    if (!users) users = {};
    if (users[options.username]) exit('User already exists');

    bcrypt.hash(options.password, 8, function(error, hash) {
        users[options.username] = { username: options.username, passwordHash: hash };
        safe.fs.writeFileSync(LOCAL_AUTH_FILE, JSON.stringify(users, null, 4));

        console.log('Done.');
    });
}

function editUser(options) {
    if (!options.username) exit('missing --username');
    if (!options.password) exit('missing --password');

    var users = safe.require(LOCAL_AUTH_FILE);
    if (!users) users = {};
    if (!users[options.username]) exit('No such user');

    bcrypt.hash(options.password, 8, function(error, hash) {
        users[options.username] = { username: options.username, passwordHash: hash };
        safe.fs.writeFileSync(LOCAL_AUTH_FILE, JSON.stringify(users, null, 4));

        console.log('Done.');
    });
}

function delUser(options) {
    if (!options.username) exit('missing --username');

    var users = safe.require(LOCAL_AUTH_FILE);
    if (!users) exit('No such user');

    if (!users[options.username]) exit('No such user');

    delete users[options.username];
    safe.fs.writeFileSync(LOCAL_AUTH_FILE, JSON.stringify(users, null, 4));

    console.log('Done.');
}

function listUsers() {
    var users = safe.require(LOCAL_AUTH_FILE);
    if (!users) exit('No users. Use `admin user-add` to add some.');

    console.log(users);
}

program.version('0.1.0');

program.command('user-add')
    .description('Add local user')
    .option('-u --username <username>', 'New username')
    .option('-p --password <password>', 'New password')
    .action(addUser);

program.command('user-edit')
    .description('Edit local user')
    .option('-u --username <username>', 'Username')
    .option('-p --password <password>', 'New password')
    .action(editUser);

program.command('user-del')
    .description('Delete local user')
    .option('-u --username <username>', 'Username')
    .action(delUser);

program.command('users')
    .description('List local users')
    .action(listUsers);

program.parse(process.argv);

if (!process.argv.slice(2).length) {
    program.outputHelp();
} else { // https://github.com/tj/commander.js/issues/338
    var knownCommand = program.commands.some(function (command) { return command._name === process.argv[2]; });
    if (!knownCommand) {
        console.error('Unknown command: ' + process.argv[2]);
        process.exit(1);
    }
}