Skip to content

fix: (PBAC) Add organization fallback on resources #22733

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

Merged
merged 9 commits into from
Jul 26, 2025

Conversation

sean-brydon
Copy link
Member

Problem

The getResourcePermissions method in PermissionCheckService only
checked team-level permissions, while the checkPermission method
correctly implements a fallback hierarchy that checks both team and
organization-level permissions. This inconsistency meant that users
with organization-level permissions wouldn't see those permissions
when using getResourcePermissions, even though they would be granted
access via checkPermission.

Business Logic Alignment

This change ensures getResourcePermissions now follows the exact same
permission hierarchy as checkPermission:

// Both methods now follow this pattern:

  1. Check team-level permissions (if customRoleId exists)
  2. Check org-level permissions (if team.parentId exists)
  3. Combine results (getResourcePermissions) or return true on first
    match (checkPermission)

Example Usage

// Before: Only returned team-level permissions
const permissions = await service.getResourcePermissions({
  userId: 123,
  teamId: 456,
  resource: Resource.EventType
});
// Result: ["eventType.read"] (missing org-level permissions)
// After: Returns combined team + organization permissions
const permissions = await service.getResourcePermissions({
  userId: 123,
  teamId: 456,
  resource: Resource.EventType
});
// Result: ["eventType.read", "eventType.create", "eventType.update"]
(includes org permissions)

Breaking Changes

None. This is a backward-compatible enhancement that only adds
functionality.

@graphite-app graphite-app bot requested a review from a team July 25, 2025 08:52
Copy link
Contributor

coderabbitai bot commented Jul 25, 2025

Walkthrough

A new method, getResourcePermissionsByRoleId, was introduced to the IPermissionRepository interface and implemented in the PermissionRepository class. This method retrieves permissions for a given role and resource. The getResourcePermissions logic in PermissionCheckService was refactored to use this new method, aggregating permissions from both team and organization roles while handling membership data with updated nested team parentId structure. Comprehensive tests were added to cover various scenarios for permission retrieval, including feature flag states, membership combinations, deduplication, and error handling. The test suite now mocks the new repository method and verifies correct behavior under multiple conditions.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15–20 minutes

  • Complexity: Moderate. The changes involve interface and repository updates, a service logic refactor, and a substantial extension of the test suite. Reviewers need to verify correct interface implementation, logic correctness in permission aggregation, and thoroughness of the new tests.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

packages/features/pbac/services/__tests__/permission-check.service.test.ts

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-playwright".

