-
Notifications
You must be signed in to change notification settings - Fork 0
fix: implement InjectTenantId middleware (#190) #200
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
…ID (#190) Replaces TenantKey::first() workaround in SecretController with proper middleware-based tenant resolution. **What Changed:** - Added InjectTenantId middleware for automatic tenant_id injection - Registered as 'tenant.inject' alias in bootstrap/app.php - Applied to all /v1/secrets and /v1/attachments routes - Removed TODO comment and hardcoded tenant logic from SecretController - Middleware respects pre-existing tenant_id (SetTenant compatibility) **Implementation:** - Single-tenant development mode: Uses first available TenantKey - Production-ready pattern: Can be extended for user-based resolution - Returns 503 when no TenantKey exists - 5 comprehensive middleware tests added **Tests:** - All 444 Secret-related tests pass - PHPStan level max: ✓ - Laravel Pint: ✓ - REUSE 3.3 compliant: ✓ Fixes #190
💡 Tip: Consider Using Draft PRsBenefits of opening PRs as drafts initially:
How to convert:
This is just a friendly reminder - feel free to continue as is! 😊 |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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.
Pull Request Overview
This PR replaces the hardcoded TenantKey::first() workaround in SecretController::store() with a proper middleware-based tenant resolution system. The new InjectTenantId middleware automatically injects tenant_id into requests for Secret-related routes, enabling cleaner controller code while maintaining the single-tenant development workflow.
Key Changes
- Introduced
InjectTenantIdmiddleware for automatic tenant resolution in single-tenant mode - Removed inline tenant resolution logic from
SecretController::store() - Applied middleware to all
/v1/secrets,/v1/attachments, and/v1/sharesroutes for consistent tenant handling
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| app/Http/Middleware/InjectTenantId.php | New middleware that injects tenant_id from first available TenantKey; returns 503 when none exists |
| app/Http/Controllers/Api/V1/SecretController.php | Removed hardcoded tenant resolution and TODO; now reads tenant_id from request |
| bootstrap/app.php | Registered InjectTenantId middleware with 'tenant.inject' alias |
| routes/api.php | Applied tenant.inject middleware to Secret, SecretAttachment, and SecretShare route groups |
| tests/Feature/Middleware/InjectTenantIdMiddlewareTest.php | Comprehensive test suite with 5 tests covering injection, 503 response, non-overwrite, authentication, and multiple tenants |
| CHANGELOG.md | Documented the fix in Unreleased section with implementation details |
- Improved error message clarity in InjectTenantId middleware - Added tenant_id validation in SecretController::store() - Updated middleware test to match new error message Changes based on Copilot PR review comments.
📌 Summary
Resolves #190 by replacing the hardcoded
TenantKey::first()workaround inSecretController::store()with a proper middleware-based tenant resolution system.🔧 What Changed
Added
app/Http/Middleware/InjectTenantId.php)tenant_idinto request for Secret routestenant_id(SetTenant compatibility)Modified
tenant.injectalias/v1/secretsand/v1/attachmentsroutesTests
✅ Quality Gates
🎯 Implementation Details
Current (Single-Tenant Mode):
Future (Multi-Tenant Production):
The middleware is structured to easily support:
$user->tenant_id)🔗 Related
📝 Migration Notes
No migration required - This is a transparent refactor. All existing Secret routes continue to work as before, but now use proper middleware instead of inline workaround.
Breaking Changes: None
Type: Bug Fix (Technical Debt)
Priority: Medium
LOC: +208, -20
Estimated Review Time: 10-15 minutes