Multi-tenant: search across all products
I have a multitenant application. My tenant is called 'organization'. I want a user to be able to ask information about a product by sending a message. I have therefore a MessageResource with below snippet. The idea is that a user can search across all products in the database, not only the products that he's owning (=assigned to his organization).
Below code works but only shows the products linked to the organization.
I also have the following:
This explains of course why I only see the products for the organization.
I read that Filament does scoping to tenant automatically, so the above global scope seems redundant. Still I wanted to have it to have a secure feeling.
Two quesions:
1) can I safely remove the global scope?
2) Is there a way I could still enforce a scope, except for this one resource (MessageResource)?
Any suggestions?
Below code works but only shows the products linked to the organization.
Select::make('product_id')
->label('Product')
->columnSpan(1)
->searchable()
->getSearchResultsUsing(function ($search) {
if (strlen($search) < 3) {
return [];
}
return Product::where('name', 'like', "%{$search}%")
->limit(50)
->get()
->pluck('name', 'id');
})
->getOptionLabelUsing(fn ($value): ?string => Product::find($value)?->name)
->disabled(false),
I also have the following:
protected static function booted(): void
{
static::addGlobalScope('organization', function (Builder $query) {
if (auth()->hasUser()) {
$query->where('organization_id', auth()->user()->organization_id);
}
});
}
This explains of course why I only see the products for the organization.
I read that Filament does scoping to tenant automatically, so the above global scope seems redundant. Still I wanted to have it to have a secure feeling.
Two quesions:
1) can I safely remove the global scope?
2) Is there a way I could still enforce a scope, except for this one resource (MessageResource)?
Any suggestions?
Solution
You would combine those. Sorry I was focusing on the resource itself but you actually need it on the select 