diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 8d36639..2a844f7 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -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); + } }); } } diff --git a/app/Http/Middleware/RestrictTenantAccess.php b/app/Http/Middleware/RestrictTenantAccess.php index 1e2a6e2..074c421 100644 --- a/app/Http/Middleware/RestrictTenantAccess.php +++ b/app/Http/Middleware/RestrictTenantAccess.php @@ -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); diff --git a/app/Models/Tenant.php b/app/Models/Tenant.php index 180b271..1f221d8 100644 --- a/app/Models/Tenant.php +++ b/app/Models/Tenant.php @@ -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. * diff --git a/app/Providers/ViewServiceProvider.php b/app/Providers/ViewServiceProvider.php index 82bc656..89baadc 100644 --- a/app/Providers/ViewServiceProvider.php +++ b/app/Providers/ViewServiceProvider.php @@ -27,7 +27,7 @@ class ViewServiceProvider extends ServiceProvider } } if (Schema::hasTable('tenants')) { - View::share('tenantsGlobal', Tenant::get()); + View::share('tenantsGlobal', Tenant::setTenantsCached()); } } } diff --git a/production-resources.yaml b/production-resources.yaml new file mode 100644 index 0000000..7bdc887 --- /dev/null +++ b/production-resources.yaml @@ -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