feat: ✨ add cache to get all tenants

This commit is contained in:
Alexis Gousseau 2025-05-28 15:28:08 +02:00
parent d51c40b17e
commit a978e6536c
5 changed files with 50 additions and 10 deletions

View file

@ -33,6 +33,9 @@ class Handler extends ExceptionHandler
public function register(): void
{
$this->reportable(function (Throwable $e) {
if (app()->bound('sentry')) {
app('sentry')->captureException($e);
}
});
}
}

View file

@ -22,19 +22,14 @@ class RestrictTenantAccess
$user = \auth('backend')->user();
if ($user) {
if (
!$request->routeIs('bo.dashboard') &&
!$request->routeIs('bo.users.*') &&
!$request->routeIs('bo.tenants.*') &&
!$request->routeIs(['bo.dashboard', 'bo.users.*', 'bo.tenants.*']) &&
$request->tenant &&
!\in_array($request->tenant, Tenant::get()->pluck('id')->toArray())
) {
// @phpstan-ignore argument.type
abort('404');
}
if (
(Tenant::count() && $request->routeIs('bo.tenants.*') && $user->can('isAdmin')) ||
(Tenant::count() && $request->routeIs('bo.users.*') && $user->can('isAdmin'))
) {
if (Tenant::count() && $request->routeIs(['bo.tenants.*', 'bo.users.*']) && $user->can('isAdmin')) {
$tenant = Tenant::first()->getKey();
tenancy()->initialize($tenant);
return $next($request);

View file

@ -2,9 +2,10 @@
namespace App\Models;
use Stancl\Tenancy\Database\Models\Tenant as BaseTenant;
use Stancl\Tenancy\Contracts\Tenant as TenantContract;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Stancl\Tenancy\Contracts\Tenant as TenantContract;
use Stancl\Tenancy\Database\Models\Tenant as BaseTenant;
use Uwil\LaravelCommon\Helpers\ToolboxHelper;
class Tenant extends BaseTenant implements TenantContract
@ -46,11 +47,18 @@ class Tenant extends BaseTenant implements TenantContract
static::created(function (self $model) {
(new Setting())->setDefaultSettings($model);
(new Formulary())->setDefaultFormularies($model);
self::resetCache();
});
static::updating(function (self $model) {
self::assertNameIsUnique($model->name, $model->getKey());
$model->id = Str::slug($model->name);
});
static::updated(function () {
self::resetCache();
});
static::deleted(function () {
self::resetCache();
});
}
// * METHODS
@ -72,6 +80,29 @@ class Tenant extends BaseTenant implements TenantContract
// * RELATIONSHIPS
/**
* Reset all cache elements related to this model.
*
* @return void
*/
private static function resetCache(): void
{
Cache::forget('tenantsGlobal');
self::setTenantsCached();
}
/**
* Set all tenants in cache.
*
* @return \Illuminate\Support\Collection
*/
public static function setTenantsCached(): \Illuminate\Support\Collection
{
return Cache::rememberForever('tenantsGlobal', function () {
return Tenant::get();
});
}
/**
* Has Many Relationship Method.
*

View file

@ -27,7 +27,7 @@ class ViewServiceProvider extends ServiceProvider
}
}
if (Schema::hasTable('tenants')) {
View::share('tenantsGlobal', Tenant::get());
View::share('tenantsGlobal', Tenant::setTenantsCached());
}
}
}

11
production-resources.yaml Normal file
View file

@ -0,0 +1,11 @@
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: $APP_DOMAIN
spec:
dnsNames:
- $APP_DOMAIN
secretName: $APP_DOMAIN
issuerRef:
name: $APP_CERT_ISSUER
kind: ClusterIssuer