(The package "eslint-plugin-playwright" was not found when loaded as a Node module from the directory "".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-playwright@latest --save-dev

The plugin "eslint-plugin-playwright" was referenced from the config file in ".eslintrc.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/getResoruces-pbac-fallback-to-org

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@keithwillcode keithwillcode added consumer core area: core, team members only labels Jul 25, 2025
@dosubot dosubot bot added the 🐛 bug Something isn't working label Jul 25, 2025
Copy link

graphite-app bot commented Jul 25, 2025

Graphite Automations

"Add consumer team as reviewer" took an action on this PR • (07/25/25)

1 reviewer was added to this PR based on Keith Williams's automation.

"Add ready-for-e2e label" took an action on this PR • (07/25/25)

1 label was added to this PR based on Keith Williams's automation.

Copy link

vercel bot commented Jul 25, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
cal ⬜️ Ignored (Inspect) Jul 25, 2025 0:53am
cal-eu ⬜️ Ignored (Inspect) Jul 25, 2025 0:53am

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7b95b8c and e747c14.

📒 Files selected for processing (3)
  • packages/features/pbac/domain/repositories/IPermissionRepository.ts (3 hunks)
  • packages/features/pbac/services/__tests__/permission-check.service.test.ts (6 hunks)
  • packages/features/pbac/services/permission-check.service.ts (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/features/pbac/domain/repositories/IPermissionRepository.ts
  • packages/features/pbac/services/tests/permission-check.service.test.ts
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{js,jsx,ts,tsx}

📄 CodeRabbit Inference Engine (.cursor/rules/review.mdc)

Flag excessive Day.js use in performance-critical code. Functions like .add, .diff, .isBefore, and .isAfter are slow, especially in timezone mode. Prefer .utc() for better performance. Where possible, replace with native Date and direct .valueOf() comparisons for faster execution. Recommend using native methods or Day.js .utc() consistently in hot paths like loops.

Files:

  • packages/features/pbac/services/permission-check.service.ts
🧬 Code Graph Analysis (1)
packages/features/pbac/services/permission-check.service.ts (1)
packages/features/pbac/domain/mappers/PermissionMapper.ts (1)
  • PermissionMapper (7-116)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Install dependencies / Yarn install & cache
🔇 Additional comments (2)
packages/features/pbac/services/permission-check.service.ts (2)

9-14: LGTM - Import changes support the new functionality.

The addition of CrudAction and CustomAction types to the import is necessary for the refactored getResourcePermissions method implementation.


246-247: LGTM - Property access updated for nested team structure.

The change from membership.team_parentId to membership.team.parentId correctly reflects the updated membership data structure with nested team objects.

@@ -20,7 +22,9 @@ export interface IPermissionRepository {
teamId: number;
userId: number;
customRoleId: string | null;
team_parentId?: number;
Copy link
Member Author

Choose a reason for hiding this comment

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

Outdated type from when this used to use ksyely


// Get team-level permissions
if (membership?.customRoleId) {
const teamActions = await this.repository.getResourcePermissionsByRoleId(
Copy link
Contributor

Choose a reason for hiding this comment

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

what if we turn getResourcePermissionsByRoleId to accept roleIds?

Copy link
Member Author

Choose a reason for hiding this comment

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

o.o

Copy link
Member Author

Choose a reason for hiding this comment

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

i think passing in a teamId + an optionalRoleId might be a good shout here. Let me play around with that

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/features/pbac/services/__tests__/permission-check.service.test.ts (1)

388-635: Excellent comprehensive test coverage for getResourcePermissions.

The test suite thoroughly covers all major scenarios including:

  • Feature flag states
  • Team-only and org-only permissions
  • Combined permission aggregation
  • Permission deduplication
  • Error handling and edge cases

The test structure is well-organized and the mocking strategy is appropriate.

Minor suggestion for the hierarchy test (lines 591-634): The test name suggests "precedence" but the implementation shows additive behavior. Consider renaming to "should combine team and org permissions additively" to better reflect the actual behavior being tested.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e54fbd6 and a34ea15.

📒 Files selected for processing (1)
  • packages/features/pbac/services/__tests__/permission-check.service.test.ts (6 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{js,jsx,ts,tsx}

📄 CodeRabbit Inference Engine (.cursor/rules/review.mdc)

Flag excessive Day.js use in performance-critical code. Functions like .add, .diff, .isBefore, and .isAfter are slow, especially in timezone mode. Prefer .utc() for better performance. Where possible, replace with native Date and direct .valueOf() comparisons for faster execution. Recommend using native methods or Day.js .utc() consistently in hot paths like loops.

Files:

  • packages/features/pbac/services/__tests__/permission-check.service.test.ts
🔇 Additional comments (3)
packages/features/pbac/services/__tests__/permission-check.service.test.ts (3)

9-9: LGTM! Import addition supports new test functionality.

The Resource import is necessary for the new getResourcePermissions test cases.


38-38: LGTM! Mock method addition aligns with interface changes.

The getResourcePermissionsByRoleId mock properly supports the new repository method.


83-83: LGTM! Team structure updated to match new membership data model.

The changes from team_parentId to team: { parentId } properly reflect the updated membership structure.

Also applies to: 198-198, 275-275

@emrysal emrysal merged commit b155d73 into main Jul 26, 2025
39 of 40 checks passed
@emrysal emrysal deleted the fix/getResoruces-pbac-fallback-to-org branch July 26, 2025 00:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working consumer core area: core, team members only ready-for-e2e
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants