PHP code example of madlines / common-security-resolver

1. Go to this page and download the library: Download madlines/common-security-resolver 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/ */

    

madlines / common-security-resolver example snippets




class PostEditVoter
{
    public function isGranted($user, $task)
    {
        // if (!($task instanceof PostEditTask)) {
        if ($task !== 'post_edit') {
            return null; // null means 'ignore'
            // returning integer 0 means the same
        }

        if ($user->hasRole('ROLE_ADMIN')) {
            return true; // agree
            // returning integer 1 means the same
        }

        return false; // disagree
        // returning integer -1 means the same
    }
}

$postEditVoter = new PostEditVoter();
// create more voters if you like

$resolver = new AccessResolver();

$resolver->addVoter($postEditVoter); // You can pass method name as second parameter. It defaults to `isGranted`
// add more voters if you like


$user = $this->getUser();

$resolver->isGranted($user, 'post_edit');
// or maybe $resolver->isGranted($user, $postEditTask);