Просмотр исходного кода

[web] Add audit log entries for project-history-version-restore and project-history-version-download

GitOrigin-RevId: 8cfe1fee733aa886cd6518d352abd95bd9da6f77
Simon Gardner 9 месяцев назад
Родитель
Сommit
bb7b8ee227

+ 51 - 0
services/web/app/src/Features/History/HistoryController.mjs

@@ -26,6 +26,7 @@ import RestoreManager from './RestoreManager.mjs'
 import { prepareZipAttachment } from '../../infrastructure/Response.mjs'
 import { prepareZipAttachment } from '../../infrastructure/Response.mjs'
 import Features from '../../infrastructure/Features.mjs'
 import Features from '../../infrastructure/Features.mjs'
 import { z, zz, validateReq } from '../../infrastructure/Validation.mjs'
 import { z, zz, validateReq } from '../../infrastructure/Validation.mjs'
+import ProjectAuditLogHandler from '../Project/ProjectAuditLogHandler.mjs'
 
 
 // Number of seconds after which the browser should send a request to revalidate
 // Number of seconds after which the browser should send a request to revalidate
 // blobs
 // blobs
@@ -192,6 +193,19 @@ async function restoreFileFromV2(req, res, next) {
     pathname
     pathname
   )
   )
 
 
+  ProjectAuditLogHandler.addEntryIfManagedInBackground(
+    projectId,
+    'project-history-version-restored',
+    userId,
+    req.ip,
+    {
+      version,
+      scope: 'file',
+      pathname,
+      restoredEntityId: entity._id,
+    }
+  )
+
   res.json({
   res.json({
     type: entity.type,
     type: entity.type,
     id: entity._id,
     id: entity._id,
@@ -211,6 +225,19 @@ async function revertFile(req, res, next) {
     {}
     {}
   )
   )
 
 
+  ProjectAuditLogHandler.addEntryIfManagedInBackground(
+    projectId,
+    'project-history-version-restored',
+    userId,
+    req.ip,
+    {
+      version,
+      scope: 'file',
+      pathname,
+      restoredEntityId: entity._id,
+    }
+  )
+
   res.json({
   res.json({
     type: entity.type,
     type: entity.type,
     id: entity._id,
     id: entity._id,
@@ -228,6 +255,18 @@ async function revertProject(req, res, next) {
     version
     version
   )
   )
 
 
+  ProjectAuditLogHandler.addEntryIfManagedInBackground(
+    projectId,
+    'project-history-version-restored',
+    userId,
+    req.ip,
+    {
+      version,
+      scope: 'project',
+      restoredEntities: reverted,
+    }
+  )
+
   res.json(reverted)
   res.json(reverted)
 }
 }
 
 
@@ -346,6 +385,7 @@ async function deleteLabel(req, res, next) {
 
 
 async function downloadZipOfVersion(req, res, next) {
 async function downloadZipOfVersion(req, res, next) {
   const { project_id: projectId, version } = req.params
   const { project_id: projectId, version } = req.params
+  const userId = SessionManager.getLoggedInUserId(req.session)
 
 
   const project = await ProjectDetailsHandler.promises.getDetails(projectId)
   const project = await ProjectDetailsHandler.promises.getDetails(projectId)
   const v1Id =
   const v1Id =
@@ -366,6 +406,17 @@ async function downloadZipOfVersion(req, res, next) {
     req,
     req,
     res
     res
   )
   )
+
+  ProjectAuditLogHandler.addEntryIfManagedInBackground(
+    projectId,
+    'project-history-version-downloaded',
+    userId,
+    req.ip,
+    {
+      version,
+      projectName: project.name,
+    }
+  )
 }
 }
 
 
 async function _pipeHistoryZipToResponse(v1ProjectId, version, name, req, res) {
 async function _pipeHistoryZipToResponse(v1ProjectId, version, name, req, res) {

+ 2 - 0
services/web/app/src/Features/Project/ProjectAuditLogHandler.mjs

@@ -15,6 +15,8 @@ const MANAGED_GROUP_PROJECT_EVENTS = [
   'project-untrashed',
   'project-untrashed',
   'project-restored',
   'project-restored',
   'project-cloned',
   'project-cloned',
+  'project-history-version-restored',
+  'project-history-version-downloaded',
   'transfer-ownership',
   'transfer-ownership',
   'project-downloaded',
   'project-downloaded',
 ]
 ]

+ 9 - 0
services/web/test/unit/src/History/HistoryController.test.mjs

@@ -103,6 +103,15 @@ describe('HistoryController', function () {
       })
       })
     )
     )
 
 
+    vi.doMock(
+      '../../../../app/src/Features/Project/ProjectAuditLogHandler.mjs',
+      () => ({
+        default: (ctx.ProjectAuditLogHandler = {
+          addEntryIfManagedInBackground: sinon.stub(),
+        }),
+      })
+    )
+
     vi.doMock('../../../../app/src/Features/User/UserGetter.mjs', () => ({
     vi.doMock('../../../../app/src/Features/User/UserGetter.mjs', () => ({
       default: (ctx.UserGetter = {}),
       default: (ctx.UserGetter = {}),
     }))
     }))