Skip to content

Conversation

@KeeganBeuthin
Copy link
Contributor

Has Functionality Implementation

This PR implements comprehensive authorization checking capabilities for the Ruby SDK.

What's Added

Core Methods:

  • has_roles?, has_permissions?, has_feature_flags?, has_billing_entitlements? - Individual authorization checks
  • has() - Unified method for checking multiple conditions at once

Advanced Features:

  • Complex Conditions: Custom Ruby blocks for sophisticated authorization logic
  • Force API Control: Choose between token-cached or fresh API data
  • Early Exit Optimization: Stops checking on first failure for better performance
  • Flexible Input: Supports arrays, single strings, symbols, and string keys

Usage Examples

Simple checks

client.has_roles?(['admin', 'user'])
client.has_permissions?('read:users')

Unified authorization

client.has(
roles: ['admin'],
permissions: ['read:users'],
feature_flags: ['dark_mode']
)

Complex conditions

client.has_roles?([
'admin',
{ role: 'manager', condition: ->(r) { r[:level] == 'senior' } }
])

…tion checking

Add complete authorization checking system with unified and individual methods:

- Add individual authorization methods: has_roles?, has_permissions?,
  has_feature_flags?, has_billing_entitlements?
- Add unified has() method supporting multiple condition types in single call
- Implement complex conditions with Ruby blocks for custom authorization logic
- Add force API parameter support (boolean and selective hash options)
- Implement early exit optimization for improved performance
- Add method aliases for cross-platform compatibility
- Add comprehensive error handling with multi-level logging fallbacks
- Support both symbol and string keys for flexible usage

Features:
- Empty array handling returns true (no constraints = allowed)
- Single item and array parameter support
- Custom role/permission/entitlement conditions with Ruby blocks
- Graceful error handling with safe defaults
- Performance-optimized early exit pattern
- Force API controls for token vs fresh data usage

Testing:
- Add comprehensive test suite covering 100+ scenarios
- Add real-world integration testing in starter kit
- Add live user data validation testing
- Cover all edge cases and error handling scenarios

Implementation provides enterprise-grade authorization checking
capabilities with Ruby-idiomatic patterns and production-ready
error handling.
Copy link

@ZulqarnainNazir ZulqarnainNazir left a comment

Choose a reason for hiding this comment

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

Great work 🎉 This PR introduces a powerful unified has() method with excellent examples, tests, and cross-SDK compatibility. The implementation is production-ready.

Strengths

  • Comprehensive coverage (roles, permissions, flags, entitlements)
  • Flexible force API parameter handling
  • Early exit optimization for performance
  • 191 lines of examples + dedicated test coverage
  • Consistent with PHP/JS SDKs

Recommendations

  1. has() method is long (~80 lines) → consider breaking into helper methods.
  2. Add more granular error handling (API errors vs. argument errors).
  3. Potential performance gain via caching/memoization.
  4. Inline documentation/examples could make the API even clearer.

Verdict: High-quality PR 🚀 — just a few refactor and maintainability suggestions.

📊 Test Results Summary

Component Implementation Documentation Testing Cross-SDK
Unified has() Method
Complex Conditions
Force API Control
Examples File N/A
Test Coverage N/A N/A

Overall Score: 5/5 components - EXCELLENT implementation

Key Metrics:

  • Lines Added: 1,099 lines (comprehensive implementation)
  • Files Changed: 6 files (well-organized changes)
  • Examples: 191 lines of detailed usage examples
  • Test Coverage: Dedicated test file included
  • Cross-SDK Compatibility: Full PHP and JavaScript SDK support

Areas for Enhancement:

  1. Method Complexity: The has() method could be refactored into smaller helper methods
  2. Error Handling: Could be more granular for different error types
  3. Performance: Consider caching for frequently accessed authorization data
  4. Unit Testing: Could benefit from more granular unit tests

This PR significantly enhances the Ruby SDK's authorization capabilities with a sophisticated unified method. The implementation is production-ready and includes excellent documentation and examples. The complexity of the main method is the primary area for improvement.

🚀 Next Steps

  1. Refactor Method Complexity: Break the has() method into smaller helper methods
  2. Enhanced Error Handling: Add more granular error handling for different scenarios
  3. Performance Optimization: Consider caching strategies for authorization data
  4. Integration Testing: Test with real Kinde instances and complex scenarios
  5. Release Planning: Consider version bump and changelog updates

Comment on lines +344 to +347
rescue StandardError => e
log_error("Error in has method: #{e.message}")
false
end

Choose a reason for hiding this comment

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

Error handling is good, but you might want to differentiate API errors vs. invalid arguments vs. unexpected exceptions. That way, debugging is easier in production.

#
# @param force_api [Boolean, Hash, nil] Force API parameter
# @return [Hash] Parsed force_api settings
def parse_force_api_parameter(force_api)

Choose a reason for hiding this comment

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

Nice flexibility here 👏. One idea: memoize or cache results if this gets called frequently in a hot path, to avoid recomputing hash merges.

# ]
# )
# # => true
def has(conditions = {}, force_api = nil)

Choose a reason for hiding this comment

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

This method is ~80 lines and handles multiple responsibilities. Consider breaking out role/permission/flag/entitlement checks into private helpers (e.g., check_roles_condition) for readability and testability

@onderay onderay requested a review from a team October 28, 2025 11:55
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.

2 participants