PHP code example of sixbyte / perchecker

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

    

sixbyte / perchecker example snippets


'Sixbyte\Perchecker\PercheckerServiceProvider',

'Perchecker' => 'Sixbyte\Perchecker\PercheckerFacade',

use Sixbyte\Perchecker\HasPermissionTrait;

use ... HasPermissionTrait

// 用户登录验证是前提
Route::get('/test', ['middleware' => ['auth', 'perchecker'], 'as' => 'test', function () {
    echo "i have permission";
}]);

# 创建新角色
$role_model = Perchecker::getRoleModel();
$role_id = $role_model->insert(['name'=>'admin','readable_name'=>'超级管理员']);

# 用户绑定新角色 多对多的关系
$user = User::find(1);
$user->roles()->attach($role_id);

# 创建权限
$permission_model = Perchecker::getPermissionModel();
$permission_id = $permission_model->insert(['name'=>'user.create']);

# 权限绑定角色 多对多关系
$role_model = Perchecker::getRoleModel();
$role = $role_model->find(1);
$role->permissions()->attach($permission_id);



/**
 * Perchecker - Laravel 5.1 Package
 * Author: [email protected].
 */
return [

    'role_model'         => 'Sixbyte\Perchecker\Models\Role', // 5.0 style

    'permission_model'   => \Sixbyte\Perchecker\Models\Permission::class,

    'route_model'        => \Sixbyte\Perchecker\Models\Route::class,

    /*
     * Forbidden callback
     */
    'forbidden_callback' => function () {
        header('HTTP/1.0 403 You don\'t have permission to do it!');
        exit('You don\'t have permission to do it!');
    },
    /**
     * route filter function
     */
    'filter_route'       => function ($route) {
        if (in_array('perchecker', $route['middleware'])) {
            return $route;
        }
        return null;
    },
    /*
     * Use template helpers
     */
    'template_helpers'   => true,

    /*
     * Super User role name
     */
    'superuser_role'     => 'superuser',

];
=
$user = Auth::user();
$user->hasPermission(1);
$user->hasPermission('user.create');
=
$user = Auth::user();
$user->hasRole(1);
$user->hasRole('admin');

$user = Auth::user();
$user->getPermissions();

$role_model = Perchecker::getRoleModel();
$role = $role_model->find(1);
$role->permissions()->attach($permission_id);
$role->getPermissions();
shell
php artisan vendor:publish
shell
php artisan migrate
shell
php artisan perchecker:routesync

'perchecker'    => 'Sixbyte\Perchecker\PercheckerMiddleware',