Laravel-Backpack / PermissionManager

Admin interface for managing users, roles, permissions, using Backpack CRUD
http://backpackforlaravel.com
Other
516 stars 166 forks source link

Can't customize UserCrudController #307

Closed jorguerra closed 2 years ago

jorguerra commented 2 years ago

I followed the instructions to customize the UserCrudController but I haven't been able to make it work.

What I did:

I've created a new controller

<?php
    namespace App\Http\Controllers\Admin;
    use Backpack\PermissionManager\app\Http\Controllers\UserCrudController as CrudController;
    use Illuminate\Contracts\Database\Eloquent\Builder;

    class UserCrudController extends CrudController
    {
        public function boot()
        {
            parent::boot();
            if(backpack_auth()->check() && backpack_auth()->user()->hasRole('entrepreneur'))
            {
                $startup_id = backpack_auth()->user()->admin?->id ?: null;
                static::addGlobalScope('userFilter', static function(Builder $builder) use($startup_id){
                    $builder->whereRaw("id in (select user_id from empresa where empresa.id={$startup_id})");
                });
            }
        }
    }

In the file app/Providers/AppServiceProvider.php inside the method register I have added the following code.

 $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
);

What I expected to happen

I expected to visualize different users filtered by the startup to which the user logged in belongs. They shoudn't see admins either.

What happened:

Nothing, none behaviour has changed.

What I've already tried to fix it:

Different things but without success

Backpack, Laravel, PHP, DB version: Backpack 5.x, Laravel 9, MySQL 8

welcome[bot] commented 2 years ago

Hello there! Thanks for opening your first issue on this repo!

Just a heads-up: Here at Backpack we use Github Issues only for tracking bugs. Talk about new features is also acceptable. This helps a lot in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.

Backpack communication mediums:

Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome awesome community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.

Thank you!

-- Justin Case The Backpack Robot

tabacitu commented 2 years ago

Hey @jorguerra ,

Which part didn't work for you? Was the UserCrudController not extended properly? If you dd('got here'); in your UserCrudController::boot(), does it reach that point? I assume yes, so this doesn't look like a bug, but an implementation error. So I'm going to close the issue - we keep Github for bugs only.

But feel free to reply here, we might be able to help with advice. Personally:

Then again, that's what I'd do, your method should theoretically work too, though I've never tried it 🤷‍♂️

Cheers!

jorguerra commented 2 years ago

I tried the dd function but it's not reached

tabacitu commented 2 years ago

Then the problem is that your custom AdminController was not reached at all. Take another look at the way you extended UserCrudController (binding) and fix that first:

// 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.