Closed flick36 closed 7 years ago
Can you explain your use case? What are you trying to achieve?
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
i dont think this can be use for multiple auth guard... for multi auth
@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
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.
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?
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
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.
@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
});
In my case the 'scope' field is always 'Null'. What's going wrong?
@ufeg02 can you show us the code you’re using?
I’m closing this issue for now, in favor of the multi-tenancy one. Any discussions about its usage should be posted there.
For all of you following along, I just added a section to the docs explaining how to set this up:
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.
How can I set Bouncer to use the guard web for all web routes if my default guard is api?
@GotaloveCode Bouncer doesn't "use" any guard, so not sure what you're asking.
Does Bouncer supports Multiple Guards? like in laravel-permissions https://github.com/spatie/laravel-permission#using-multiple-guards