-
Notifications
You must be signed in to change notification settings - Fork 0
Changes: Update src/lib/permissions.ts to return a clear
#130
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
Conversation
…lear `PermissionDenied` result object (instead of throwing) for denied chec
WalkthroughThe pull request refactors permission-checking functions in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 inconclusive)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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.
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 (2)
src/lib/permissions.ts (1)
4-23: Consider removing redundant type parameters.The
<boolean>type parameter inerror<boolean>(...)calls is redundant since TypeScript can infer it from the function's return typeResult<boolean>.Apply this diff to simplify the code:
export function hasProjectPermission(userRole: Role, requiredRoles: Role[]): Result<boolean> { if (requiredRoles.includes(userRole)) { return success(true); } - return error<boolean>("Permission denied", ErrorCodes.FORBIDDEN); + return error("Permission denied", ErrorCodes.FORBIDDEN); } export function isProjectAdmin(userRole: Role): Result<boolean> { if (userRole === Role.ADMIN) { return success(true); } - return error<boolean>("Permission denied", ErrorCodes.FORBIDDEN); + return error("Permission denied", ErrorCodes.FORBIDDEN); } export function isProjectMember(userRole: Role): Result<boolean> { if (userRole === Role.USER || userRole === Role.ADMIN) { return success(true); } - return error<boolean>("Permission denied", ErrorCodes.FORBIDDEN); + return error("Permission denied", ErrorCodes.FORBIDDEN); }src/lib/action.ts (1)
92-94: Logic is correct, but consider utilizing the Result error information.The authorization check correctly handles the
Result<boolean>return type using.success === false. The short-circuit evaluation ensureshasProjectPermissionisn't called whenprojectMemberis null/undefined, preventing runtime errors.However, the error information (error message and errorCode) from the
Resultis being discarded. ThehasProjectPermissionfunction returns detailed error context (ErrorCodes.FORBIDDEN), but this is replaced with a generic error message.Consider propagating the Result's error information:
- if (!projectMember || hasProjectPermission(projectMember.role, requiredRoles).success === false) { - throw new Error('Permission denied: Not authorized to perform this action on this project.'); + if (!projectMember) { + throw new Error('Permission denied: Not a member of this project.'); + } + + const permissionResult = hasProjectPermission(projectMember.role, requiredRoles); + if (!permissionResult.success) { + throw new Error(permissionResult.error); }This approach:
- Separates the membership check from the permission check for clarity
- Preserves the error message and context from the permission check
- Maintains the same error-throwing behavior expected by the middleware
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/lib/action.ts(1 hunks)src/lib/permissions.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/lib/permissions.ts (1)
src/lib/result.ts (3)
Result(18-18)success(25-30)error(38-44)
src/lib/action.ts (1)
src/lib/permissions.ts (1)
hasProjectPermission(4-9)
🔇 Additional comments (2)
src/lib/permissions.ts (2)
2-2: LGTM!The updated imports correctly include all the necessary types and functions for the Result pattern refactor.
4-9: Breaking API change is properly integrated across all call sites.The function signature change from returning
booleantoResult<boolean>is correct and aligns with the PR objectives. The logic properly returnssuccess(true)when permission is granted anderror<boolean>("Permission denied", ErrorCodes.FORBIDDEN)when denied.All call sites have been updated. The single identified usage in
src/lib/action.tsline 92 correctly handles theResult<boolean>return type by accessing the.successproperty:hasProjectPermission(projectMember.role, requiredRoles).success === false.
|
that clearer |
Changes requested:
Update
src/lib/permissions.tsto return a clearPermissionDeniedresult object (instead of throwing) for denied checks; adjust a single small call site if needed.Please review.
Summary by CodeRabbit
Bug Fixes
Refactor
✏️ Tip: You can customize this high-level summary in your review settings.