1. Go to this page and download the library: Download backpack/permissionmanager 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/ */
backpack / permissionmanager example snippets
namespace App\Models;
use Backpack\CRUD\app\Models\Traits\CrudTrait; // <------------------------------- this one
use Spatie\Permission\Traits\HasRoles;// <---------------------- and this one
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use CrudTrait; // <----- this
use HasRoles; // <------ and this
/**
* Your User Model content
*/
// in some ServiceProvider, AppServiceProvider for example
$this->app->bind(
\Backpack\PermissionManager\app\Http\Controllers\UserCrudController::class, //this is package controller
\App\Http\Controllers\Admin\UserCrudController::class //this should be your own controller
);
// this tells Laravel that when UserCrudController is requested, your own UserCrudController should be served.
namespace App\Http\Controllers\Admin;
use Backpack\PermissionManager\app\Http\Controllers\UserCrudController as BackpackUserCrudController;
class UserCrudController extends BackpackUserCrudController
{
use \App\Traits\CrudPermissionTrait;
public function setup()
{
parent::setup();
$this->setAccessUsingPermissions();
}
}
$this->app->bind(
\Backpack\PermissionManager\app\Http\Controllers\UserCrudController::class, // package controller
\App\Http\Controllers\Admin\UserCrudController::class // the controller using CrudPermissionTrait
);
Route::group([
'namespace' => 'App\Http\Controllers\Admin', // the new namespace
'prefix' => config('backpack.base.route_prefix', 'admin'),
'middleware' => ['web', backpack_middleware()],
], function () {
// the adapted controllers
Route::crud('user', 'UserCrudController');
// Route::crud('role', 'RoleCrudController');
});
Route::group([
'namespace' => '\Backpack\PermissionManager\app\Http\Controllers', // the original namespace
'prefix' => config('backpack.base.route_prefix', 'admin'),
'middleware' => ['web', backpack_middleware()],
], function () {
// to original controllers
// not modified yet in this example
Route::crud('permission', 'PermissionCrudController');
Route::crud('role', 'RoleCrudController');
});
namespace Database\Seeders;
use App\Models\User;
use Illuminate\Database\Seeder;
use Backpack\PermissionManager\app\Models\Permission;
use Backpack\PermissionManager\app\Models\Role;
class PermissionSeeder extends Seeder
{
/**
* Run the database Permission seed.
* Permissions are fixed in code and are seeded here.
* use 'php artisan db:seed --class=PermissionSeeder --force' in production
*
* @return void
*/
public function run()
{
// create permission for each combination of table.level
collect([ // tables
'users',
'roles',
])
->crossJoin([ // levels
'see',
'edit',
])
->each(
fn (array $item) => Permission::firstOrCreate([
'name' => implode('.', $item),
])
->save()
)
//
;
User::first()
->givePermissionTo(['users.edit']);
}
}
artisan db:seed --class=PermissionSeeder --force
shell
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="permission-migrations"
php artisan migrate
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="permission-config"
// then, add the Spatie\Permission\Traits\HasRoles trait to your User model(s)
php
namespace App\Traits;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
/**
* CrudPermissionTrait: use Permissions to configure Backpack
*/
trait CrudPermissionTrait
{
// the operations defined for CRUD controller
public array $operations = ['list', 'show', 'create', 'update', 'delete'];
/**
* set CRUD access using spatie Permissions defined for logged in user
*
* @return void
*/
public function setAccessUsingPermissions()
{
// default
$this->crud->denyAccess($this->operations);
// get context
$table = CRUD::getModel()->getTable();
$user = request()->user();
// double check if no authenticated user
if (!$user) {
return; // allow nothing
}
// enable operations depending on permission
foreach ([
// permission level => [crud operations]
'see' => ['list', 'show'], // e.g. permission 'users.see' allows to display users
'edit' => ['list', 'show', 'create', 'update', 'delete'], // e.g. 'users.edit' permission allows all operations
] as $level => $operations) {
if ($user->can("$table.$level")) {
$this->crud->allowAccess($operations);
}
}
}
}
routes/backpack/permissionmanager.php
config/backpack/permissionmanager.php
config/backpack/permissionmanager.php
routes/backpack/permissionmanager.php
config/backpack/base.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.