mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-05-29 12:01:32 +02:00
Update user audit log when an admin adds email GitOrigin-RevId: 758199e2f0f3501701b42bf359ce5ccd52cb3da0
49 lines
1.1 KiB
JavaScript
49 lines
1.1 KiB
JavaScript
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:
|
|
*
|
|
* - userId: the user on behalf of whom the operation was performed
|
|
* - operation: a string identifying the type of operation
|
|
* - initiatorId: who performed the operation
|
|
* - ipAddress: the IP address of the initiator
|
|
* - info: an object detailing what happened
|
|
*/
|
|
async function addEntry(userId, operation, initiatorId, ipAddress, info = {}) {
|
|
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) {
|
|
throw new OError({
|
|
message: 'user not found',
|
|
info: { userId }
|
|
})
|
|
}
|
|
}
|
|
|
|
const UserAuditLogHandler = {
|
|
promises: {
|
|
addEntry
|
|
}
|
|
}
|
|
|
|
module.exports = UserAuditLogHandler
|