PHP code example of jstewmc / authorize-group

1. Go to this page and download the library: Download jstewmc/authorize-group 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/ */

    

jstewmc / authorize-group example snippets


use Jstewmc\AuthorizeGroup;

// grant permissions to roles
$roles = [
    // the "administrator" role...
    'administrator' => [
        // for the "users" resource...
        'users' => [
            // has the "create" action
            'create'   
        ]
    ]
];

// assign roles to groups
$groups = [
    // the "administrators" group...
    'administrators' => [
        // has the "administrator" role
        'administrator'
    ]
];

// implement a group named "administrators"
$group = new class implements Group {
    public function getName(): string {
        return 'administrators';
    }
}

// create our authorization service
$authorizer = new Authorize($groups, $roles);

// is the group authorized to create users? (yes)
$authorizer($group, 'create', 'users');

// is the group authorized to delete users? (no)
$authorizer($group, 'delete', 'users');