PHP code example of dtkahl / php-access-control

1. Go to this page and download the library: Download dtkahl/php-access-control 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/ */

    

dtkahl / php-access-control example snippets


$role_member = new AccessRole(
    "member", // role name
    [
        "access",                           // rights as string
        "blog_post.edit"                    // you can namespace rights with a dot, this is often used for object identifier
        "blog_post" => ["view", "create"]   // instead of a dot you can also use an array to create namespaces
    ],
);

$role_admin = new AccessRole(
    "admin", // role name
    ["do_admin_stuff"],  // global rights as array
    [],
    $role_member // extend the member role so you dont have to specify all rights a second time
);

$object_blog = new AccessObject(
    "blog", // identifier of an object
    [$role_author, $role_subscriber] // array of object related roles
);

$judge = new Judge(
    [$role_member], // array of all defined global roles
    [$object_blog, $object_comment], // array of all defined objects
    $user // optional, default user to check rights for
);

$comment = BlogComment::find('1');
$judge->checkRight('edit', $comment); // check if the default user is allowed to edit a specific comment

$comment = BlogComment::find('1');
$judge->checkRole('creator', $comment); // check if the default user is the creator of this comment

composer