PHP code example of inani / maravel-permissions

1. Go to this page and download the library: Download inani/maravel-permissions 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/ */

    

inani / maravel-permissions example snippets


'providers' => [
    ...
    Inani\Maravel\Providers\MaravelServiceProvider::class,
    ...
];



return [
    // Define the list of actions to be checked against
    'actions' => [
        'add', 'delete', 'create', 'search', 'update'
    ],

    // define the class path for the entities
    'entities' => [
        \App\Models\User::class,
    ],
];


use Inani\Maravel\HasRole;

class User extends Model
{
    use HasRole;
    ...
}

// Having a user
$user = User::first();

// Create a new role, description is not mandotary
$userManager = RoleBuilder::create('User Manager', 'The role to manage users')
                ->havingPower([
                   'name' => 'can_update',
                   'description' => 'The abilitiy to update a user',
                   'action' => 'update',
                   'entity' => \App\Models\User::class,
               ]);

// we can grant a power to it
$userManager = RoleBuilder::of($userManager)
                        ->grant([
                             'name' => 'can_create',
                             'description' => 'The abilitiy to create a user',
                             'action' => 'create',
                             'entity' => \App\Models\User::class,
                         ]);

// Or take it off
$ability = Ability::first();

$storm = RoleBuilder::of($userManager)->takeOff($ability);


// bless the user with the abilities of the role
$user->roleManager()->blessWith($storm);


// check if it has the ability
$user->roleManager()->owns($ability);

// check if it has one of the provided abilities
$user->roleManager()->ownsOneOf([$ability, $anOtherAbility]);

// make it human again (remove its role)
$user->roleManager()->humanize();



// Create Ability
$ability = Ability::create([
    'name' => 'post_write',
    'description' => 'Abitlity to create new Posts',
    'action' => 'add',
    'entity' => \App\Models\Post::class,
]);

// Create a Marvel
$writer = Role::create([
    'name' => 'Webmaster',
    'description' => 'A Role that allows you create new posts'
]);

// Grant the ability
$writer->grant($ability);

// remove a certain ability
$writer->takeOff($ability);

// remove all and keep only those
$abilities = [1, 2]; // or the models
$writer->keep($abilities);

// bless it to our user
$user = \App\Models\User::first();

$user->roleManager()->blessWith($writer);
bash
php artisan vendor:publish
bash
php artisan migrate