2020-07-21 10:29:08 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
|
|
|
const { User } = require('../../models/User')
|
|
|
|
|
|
|
|
const MAX_AUDIT_LOG_ENTRIES = 200
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an audit log entry
|
|
|
|
*
|
|
|
|
* The entry should include at least the following fields:
|
|
|
|
*
|
2020-08-10 09:58:01 -04:00
|
|
|
* - userId: the user on behalf of whom the operation was performed
|
2020-07-21 10:29:08 -04:00
|
|
|
* - operation: a string identifying the type of operation
|
|
|
|
* - initiatorId: who performed the operation
|
2020-08-10 09:58:01 -04:00
|
|
|
* - ipAddress: the IP address of the initiator
|
2020-07-21 10:29:08 -04:00
|
|
|
* - info: an object detailing what happened
|
|
|
|
*/
|
|
|
|
async function addEntry(userId, operation, initiatorId, ipAddress, info = {}) {
|
2020-08-12 10:19:33 -04:00
|
|
|
if (!operation || !ipAddress)
|
|
|
|
throw new OError('missing required audit log data', {
|
|
|
|
operation,
|
|
|
|
initiatorId,
|
|
|
|
ipAddress
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!initiatorId && operation !== 'reset-password')
|
|
|
|
throw new OError('missing initiatorId for audit log', {
|
|
|
|
operation,
|
|
|
|
ipAddress
|
|
|
|
})
|
|
|
|
|
2020-07-21 10:29:08 -04:00
|
|
|
const timestamp = new Date()
|
|
|
|
const entry = {
|
|
|
|
operation,
|
|
|
|
initiatorId,
|
|
|
|
info,
|
|
|
|
ipAddress,
|
|
|
|
timestamp
|
|
|
|
}
|
|
|
|
const result = await User.updateOne(
|
|
|
|
{ _id: userId },
|
|
|
|
{
|
|
|
|
$push: {
|
|
|
|
auditLog: { $each: [entry], $slice: -MAX_AUDIT_LOG_ENTRIES }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
).exec()
|
|
|
|
if (result.nModified === 0) {
|
2020-08-11 05:28:29 -04:00
|
|
|
throw new OError('user not found', { userId })
|
2020-07-21 10:29:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const UserAuditLogHandler = {
|
|
|
|
promises: {
|
|
|
|
addEntry
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = UserAuditLogHandler
|