-
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 #1149 from OneCommunityGlobal/Yashwanth_individual…
…_permission_change_show_backend Yashwanth_Backend_for_permission_log_table
- Loading branch information
Showing
7 changed files
with
110 additions
and
6 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
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 |
---|---|---|
|
@@ -17,6 +17,7 @@ const userService = require('../services/userService'); | |
// const { authorizedUserSara, authorizedUserJae } = process.env; | ||
const authorizedUserSara = `[email protected]`; // To test this code please include your email here | ||
const authorizedUserJae = `[email protected]`; | ||
const logUserPermissionChangeByAccount = require('../utilities/logUserPermissionChangeByAccount'); | ||
|
||
const { hasPermission, canRequestorUpdateUser } = require('../utilities/permissions'); | ||
const helper = require('../utilities/permissions'); | ||
|
@@ -696,6 +697,7 @@ const userProfileController = function (UserProfile, Project) { | |
(await hasPermission(req.body.requestor, 'putUserProfilePermissions')) | ||
) { | ||
record.permissions = req.body.permissions; | ||
await logUserPermissionChangeByAccount(req); | ||
} | ||
|
||
if (req.body.endDate !== undefined) { | ||
|
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,23 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const { Schema } = mongoose; | ||
|
||
const User = require('./userProfile'); | ||
|
||
|
||
const UserPermissionChangeLog = new Schema({ | ||
logDateTime: { type: String, required: true }, | ||
userId: { | ||
type: mongoose.Types.ObjectId, | ||
ref: User, | ||
required: true, | ||
}, | ||
individualName: { type: String }, | ||
permissions: { type: [String], required: true }, | ||
permissionsAdded: { type: [String], default: [] }, | ||
permissionsRemoved: { type: [String], default: [] }, | ||
requestorRole: { type: String }, | ||
requestorEmail: { type: String, required: true }, | ||
}); | ||
|
||
module.exports = mongoose.model('UserPermissionChangeLog', UserPermissionChangeLog, 'UserPermissionChangeLogs'); |
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
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,58 @@ | ||
const moment = require('moment-timezone'); | ||
const UserPermissionChangeLog = require('../models/userPermissionChangeLog'); | ||
const UserProfile = require('../models/userProfile'); | ||
|
||
const logUserPermissionChangeByAccount = async (req) => { | ||
const { permissions, firstName, lastName, requestor } = req.body; | ||
const dateTime = moment().tz('America/Los_Angeles').format(); | ||
|
||
try { | ||
let permissionsAdded = []; | ||
let permissionsRemoved = []; | ||
const { userId } = req.params; | ||
const Permissions = permissions.frontPermissions; | ||
const requestorEmailId = await UserProfile.findById(requestor.requestorId).select('email').exec(); | ||
const document = await findLatestRelatedLog(userId); | ||
|
||
if (document) { | ||
const docPermissions = Array.isArray(document.permissions) ? document.permissions : []; | ||
if(JSON.stringify(docPermissions) === JSON.stringify(Permissions)) { | ||
return; | ||
} | ||
permissionsRemoved = docPermissions.filter((item) => !Permissions.includes(item)); | ||
permissionsAdded = Permissions.filter((item) => !docPermissions.includes(item)); | ||
} else { | ||
permissionsAdded = Permissions; | ||
} | ||
|
||
const logEntry = new UserPermissionChangeLog({ | ||
logDateTime: dateTime, | ||
userId, | ||
individualName: `INDIVIDUAL: ${firstName} ${lastName}`, | ||
permissions: Permissions, | ||
permissionsAdded, | ||
permissionsRemoved, | ||
requestorRole: requestor.role, | ||
requestorEmail: requestorEmailId.email, | ||
}); | ||
|
||
await logEntry.save(); | ||
console.log('Permission change logged successfully'); | ||
} catch (error) { | ||
console.error('Error logging permission change:', error); | ||
} | ||
}; | ||
|
||
const findLatestRelatedLog = (userId) => new Promise((resolve, reject) => { | ||
UserPermissionChangeLog.findOne({ userId }) | ||
.sort({ logDateTime: -1 }) | ||
.exec((err, document) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(document); | ||
}); | ||
}); | ||
|
||
module.exports = logUserPermissionChangeByAccount; |