2016-04-29 23:49:56 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
EventLogError: EventLogError,
|
|
|
|
|
|
|
|
|
|
add: add,
|
|
|
|
|
get: get,
|
2016-04-30 11:49:51 -07:00
|
|
|
getAllPaged: getAllPaged,
|
|
|
|
|
|
2016-04-30 19:49:50 -07:00
|
|
|
// keep in sync with webadmin index.js filter
|
2016-04-30 13:56:03 -07:00
|
|
|
ACTION_ACTIVATE: 'cloudron.activate',
|
2016-04-30 13:34:06 -07:00
|
|
|
ACTION_APP_CONFIGURE: 'app.configure',
|
|
|
|
|
ACTION_APP_INSTALL: 'app.install',
|
|
|
|
|
ACTION_APP_RESTORE: 'app.restore',
|
|
|
|
|
ACTION_APP_UNINSTALL: 'app.uninstall',
|
|
|
|
|
ACTION_APP_UPDATE: 'app.update',
|
2016-04-30 13:56:03 -07:00
|
|
|
ACTION_BACKUP: 'cloudron.backup',
|
2016-04-30 22:27:33 -07:00
|
|
|
ACTION_CERTIFICATE_RENEWAL: 'certificate.renew',
|
2016-04-30 11:49:51 -07:00
|
|
|
ACTION_CLI_MODE: 'settings.climode',
|
2016-04-30 13:56:03 -07:00
|
|
|
ACTION_PROFILE: 'user.profile',
|
|
|
|
|
ACTION_REBOOT: 'cloudron.reboot',
|
|
|
|
|
ACTION_UPDATE: 'cloudron.update',
|
|
|
|
|
ACTION_USER_ADD: 'user.add',
|
2016-04-30 23:16:37 -07:00
|
|
|
ACTION_USER_LOGIN: 'user.login',
|
2016-04-30 13:56:03 -07:00
|
|
|
ACTION_USER_REMOVE: 'user.remove',
|
|
|
|
|
ACTION_USER_UPDATE: 'user.update'
|
2016-04-29 23:49:56 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var assert = require('assert'),
|
|
|
|
|
DatabaseError = require('./databaseerror.js'),
|
2016-04-30 11:49:51 -07:00
|
|
|
debug = require('debug')('box:eventlog'),
|
2016-04-29 23:49:56 -07:00
|
|
|
eventlogdb = require('./eventlogdb.js'),
|
|
|
|
|
util = require('util'),
|
|
|
|
|
uuid = require('node-uuid');
|
|
|
|
|
|
2016-04-30 11:49:51 -07:00
|
|
|
var NOOP_CALLBACK = function (error) { if (error) debug(error); };
|
|
|
|
|
|
2016-04-29 23:49:56 -07:00
|
|
|
function EventLogError(reason, errorOrMessage) {
|
|
|
|
|
assert.strictEqual(typeof reason, 'string');
|
|
|
|
|
assert(errorOrMessage instanceof Error || typeof errorOrMessage === 'string' || typeof errorOrMessage === 'undefined');
|
|
|
|
|
|
|
|
|
|
Error.call(this);
|
|
|
|
|
Error.captureStackTrace(this, this.constructor);
|
|
|
|
|
|
|
|
|
|
this.name = this.constructor.name;
|
|
|
|
|
this.reason = reason;
|
|
|
|
|
if (typeof errorOrMessage === 'undefined') {
|
|
|
|
|
this.message = reason;
|
|
|
|
|
} else if (typeof errorOrMessage === 'string') {
|
|
|
|
|
this.message = errorOrMessage;
|
|
|
|
|
} else {
|
|
|
|
|
this.message = 'Internal error';
|
|
|
|
|
this.nestedError = errorOrMessage;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
util.inherits(EventLogError, Error);
|
|
|
|
|
EventLogError.INTERNAL_ERROR = 'Internal error';
|
2016-04-30 13:00:44 -07:00
|
|
|
EventLogError.NOT_FOUND = 'Not Found';
|
2016-04-29 23:49:56 -07:00
|
|
|
|
2016-04-30 13:56:03 -07:00
|
|
|
function add(action, req, data, callback) {
|
2016-04-29 23:49:56 -07:00
|
|
|
assert.strictEqual(typeof action, 'string');
|
2016-04-30 13:56:03 -07:00
|
|
|
assert.strictEqual(typeof req, 'object');
|
2016-04-29 23:49:56 -07:00
|
|
|
assert.strictEqual(typeof data, 'object');
|
2016-04-30 11:49:51 -07:00
|
|
|
assert(!callback || typeof callback === 'function');
|
|
|
|
|
|
|
|
|
|
callback = callback || NOOP_CALLBACK;
|
2016-04-29 23:49:56 -07:00
|
|
|
|
2016-04-30 13:00:44 -07:00
|
|
|
var id = uuid.v4();
|
2016-04-30 22:35:46 -07:00
|
|
|
var ip = (req.headers ? req.headers['x-forwarded-for'] : null) || req.ip || null;
|
2016-04-30 23:04:54 -07:00
|
|
|
var source = { ip: ip, username: req.user ? req.user.username : null, userId: req.user ? req.user.id : null };
|
2016-04-30 13:56:03 -07:00
|
|
|
|
2016-04-30 13:00:44 -07:00
|
|
|
eventlogdb.add(id, action, source, data, function (error) {
|
2016-04-29 23:49:56 -07:00
|
|
|
if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error));
|
|
|
|
|
|
2016-04-30 13:00:44 -07:00
|
|
|
callback(null, { id: id });
|
2016-04-29 23:49:56 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function get(id, callback) {
|
|
|
|
|
assert.strictEqual(typeof id, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
eventlogdb.get(id, function (error, result) {
|
2016-04-30 13:00:44 -07:00
|
|
|
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new EventLogError(EventLogError.NOT_FOUND, 'No such event'));
|
2016-04-29 23:49:56 -07:00
|
|
|
if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error));
|
|
|
|
|
|
|
|
|
|
callback(null, result);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getAllPaged(page, perPage, callback) {
|
|
|
|
|
assert.strictEqual(typeof page, 'number');
|
|
|
|
|
assert.strictEqual(typeof perPage, 'number');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
eventlogdb.getAllPaged(page, perPage, function (error, boxes) {
|
|
|
|
|
if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error));
|
|
|
|
|
|
|
|
|
|
callback(null, boxes);
|
|
|
|
|
});
|
|
|
|
|
}
|