fix: findCustomRoles/countCustomRoles miss roles with unset protected field - #41033
fix: findCustomRoles/countCustomRoles miss roles with unset protected field#41033parashuramGP wants to merge 2 commits into
protected field#41033Conversation
… unset
The queries previously used `protected: false` which only matches roles whose
`protected` field is explicitly false. Legacy custom roles created in older
Rocket.Chat versions don't have a `protected` field at all, so they were
incorrectly excluded from `findCustomRoles` and `countCustomRoles`.
Switch the filter to `protected: { $ne: true }` so both `false` and
missing/undefined values are treated as non-protected.
Fixes RocketChat#40798
|
Looks like this PR is not ready to merge, because of the following issues:
Please fix the issues and try again If you have any trouble, please check the PR guidelines |
🦋 Changeset detectedLatest commit: 478fe97 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
|
WalkthroughIn Custom roles protected filter fix
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Suggested labels
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/models/src/models/Roles.ts (1)
141-152: 🧹 Nitpick | 🔵 TrivialAdd test coverage for the filter change.
Verification confirms that no existing tests reference
findCustomRolesorcountCustomRoles, despite these methods being used in production code (AppRoleBridgeand statistics calculation). This bug fix affects a subtle MongoDB filter behavior—consider adding unit tests that verify both methods correctly handle roles with missingprotectedfields to prevent regressions.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/models/src/models/Roles.ts` around lines 141 - 152, Add unit tests for the findCustomRoles and countCustomRoles methods in the Roles class to verify they correctly filter out protected roles and handle roles with missing protected fields. Create test cases that ensure both methods return only non-protected custom roles (where protected is either explicitly false or missing/undefined) to prevent regressions in this MongoDB filter behavior that is currently used in production code like AppRoleBridge.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/models/src/models/Roles.ts`:
- Around line 141-152: Add unit tests for the findCustomRoles and
countCustomRoles methods in the Roles class to verify they correctly filter out
protected roles and handle roles with missing protected fields. Create test
cases that ensure both methods return only non-protected custom roles (where
protected is either explicitly false or missing/undefined) to prevent
regressions in this MongoDB filter behavior that is currently used in production
code like AppRoleBridge.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 38004786-2147-4dd9-a75e-e831a89da521
📒 Files selected for processing (2)
.changeset/fix-find-custom-roles-protected-filter.mdpackages/models/src/models/Roles.ts
📜 Review details
⏰ Context from checks skipped due to timeout. (1)
- GitHub Check: cubic · AI code reviewer
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx,js}
📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)
**/*.{ts,tsx,js}: Write concise, technical TypeScript/JavaScript with accurate typing in Playwright tests
Avoid code comments in the implementation
Files:
packages/models/src/models/Roles.ts
🧠 Learnings (4)
📚 Learning: 2026-03-16T21:50:37.589Z
Learnt from: amitb0ra
Repo: RocketChat/Rocket.Chat PR: 39676
File: .changeset/migrate-users-register-openapi.md:3-3
Timestamp: 2026-03-16T21:50:37.589Z
Learning: For changes related to OpenAPI migrations in Rocket.Chat/OpenAPI, when removing endpoint types and validators from rocket.chat/rest-typings (e.g., UserRegisterParamsPOST, /v1/users.register) document this as a minor changeset (not breaking) per RocketChat/Rocket.Chat-Open-API#150 Rule 7. Note that the endpoint type is re-exposed via a module augmentation .d.ts in the consuming package (e.g., packages/web-ui-registration/src/users-register.d.ts). In reviews, ensure the changeset clearly states: this is a non-breaking change, the major version should not be bumped, and the changeset reflects a minor version bump. Do not treat this as a breaking change during OpenAPI migrations.
Applied to files:
.changeset/fix-find-custom-roles-protected-filter.md
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In the Rocket.Chat repository, do not reference Biome lint rules in code review feedback. Biome is not used even if biome.json exists; only reference Biome rules if there is explicit, project-wide usage documented. For TypeScript files, review lint implications without Biome guidance unless the project enables Biome rules.
Applied to files:
packages/models/src/models/Roles.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In this repository (RocketChat/Rocket.Chat), Biome lint rules are not used even if a biome.json exists. When reviewing TypeScript files (e.g., packages/ui-voip/src/providers/useMediaSession.ts), ensure lint suggestions do not reference Biome-specific rules. Rely on general ESLint/TypeScript lint rules and project conventions instead.
Applied to files:
packages/models/src/models/Roles.ts
📚 Learning: 2026-05-06T12:21:44.083Z
Learnt from: juliajforesti
Repo: RocketChat/Rocket.Chat PR: 40256
File: apps/meteor/client/components/CreateDiscussion/CreateDiscussion.tsx:121-149
Timestamp: 2026-05-06T12:21:44.083Z
Learning: Field wrappers in rocket.chat/fuselage-forms (Field, FieldLabel, FieldRow, FieldError, FieldHint) auto-create htmlFor/id associations, aria-describedby, and role="alert" for errors. Do not manually set htmlFor, id, aria-describedby, or role attributes when using these wrappers. This automatic wiring does not apply to plain rocket.chat/fuselage components, which require explicit ID wiring per the accessibility docs. In code reviews, prefer using fuselage-forms wrappers for form fields and verify there is no unnecessary manual ID/aria wiring in files that use these wrappers. If a component uses plain fuselage components, ensure proper id wiring as per docs.
Applied to files:
packages/models/src/models/Roles.ts
🔇 Additional comments (2)
packages/models/src/models/Roles.ts (1)
141-145: LGTM!Also applies to: 147-152
.changeset/fix-find-custom-roles-protected-filter.md (1)
1-6: LGTM!
Shevilll
left a comment
There was a problem hiding this comment.
Thanks for the clear writeup and repro steps. Switching to { protected: { $ne: true } } is the right semantic for "all non-protected roles," including legacy docs where the field was never stored.
Two notes:
- Heads up that #41031 makes the identical two-line change. This PR is the more tightly scoped of the two (query fix + changeset + linked issue), so it seems like the better one to land.
- Since you offered — a small unit test would lock this in: insert a role without
protectedand assert it shows up infindCustomRoles()/ is counted bycountCustomRoles(). The existing suite doesn't exercise this query shape.
Looks correct to me otherwise.
Proposed changes
findCustomRolesandcountCustomRolesinpackages/models/src/models/Roles.tscurrently filter roles with{ protected: false }. MongoDB equality comparison againstfalseonly matches documents where the field is explicitlyfalse— documents whereprotectedis missing/undefined are excluded.On Rocket.Chat instances upgraded from older versions, custom roles created years ago do not have a
protectedfield stored at all. As a result, these legitimate custom roles are silently dropped from both queries, causing false negatives when listing or counting them.Switching the filter to
{ protected: { $ne: true } }makes the query match bothfalseand missing values, which matches the semantic intent ("all roles that are not protected") and restores correct behavior for legacy databases.Issue(s)
Fixes #40798
(Also patches
countCustomRoles, which has the same bug — not mentioned in the issue but the fix is identical and trivial.)Steps to test or reproduce
rocketchat_rolescollection without aprotectedfield:Roles.findCustomRoles().toArray()— before this PR the legacy role is missing; after the PR it appears.Roles.countCustomRoles().Further comments
Filter<IRole>already permits{ $ne: T }operator expressions).Rolesdoesn't cover this query shape; happy to add a unit test if reviewers prefer.Summary by CodeRabbit
Bug Fixes