Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: log form updates #2063

Merged
merged 4 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/app/modules/auth/auth.middlewares.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { StatusCodes } from 'http-status-codes'

import { createLoggerWithLabel } from '../../config/logger'
import { createReqMeta } from '../../utils/request'
import { ControllerHandler } from '../core/core.types'
import * as UserService from '../user/user.service'

import { isUserInSession } from './auth.utils'

const logger = createLoggerWithLabel(module)

/**
* Middleware that only allows authenticated users to pass through to the next
* handler.
Expand Down Expand Up @@ -53,3 +57,32 @@ export const denyRpSpStudentEmails: ControllerHandler<
.json({ message: 'User not found' }),
)
}

/**
* Logs all admin actions which change database state (i.e. non-GET requests)
* @returns next
*/
export const logAdminAction: ControllerHandler<
unknown,
unknown,
unknown
> = async (req, res, next) => {
tshuli marked this conversation as resolved.
Show resolved Hide resolved
const sessionUserId = (req.session as Express.AuthedSession).user._id
const body = req.body
const method = req.method

if (req.method.toLowerCase() !== 'get') {
logger.info({
message: 'Admin attempting to make changes',
meta: {
action: 'logAdminAction',
method,
...createReqMeta(req),
sessionUserId,
body,
tshuli marked this conversation as resolved.
Show resolved Hide resolved
},
})
}

return next()
}
4 changes: 4 additions & 0 deletions src/app/routes/api/v3/admin/forms/admin-forms.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Router } from 'express'

import {
denyRpSpStudentEmails,
logAdminAction,
withUserAuthentication,
} from '../../../../../modules/auth/auth.middlewares'

Expand All @@ -19,6 +20,9 @@ export const AdminFormsRouter = Router()
AdminFormsRouter.use(withUserAuthentication)
AdminFormsRouter.use(denyRpSpStudentEmails)

// Log all non-get admin actions
AdminFormsRouter.use(logAdminAction)

AdminFormsRouter.use(AdminFormsSettingsRouter)
AdminFormsRouter.use(AdminFormsFeedbackRouter)
AdminFormsRouter.use(AdminFormsFormRouter)
Expand Down