JosephSilber / bouncer

Laravel Eloquent roles and abilities.
MIT License
3.45k stars 333 forks source link

Multiple Guards #177

Closed flick36 closed 7 years ago

flick36 commented 7 years ago

Does Bouncer supports Multiple Guards? like in laravel-permissions https://github.com/spatie/laravel-permission#using-multiple-guards

JosephSilber commented 7 years ago

Can you explain your use case? What are you trying to achieve?

flick36 commented 7 years ago

Ok let's say i have an app with 2 custom guards, one for the clients and one for the administratos, sales team, doctors, nurses, (this is a hospital we are talking as an example) the client can login and can view some pages thanks to the guards you coded in your app, it has it's own dashboard, and stuffs, but the admin and the other ones, have also another dashboard they can see, and in there they have some roles and permissions, but the clients, need another set of permissions cause they're using a different guard type, why we need another guard type you may ask, well as it turns out, the doctor itself can be a client of the hospital, so he needs to login as a client to see he´s other dashboard... u know what i mean? sorry for my bad english :S

codeitlikemiley commented 7 years ago

i dont think this can be use for multiple auth guard... for multi auth

Lloople commented 7 years ago

@flick36 Don't know if I understood what you mean, but you can divide the app in roles or actions using Bouncer.

For example what I'm doing in my app is that the clients has the role 'client', admins has role 'administrator', and my root user has 'client', 'administrator' and 'root' so I can do everything.

Maybe you can assing the role 'client' and 'doctor' to the doctor to see both dashboards

jonagoldman commented 7 years ago

One reason I use Bouncer instead of Spatie's "laravel-permission" is that I don't need multi guard support so I don't want my database polluted with data that will never be used. I think that you should use Spatie package for that functionality.

JosephSilber commented 7 years ago

why we need another guard type you may ask, well as it turns out, the doctor itself can be a client of the hospital, so he needs to login as a client to see he´s other dashboard... u know what i mean?

@flick36 I'm sorry, but I'm not sure I understand. Why doesn't the doctor have both a client and a doctor role?

flick36 commented 7 years ago

Cause, its a different dashboard with different logic, controllers, routes etc that are protected with another guard.

Sent from my Htc HTC 10 using FastHub

coolynx commented 7 years ago

Looks like the misunderstanding here is that Bouncer is too "simple" and does not have all those whistles attached to it.

If I get it correctly on what @flick36 asks is support for multiple user providers that look like this example of multiple guards.

Probably in spatie/laravel-permission they have a wrapper that does all that staff for a user out of the box but Bouncer does not. However, you can do the same just have to write this code yourself.

I have not tested multi auth, but current implementation of Bouncer roles saves full namespace of a user provider, like this example:

// default user provider
App\User;
// and admin user provider
App\Admin\User;

So, you should not have a problem with multiple user providers. The same goes with permissions or abilities in Bouncer. But as I mentioned before - you should write this logic by yourself.

In theory you should be able to attach a role doctor with specific abilities to a user dr.john in App\Admin\User provider and a client role to another user with the same name dr.john in other App\User provider.

JosephSilber commented 7 years ago

@flick36 to me, this is similar to multi-tenancy. With multi-tenancy support in Bouncer, you can create a middleware that scopes bouncer:

namespace App\Http\Middleware;

class ScopeBouncer
{
    public function handle($request, $next, $scope)
    {
        Bouncer::scope()->to($scope);

        return $next($request);
    }
}

Then register it in your route middleware:

protected $routeMiddleware = [
    // ...all existing ones...
    'scope-bouncer' => \App\Http\Middleware\ScopeBouncer::class,
];

Then in your routes file, you can group your routes around these:

Route::middleware('scope-bouncer:1')->group(function () {
    // frontend groups
});

Route::middleware('scope-bouncer:2')->group(function () {
    // dashboard groups
});
centiveo commented 7 years ago

In my case the 'scope' field is always 'Null'. What's going wrong?

JosephSilber commented 7 years ago

@ufeg02 can you show us the code you’re using?

JosephSilber commented 7 years ago

I’m closing this issue for now, in favor of the multi-tenancy one. Any discussions about its usage should be posted there.

JosephSilber commented 6 years ago

For all of you following along, I just added a section to the docs explaining how to set this up:

Can I use a different set of roles & abilities for the public section of my site and the dashboard section, respectively?

fibis commented 3 years ago

For us, it would be petty useful, if Bouncer would support multiple guards or at least the possibility to define the guard that should be used instead of the default guard.

Our default guard is "web" but we only use Bouncer for requests to our API, so bouncer should use the "api" guard. Since we can't change our default guard to that, we are using the following hack in the Bouncer middleware: Config::set('auth.defaults.guard', 'api');

Since this is not the nicest solution, we would appreciate it if the possibility would come to Bouncer.

GotaloveCode commented 3 years ago

How can I set Bouncer to use the guard web for all web routes if my default guard is api?

JosephSilber commented 3 years ago

@GotaloveCode Bouncer doesn't "use" any guard, so not sure what you're asking.