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

Yashwanth_Backend_for_permission_log_table #1149

Merged

Conversation

yashwanth170
Copy link
Contributor

@yashwanth170 yashwanth170 commented Nov 14, 2024

Description

Bug_description
The permission change log table only logs changes made to role, so I have added additional features to log permission changes made to individual account. Now the table shows latest logs of changes made to both individual and role

Related PRS (if any):

This frontend PR is related to the ##2868 Frontend PR.
To test this backend PR you need to checkout the #2868 frontend PR.

Main changes explained:

  • Developed a new MongoDB model to store logs of individual permission changes.
  • Implemented a new controller class with a POST method to save data into the MongoDB model and a GET API to retrieve logs from both UserPermissionChangeLog and PermissionChangeLog, sorting them accordingly.

How to test:

  1. check into current branch, and check into frontend branch Yashwanth_individual_permission_change_show
  2. do npm install and ... to run this PR locally
  3. Clear site data/cache
  4. log as admin user
  5. go to dashboard→ Other Links → Permission Management → Make changes to a role or individual and verify that the table updates accordingly.

Screenshots or videos of changes:

Note:

Test the updated implementation across different user types and let me know if I missed any expected conventions or if you have additional suggestions. I’ve also resolved an existing issue where submitting without adding or removing any permissions no longer logs duplicate entries for both role and user permissions.

@yashwanth170 yashwanth170 added the High Priority - Please Review First This is an important PR we'd like to get merged as soon as possible label Nov 14, 2024
Copy link
Contributor

@nathanah nathanah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The POST request should not have it's own API so that the updates are atomic. The GET request should be updating/replacing the original GET request.

@@ -116,6 +117,10 @@ const routes = function (userProfile, project) {

userProfileRouter.route('/userProfile/teamCode/list').get(controller.getAllTeamCode);

userProfileRouter.post('/logPermissionChanges', logUserPermissionController.logPermissionChanges);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
userProfileRouter.post('/logPermissionChanges', logUserPermissionController.logPermissionChanges);

You shouldn't be able to edit history without making the edits. Instead, call logPermissionChanges from wherever the permissions are changed (putUserProfile() I think?). That unites both API calls into one and can guarantee that the updates are atomic.

Uniting the API calls makes it so devs testing with Postman don't have to make multiple API calls to make the history accurate and ensures that they can't make API calls to give false update history. You currently don't check for requestor permissions in logPermissionChanges, so anyone could add false information with this API using something like Postman.

Comment on lines 13 to 15
existingPermissions,
addedPermissions,
removedPermissions,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would avoid doing the added/removed/existing permission logic on the frontend. Only requesting newPermissions from the frontend should be enough to derive the info on the backend.

Can find 'existingPermissions'/'oldPermissions' from the DB to ensure the info is accurate.

addedPermissions = newPermissions.filter(x=> !oldPermissions.contains(x));
removedPermissions = oldPermissions.filter(x => !newPermissions.contains(x));

Comment on lines 41 to 45
const userProfile = await UserProfile.findOne({ _id: req.params.userId }).exec();

if (userProfile) {
if (userProfile.role !== 'Owner') {
res.status(204).send([]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this checking for permission to view the history? I think Admins might also want to see the history? not sure.
Could use await hasPermission(req.body.requestor, 'getPermissionHistory') or something (could use an existing permission or create a new one). There shouldn't be hardcoded references to specific roles if avoidable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that's just copied from getPermissionChangeLogs... I guess it is only Owners then. Would've been easier to find if this was modifying that file instead of creating a new file.

@one-community
Copy link
Member

Thank you all, merging!

@one-community one-community merged commit 70b6b76 into development Dec 29, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
High Priority - Please Review First This is an important PR we'd like to get merged as soon as possible
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants