laravel-permission: Multi tenant multi database support, tried all but not working.
I tried to extend the models, as well as updating permission config files while switching to tenant database but still when I try to update the role and permissions it does not work and always fetching roles and permissions using default database connection.
<?php
namespace App\Client;
use Spatie\Permission\Models\Role as ModelsRole;
class Role extends ModelsRole
{
protected $connection = 'client';
protected $table = 'roles';
protected $fillable = ['name'];
public function __construct()
{
parent::__construct();
if ($connection = config('permission.db_connection')) {
$this->setConnection($connection);
}
}
}
Permission model
<?php
namespace App\Client;
use Spatie\Permission\Models\Permission as ModelsPermission;
class Permission extends ModelsPermission
{
protected $connection = 'client';
protected $table = 'permissions';
protected $fillable = ['name'];
public function __construct()
{
parent::__construct();
if ($connection = config('permission.db_connection')) {
$this->setConnection($connection);
}
}
}
Configuration update while switching to tenant connection
'permission.db_connection' => 'client',
'permission.models.permission' => Permission::class,
'permission.models.role' => Role::class,
Role and permission seeder
<?php
use App\Client\Permission;
use App\Client\Role;
use Illuminate\Database\Seeder;
class ClientRoleAndPermissionSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//add client permissions.
$client_permissions = [
'manage properties',
'manage agents',
];
foreach($client_permissions as $p)
{
Permission::firstOrCreate([
'name' => $p,
'guard_name' => 'client'
]);
}
//add client roles and assign general permissions to get start.
Role::firstOrCreate([
'name' => 'account manager',
'guard_name' => 'client'
])->givePermissionTo([ 'manage agents']);
}
}
Now when I try to update permission, I receive an error that permission does not exist, It means permissions are still being fetched from the main database.
public function updatePermissions(Request $request)
{
$this->validate($request, [
'role' => 'required|numeric',
]);
$role = Role::findById($request->role);
return $role->givePermissionTo('manage properties');
flash()->success('Permissions updated for the <b> ' . $role->name . '</b>');
return back();
}
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 15
Yes. Multi-tenancy introduces a number of complications. Spatie and others have written posts and released packages that help simplify the complexities of that. You should study those concepts if you intend to do multi-tenancy in your apps.
One of those complications is switching out the config to alternate configs when you switch tenants. This is most often best done by observing an event that you fire when switching to a specific tenant. That’s the basic fundamental to accommodate; again, read up on the concepts before implementing them, and if using a package be sure to study how that package implements it before using it.
No, you must switch the config during runtime. This package (like most) only knows ONE configuration at any time. But when you decide that you’re in a specific tenant that’s when you must switch the config to use the config that suits the tenant. In this case he’s not switching table names, but rather model names.