Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PermissionAlreadyExists with multitenancy #1071

Closed
adminfriso opened this issue Mar 28, 2019 · 3 comments
Closed

PermissionAlreadyExists with multitenancy #1071

adminfriso opened this issue Mar 28, 2019 · 3 comments

Comments

@adminfriso
Copy link

adminfriso commented Mar 28, 2019

When connecting to a new and clean tenant, and try to seed it, it will give a PermissionAlreadyExists error. however the specific tenant it uses is empty. there is literally no data in it. In the seeders i try to forget all caches etc. Could there be something else I am doing wrong?

Console

Seeding: RolesAndPermissionsSeeder

In PermissionAlreadyExists.php line 11:
                                                            
  A `Guard App` permission already exists for guard `web`.  

Migration

       // Reset cached roles and permissions
        app()['cache']->forget('spatie.permission.cache');
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        // Permissions
        Permission::create(['name' => 'Guard App']);

Context command

 config(['database.connections.' . $tenant['database'] . '.driver' => 'mysql']);
            config(['database.connections.' . $tenant['database'] . '.host' => config('database.connections.mysql_default.host')]);
            config(['database.connections.' . $tenant['database'] . '.port' => config('database.connections.mysql_default.port')]);
            config(['database.connections.' . $tenant['database'] . '.database' => $tenant['database']]);
            config(['database.connections.' . $tenant['database'] . '.username' => $tenant['username']]);
            config(['database.connections.' . $tenant['database'] . '.password' => secret;) ]);

if ($seed) {
                $this->call('db:seed', ['--class' => 'TenantSeeder', '--database' => $tenant['database']]);
            }

PS: I LOVE YOUR PACKAGES!!!!

@drbyte
Copy link
Collaborator

drbyte commented Mar 28, 2019

Hmmm ... first thing coming to mind is whether the db:seed call is properly connecting to the right database.

I know this package is agnostic about which db connection it's using ... as in, it just uses the primary/default connection. So maybe that's not actually "agnostic", but rather highly opinionated as in "unconfigurable". :)

You might need to override the models and set your own $connection property in them, as hinted at in the comments on this old non-merged PR: #719

Aside: Unfortunately I'm going to be AFK for the rest of the week and unable to look at related code until then. But maybe the above gives you something to explore in the meantime. I'd like to see what you discover.

@adminfriso
Copy link
Author

turned out it was a caching issue. The package works with only 1 cache, which initialises before the middleware initialises where the multitenancy switching is found.

current FIX:
give the cache a prefix bases on db name+default from package after switching to the tenants.

@drbyte
Copy link
Collaborator

drbyte commented Apr 16, 2019

Thanks for posting back with the details of the cause, and a solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants