+ oputil.js user rm USERNAME

* Fix some of my horrid spelling...
This commit is contained in:
Bryan Ashby 2018-12-15 23:42:19 -07:00
parent fd5b50fc08
commit 3eaf4dd0d8
4 changed files with 118 additions and 14 deletions

View File

@ -20,7 +20,7 @@ exports.getTransactionDatabase = getTransactionDatabase;
exports.getModDatabasePath = getModDatabasePath; exports.getModDatabasePath = getModDatabasePath;
exports.loadDatabaseForMod = loadDatabaseForMod; exports.loadDatabaseForMod = loadDatabaseForMod;
exports.getISOTimestampString = getISOTimestampString; exports.getISOTimestampString = getISOTimestampString;
exports.sanatizeString = sanatizeString; exports.sanitizeString = sanitizeString;
exports.initializeDatabases = initializeDatabases; exports.initializeDatabases = initializeDatabases;
exports.dbs = dbs; exports.dbs = dbs;
@ -76,7 +76,7 @@ function getISOTimestampString(ts) {
return ts.format('YYYY-MM-DDTHH:mm:ss.SSSZ'); return ts.format('YYYY-MM-DDTHH:mm:ss.SSSZ');
} }
function sanatizeString(s) { function sanitizeString(s) {
return s.replace(/[\0\x08\x09\x1a\n\r"'\\%]/g, c => { // eslint-disable-line no-control-regex return s.replace(/[\0\x08\x09\x1a\n\r"'\\%]/g, c => { // eslint-disable-line no-control-regex
switch (c) { switch (c) {
case '\0' : return '\\0'; case '\0' : return '\\0';

View File

@ -5,7 +5,7 @@ const fileDb = require('./database.js').dbs.file;
const Errors = require('./enig_error.js').Errors; const Errors = require('./enig_error.js').Errors;
const { const {
getISOTimestampString, getISOTimestampString,
sanatizeString sanitizeString
} = require('./database.js'); } = require('./database.js');
const Config = require('./config.js').get; const Config = require('./config.js').get;
@ -565,14 +565,14 @@ module.exports = class FileEntry {
`f.file_id IN ( `f.file_id IN (
SELECT rowid SELECT rowid
FROM file_fts FROM file_fts
WHERE file_fts MATCH ":${sanatizeString(filter.terms)}" WHERE file_fts MATCH ":${sanitizeString(filter.terms)}"
)` )`
); );
} }
if(filter.tags && filter.tags.length > 0) { if(filter.tags && filter.tags.length > 0) {
// build list of quoted tags; filter.tags comes in as a space and/or comma separated values // build list of quoted tags; filter.tags comes in as a space and/or comma separated values
const tags = filter.tags.replace(/,/g, ' ').replace(/\s{2,}/g, ' ').split(' ').map( tag => `"${sanatizeString(tag)}"` ).join(','); const tags = filter.tags.replace(/,/g, ' ').replace(/\s{2,}/g, ' ').split(' ').map( tag => `"${sanitizeString(tag)}"` ).join(',');
appendWhereClause( appendWhereClause(
`f.file_id IN ( `f.file_id IN (

View File

@ -8,7 +8,7 @@ const createNamedUUID = require('./uuid_util.js').createNamedUUID;
const Errors = require('./enig_error.js').Errors; const Errors = require('./enig_error.js').Errors;
const ANSI = require('./ansi_term.js'); const ANSI = require('./ansi_term.js');
const { const {
sanatizeString, sanitizeString,
getISOTimestampString } = require('./database.js'); getISOTimestampString } = require('./database.js');
const { const {
@ -354,7 +354,7 @@ module.exports = class Message {
[ 'toUserName', 'fromUserName' ].forEach(field => { [ 'toUserName', 'fromUserName' ].forEach(field => {
if(_.isString(filter[field]) && filter[field].length > 0) { if(_.isString(filter[field]) && filter[field].length > 0) {
appendWhereClause(`m.${_.snakeCase(field)} LIKE "${sanatizeString(filter[field])}"`); appendWhereClause(`m.${_.snakeCase(field)} LIKE "${sanitizeString(filter[field])}"`);
} }
}); });
@ -375,7 +375,7 @@ module.exports = class Message {
`m.message_id IN ( `m.message_id IN (
SELECT rowid SELECT rowid
FROM message_fts FROM message_fts
WHERE message_fts MATCH ":${sanatizeString(filter.terms)}" WHERE message_fts MATCH ":${sanitizeString(filter.terms)}"
)` )`
); );
} }

View File

@ -2,10 +2,13 @@
/* eslint-disable no-console */ /* eslint-disable no-console */
'use strict'; 'use strict';
const printUsageAndSetExitCode = require('./oputil_common.js').printUsageAndSetExitCode; const {
const ExitCodes = require('./oputil_common.js').ExitCodes; printUsageAndSetExitCode,
const argv = require('./oputil_common.js').argv; getAnswers,
const initConfigAndDatabases = require('./oputil_common.js').initConfigAndDatabases; ExitCodes,
argv,
initConfigAndDatabases
} = require('./oputil_common.js');
const getHelpFor = require('./oputil_help.js').getHelpFor; const getHelpFor = require('./oputil_help.js').getHelpFor;
const Errors = require('../enig_error.js').Errors; const Errors = require('../enig_error.js').Errors;
const UserProps = require('../user_property.js'); const UserProps = require('../user_property.js');
@ -111,8 +114,109 @@ function setUserPassword(user) {
); );
} }
function removeUser() { function removeUserRecordsFromDbAndTable(dbName, tableName, userId, col, cb) {
console.error('NOT YET IMPLEMENTED'); const db = require('../../core/database.js').dbs[dbName];
db.run(
`DELETE FROM ${tableName}
WHERE ${col} = ?;`,
[ userId ],
err => {
return cb(err);
}
);
}
function removeUser(user) {
async.series(
[
(callback) => {
if(false === argv.prompt) {
return callback(null);
}
console.info('About to permanently delete the following user:');
console.info(`Username : ${user.username}`);
console.info(`Real name: ${user.properties[UserProps.RealName] || 'N/A'}`);
console.info(`User ID : ${user.userId}`);
console.info('WARNING: This cannot be undone!');
getAnswers([
{
name : 'proceed',
message : `Proceed in deleting ${user.username}?`,
type : 'confirm',
}
],
answers => {
if(answers.proceed) {
return callback(null);
}
return callback(Errors.General('User canceled'));
});
},
(callback) => {
// op has confirmed they are wanting ready to proceed (or passed --no-prompt)
const DeleteFrom = {
message : [ 'user_message_area_last_read' ],
system : [ 'user_event_log', ],
user : [ 'user_group_member', 'user' ],
};
async.eachSeries(Object.keys(DeleteFrom), (dbName, nextDbName) => {
const tables = DeleteFrom[dbName];
async.eachSeries(tables, (tableName, nextTableName) => {
const col = ('user' === dbName && 'user' === tableName) ? 'id' : 'user_id';
removeUserRecordsFromDbAndTable(dbName, tableName, user.userId, col, err => {
return nextTableName(err);
});
},
err => {
return nextDbName(err);
});
},
err => {
return callback(err);
});
},
(callback) => {
//
// Clean up *private* messages *to* this user
//
const Message = require('../../core/message.js');
const MsgDb = require('../../core/database.js').dbs.message;
const filter = {
resultType : 'id',
privateTagUserId : user.userId,
};
Message.findMessages(filter, (err, ids) => {
if(err) {
return callback(err);
}
async.eachSeries(ids, (messageId, nextMessageId) => {
MsgDb.run(
`DELETE FROM message
WHERE message_id = ?;`,
[ messageId ],
err => {
return nextMessageId(err);
}
);
},
err => {
return callback(err);
});
});
}
],
err => {
if(err) {
return console.error(err.reason ? err.reason : err.message);
}
console.info('User has been deleted.');
}
);
} }
function modUserGroups(user) { function modUserGroups(user) {