PHP code example of jdecool / security-role-checker-bundle

1. Go to this page and download the library: Download jdecool/security-role-checker-bundle 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/ */

    

jdecool / security-role-checker-bundle example snippets




public function registerBundles()
{
    $bundles = [
        // ...
        new JDecool\Bundle\SecurityRoleCheckerBundle\JDecoolSecurityRoleCheckerBundle(),
    ];

    // ...

    return $bundles;
}

class MyController
{
    public function myAction()
    {
        $roleChecker = $this->get('jdecool.security.role_checker');
        
        var_dump($role->hasRole('ROLE_USER')); // checking role for current user
        
        $userWithRole = $this->getDoctrine()->getRepository(/* ... */)->find(1);
        var_dump($role->hasRole('ROLE_USER', $userWithRole)); // true
        
        $userWithoutRole = $this->getDoctrine()->getRepository(/* ... */)->find(2);
        var_dump($role->hasRole('ROLE_USER', $userWithoutRole)); // false
        
        // ... 
    }
}