Greetings
In regards to Roles and Permission - just wondering what people are doing with multiple permission tables when they setup the 'AuthServiceProvider.php' file...
For example, I am using this below code for a single permission table, which works great!... However, as the application grows into difference directions - I believe a 2nd Roles / Permission table would be nice to implement...
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Gate;
use App\Models\RBAC\Permission;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider {
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot() {
$this->registerPolicies();
//Fetch all Permissions from the database...
foreach ($this->getPermissions() as $permission) {
//With each permission - register it with the 'Gate' class...
Gate::define($permission->section . '_' . $permission->access_type, function ($user) use ($permission) {
//Return true if the User has the given role...
//(i.e. Does the User have any of the roles, which are associated
//to this given permissions)
return $user->hasRole($permission->roles);
});
}
}
I thought a quick and easy way to control which permissions would be relevant to the user would be to use something like getting the authenticated user, whereby the 'guard' parameter could control which permissions are relevant... However, this didn't go to plan, as the 'Auth::guard' return null. Below is an example of the code with 2x permissions table that I tried to use...
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Gate;
use App\Models\RBAC\Permission_demo;
use App\Models\RBAC\Permission_admin;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider {
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot() {
$this->registerPolicies();
//Set Permissions if login as DEMO...
if (Auth::guard('demo')->check()) {
//Fetch all Permissions from the database...
foreach ($this->getPermissions_demo() as $permission_demo) {
//With each permission - register it with the 'Gate' class...
Gate::define($permission_demo->section . '_' . $permission_demo->access_type, function ($user) use ($permission_demo) {
//Return true if the User has the given role...
//(i.e. Does the User have any of the roles, which are associated
//to this given permissions)
return $user->hasRole($permission_demo->roles_demo);
});
}
}
//Set Permissions if login as ADMIN...
if (Auth::guard('admin')->check()) {
//Fetch all Permissions from the database...
foreach ($this->getPermissions_admin() as $permission_admin) {
//With each permission - register it with the 'Gate' class...
Gate::define($permission_admin->section . '_' . $permission_admin->access_type, function ($user) use ($permission_admin) {
//Return true if the User has the given role...
//(i.e. Does the User have any of the roles, which are associated
//to this given permissions)
return $user->hasRole($permission_admin->roles_admin);
});
}
}
}
I suppose that I could revise the current permissions table (and I guess this may happen eventually)... But I thought I would reach out to the Laracast community first and see if anyone else had stumble across this issue with 'AuthServiceProvider.php'.
Thanks in advance for your time and assistance.