-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #530 from OneCommunityGlobal/jerry_track_who_chang…
…es_permissions Jerry - Track Who Changes Permissions On Permissions Management Page
- Loading branch information
Showing
6 changed files
with
143 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const UserProfile = require('../models/userProfile'); | ||
|
||
const permissionChangeLogController = function (PermissionChangeLog) { | ||
|
||
const getPermissionChangeLogs = async function (req, res) { | ||
|
||
try { | ||
const userProfile = await UserProfile.findOne({ _id: req.params.userId }).exec() | ||
|
||
if (userProfile) { | ||
if (userProfile.role !== 'Owner') { | ||
res.status(204).send([]) | ||
} else { | ||
const changeLogs = await PermissionChangeLog.find({}) | ||
res.status(200).send(changeLogs) | ||
} | ||
} else { | ||
res.status(403).send(`User (${req.params.userId}) not found.`) | ||
} | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
} | ||
|
||
return { | ||
getPermissionChangeLogs | ||
} | ||
} | ||
|
||
module.exports = permissionChangeLogController |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const mongoose = require('mongoose'); | ||
const { Schema } = mongoose; | ||
|
||
const User = require('./userProfile'); | ||
const rolesMergedPermissions = require('./role') | ||
|
||
const PermissionChangeLog = new Schema({ | ||
logDateTime: { type: String, required: true }, | ||
roleId: { | ||
type: mongoose.Types.ObjectId, | ||
ref: rolesMergedPermissions, | ||
required: true | ||
}, | ||
roleName: { type: String }, | ||
permissions: { type: [String], required: true }, | ||
permissionsAdded: { type: [String], required: true }, | ||
permissionsRemoved: { type: [String], required: true }, | ||
requestorId: { | ||
type: mongoose.Types.ObjectId, | ||
ref: User | ||
}, | ||
requestorRole: { type: String }, | ||
requestorEmail: { type: String, required: true}, | ||
}); | ||
|
||
module.exports = mongoose.model('permissionChangeLog', PermissionChangeLog, 'permissionChangeLogs'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const express = require('express'); | ||
|
||
const routes = function (permissionChangeLog) { | ||
const controller = require('../controllers/permissionChangeLogsController')(permissionChangeLog) | ||
|
||
const permissionChangeLogRouter = express.Router() | ||
|
||
permissionChangeLogRouter.route("/permissionChangeLogs/:userId") | ||
.get(controller.getPermissionChangeLogs) | ||
|
||
return permissionChangeLogRouter | ||
} | ||
|
||
module.exports = routes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const moment = require("moment-timezone"); | ||
const PermissionChangeLog = require("../models/permissionChangeLog") | ||
|
||
// Middleware function | ||
const changedPermissionsLogger = async (req, res, next) => { | ||
await logPermissionChangeByAccount(req.body) | ||
next(); | ||
}; | ||
|
||
// Helper function finds the latest log related to the permission | ||
const findLatestRelatedLog = (roleId) => { | ||
|
||
return new Promise((resolve, reject) => { | ||
PermissionChangeLog.findOne({ roleId: roleId }) | ||
.sort({ logDateTime: -1 }) | ||
.exec((err, document) => { | ||
if (err) { | ||
console.error(err); | ||
reject(err); | ||
return; | ||
} | ||
resolve(document); | ||
}); | ||
}) | ||
} | ||
|
||
// Function saves logs to hgnData_dev.permissionChangeLogs collection | ||
const logPermissionChangeByAccount = async (requestBody) => { | ||
const { roleId, roleName, permissions, requestor, role, email } = requestBody | ||
const dateTime = moment().tz("America/Los_Angeles").format(); | ||
|
||
try { | ||
let permissionsAdded = [] | ||
let permissionsRemoved = [] | ||
|
||
// Find the latest log related to permission | ||
const document = await findLatestRelatedLog(roleId) | ||
|
||
if (document) { | ||
permissionsRemoved = document.permissions.filter(item => !(permissions.includes(item))) | ||
permissionsAdded = permissions.filter(item => !(document.permissions.includes(item))) | ||
} else { | ||
// else this is the first permissions change log for this particular role | ||
permissionsAdded = permissions | ||
} | ||
|
||
|
||
const logEntry = new PermissionChangeLog({ | ||
logDateTime: dateTime, | ||
roleId: roleId, | ||
roleName: roleName, | ||
permissions: permissions, | ||
permissionsAdded: permissionsAdded, | ||
permissionsRemoved: permissionsRemoved, | ||
requestorId: requestor.requestorId, | ||
requestorRole: role, | ||
requestorEmail: email, | ||
}) | ||
|
||
await logEntry.save() | ||
|
||
} catch (error) { | ||
console.error('Error logging permission change:', error); | ||
res.status(500).json({ error: 'Failed to log permission change' }); | ||
} | ||
} | ||
|
||
module.exports = changedPermissionsLogger; |