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

Tenant Route Model binding not working? #18

Closed
devonmather opened this issue May 18, 2019 · 4 comments
Closed

Tenant Route Model binding not working? #18

devonmather opened this issue May 18, 2019 · 4 comments

Comments

@devonmather
Copy link

devonmather commented May 18, 2019

Hey All,

I'm trying to dig into this but running into a few issues. I might be missing something as this seems like something that would work out of the box based on the documentation.

Currently, I have a product owned by Tenant 1
http://tenant1.exampledomain.test/products/2

If I then log in as Tenant 2 and navigate to this URL
http://tenant2.exampledomain.test/products/2

Then I can see the contents of that product, although I assume I shouldn't?

Quick run down as per the docs;

  • Register RomegaDigital\Multitenancy\MultitenancyServiceProvider::class in config/app.php
  • Run php artisan multitenancy:install
  • Add HasTenants, HasRoles to User model
  • Add BelongsToTenant to Product model
  • Add 'tenant.auth' => \RomegaDigital\Multitenancy\Middleware\TenantMiddleware::class to routeMiddleware in app/Http/Kernel.php
  • I have then wrapped by routes in the route group as below
Route::group(['middleware' => ['tenant.auth']], function () {
	Route::resource('/products', 'ProductController');
});

Am I missing something, or does tenant route model binding not work out of the box? My expectation here would be a 403 response to the user.

Thanks for any feedback, and let me know if I can provide more helpful info to debug.

@devonmather devonmather changed the title Route Model binding, not working? Tenant Route Model binding, not working? May 18, 2019
@devonmather devonmather changed the title Tenant Route Model binding, not working? Tenant Route Model binding not working? May 18, 2019
@bradenkeith
Copy link
Member

bradenkeith commented May 18, 2019 via email

@devonmather
Copy link
Author

Thanks for replying to this on the weekend. I can confirm the trait is on the model.

I have echoed out the apply method to see if it was exiting early or anything.

public function applyTenantScope(Model $model)
{
    echo 'reached 1, ';
    if (is_null($this->tenant)) {
        $this->deferredModels->push($model);

        return;
    }
    
    echo 'reached 2, ';
    if ($this->tenant->domain === 'admin') {
        return;
    }

    echo 'reached 3, ';
    $model->addGlobalScope('tenant', function (Builder $builder) use ($model) {
        echo 'reached 4, ';
        $builder->where($model->qualifyColumn('tenant_id'), '=', $this->tenant->id);
    });
    echo 'reached 5, ';
}

That results in the following output "reached 1, reached 1, reached 2, reached 3, reached 5,"

For some reason the addGlobalScope function is not being invoked?

@devonmather
Copy link
Author

Is there any reason this function would be skipped?

$model->addGlobalScope('tenant', function (Builder $builder) use ($model)

@bradenkeith
Copy link
Member

When setup correctly, I can't imagine an instance where that'd be the case. If you are able to provide a repo of your code, I can review it and see where things are going wrong!

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