PHP code example of maximkou / laravel-simple-voters

1. Go to this page and download the library: Download maximkou/laravel-simple-voters library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

maximkou / laravel-simple-voters example snippets


is_granted('edit', $post) // return true or false
// or using Facade
Access::isGranted('edit', $post)

is_granted(['read', 'write'], $post, $user) // return true or false
// or using Facade
Access::isGranted(['read', 'write'], $post, $user)

'providers' => [
    Maximkou\SimpleVoters\SimpleVotersServiceProvider::class,

'aliases' => [
    'Access' => Maximkou\SimpleVoters\Facades\Access::class,

// file config/voters.php
/**
 * Available out of the box strategies: affirmative, unanimous, consensus.
 * You can use custom voting strategy by registering service with name 'simple_voters.strategies.{strategy_name}'
 */
'strategy' => 'unanimous',

/**
 * If pro and contra votes count is equal, or all voters abstain, used this value
 */
'is_granted_by_default' => true,

/**
 * List of Voter classes.
 */
'voters' => [
    // put here your voters classes
],

class PostVoter extends AbstractVoter
{
    protected function supports($action, $object)
    {
        return in_array('action', ['edit', 'remove']) && $object instanceOf Post;
    }
    
    protected function isGranted($action, $object, $user)
    {
        $checker = "can".ucfirst($action);
        
        return $this->$checker($object, $user);
    }
    
    private function canEdit($object, $user)
    {
        return $object->user_id = $user->id;
    }
    
    private function canRemove($object, $user)
    {
        return $object->user_id = $user->id;
    }
}

use Maximkou\SimpleVoters\Services\Access;
use Maximkou\SimpleVoters\GrantStrategies;

$accessChecker = new Access(
    new GrantStrategies\Affirmative($listVoters), // choose voting strategy
    new MyAuthUserResolver() // pass your user resolver
);

$accessChecker->isGranted('action', $object); // true/false?
bash
php artisan vendor:publish --provider="Maximkou\SimpleVoters\SimpleVotersServiceProvider"