Replies: 4 comments 10 replies
-
I agree: we could probably safely switch that to use what the Registrar has resolved. |
Beta Was this translation helpful? Give feedback.
-
You wrote:
What were the exceptions? Was it just "permission not found for guard"? (which would signify wrong model or wrong connection) |
Beta Was this translation helpful? Give feedback.
-
Interestingly, in your new tests, if you replace the lookup on permission by "name", with the model instance of the created permission, the test passes. For example: public function it_can_manage_roles_and_permissions_on_multiple_schemas_2()
{
/*
ray()->clearAll()->showApp();
DB::listen(function($query) {
ray($query)->orange();
});
*/
$roleApp1Name = 'testRoleApp1InWebGuard';
$roleApp2Name = 'testRoleApp2InWebGuard';
$permissionApp1Name = 'testPermissionApp1InWebGuard';
$permissionApp2Name = 'testPermissionApp2InWebGuard';
$this->assertFalse($this->testUser->hasRole($roleApp1Name));
$this->assertFalse($this->testCustomer->hasRole($roleApp2Name));
$roleApp1 = App1\Role::findOrCreate($roleApp1Name, 'web');
$roleApp2 = App2\Role::findOrCreate($roleApp2Name, 'web');
App1\Permission::findOrCreate($permissionApp1Name, 'web');
- App2\Permission::findOrCreate($permissionApp2Name, 'web');
+ $p2 = App2\Permission::findOrCreate($permissionApp2Name, 'web');
$roleApp1->givePermissionTo([$permissionApp1Name]);
$roleApp2->givePermissionTo([$permissionApp2Name]);
/*
ray([
'roles in app1' => App1\Role::all()->pluck('name')->toArray(),
'permissions in app1' => App1\Permission::all()->pluck('name')->toArray(),
'roles in app2' => App2\Role::all()->pluck('name')->toArray(),
'permissions in app2' => App2\Permission::all()->pluck('name')->toArray(),
'permissions in roleApp1' => $roleApp1->permissions->pluck('name')->toArray(),
'permissions in roleApp2' => $roleApp2->permissions->pluck('name')->toArray(),
]);
*/
$this->assertTrue($roleApp1->hasPermissionTo($permissionApp1Name));
- $this->assertTrue($roleApp2->hasPermissionTo($permissionApp2Name)); // NOTE: -> Tests failed, problem related to PermissionRegistrar method loadPermissions, the feeling is that the entire cache must reason per connection (something like namespace)
+ $this->assertTrue($roleApp2->hasPermissionTo($p2));
$this->assertFalse($this->testUser->hasRole($roleApp1Name));
$this->assertFalse($this->testCustomer->hasRole($roleApp2Name));
$this->testUser->assignRole($roleApp1Name);
$this->assertTrue($this->testUser->hasRole($roleApp1Name));
$this->testCustomer->assignRole($roleApp2Name);
$this->assertTrue($this->testCustomer->hasRole($roleApp2Name));
$this->testUser->unsetRelation('roles');
$this->testCustomer->unsetRelation('roles');
$this->assertTrue($this->testUser->hasRole($roleApp1Name));
$this->assertTrue($this->testCustomer->hasRole($roleApp2Name));
$this->assertTrue($this->testUser->hasPermissionTo($permissionApp1Name));
$this->assertTrue($this->testUser->can($permissionApp1Name));
$this->assertFalse($this->testUser->checkPermissionTo($permissionApp2Name));
- $this->assertTrue($this->testCustomer->hasPermissionTo($permissionApp2Name));
+ $this->assertTrue($this->testCustomer->hasPermissionTo($p2));
$this->assertFalse($this->testCustomer->checkPermissionTo($permissionApp1Name));
} |
Beta Was this translation helpful? Give feedback.
-
Ehi @drbyte I push #2623 as a possible starting point and solution. |
Beta Was this translation helpful? Give feedback.
-
Hello everyone, first of all I would like to thank you and congratulate you for the package,
I wanted to try to describe for a moment the difficulties I am encountering (hoping to findsome useful ideas) using the library in multiple "contexts" for the same application.
I try to represent in a concise way that I think I express myself better:
Case 1
Connection “1” on db1 -> User has roles and permissions teams disable (users / roles / permissions all in db1)
Connection “2” on db2 -> Customer has roles and permissions teams enable (customers / roles / permissions all in db2)
UserRole extend SpatieRole on connection “1”
UserPermission extend SpatiePermission on Connection “1”
CustomerRole extend SpatieRole on connection “2”
CustomerPermission extend SpatiePermission on connection “2”
Case 2 (multitenancy)
Landlord -> Connection “landlord” on db1 -> User has roles and permissions teams disable (users / roles / permissions all in db1)
Tenant n -> Connection “tenant” on db-n -> Profile has roles and permissions teams enable (profiles / roles / permissions all in db-n)
Landlord/Role extend SpatieRole on connection “landlord”
Landlord/Permission extend SpatiePermission with Connection “landlord”
Tenant/Role extend SpatieRole on connection “tenant”
Tenant/Permission extend SpatiePermission on connection “tenant”
I have found that in both cases it is not enough to overwrite the traits or class methods of roles and permissions, but rather it is necessary to "work" on the configuration (and PermissionRegistrar::class) at runtime, modifying the behaviors from time to time and restoring them.
Basically, for example, you cannot call in succession (because it generates exceptions):Case 1 : $user->roles; […] $customer->roles;Case 2 : $user->roles; […] $profile->roles;Basically it fails to verify the permissions correctly in succession (see test).
Eg with spatie/laravel-multitenancy the solution that I found "functional" is the following:
Note for example:
laravel-permission/src/Traits/HasRoles.php
Line 51 in b6b9f4f
it relies on reading the configuration rather than on the getRoleClass trait method (same for permissions)
laravel-permission/src/Traits/HasRoles.php
Line 36 in b6b9f4f
There are also numerous other points of similar behavior.
I understand the importance and opportunity of isolating contexts but I have the feeling that this "forcing" makes the management of some use cases complex / expensive / limiting.
I forked a small example in order to test, debug and understand some problems I'm encountering,
I hope I have explained myself, I probably don't have enough experience and if so I apologize if the assessments and considerations are incorrect, I ask you for advice if there are alternative solutions.
Thank you
Beta Was this translation helpful? Give feedback.
All reactions