You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/events.js

117 lines
2.9 KiB
JavaScript

11 years ago
'use strict';
11 years ago
var fs = require('fs'),
winston = require('winston'),
11 years ago
path = require('path'),
nconf = require('nconf'),
user = require('./user');
(function(events) {
11 years ago
var logFileName = 'logs/events.log';
11 years ago
events.logPasswordChange = function(uid) {
logWithUser(uid, 'changed password');
};
events.logAdminChangeUserPassword = function(adminUid, theirUid, callback) {
logAdminEvent(adminUid, theirUid, 'changed password of', callback);
};
events.logAdminUserDelete = function(adminUid, theirUid, callback) {
logAdminEvent(adminUid, theirUid, 'deleted', callback);
};
11 years ago
function logAdminEvent(adminUid, theirUid, message, callback) {
11 years ago
user.getMultipleUserFields([adminUid, theirUid], ['username'], function(err, userData) {
if(err) {
return winston.error('Error logging event. ' + err.message);
}
var msg = userData[0].username + '(uid ' + adminUid + ') ' + message + ' ' + userData[1].username + '(uid ' + theirUid + ')';
events.log(msg, callback);
11 years ago
});
}
11 years ago
events.logPasswordReset = function(uid) {
logWithUser(uid, 'reset password');
};
11 years ago
events.logEmailChange = function(uid, oldEmail, newEmail) {
logWithUser(uid,'changed email from "' + oldEmail + '" to "' + newEmail +'"');
};
11 years ago
events.logUsernameChange = function(uid, oldUsername, newUsername) {
logWithUser(uid,'changed username from "' + oldUsername + '" to "' + newUsername +'"');
};
11 years ago
events.logAdminLogin = function(uid) {
logWithUser(uid, 'logged into admin panel');
};
11 years ago
events.logPostEdit = function(uid, pid) {
logWithUser(uid, 'edited post (pid ' + pid + ')');
};
11 years ago
events.logPostDelete = function(uid, pid) {
logWithUser(uid, 'deleted post (pid ' + pid + ')');
};
11 years ago
events.logPostRestore = function(uid, pid) {
logWithUser(uid, 'restored post (pid ' + pid + ')');
};
11 years ago
events.logTopicDelete = function(uid, tid) {
logWithUser(uid, 'deleted topic (tid ' + tid + ')');
};
11 years ago
events.logTopicRestore = function(uid, tid) {
logWithUser(uid, 'restored topic (tid ' + tid + ')');
};
11 years ago
function logWithUser(uid, string) {
11 years ago
user.getUserField(uid, 'username', function(err, username) {
if(err) {
11 years ago
return winston.error('Error logging event. ' + err.message);
11 years ago
}
11 years ago
var msg = username + '(uid ' + uid + ') ' + string;
11 years ago
events.log(msg);
});
}
11 years ago
events.log = function(msg, callback) {
var logFile = path.join(nconf.get('base_dir'), logFileName);
11 years ago
11 years ago
msg = '[' + new Date().toUTCString() + '] - ' + msg;
fs.appendFile(logFile, msg + '\n', function(err) {
if(err) {
winston.error('Error logging event. ' + err.message);
if (typeof callback === 'function') {
callback(err);
}
return;
}
if (typeof callback === 'function') {
callback();
}
11 years ago
});
};
11 years ago
events.getLog = function(callback) {
var logFile = path.join(nconf.get('base_dir'), logFileName);
11 years ago
11 years ago
fs.readFile(logFile, function(err, res) {
if(err) {
return callback(null, 'No logs found!');
}
11 years ago
callback(null, res);
});
};
11 years ago
}(module.exports));