Search code examples
laravellaravel-backpack

configure my PermissionCrudController to manipulate permissions in Backpack for Laravel PermissionManager


Previously I created my PermissionCrudController (App\Http\Controllers\Admin) and in the setup() I could use the created permissions to allow access to the crud functions.

namespace App\Http\Controllers\Admin;
  
use Backpack\PermissionManager\app\Http\Controllers\PermissionCrudController as PermissionControllerPrincipal;

/**
 * Class PermissionCrudController
 * @package App\Http\Controllers\Admin
 * @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
 */
class PermissionCrudController extends PermissionControllerPrincipal
{
    /**
     * Configure the CrudPanel object. Apply settings to all operations.
     * 
     * @return void
     */
    public function setup()
    {
        parent::setup();

        if (!backpack_user()->can('permiso_listar_admin')) {
            $this->crud->denyAccess('list'); //not working
        }

        $this->crud->denyAccess('create'); //not working
        $this->crud->denyAccess('update'); //not working
        $this->crud->denyAccess('delete'); //not working
    }

}

But currently it doesn't work anymore. What would be the correct or updated way to do it?

At the moment I only have to block access for everyone on "App\Config\Backpack\Permissionmanager.php" the array:

'allow_permission_create' => false,
'allow_permission_update' => false,
'allow_permission_delete' => false,
'allow_role_create' => false,
'allow_role_update' => false,
'allow_role_delete' => false,

But the requirement is that some users can create roles.


Solution

  • I am not sure if you have done this already, but you need to tell the service container to use your controller instead of the original one:

    namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    
    class AppServiceProvider extends ServiceProvider
    {
        /**
         * Register any application services.
         */
        public function register(): void
        {
            $this->app->bind(
                \Backpack\PermissionManager\app\Http\Controllers\PermissionCrudController::class,
                \App\Http\Controllers\Admin\PermissionCrudController::class
            );
        }
    
        /**
         * Bootstrap any application services.
         */
        public function boot(): void
        {
            //
        }
    }
    

    Let us know if this resolves the issue for you.

    Cheers!