Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Bouncer Laravel Package

silber/bouncer

Roles and abilities for Laravel with a clean, expressive API. Bouncer manages user authorization, supports Eloquent models, caching, gates and policies, and fluent assignment/checks like can() and is(). Great for flexible, database-driven permissions.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Role-Based Access Control (RBAC) + Attribute-Based Access Control (ABAC) Hybrid: Bouncer provides a robust, Eloquent-based RBAC system with fine-grained abilities (e.g., can('edit', Post::class)), aligning well with Laravel’s native authorization patterns. The ability to mix roles and direct abilities (e.g., allow($user)->to('publish', Article::class)) makes it versatile for complex permission models.
  • Policy Integration: Bouncer’s design to run after policies by default (configurable) ensures compatibility with Laravel’s built-in policy system, avoiding conflicts while allowing granular overrides.
  • Multi-Tenancy Support: Native scoping for multi-tenant applications (e.g., Bouncer::scope()->onceTo($tenantId, ...)) reduces boilerplate for tenant-isolated permissions.
  • Ownership Model: Built-in support for ownership-based permissions (e.g., can('update', Post::class, $post)) simplifies resource-specific access control.

Integration Feasibility

  • Laravel Ecosystem Alignment: Deep integration with Eloquent, Gates, and Policies minimizes friction. The package leverages Laravel’s service container, migrations, and caching systems natively.
  • Database Agnostic: Supports PostgreSQL, MySQL, Oracle, and SQLite with minimal adjustments (e.g., JSON column defaults for older MySQL versions).
  • Migration Path: Backward-compatible for Laravel 11+ (v1.0.2+) with clear upgrade paths for older versions (e.g., v1.0.1 for Laravel 10). Schema changes are minimal (e.g., nullable entity_id/entity_type for allowEveryone()).
  • Testing Support: Uses Workbench for real-app testing, reducing flakiness in CI/CD pipelines.

Technical Risk

  • Breaking Changes in v1.0.0:
    • Removal of Levels (niche feature) and reordering of policy checks may require refactoring existing authorization logic.
    • Morph Map Migration: Custom models/table names now auto-register with Laravel’s morph map, requiring DB updates if previously manual.
    • Cross-Request Cache: Changes to cached role representation may necessitate Bouncer::refresh() post-upgrade.
  • Performance Overheads:
    • Caching is configurable but may introduce complexity if misconfigured (e.g., stale cached roles).
    • Multi-tenancy scoping adds query overhead for scoped operations.
  • Dependency Risks:
    • Tight coupling with Laravel’s auth system (e.g., Auth::user()) may complicate custom auth setups (e.g., API tokens).
    • PHPUnit 11+ requirement for newer versions may impact legacy test suites.

Key Questions

  1. Authorization Complexity:
    • Does the team’s permission model require Levels (removed in v1.0.0) or other niche features? If so, evaluate alternatives or custom extensions.
    • Are there existing policies that rely on Bouncer’s pre-policy execution? Test with Bouncer::runAfterPolicies() to validate behavior.
  2. Multi-Tenancy:
    • Is multi-tenancy a requirement? If so, validate the scoping mechanism’s performance impact on high-traffic endpoints.
    • Are tenant IDs dynamically resolved (e.g., from middleware)? Ensure compatibility with Bouncer::scope()->onceTo().
  3. Migration Effort:
    • For custom models/tables: Are existing entity_type values stored as raw class names? If yes, plan for the morph map migration.
    • For allowEveryone(): Are entity_id/entity_type nullable in the permissions table? If not, update the schema.
  4. Caching Strategy:
    • Is cross-request caching enabled? If yes, test Bouncer::refresh() post-upgrade to clear stale data.
    • Are there performance bottlenecks in cached role resolution? Benchmark with/without caching.
  5. Testing:
    • Does the test suite use Workbench? If not, evaluate the effort to adopt it for real-app testing.
    • Are there edge cases for soft-deleted roles/abilities (e.g., pivot records not deleted)?

Integration Approach

Stack Fit

  • Laravel Core: Bouncer is a first-class citizen in Laravel’s auth stack, integrating seamlessly with:
    • Eloquent Models: Uses HasAbilities/Authorizable traits for model-based permissions.
    • Gates/Policies: Extends Laravel’s native Gate facade with can(), canAny(), etc.
    • Middleware: Can be used in authorize() middleware or policy methods.
    • Events: Supports Bouncer\Events\AbilityAdded, RoleAssigned, etc.
  • Database: Optimized for Eloquent’s ORM with support for all major databases (MySQL, PostgreSQL, Oracle, SQLite).
  • Caching: Leverages Laravel’s cache drivers (Redis, Memcached, etc.) for role/ability caching.
  • Testing: Compatible with Pest, PHPUnit, and Laravel’s testing helpers.

Migration Path

  1. Assessment Phase:
    • Audit existing authorization logic (policies, gates, middleware) for Bouncer compatibility.
    • Identify custom models/tables using Bouncer and check for morph map dependencies.
    • Review database schema for nullable entity_id/entity_type (if using allowEveryone()).
  2. Dependency Update:
    • Update Laravel to v11+ (for v1.0.2+) or v10 (for v1.0.1) based on project constraints.
    • Update PHP to 8.0+ (v1.0.0-rc.10+) or 7.4+ (older versions).
    • Update Composer dependencies (e.g., PHPUnit 11+ for newer Bouncer versions).
  3. Schema Migration:
    • Run Bouncer’s migrations (php artisan vendor:publish --provider="Bouncer\BouncerServiceProvider").
    • For custom models: Update entity_type values to morph map classes if needed.
    • For allowEveryone(): Alter permissions table to make entity_id/entity_type nullable.
  4. Code Changes:
    • Replace custom getClipboardInstance() calls (removed in v1.0.0-rc.7).
    • Update policy check order if relying on Bouncer’s pre-policy execution (use Bouncer::runAfterPolicies()).
    • Replace Levels logic with roles/abilities if needed.
  5. Testing:
    • Test all authorization flows, especially:
      • Role/ability assignments (allow(), revoke()).
      • Multi-tenancy scoping.
      • Policy overrides (e.g., Bouncer::allow() vs. policy deny()).
    • Validate caching behavior with Bouncer::refresh().

Compatibility

  • Laravel Versions:
    • v1.0.2+: Laravel 11–13 (latest).
    • v1.0.1: Laravel 10.
    • v1.0.0-rc.13: Laravel 9.
    • v1.0.0-rc.6: Laravel 6–8.
    • v0.1.1: Laravel 5.2.
  • PHP Versions:
    • v1.0.0-rc.11+: PHP 8.1+.
    • v1.0.0-rc.10: PHP 8.0+.
    • Older versions: PHP 7.4+ (varies by release).
  • Third-Party:
    • Lighthouse GraphQL: Supports return types for nested mutations.
    • Spatie Laravel-Permission: Avoid conflicts by ensuring no overlapping migrations/tables.

Sequencing

  1. Non-Production First:
    • Test in a staging environment with a copy of production data.
    • Use feature flags to toggle Bouncer integration incrementally.
  2. Phased Rollout:
    • Phase 1: Replace simple gates with Bouncer abilities (e.g., Gate::define('edit-post', ...)Bouncer::allow($user)->to('edit', Post::class)).
    • Phase 2: Migrate roles to Bouncer’s role system.
    • Phase 3: Enable multi-tenancy scoping if applicable.
  3. Fallbacks:
    • Implement circuit breakers for Bouncer failures (e.g., cache issues) to fall back to policies.
    • Log Bouncer-related errors to monitor adoption risks.

Operational Impact

Maintenance

  • Proactive Updates:
    • Monitor Bouncer’s release cycle (quarterly major releases) and Laravel compatibility.
    • Subscribe to the GitHub repo for breaking changes.
  • Dependency Management:
    • Pin Bouncer to a specific version in composer.json to avoid auto-updates during minor releases.
    • Use composer why-not to audit dependency conflicts.
  • Schema Drift:
    • Document Bouncer’s table structure (roles, abilities, `
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport