PHP code example of ykozhemiaka / simple-permission

1. Go to this page and download the library: Download ykozhemiaka/simple-permission 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/ */

    

ykozhemiaka / simple-permission example snippets


use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;

$capsule->addConnection([
            'driver' => 'mysql',
            'host' => 'localhost',
            'database' => 'simple_permissions',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8mb4'
        ]);

$capsule->setAsGlobal();
 
$capsule->bootEloquent();

use SimplePermission\Database\Schemas\UsersSchema;

$userSchema = new UsersSchema($capsule);
$userSchema->createTable();

use SimplePermission\Database\Schemas\PermissionSchemas;

$permissionSchemas = new PermissionSchemas($capsule);
$permissionSchemas->createTable();

namespace SimplePermission\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasPermission;

    protected $table = 'custom-table-name';

...

use SimplePermission\Repositories\PermissionRepository;

$permissionRepo = new PermissionRepository();
$permission = $permissionRepo->create('Edit article');

$id = 1;

$permission = $permissionRepo->update('Edit posts', 1)

$permissionRepo->deleteByName('Edit article');

$permissionRepo->deleteById(2)

$role = Role::find(1);
//or you can use role id $role = 1;

$permission = $permissionRepo->whereId(1)->assignRole($role);

$permissionRepo->whereId(1)->terminateToRole($role);

$user = User::find(1);

$role = Role::find(1);
//or you can use role id $role = 1;
$permission = Permission::find(1);
//or you can use permission id $permission = 1;

$user->allowTo($permission);

$user->grantRole($role);

$user->ableTo('Edit posts');

$user->hasRole('Admin');

$user->terminateToPermission($permission);

$user->terminateToRole($role);

$roleRepo = new RoleRepository();
$role = $roleRepo->create('Admin');

$role->permissions;

$role = $roleRepo->update('Moderator', $role->id);

$roleRepo->deleteByName($role->name);

$roleRepo->deleteById($role->id);

$role = $roleRepo->whereId(1)->grant($permission)

$role = $roleRepo->whereId(1)->assignRoleTo($user)

$roleTerminate = $roleRepo->whereId(1)->terminateToUser($user);

$role = $roleRepo->whereId(1)->terminateToPermission($permission);