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

Component Assertion Voter Laravel Package

appsco/component-assertion-voter

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer:

    composer require appsco/component-assertion-voter
    

    Register the service provider in config/app.php under providers:

    Appsco\ComponentAssertionVoter\ComponentAssertionVoterServiceProvider::class,
    
  2. Basic Usage The package provides a AssertionVoter trait for authorization logic. Start by creating a voter class:

    use Appsco\ComponentAssertionVoter\ComponentAssertionVoter;
    
    class UserVoter implements CanVote
    {
        use ComponentAssertionVoter;
    
        public function canVote($user, $ability, $model)
        {
            return $this->assert($user, $ability, $model);
        }
    }
    
  3. First Use Case Register the voter in AuthServiceProvider:

    protected $voters = [
        'user' => \App\UserVoter::class,
    ];
    

Implementation Patterns

Core Workflow

  1. Define Assertions Extend the ComponentAssertionVoter trait and override assert() to define custom logic:

    protected function assert($user, $ability, $model)
    {
        switch ($ability) {
            case 'edit':
                return $user->isAdmin() || $user->owns($model);
            case 'delete':
                return $user->isAdmin();
            default:
                return false;
        }
    }
    
  2. Reusable Components Break down assertions into smaller, testable components:

    protected function canEditOwnContent($user, $model)
    {
        return $user->owns($model);
    }
    
    protected function canDeleteAdminOnly($user)
    {
        return $user->isAdmin();
    }
    
  3. Integration with Laravel Gates Use the voter with Laravel’s gate system:

    Gate::resource('post', 'App\Post', function ($user, $post) {
        return (new UserVoter)->canVote($user, 'edit', $post);
    });
    
  4. Policy Integration Combine with Laravel’s policies for modularity:

    class PostPolicy implements Policy
    {
        public function edit(User $user, Post $post)
        {
            return (new UserVoter)->assert($user, 'edit', $post);
        }
    }
    

Gotchas and Tips

Pitfalls

  1. Deprecated Package

    • Last updated in 2014; may not work with modern Laravel (5.8+). Test thoroughly.
    • No active maintenance; fork or refactor if critical.
  2. Trait Overhead

    • The ComponentAssertionVoter trait is minimal but assumes a specific assert() method signature. Override carefully to avoid conflicts.
  3. No Built-in Logging

    • Debugging failed assertions requires manual logging or dd() debugging.

Debugging Tips

  1. Inspect Voter Calls Override assert() to log inputs:

    protected function assert($user, $ability, $model)
    {
        \Log::debug("Voter called for: {$ability}", [
            'user' => $user->id,
            'model' => get_class($model),
        ]);
        // ... rest of logic
    }
    
  2. Test Assertions Isolated Unit test assertions separately from Laravel’s gate system:

    public function testCanEditOwnPost()
    {
        $user = User::factory()->create();
        $post = Post::factory()->create(['user_id' => $user->id]);
        $this->assertTrue((new UserVoter)->assert($user, 'edit', $post));
    }
    

Extension Points

  1. Custom Assertion Logic Extend the trait to add helper methods:

    trait CustomAssertions
    {
        protected function isActive($user)
        {
            return $user->active;
        }
    }
    
    class UserVoter
    {
        use ComponentAssertionVoter, CustomAssertions;
    }
    
  2. Dynamic Ability Handling Use closures for dynamic abilities:

    protected function assert($user, $ability, $model)
    {
        $rules = [
            'edit' => fn($u, $m) => $u->isAdmin() || $u->owns($m),
            'delete' => fn($u, $m) => $u->isAdmin(),
        ];
        return $rules[$ability]($user, $model);
    }
    
  3. Cache Assertions Cache results for performance (if assertions are expensive):

    protected $cache = [];
    
    protected function assert($user, $ability, $model)
    {
        $key = "assertion:{$user->id}:{$ability}:{$model->id}";
        if (isset($this->cache[$key])) {
            return $this->cache[$key];
        }
        // ... logic
        $this->cache[$key] = $result;
        return $result;
    }
    
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.
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager