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

refactor: remove authorization method calls (server) #34986

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

MarcosSpessatto
Copy link
Member

@MarcosSpessatto MarcosSpessatto commented Jan 21, 2025

https://rocketchat.atlassian.net/browse/CORE-935

Proposed changes (including videos or screenshots)

Issue(s)

Steps to test or reproduce

Further comments


This pull request refactors the authorization methods in the Rocket.Chat server codebase to enhance code modularity, maintainability, and reusability. The changes include:

  • In roles.ts, a direct Meteor method call is replaced with a dedicated function call for adding a user to a role.
  • In addUserToRole.ts, the core logic for adding a user to a role is extracted into a separate async function, simplifying the Meteor method implementation.
  • In removeUserFromRole.ts, the logic for removing a user from a role is extracted into a separate function, updating the Meteor method to use this new function.
  • In index.ts of the bot helpers, bot helper methods are modified to use direct function calls instead of Meteor.callAsync, a userId parameter is added to the request method, and role management functions are updated with proper user authentication.
  • In setAdminStatus.ts, direct Meteor method calls are replaced with imported function calls for managing admin roles.

These changes aim to improve the overall structure and efficiency of the authorization-related code in the Rocket.Chat server.

Copy link
Contributor

dionisio-bot bot commented Jan 21, 2025

Looks like this PR is not ready to merge, because of the following issues:

  • This PR is missing the 'stat: QA assured' label

Please fix the issues and try again

If you have any trouble, please check the PR guidelines

Copy link

changeset-bot bot commented Jan 21, 2025

⚠️ No Changeset found

Latest commit: cca6795

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@MarcosSpessatto MarcosSpessatto added this to the 7.4.0 milestone Jan 21, 2025
Copy link
Contributor

github-actions bot commented Jan 21, 2025

PR Preview Action v1.6.0

🚀 View preview at
https://RocketChat.github.io/Rocket.Chat/pr-preview/pr-34986/

Built to branch gh-pages at 2025-03-07 13:04 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

Copy link

codecov bot commented Jan 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 59.48%. Comparing base (bbf12a1) to head (cca6795).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop   #34986      +/-   ##
===========================================
+ Coverage    59.46%   59.48%   +0.02%     
===========================================
  Files         2830     2830              
  Lines        68579    68720     +141     
  Branches     15177    15204      +27     
===========================================
+ Hits         40778    40877      +99     
- Misses       25132    25171      +39     
- Partials      2669     2672       +3     
Flag Coverage Δ
unit 75.50% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@MarcosSpessatto MarcosSpessatto marked this pull request as ready for review January 21, 2025 18:10
@MarcosSpessatto MarcosSpessatto modified the milestones: 7.4.0, 7.5.0 Feb 14, 2025
@scuciatto
Copy link
Member

@kody start-review

Copy link

kodus-ai bot commented Feb 28, 2025

Code Review Completed! 🔥

The code review was successfully completed based on your current configurations.

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a 👍 for helpful suggestions or a 👎 if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Security
Code Style
Kody Rules
Refactoring
Error Handling
Maintainability
Potential Issues
Documentation And Comments
Performance And Optimization

Access your configuration settings here.

@@ -81,7 +82,7 @@ API.v1.addRoute(
throw new Meteor.Error('error-user-already-in-role', 'User already in role');
}

await Meteor.callAsync('authorization:addUserToRole', role._id, user.username, roomId);
await addUserToRole(this.userId, role._id, user.username, roomId);
Copy link

Choose a reason for hiding this comment

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

kody code-reviewError Handling

try {
  await addUserToRole(this.userId, role._id, user.username, roomId);
} catch (error) {
  throw new Meteor.Error('error-role-assignment-failed', 'Failed to assign user to role', { error: error.message });
}

Multiple functions lack proper error handling, which can lead to unhandled exceptions during role assignments and modifications.

This issue appears in multiple locations:

  • apps/meteor/app/api/server/v1/roles.ts: Lines 85-85
  • apps/meteor/app/authorization/server/methods/addUserToRole.ts: Lines 78-88
  • apps/meteor/app/bot-helpers/server/index.ts: Lines 66-68
  • apps/meteor/app/bot-helpers/server/index.ts: Lines 70-72
  • apps/meteor/app/lib/server/methods/setAdminStatus.ts: Lines 39-43

Please add try-catch blocks around all relevant function calls to handle potential errors gracefully.

Talk to Kody by mentioning @kody

Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.

Comment on lines +28 to +30
throw new Meteor.Error('error-invalid-arguments', 'Invalid arguments', {
method: 'authorization:removeUserFromRole',
});
Copy link

Choose a reason for hiding this comment

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

kody code-reviewError Handling

throw new Meteor.Error('error-invalid-arguments', `Invalid arguments: roleId and username must be non-empty strings`, {

The error message for invalid arguments should be more specific about which argument(s) failed validation to help with debugging and maintainability

Talk to Kody by mentioning @kody

Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants