init
This commit is contained in:
64 files changed
+17178
No files matched your search
@@ -0,0 +1,12 @@
|
|||||||
|
/CONTRIBUTING.md export-ignore
|
||||||
|
/SECURITY.md export-ignore
|
||||||
|
/tests export-ignore
|
||||||
|
/tests-js export-ignore
|
||||||
|
/.github export-ignore
|
||||||
|
/phpunit.xml export-ignore
|
||||||
|
/vitest.config.js export-ignore
|
||||||
|
/webpack.mix.js export-ignore
|
||||||
|
/package.json export-ignore
|
||||||
|
/package-lock.json export-ignore
|
||||||
|
/.gitattributes export-ignore
|
||||||
|
/.gitignore export-ignore
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
.idea/
|
||||||
|
/.env
|
||||||
|
/.env.*
|
||||||
|
/auth.json
|
||||||
|
/.composer/
|
||||||
|
/.phpunit.cache/
|
||||||
|
/.phpunit.result.cache
|
||||||
|
/composer.lock
|
||||||
|
/node_modules/
|
||||||
|
/vendor/
|
||||||
|
/workbench/storage/
|
||||||
|
npm-debug.log
|
||||||
|
.DS_Store
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
## [1.0.0] - 2026-09-09
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Native Nova tenant selector with server-side search and pagination.
|
||||||
|
- Request-scoped current tenant context and container aliases.
|
||||||
|
- Cookie-backed SPA tenant switching and dynamic Nova branding.
|
||||||
|
- Tenant presentation, provider, user access, and context contracts.
|
||||||
|
- Optional tenant-aware Nova resource base and reusable context helpers.
|
||||||
|
- English and Persian translations.
|
||||||
|
|
||||||
|
[1.0.0]: https://github.com/MeghdadFadaee/nova-tenancy/releases/tag/v1.0.0
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
# Contributing
|
||||||
|
|
||||||
|
Thank you for considering a contribution to Nova Tenancy.
|
||||||
|
|
||||||
|
## Development setup
|
||||||
|
|
||||||
|
This package requires access to Laravel Nova. Use an existing local Nova installation or Composer authentication configured outside the repository. Never commit Nova credentials, `auth.json`, `.env` files, or vendor sources.
|
||||||
|
|
||||||
|
Install dependencies and run the verification commands:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
composer install
|
||||||
|
npm install
|
||||||
|
composer test
|
||||||
|
npm test
|
||||||
|
npm run production
|
||||||
|
vendor/bin/pint --test
|
||||||
|
```
|
||||||
|
|
||||||
|
Production assets under `dist/` must be rebuilt and included when frontend source changes.
|
||||||
|
|
||||||
|
## Pull requests
|
||||||
|
|
||||||
|
- Keep changes focused and backward compatible where practical.
|
||||||
|
- Add or update tests for changed behavior.
|
||||||
|
- Update `CHANGELOG.md` under `Unreleased` for user-facing changes.
|
||||||
|
- Update the README when a public contract, configuration option, or setup step changes.
|
||||||
|
- Do not include application-specific tenant assumptions or dependencies on a full tenancy framework.
|
||||||
|
|
||||||
|
For security vulnerabilities, follow [SECURITY.md](SECURITY.md) instead of opening a public issue.
|
||||||
+21
@@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2026 Meghdad Fadaee
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
@@ -0,0 +1,231 @@
|
|||||||
|
# Nova Tenancy
|
||||||
|
|
||||||
|
[](https://packagist.org/packages/meghdadfadaee/nova-tenancy)
|
||||||
|
[](https://packagist.org/packages/meghdadfadaee/nova-tenancy)
|
||||||
|
[](LICENSE.md)
|
||||||
|
|
||||||
|
A native Laravel Nova 5 tenant selector, request-scoped tenant context, and dynamic tenant branding. It does not provide database tenancy and does not depend on an HTML card, logo, or third-party tenancy package.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- Tenant selection without leaving Nova's SPA lifecycle.
|
||||||
|
- Dynamic header title and logo for the selected tenant.
|
||||||
|
- Encrypted cookie persistence with authorization checked on every resolution.
|
||||||
|
- Server-side tenant search and pagination.
|
||||||
|
- Request-scoped context plus `CurrentTenantId` and `CurrentTenantModel` bindings.
|
||||||
|
- Opt-in tenant-aware Nova resource scoping and assignment.
|
||||||
|
- Dark mode, responsive layout, keyboard focus, reduced motion, and RTL support.
|
||||||
|
- No database migrations and no dependency on a full tenancy framework.
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
- PHP 8.1+
|
||||||
|
- Laravel 10–13
|
||||||
|
- Laravel Nova 5.8+
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
This package requires an existing licensed Nova installation. Configure Composer authentication for Nova through Nova's documented mechanism; never commit `auth.json` or credentials. Then install the package:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
composer require meghdadfadaee/nova-tenancy
|
||||||
|
php artisan vendor:publish --tag=nova-tenancy-config
|
||||||
|
```
|
||||||
|
|
||||||
|
The package auto-registers its Nova tool. Its normal sidebar item is disabled by default because the selector is intended to live in the user menu.
|
||||||
|
|
||||||
|
No migrations are required.
|
||||||
|
|
||||||
|
## Configure a tenant model
|
||||||
|
|
||||||
|
Implement `NovaTenant` on the model that represents a tenant. The included trait maps common Eloquent attributes and can be configured in `config/nova-tenancy.php`.
|
||||||
|
|
||||||
|
```php
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Concerns\HasNovaTenantPresentation;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\NovaTenant;
|
||||||
|
|
||||||
|
class Team extends Model implements NovaTenant
|
||||||
|
{
|
||||||
|
use HasNovaTenantPresentation;
|
||||||
|
|
||||||
|
public function novaTenantLogoUrl(): ?string
|
||||||
|
{
|
||||||
|
return $this->pic_url;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The contract exposes:
|
||||||
|
|
||||||
|
```php
|
||||||
|
public function novaTenantKey(): int|string;
|
||||||
|
public function novaTenantTitle(): string;
|
||||||
|
public function novaTenantLogoUrl(): ?string;
|
||||||
|
|
||||||
|
/** @return array{description?: string|null, badge?: string|null, accent?: string|null} */
|
||||||
|
public function novaTenantMeta(): array;
|
||||||
|
```
|
||||||
|
|
||||||
|
Accent values are accepted only as six-digit hex colors before they reach the frontend.
|
||||||
|
|
||||||
|
## Define tenant access
|
||||||
|
|
||||||
|
The default provider asks the authenticated user for an Eloquent query. That query is the authorization boundary for the list, search, and selection endpoints.
|
||||||
|
|
||||||
|
```php
|
||||||
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\ProvidesNovaTenants;
|
||||||
|
|
||||||
|
class User extends Authenticatable implements ProvidesNovaTenants
|
||||||
|
{
|
||||||
|
public function novaTenantQuery(Request $request): Builder
|
||||||
|
{
|
||||||
|
return $this->teams()
|
||||||
|
->where('visible', true)
|
||||||
|
->orderBy('priority');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
For more control, extend `EloquentTenantProvider` (or implement `TenantProvider`) and set its class as `provider` in the package configuration. Custom providers control access, searching, ordering, and key resolution while still returning Eloquent models implementing `NovaTenant`. The base provider supplies safe default `search()` and `find()` implementations derived from the same authorized query.
|
||||||
|
|
||||||
|
## Add the user-menu link
|
||||||
|
|
||||||
|
Nova supports a single user-menu callback, so the package provides an item that safely composes with the application's existing items:
|
||||||
|
|
||||||
|
```php
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Laravel\Nova\Menu\Menu;
|
||||||
|
use Laravel\Nova\Nova;
|
||||||
|
use MeghdadFadaee\NovaTenancy\NovaTenancy;
|
||||||
|
|
||||||
|
Nova::userMenu(function (Request $request, Menu $menu): Menu {
|
||||||
|
return $menu->append(NovaTenancy::userMenuItem());
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
The sidebar link can instead be enabled with `menu.sidebar`.
|
||||||
|
|
||||||
|
## Resolve the current tenant
|
||||||
|
|
||||||
|
The context and convenience aliases are scoped once per Laravel request or worker lifecycle:
|
||||||
|
|
||||||
|
```php
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\CurrentTenantContext;
|
||||||
|
use MeghdadFadaee\NovaTenancy\NovaTenancy;
|
||||||
|
|
||||||
|
$context = app(CurrentTenantContext::class);
|
||||||
|
|
||||||
|
$context->id(); // int|string|null
|
||||||
|
$context->model(); // NovaTenant model|null
|
||||||
|
$context->requireId(); // throws TenantNotSelected
|
||||||
|
$context->requireModel(); // throws TenantNotSelected
|
||||||
|
|
||||||
|
app('CurrentTenantId');
|
||||||
|
app('CurrentTenantModel');
|
||||||
|
|
||||||
|
NovaTenancy::id();
|
||||||
|
NovaTenancy::tenant();
|
||||||
|
```
|
||||||
|
|
||||||
|
For explicitly propagated jobs or commands, `run()` activates a tenant only for the callback and restores the previous context:
|
||||||
|
|
||||||
|
```php
|
||||||
|
$context->run($team, fn () => $service->generateReport());
|
||||||
|
```
|
||||||
|
|
||||||
|
The package does not automatically propagate tenants into queues, change database connections, scope application models, or partition caches and filesystems.
|
||||||
|
|
||||||
|
## Tenant-aware Nova resources
|
||||||
|
|
||||||
|
Extend `TenantResource` when a Nova resource must always use the selected tenant:
|
||||||
|
|
||||||
|
```php
|
||||||
|
use MeghdadFadaee\NovaTenancy\Nova\TenantResource;
|
||||||
|
|
||||||
|
class Invoice extends TenantResource
|
||||||
|
{
|
||||||
|
public static $model = \App\Models\Invoice::class;
|
||||||
|
|
||||||
|
protected static function tenantColumn(): string
|
||||||
|
{
|
||||||
|
return 'team_id';
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function afterTenantScope(
|
||||||
|
NovaRequest $request,
|
||||||
|
Builder $query,
|
||||||
|
string $operation,
|
||||||
|
): Builder {
|
||||||
|
return $query->latest();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The base class scopes index, detail, edit, replicate, relatable, and Scout queries. It also forces the tenant column during creation and update. Its security-sensitive entry points are final; use the provided hooks to customize them.
|
||||||
|
|
||||||
|
For lenses, metrics, actions, or resources with another required base class, use `InteractsWithCurrentTenant` explicitly:
|
||||||
|
|
||||||
|
```php
|
||||||
|
return static::scopeToCurrentTenant($query, 'team_id');
|
||||||
|
```
|
||||||
|
|
||||||
|
`RequireTenant` is available for custom routes that must not run without a selection.
|
||||||
|
|
||||||
|
## SPA switching and branding
|
||||||
|
|
||||||
|
Selection uses Nova's authenticated request client, stores the authorized tenant key in an encrypted HTTP-only cookie, broadcasts `nova-tenancy:changed`, and navigates with `Nova.visit()`. The page is not reloaded.
|
||||||
|
|
||||||
|
The package overrides Nova's `AppLogo` component with the selected tenant's logo and title. The image source remains the fixed authenticated endpoint `/nova-vendor/nova-tenancy/current/logo`; remote images are redirected to rather than fetched by the server. Only root-relative, HTTP, and HTTPS logo URLs are accepted by the default response and tenant list payload.
|
||||||
|
|
||||||
|
If another package also replaces `AppLogo`, the last component registered by Nova wins.
|
||||||
|
|
||||||
|
## Customization
|
||||||
|
|
||||||
|
The published config controls cookie attributes, routes, presentation fields, search size/debounce, branding, menu placement, the default resource column, and the post-selection landing path.
|
||||||
|
|
||||||
|
Runtime callbacks are available for application logic that cannot live in cached config:
|
||||||
|
|
||||||
|
```php
|
||||||
|
NovaTenancy::authorizeUsing(
|
||||||
|
fn (Request $request): bool => $request->user()->can('select-tenants')
|
||||||
|
);
|
||||||
|
|
||||||
|
NovaTenancy::landingUsing(
|
||||||
|
fn (Request $request, Model $tenant): string => '/dashboards/main'
|
||||||
|
);
|
||||||
|
|
||||||
|
NovaTenancy::logoResponseUsing(
|
||||||
|
fn (Request $request, Model $tenant): Response => Storage::disk('private')
|
||||||
|
->response($tenant->logo_path)
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
`TenantSelected` and `TenantCleared` events are dispatched after changes. English and Persian translations can be published with:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
php artisan vendor:publish --tag=nova-tenancy-translations
|
||||||
|
```
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
```bash
|
||||||
|
composer test
|
||||||
|
npm test
|
||||||
|
npm run production
|
||||||
|
vendor/bin/pint
|
||||||
|
```
|
||||||
|
|
||||||
|
The tests use Orchestra Testbench. Production Nova assets in `dist/` are part of package releases so consuming applications do not need Node.js.
|
||||||
|
|
||||||
|
Please see [CONTRIBUTING.md](CONTRIBUTING.md) before submitting a change. Security issues should be reported according to [SECURITY.md](SECURITY.md), not through a public issue.
|
||||||
|
|
||||||
|
## Changelog
|
||||||
|
|
||||||
|
Please see [CHANGELOG.md](CHANGELOG.md) for release history.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Nova Tenancy is open-sourced software licensed under the [MIT license](LICENSE.md).
|
||||||
+16
@@ -0,0 +1,16 @@
|
|||||||
|
# Security Policy
|
||||||
|
|
||||||
|
## Supported versions
|
||||||
|
|
||||||
|
| Version | Supported |
|
||||||
|
|------------------|-----------|
|
||||||
|
| 1.x | Yes |
|
||||||
|
| Earlier versions | No |
|
||||||
|
|
||||||
|
## Reporting a vulnerability
|
||||||
|
|
||||||
|
Please report security vulnerabilities privately through the repository's GitHub Security Advisory feature. Do not disclose the issue publicly until a fix is available.
|
||||||
|
|
||||||
|
Include the affected package version, Laravel and Nova versions, reproduction steps, expected behavior, and the security impact. Reports will be reviewed as soon as practical, and coordinated disclosure will be arranged when the issue is confirmed.
|
||||||
|
|
||||||
|
Never include real Nova credentials, application secrets, tenant data, or production cookies in a report.
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
{
|
||||||
|
"name": "meghdadfadaee/nova-tenancy",
|
||||||
|
"description": "A framework-agnostic tenant selector, context manager, and dynamic branding tool for Laravel Nova.",
|
||||||
|
"type": "library",
|
||||||
|
"keywords": [
|
||||||
|
"laravel",
|
||||||
|
"nova",
|
||||||
|
"multi-tenancy",
|
||||||
|
"nova-tool",
|
||||||
|
"saas",
|
||||||
|
"tenancy",
|
||||||
|
"tenant-switcher"
|
||||||
|
],
|
||||||
|
"homepage": "https://github.com/MeghdadFadaee/nova-tenancy",
|
||||||
|
"license": "MIT",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Meghdad Fadaee"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"support": {
|
||||||
|
"issues": "https://github.com/MeghdadFadaee/nova-tenancy/issues",
|
||||||
|
"source": "https://github.com/MeghdadFadaee/nova-tenancy"
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"php": "^8.1",
|
||||||
|
"illuminate/database": "^10.48|^11.44|^12.1|^13.0",
|
||||||
|
"illuminate/support": "^10.48|^11.44|^12.1|^13.0",
|
||||||
|
"laravel/nova": "^5.8"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"laravel/nova-devtool": "^1.9",
|
||||||
|
"laravel/pint": "^1.20",
|
||||||
|
"orchestra/testbench-core": "^8.35|^9.12|^10.1|^11.0",
|
||||||
|
"pestphp/pest": "^2.36|^3.8|^4.0|^5.0",
|
||||||
|
"pestphp/pest-plugin-laravel": "^2.4|^3.0|^4.0|^5.0"
|
||||||
|
},
|
||||||
|
"repositories": [
|
||||||
|
{
|
||||||
|
"type": "composer",
|
||||||
|
"url": "https://nova.laravel.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"MeghdadFadaee\\NovaTenancy\\": "src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"autoload-dev": {
|
||||||
|
"psr-4": {
|
||||||
|
"MeghdadFadaee\\NovaTenancy\\Tests\\": "tests/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"extra": {
|
||||||
|
"branch-alias": {
|
||||||
|
"dev-main": "1.x-dev"
|
||||||
|
},
|
||||||
|
"laravel": {
|
||||||
|
"providers": [
|
||||||
|
"MeghdadFadaee\\NovaTenancy\\NovaTenancyServiceProvider"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "pest",
|
||||||
|
"format": "pint"
|
||||||
|
},
|
||||||
|
"config": {
|
||||||
|
"allow-plugins": {
|
||||||
|
"pestphp/pest-plugin": true
|
||||||
|
},
|
||||||
|
"sort-packages": true
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,146 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use MeghdadFadaee\NovaTenancy\Providers\UserTenantProvider;
|
||||||
|
|
||||||
|
return [
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Tenant Provider
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| The provider is responsible for returning the authenticated user's
|
||||||
|
| authorized tenant query, applying searches, and resolving a selected
|
||||||
|
| tenant. You may replace this with any TenantProvider implementation.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'provider' => UserTenantProvider::class,
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Selection Cookie
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| Nova Tenancy stores only the selected tenant key in this encrypted,
|
||||||
|
| HTTP-only cookie. A null secure value follows the current request's
|
||||||
|
| security, while the default lifetime remembers a selection for a year.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'cookie' => [
|
||||||
|
'name' => 'nova_tenant',
|
||||||
|
'minutes' => 60 * 24 * 365,
|
||||||
|
'path' => '/',
|
||||||
|
'domain' => null,
|
||||||
|
'secure' => null,
|
||||||
|
'http_only' => true,
|
||||||
|
'same_site' => 'lax',
|
||||||
|
],
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Routes
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| The URI is relative to Nova's configured path. The API prefix is an
|
||||||
|
| application-relative path protected by Nova's authentication and the
|
||||||
|
| package authorization middleware. Avoid trailing slashes in both.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'routes' => [
|
||||||
|
'uri' => 'nova-tenancy',
|
||||||
|
'api_prefix' => 'nova-vendor/nova-tenancy',
|
||||||
|
],
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Tenant Selection
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| These values control server pagination, search debounce in milliseconds,
|
||||||
|
| and the Nova-relative SPA destination used after a tenant is selected.
|
||||||
|
| A landingUsing callback may be registered for dynamic destinations.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'selection' => [
|
||||||
|
'per_page' => 12,
|
||||||
|
'max_per_page' => 48,
|
||||||
|
'search_debounce' => 300,
|
||||||
|
'landing_path' => '/',
|
||||||
|
],
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Tenant Model Presentation
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| These columns are used by HasNovaTenantPresentation and the default
|
||||||
|
| provider. Leave optional presentation columns null when they are not
|
||||||
|
| available. Individual contract methods may be overridden on the model.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'model' => [
|
||||||
|
'key_column' => null,
|
||||||
|
'title_attribute' => 'name',
|
||||||
|
'logo_attribute' => 'logo_url',
|
||||||
|
'description_attribute' => null,
|
||||||
|
'badge_attribute' => null,
|
||||||
|
'accent_attribute' => null,
|
||||||
|
'search_columns' => ['name'],
|
||||||
|
],
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Nova Branding
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| When enabled, Nova's header logo is replaced by the current tenant's
|
||||||
|
| image and title. The link may be "selector", "home", or a Nova-relative
|
||||||
|
| path. The fallback logo may be a root-relative, HTTP, or HTTPS URL.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'branding' => [
|
||||||
|
'enabled' => true,
|
||||||
|
'show_logo' => true,
|
||||||
|
'show_title' => true,
|
||||||
|
'link' => 'selector',
|
||||||
|
'fallback_logo' => null,
|
||||||
|
],
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Navigation Menu
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| A null label uses the package translation. The user-menu link is added
|
||||||
|
| with NovaTenancy::userMenuItem(). Enable sidebar to also expose the
|
||||||
|
| selector as a normal Nova tool menu section.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'menu' => [
|
||||||
|
'label' => null,
|
||||||
|
'icon' => 'building-office-2',
|
||||||
|
'sidebar' => false,
|
||||||
|
],
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Tenant-Aware Nova Resources
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| This is the default foreign-key column used by TenantResource and its
|
||||||
|
| query helpers. A resource may override tenantColumn when its schema uses
|
||||||
|
| another name, such as team_id or app_id.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'resources' => [
|
||||||
|
'tenant_column' => 'tenant_id',
|
||||||
|
],
|
||||||
|
];
|
||||||
Vendored
+1
@@ -0,0 +1 @@
|
|||||||
|
.nova-tenancy-page{--nt-primary:#7c3aed;--nt-primary-strong:#5b21b6;--nt-surface:hsla(0,0%,100%,.88);--nt-border:rgba(226,232,240,.9);--nt-muted:#64748b;display:flex;flex-direction:column;gap:1.5rem;padding-bottom:3rem}.nova-tenancy-hero{align-items:flex-end;background:radial-gradient(circle at 12% 10%,rgba(167,139,250,.48),transparent 34%),radial-gradient(circle at 88% 85%,rgba(56,189,248,.3),transparent 38%),linear-gradient(135deg,#251259,#4c1d95 48%,#172554);border:1px solid hsla(0,0%,100%,.16);border-radius:1.5rem;box-shadow:0 24px 60px rgba(49,46,129,.22);color:#fff;display:flex;gap:2rem;justify-content:space-between;min-height:15rem;overflow:hidden;padding:clamp(1.5rem,4vw,3rem);position:relative}.nova-tenancy-hero:after{background-image:linear-gradient(hsla(0,0%,100%,.04) 1px,transparent 0),linear-gradient(90deg,hsla(0,0%,100%,.04) 1px,transparent 0);background-size:32px 32px;content:"";inset:0;-webkit-mask-image:linear-gradient(180deg,#000,transparent);mask-image:linear-gradient(180deg,#000,transparent);pointer-events:none;position:absolute}.nova-tenancy-hero__glow{background:hsla(0,0%,100%,.09);border-radius:9999px;filter:blur(2px);height:22rem;position:absolute;right:-5rem;top:-8rem;width:22rem}.nova-tenancy-current,.nova-tenancy-hero__content{position:relative;z-index:1}.nova-tenancy-hero__content{max-width:42rem}.nova-tenancy-kicker{color:#c4b5fd;font-size:.72rem;font-weight:800;letter-spacing:.16em;margin:0 0 .65rem;text-transform:uppercase}.nova-tenancy-title{font-size:clamp(2rem,5vw,3.5rem);font-weight:750;letter-spacing:-.045em;line-height:1;margin:0}.nova-tenancy-description{color:rgba(237,233,254,.82);font-size:1rem;line-height:1.7;margin:1rem 0 0;max-width:35rem}.nova-tenancy-current{align-items:center;backdrop-filter:blur(14px);background:hsla(0,0%,100%,.1);border:1px solid hsla(0,0%,100%,.16);border-radius:1rem;display:flex;gap:.8rem;min-width:min(100%,17rem);padding:.8rem}.nova-tenancy-card__logo,.nova-tenancy-current__logo{border-radius:.9rem;display:grid;flex:none;font-weight:800;overflow:hidden;place-items:center}.nova-tenancy-current__logo{background:#fff;color:#4c1d95;height:3rem;width:3rem}.nova-tenancy-card__logo img,.nova-tenancy-current__logo img{height:100%;-o-object-fit:cover;object-fit:cover;width:100%}.nova-tenancy-current__copy{display:flex;flex:1;flex-direction:column;min-width:0}.nova-tenancy-current__copy span{color:rgba(237,233,254,.7);font-size:.68rem;font-weight:700;letter-spacing:.08em;text-transform:uppercase}.nova-tenancy-current__copy strong{margin-top:.15rem;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.nova-tenancy-pulse{background:#34d399;border:2px solid hsla(0,0%,100%,.8);border-radius:9999px;box-shadow:0 0 0 4px rgba(52,211,153,.2);height:.55rem;width:.55rem}.nova-tenancy-toolbar{align-items:center;display:flex;gap:1rem}.nova-tenancy-search{align-items:center;background:var(--nt-surface);border:1px solid var(--nt-border);border-radius:.9rem;box-shadow:0 4px 18px rgba(15,23,42,.04);display:flex;flex:1;gap:.75rem;max-width:34rem;min-width:0;padding-inline:1rem;transition:border-color .16s,box-shadow .16s}.nova-tenancy-search:focus-within{border-color:rgba(124,58,237,.55);box-shadow:0 0 0 3px rgba(124,58,237,.12)}.nova-tenancy-search svg{fill:none;stroke:var(--nt-muted);stroke-linecap:round;stroke-width:1.8;width:1.2rem}.nova-tenancy-search input{background:transparent;border:0;color:#0f172a;outline:0;padding:.85rem 0;width:100%}.nova-tenancy-count{background:#ede9fe;border-radius:9999px;color:var(--nt-primary-strong);display:grid;font-size:.78rem;font-weight:800;height:2.5rem;min-width:2.5rem;place-items:center}.nova-tenancy-grid{display:grid;gap:1rem;grid-template-columns:repeat(auto-fill,minmax(min(100%,16rem),1fr))}.nova-tenancy-card{--tenant-accent:#7c3aed;background:var(--nt-surface);border:1px solid var(--nt-border);border-radius:1.25rem;box-shadow:0 10px 32px rgba(15,23,42,.055);display:flex;flex-direction:column;min-height:17rem;overflow:hidden;padding:1.25rem;position:relative;transition:transform .18s ease,border-color .18s ease,box-shadow .18s ease}.nova-tenancy-card:hover{border-color:color-mix(in srgb,var(--tenant-accent) 42%,transparent);box-shadow:0 18px 42px rgba(15,23,42,.1);transform:translateY(-3px)}.nova-tenancy-card--selected{border-color:color-mix(in srgb,var(--tenant-accent) 70%,transparent);box-shadow:0 0 0 2px color-mix(in srgb,var(--tenant-accent) 16%,transparent),0 18px 42px rgba(15,23,42,.09)}.nova-tenancy-card__accent{background:linear-gradient(90deg,var(--tenant-accent),color-mix(in srgb,var(--tenant-accent) 25%,#fff));height:.26rem;inset:0 0 auto;position:absolute}.nova-tenancy-card__header{align-items:flex-start;display:flex;gap:1rem;justify-content:space-between}.nova-tenancy-card__logo{background:color-mix(in srgb,var(--tenant-accent) 11%,#fff);color:var(--tenant-accent);font-size:1.35rem;height:4rem;width:4rem}.nova-tenancy-badge{background:color-mix(in srgb,var(--tenant-accent) 10%,#fff);border-radius:9999px;Line truncated
|
||||||
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
/*! For license information please see tool.js.LICENSE.txt */
|
||||||
|
(()=>{"use strict";var e,n={733(){const e=Vue;var n=function(e,n){return null!==e&&null!==n&&String(e)===String(n)},t=function(e){return String(e||"T").trim().charAt(0).toLocaleUpperCase()},r=function(e){return{"--tenant-accent":e.accent||"#7c3aed"}};function a(e){return function(e){if(Array.isArray(e))return o(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||function(e,n){if(e){if("string"==typeof e)return o(e,n);var t={}.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?o(e,n):void 0}}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function o(e,n){(null==n||n>e.length)&&(n=e.length);for(var t=0,r=Array(n);t<n;t++)r[t]=e[t];return r}function c(){var e,n,t="function"==typeof Symbol?Symbol:{},r=t.iterator||"@@iterator",a=t.toStringTag||"@@toStringTag";function o(t,r,a,o){var c=r&&r.prototype instanceof u?r:u,s=Object.create(c.prototype);return l(s,"_invoke",function(t,r,a){var o,c,l,u=0,s=a||[],v=!1,d={p:0,n:0,v:e,a:p,f:p.bind(e,4),d:function(n,t){return o=n,c=0,l=e,d.n=t,i}};function p(t,r){for(c=t,l=r,n=0;!v&&u&&!a&&n<s.length;n++){var a,o=s[n],p=d.p,f=o[2];t>3?(a=f===r)&&(l=o[(c=o[4])?5:(c=3,3)],o[4]=o[5]=e):o[0]<=p&&((a=t<2&&p<o[1])?(c=0,d.v=r,d.n=o[1]):p<f&&(a=t<3||o[0]>r||r>f)&&(o[4]=t,o[5]=r,d.n=f,c=0))}if(a||t>1)return i;throw v=!0,r}return function(a,s,f){if(u>1)throw TypeError("Generator is already running");for(v&&1===s&&p(s,f),c=s,l=f;(n=c<2?e:l)||!v;){o||(c?c<3?(c>1&&(d.n=-1),p(c,l)):d.n=l:d.v=l);try{if(u=2,o){if(c||(a="next"),n=o[a]){if(!(n=n.call(o,l)))throw TypeError("iterator result is not an object");if(!n.done)return n;l=n.value,c<2&&(c=0)}else 1===c&&(n=o.return)&&n.call(o),c<2&&(l=TypeError("The iterator does not provide a '"+a+"' method"),c=1);o=e}else if((n=(v=d.n<0)?l:t.call(r,d))!==i)break}catch(n){o=e,c=1,l=n}finally{u=1}}return{value:n,done:v}}}(t,a,o),!0),s}var i={};function u(){}function s(){}function v(){}n=Object.getPrototypeOf;var d=[][r]?n(n([][r]())):(l(n={},r,function(){return this}),n),p=v.prototype=u.prototype=Object.create(d);function f(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,v):(e.__proto__=v,l(e,a,"GeneratorFunction")),e.prototype=Object.create(p),e}return s.prototype=v,l(p,"constructor",v),l(v,"constructor",s),s.displayName="GeneratorFunction",l(v,a,"GeneratorFunction"),l(p),l(p,a,"Generator"),l(p,r,function(){return this}),l(p,"toString",function(){return"[object Generator]"}),(c=function(){return{w:o,m:f}})()}function l(e,n,t,r){var a=Object.defineProperty;try{a({},"",{})}catch(e){a=0}l=function(e,n,t,r){function o(n,t){l(e,n,function(e){return this._invoke(n,t,e)})}n?a?a(e,n,{value:t,enumerable:!r,configurable:!r,writable:!r}):e[n]=t:(o("next",0),o("throw",1),o("return",2))},l(e,n,t,r)}function i(e,n,t,r,a,o,c){try{var l=e[o](c),i=l.value}catch(e){return void t(e)}l.done?n(i):Promise.resolve(i).then(r,a)}function u(e){return function(){var n=this,t=arguments;return new Promise(function(r,a){var o=e.apply(n,t);function c(e){i(o,r,a,c,l,"next",e)}function l(e){i(o,r,a,c,l,"throw",e)}c(void 0)})}}var s={class:"nova-tenancy-page"},v={class:"nova-tenancy-hero"},d={class:"nova-tenancy-hero__content"},p={class:"nova-tenancy-kicker"},f={class:"nova-tenancy-title"},y={class:"nova-tenancy-description"},m={key:0,class:"nova-tenancy-current"},g={class:"nova-tenancy-current__logo"},k=["src"],h={key:1},b={class:"nova-tenancy-current__copy"},E=["aria-label"],N={class:"nova-tenancy-search"},B={class:"nova-tenancy-sr-only"},V=["placeholder"],_={key:0,class:"nova-tenancy-count"},S={key:0,class:"nova-tenancy-grid","aria-busy":"true"},w={key:1,class:"nova-tenancy-state",role:"alert"},P={key:2,class:"nova-tenancy-state"},O={key:3,class:"nova-tenancy-grid"},T=["aria-current"],D={class:"nova-tenancy-card__header"},j={class:"nova-tenancy-card__logo"},C=["src","onError"],x={key:1},M={key:0,class:"nova-tenancy-badge"},G=["title"],U={class:"nova-tenancy-card__body"},A={key:0},F={key:1,class:"nova-tenancy-card__key"},L=["disabled","aria-busy","onClick"],q={key:0,class:"nova-tenancy-spinner","aria-hidden":"true"},I={key:1,viewBox:"0 0 20 20","aria-hidden":"true"},$={key:4,class:"nova-tenancy-pagination","aria-label":"Pagination"},z=["disabled"],K=["disabled"];const H={__name:"NovaTenancy",setup:function(o){var l,i,H,R=Nova.config("novaTenancy")||{},W=R.copy||{},Z=(0,e.ref)([]),J=(0,e.ref)(null!==(l=null===(i=R.current)||void 0===i?void 0:i.key)&&void 0!==l?l:null),Q=(0,e.ref)(""),X=(0,e.ref)(!0),Y=(0,e.ref)(!1),ee=(0,e.ref)(null),ne=(0,e.reactive)(new Set),te=(0,e.reactive)({currentPage:1,lastPage:1,perPage:Number(R.perPage||12),total:0}),re=0,ae=(0,e.computed)(function(){var e,t;return Z.value.find(function(e){return ie(e)})||(n(null!==(e=Line truncated
|
||||||
Vendored
+1
@@ -0,0 +1 @@
|
|||||||
|
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */
|
||||||
Vendored
+4
@@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"/js/tool.js": "/js/tool.js?id=c0dece73f13546b93269b715fbe0c31e",
|
||||||
|
"/css/tool.css": "/css/tool.css?id=52168e338d44c342bde21e45f3b08f9e"
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
'menu' => [
|
||||||
|
'label' => 'Select tenant',
|
||||||
|
],
|
||||||
|
'errors' => [
|
||||||
|
'selection_required' => 'Select a tenant before continuing.',
|
||||||
|
],
|
||||||
|
'page' => [
|
||||||
|
'title' => 'Choose your workspace',
|
||||||
|
'kicker' => 'Tenant context',
|
||||||
|
'description' => 'Choose where you want to work. Nova will update the active context without reloading the application.',
|
||||||
|
'current' => 'Current workspace',
|
||||||
|
'search' => 'Search tenants',
|
||||||
|
'search_placeholder' => 'Search by name…',
|
||||||
|
'select' => 'Open workspace',
|
||||||
|
'selected' => 'Selected',
|
||||||
|
'empty_title' => 'No tenants found',
|
||||||
|
'empty_description' => 'Try a different search or ask an administrator to grant access.',
|
||||||
|
'error' => 'We could not load your tenants. Please try again.',
|
||||||
|
'retry' => 'Try again',
|
||||||
|
'previous' => 'Previous',
|
||||||
|
'next' => 'Next',
|
||||||
|
'select_tenant' => 'Select tenant',
|
||||||
|
],
|
||||||
|
];
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
'menu' => [
|
||||||
|
'label' => 'انتخاب مجموعه',
|
||||||
|
],
|
||||||
|
'errors' => [
|
||||||
|
'selection_required' => 'برای ادامه یک مجموعه را انتخاب کنید.',
|
||||||
|
],
|
||||||
|
'page' => [
|
||||||
|
'title' => 'محیط کاری خود را انتخاب کنید',
|
||||||
|
'kicker' => 'مجموعه فعال',
|
||||||
|
'description' => 'مجموعهای را که میخواهید در آن کار کنید انتخاب کنید. نووا بدون بارگذاری مجدد به محیط جدید منتقل میشود.',
|
||||||
|
'current' => 'محیط فعلی',
|
||||||
|
'search' => 'جستجوی مجموعهها',
|
||||||
|
'search_placeholder' => 'جستجو بر اساس نام…',
|
||||||
|
'select' => 'ورود به محیط',
|
||||||
|
'selected' => 'انتخاب شده',
|
||||||
|
'empty_title' => 'مجموعهای پیدا نشد',
|
||||||
|
'empty_description' => 'عبارت دیگری را جستجو کنید یا از مدیر بخواهید دسترسی شما را بررسی کند.',
|
||||||
|
'error' => 'بارگذاری مجموعهها ممکن نشد. دوباره تلاش کنید.',
|
||||||
|
'retry' => 'تلاش دوباره',
|
||||||
|
'previous' => 'قبلی',
|
||||||
|
'next' => 'بعدی',
|
||||||
|
'select_tenant' => 'انتخاب مجموعه',
|
||||||
|
],
|
||||||
|
];
|
||||||
Generated
+13847
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"name": "nova-tenancy",
|
||||||
|
"private": true,
|
||||||
|
"scripts": {
|
||||||
|
"dev": "mix",
|
||||||
|
"watch": "mix watch",
|
||||||
|
"production": "mix --production",
|
||||||
|
"test": "vitest run"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@vitejs/plugin-vue": "^6.0.0",
|
||||||
|
"@vue/test-utils": "^2.4.6",
|
||||||
|
"jsdom": "^26.1.0",
|
||||||
|
"laravel-nova-devtool": "file:vendor/laravel/nova-devtool",
|
||||||
|
"vitest": "^3.2.4",
|
||||||
|
"vue": "^3.5.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
+17
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
|
||||||
|
bootstrap="vendor/autoload.php"
|
||||||
|
colors="true">
|
||||||
|
<testsuites>
|
||||||
|
<testsuite name="Nova Tenancy">
|
||||||
|
<directory>tests</directory>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
|
<php>
|
||||||
|
<env name="APP_ENV" value="testing"/>
|
||||||
|
<env name="DB_CONNECTION" value="testing"/>
|
||||||
|
<env name="CACHE_STORE" value="array"/>
|
||||||
|
<env name="SESSION_DRIVER" value="array"/>
|
||||||
|
</php>
|
||||||
|
</phpunit>
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
module.exports = {
|
||||||
|
plugins: {
|
||||||
|
autoprefixer: {},
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -0,0 +1,588 @@
|
|||||||
|
.nova-tenancy-page {
|
||||||
|
--nt-primary: #7c3aed;
|
||||||
|
--nt-primary-strong: #5b21b6;
|
||||||
|
--nt-surface: rgb(255 255 255 / 0.88);
|
||||||
|
--nt-border: rgb(226 232 240 / 0.9);
|
||||||
|
--nt-muted: #64748b;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 1.5rem;
|
||||||
|
padding-bottom: 3rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-hero {
|
||||||
|
position: relative;
|
||||||
|
display: flex;
|
||||||
|
min-height: 15rem;
|
||||||
|
align-items: flex-end;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 2rem;
|
||||||
|
overflow: hidden;
|
||||||
|
border: 1px solid rgb(255 255 255 / 0.16);
|
||||||
|
border-radius: 1.5rem;
|
||||||
|
padding: clamp(1.5rem, 4vw, 3rem);
|
||||||
|
color: white;
|
||||||
|
background:
|
||||||
|
radial-gradient(circle at 12% 10%, rgb(167 139 250 / 0.48), transparent 34%),
|
||||||
|
radial-gradient(circle at 88% 85%, rgb(56 189 248 / 0.3), transparent 38%),
|
||||||
|
linear-gradient(135deg, #251259 0%, #4c1d95 48%, #172554 100%);
|
||||||
|
box-shadow: 0 24px 60px rgb(49 46 129 / 0.22);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-hero::after {
|
||||||
|
position: absolute;
|
||||||
|
inset: 0;
|
||||||
|
background-image: linear-gradient(rgb(255 255 255 / 0.04) 1px, transparent 1px),
|
||||||
|
linear-gradient(90deg, rgb(255 255 255 / 0.04) 1px, transparent 1px);
|
||||||
|
background-size: 32px 32px;
|
||||||
|
content: '';
|
||||||
|
mask-image: linear-gradient(to bottom, black, transparent);
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-hero__glow {
|
||||||
|
position: absolute;
|
||||||
|
top: -8rem;
|
||||||
|
right: -5rem;
|
||||||
|
width: 22rem;
|
||||||
|
height: 22rem;
|
||||||
|
border-radius: 9999px;
|
||||||
|
background: rgb(255 255 255 / 0.09);
|
||||||
|
filter: blur(2px);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-hero__content,
|
||||||
|
.nova-tenancy-current {
|
||||||
|
position: relative;
|
||||||
|
z-index: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-hero__content {
|
||||||
|
max-width: 42rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-kicker {
|
||||||
|
margin: 0 0 0.65rem;
|
||||||
|
color: #c4b5fd;
|
||||||
|
font-size: 0.72rem;
|
||||||
|
font-weight: 800;
|
||||||
|
letter-spacing: 0.16em;
|
||||||
|
text-transform: uppercase;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-title {
|
||||||
|
margin: 0;
|
||||||
|
font-size: clamp(2rem, 5vw, 3.5rem);
|
||||||
|
font-weight: 750;
|
||||||
|
letter-spacing: -0.045em;
|
||||||
|
line-height: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-description {
|
||||||
|
max-width: 35rem;
|
||||||
|
margin: 1rem 0 0;
|
||||||
|
color: rgb(237 233 254 / 0.82);
|
||||||
|
font-size: 1rem;
|
||||||
|
line-height: 1.7;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-current {
|
||||||
|
display: flex;
|
||||||
|
min-width: min(100%, 17rem);
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.8rem;
|
||||||
|
border: 1px solid rgb(255 255 255 / 0.16);
|
||||||
|
border-radius: 1rem;
|
||||||
|
padding: 0.8rem;
|
||||||
|
background: rgb(255 255 255 / 0.1);
|
||||||
|
backdrop-filter: blur(14px);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-current__logo,
|
||||||
|
.nova-tenancy-card__logo {
|
||||||
|
display: grid;
|
||||||
|
flex: none;
|
||||||
|
place-items: center;
|
||||||
|
overflow: hidden;
|
||||||
|
border-radius: 0.9rem;
|
||||||
|
font-weight: 800;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-current__logo {
|
||||||
|
width: 3rem;
|
||||||
|
height: 3rem;
|
||||||
|
color: #4c1d95;
|
||||||
|
background: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-current__logo img,
|
||||||
|
.nova-tenancy-card__logo img {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
object-fit: cover;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-current__copy {
|
||||||
|
display: flex;
|
||||||
|
min-width: 0;
|
||||||
|
flex: 1;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-current__copy span {
|
||||||
|
color: rgb(237 233 254 / 0.7);
|
||||||
|
font-size: 0.68rem;
|
||||||
|
font-weight: 700;
|
||||||
|
letter-spacing: 0.08em;
|
||||||
|
text-transform: uppercase;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-current__copy strong {
|
||||||
|
overflow: hidden;
|
||||||
|
margin-top: 0.15rem;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-pulse {
|
||||||
|
width: 0.55rem;
|
||||||
|
height: 0.55rem;
|
||||||
|
border: 2px solid rgb(255 255 255 / 0.8);
|
||||||
|
border-radius: 9999px;
|
||||||
|
background: #34d399;
|
||||||
|
box-shadow: 0 0 0 4px rgb(52 211 153 / 0.2);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-toolbar {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-search {
|
||||||
|
display: flex;
|
||||||
|
min-width: 0;
|
||||||
|
max-width: 34rem;
|
||||||
|
flex: 1;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.75rem;
|
||||||
|
border: 1px solid var(--nt-border);
|
||||||
|
border-radius: 0.9rem;
|
||||||
|
padding-inline: 1rem;
|
||||||
|
background: var(--nt-surface);
|
||||||
|
box-shadow: 0 4px 18px rgb(15 23 42 / 0.04);
|
||||||
|
transition: border-color 160ms, box-shadow 160ms;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-search:focus-within {
|
||||||
|
border-color: rgb(124 58 237 / 0.55);
|
||||||
|
box-shadow: 0 0 0 3px rgb(124 58 237 / 0.12);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-search svg {
|
||||||
|
width: 1.2rem;
|
||||||
|
fill: none;
|
||||||
|
stroke: var(--nt-muted);
|
||||||
|
stroke-linecap: round;
|
||||||
|
stroke-width: 1.8;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-search input {
|
||||||
|
width: 100%;
|
||||||
|
border: 0;
|
||||||
|
outline: 0;
|
||||||
|
padding: 0.85rem 0;
|
||||||
|
color: #0f172a;
|
||||||
|
background: transparent;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-count {
|
||||||
|
display: grid;
|
||||||
|
min-width: 2.5rem;
|
||||||
|
height: 2.5rem;
|
||||||
|
place-items: center;
|
||||||
|
border-radius: 9999px;
|
||||||
|
color: var(--nt-primary-strong);
|
||||||
|
font-size: 0.78rem;
|
||||||
|
font-weight: 800;
|
||||||
|
background: #ede9fe;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-grid {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(auto-fill, minmax(min(100%, 16rem), 1fr));
|
||||||
|
gap: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-card {
|
||||||
|
--tenant-accent: #7c3aed;
|
||||||
|
position: relative;
|
||||||
|
display: flex;
|
||||||
|
min-height: 17rem;
|
||||||
|
flex-direction: column;
|
||||||
|
overflow: hidden;
|
||||||
|
border: 1px solid var(--nt-border);
|
||||||
|
border-radius: 1.25rem;
|
||||||
|
padding: 1.25rem;
|
||||||
|
background: var(--nt-surface);
|
||||||
|
box-shadow: 0 10px 32px rgb(15 23 42 / 0.055);
|
||||||
|
transition: transform 180ms ease, border-color 180ms ease, box-shadow 180ms ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-card:hover {
|
||||||
|
transform: translateY(-3px);
|
||||||
|
border-color: color-mix(in srgb, var(--tenant-accent) 42%, transparent);
|
||||||
|
box-shadow: 0 18px 42px rgb(15 23 42 / 0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-card--selected {
|
||||||
|
border-color: color-mix(in srgb, var(--tenant-accent) 70%, transparent);
|
||||||
|
box-shadow: 0 0 0 2px color-mix(in srgb, var(--tenant-accent) 16%, transparent),
|
||||||
|
0 18px 42px rgb(15 23 42 / 0.09);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-card__accent {
|
||||||
|
position: absolute;
|
||||||
|
inset: 0 0 auto;
|
||||||
|
height: 0.26rem;
|
||||||
|
background: linear-gradient(90deg, var(--tenant-accent), color-mix(in srgb, var(--tenant-accent) 25%, white));
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-card__header {
|
||||||
|
display: flex;
|
||||||
|
align-items: flex-start;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-card__logo {
|
||||||
|
width: 4rem;
|
||||||
|
height: 4rem;
|
||||||
|
color: var(--tenant-accent);
|
||||||
|
font-size: 1.35rem;
|
||||||
|
background: color-mix(in srgb, var(--tenant-accent) 11%, white);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-badge {
|
||||||
|
border-radius: 9999px;
|
||||||
|
padding: 0.35rem 0.65rem;
|
||||||
|
color: var(--tenant-accent);
|
||||||
|
font-size: 0.68rem;
|
||||||
|
font-weight: 800;
|
||||||
|
background: color-mix(in srgb, var(--tenant-accent) 10%, white);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-check {
|
||||||
|
display: grid;
|
||||||
|
width: 1.8rem;
|
||||||
|
height: 1.8rem;
|
||||||
|
place-items: center;
|
||||||
|
border-radius: 9999px;
|
||||||
|
color: white;
|
||||||
|
background: var(--tenant-accent);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-check svg {
|
||||||
|
width: 1rem;
|
||||||
|
fill: none;
|
||||||
|
stroke: currentColor;
|
||||||
|
stroke-linecap: round;
|
||||||
|
stroke-linejoin: round;
|
||||||
|
stroke-width: 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-card__body {
|
||||||
|
flex: 1;
|
||||||
|
padding-block: 1.2rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-card__body h2 {
|
||||||
|
margin: 0;
|
||||||
|
color: #0f172a;
|
||||||
|
font-size: 1.05rem;
|
||||||
|
font-weight: 750;
|
||||||
|
line-height: 1.4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-card__body p {
|
||||||
|
display: -webkit-box;
|
||||||
|
overflow: hidden;
|
||||||
|
margin: 0.55rem 0 0;
|
||||||
|
color: var(--nt-muted);
|
||||||
|
font-size: 0.84rem;
|
||||||
|
line-height: 1.6;
|
||||||
|
-webkit-box-orient: vertical;
|
||||||
|
-webkit-line-clamp: 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-card__body .nova-tenancy-card__key {
|
||||||
|
color: color-mix(in srgb, var(--tenant-accent) 68%, #64748b);
|
||||||
|
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-select,
|
||||||
|
.nova-tenancy-button,
|
||||||
|
.nova-tenancy-pagination button {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
gap: 0.55rem;
|
||||||
|
border: 0;
|
||||||
|
border-radius: 0.75rem;
|
||||||
|
font-weight: 750;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: transform 150ms, background-color 150ms, opacity 150ms;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-select {
|
||||||
|
width: 100%;
|
||||||
|
min-height: 2.7rem;
|
||||||
|
color: white;
|
||||||
|
background: var(--tenant-accent);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-select:not(:disabled):hover,
|
||||||
|
.nova-tenancy-button:hover,
|
||||||
|
.nova-tenancy-pagination button:not(:disabled):hover {
|
||||||
|
transform: translateY(-1px);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-select--selected {
|
||||||
|
color: var(--tenant-accent);
|
||||||
|
background: color-mix(in srgb, var(--tenant-accent) 10%, white);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-select:disabled,
|
||||||
|
.nova-tenancy-pagination button:disabled {
|
||||||
|
cursor: default;
|
||||||
|
opacity: 0.72;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-select svg {
|
||||||
|
width: 1rem;
|
||||||
|
fill: none;
|
||||||
|
stroke: currentColor;
|
||||||
|
stroke-linecap: round;
|
||||||
|
stroke-linejoin: round;
|
||||||
|
stroke-width: 1.8;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-spinner {
|
||||||
|
width: 1rem;
|
||||||
|
height: 1rem;
|
||||||
|
border: 2px solid rgb(255 255 255 / 0.4);
|
||||||
|
border-top-color: white;
|
||||||
|
border-radius: 9999px;
|
||||||
|
animation: nova-tenancy-spin 650ms linear infinite;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-card--skeleton {
|
||||||
|
gap: 1.3rem;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-card--skeleton span {
|
||||||
|
display: block;
|
||||||
|
height: 1rem;
|
||||||
|
border-radius: 9999px;
|
||||||
|
background: linear-gradient(90deg, #f1f5f9, #e2e8f0, #f1f5f9);
|
||||||
|
background-size: 220% 100%;
|
||||||
|
animation: nova-tenancy-shimmer 1.4s ease infinite;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-card--skeleton span:first-child {
|
||||||
|
width: 4rem;
|
||||||
|
height: 4rem;
|
||||||
|
border-radius: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-card--skeleton span:nth-child(2) { width: 70%; }
|
||||||
|
.nova-tenancy-card--skeleton span:nth-child(3) { width: 45%; }
|
||||||
|
|
||||||
|
.nova-tenancy-state {
|
||||||
|
display: flex;
|
||||||
|
min-height: 18rem;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
flex-direction: column;
|
||||||
|
border: 1px dashed var(--nt-border);
|
||||||
|
border-radius: 1.25rem;
|
||||||
|
padding: 2rem;
|
||||||
|
text-align: center;
|
||||||
|
background: var(--nt-surface);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-state__icon {
|
||||||
|
display: grid;
|
||||||
|
width: 3.5rem;
|
||||||
|
height: 3.5rem;
|
||||||
|
place-items: center;
|
||||||
|
border-radius: 1rem;
|
||||||
|
color: #be123c;
|
||||||
|
font-size: 1.5rem;
|
||||||
|
font-weight: 800;
|
||||||
|
background: #ffe4e6;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-state__icon--empty {
|
||||||
|
color: var(--nt-primary);
|
||||||
|
background: #ede9fe;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-state__icon svg {
|
||||||
|
width: 1.8rem;
|
||||||
|
fill: none;
|
||||||
|
stroke: currentColor;
|
||||||
|
stroke-linecap: round;
|
||||||
|
stroke-linejoin: round;
|
||||||
|
stroke-width: 1.5;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-state h2 {
|
||||||
|
margin: 1rem 0 0;
|
||||||
|
color: #0f172a;
|
||||||
|
font-size: 1.1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-state p {
|
||||||
|
max-width: 28rem;
|
||||||
|
margin: 0.5rem 0 0;
|
||||||
|
color: var(--nt-muted);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-button {
|
||||||
|
margin-top: 1rem;
|
||||||
|
padding: 0.65rem 1rem;
|
||||||
|
color: white;
|
||||||
|
background: var(--nt-primary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-pagination {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
gap: 0.8rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-pagination button {
|
||||||
|
border: 1px solid var(--nt-border);
|
||||||
|
padding: 0.55rem 0.9rem;
|
||||||
|
color: #334155;
|
||||||
|
background: var(--nt-surface);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-pagination span {
|
||||||
|
color: var(--nt-muted);
|
||||||
|
font-size: 0.8rem;
|
||||||
|
font-weight: 700;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-brand {
|
||||||
|
display: flex;
|
||||||
|
min-width: 0;
|
||||||
|
max-width: 13.5rem;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.65rem;
|
||||||
|
border: 0;
|
||||||
|
padding: 0;
|
||||||
|
color: #334155;
|
||||||
|
background: transparent;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-brand__logo {
|
||||||
|
width: 2.5rem;
|
||||||
|
height: 2.5rem;
|
||||||
|
flex: none;
|
||||||
|
border-radius: 0.7rem;
|
||||||
|
object-fit: cover;
|
||||||
|
box-shadow: 0 3px 12px rgb(15 23 42 / 0.12);
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-brand__title {
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
|
font-size: 0.86rem;
|
||||||
|
font-weight: 750;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-select:focus-visible,
|
||||||
|
.nova-tenancy-button:focus-visible,
|
||||||
|
.nova-tenancy-pagination button:focus-visible,
|
||||||
|
.nova-tenancy-brand:focus-visible {
|
||||||
|
outline: 2px solid var(--nt-primary);
|
||||||
|
outline-offset: 3px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-sr-only {
|
||||||
|
position: absolute;
|
||||||
|
width: 1px;
|
||||||
|
height: 1px;
|
||||||
|
overflow: hidden;
|
||||||
|
clip: rect(0, 0, 0, 0);
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.dark .nova-tenancy-page {
|
||||||
|
--nt-surface: rgb(30 41 59 / 0.84);
|
||||||
|
--nt-border: rgb(71 85 105 / 0.72);
|
||||||
|
--nt-muted: #94a3b8;
|
||||||
|
}
|
||||||
|
|
||||||
|
.dark .nova-tenancy-search input,
|
||||||
|
.dark .nova-tenancy-card__body h2,
|
||||||
|
.dark .nova-tenancy-state h2,
|
||||||
|
.dark .nova-tenancy-brand,
|
||||||
|
.dark .nova-tenancy-pagination button {
|
||||||
|
color: #f8fafc;
|
||||||
|
}
|
||||||
|
|
||||||
|
.dark .nova-tenancy-count,
|
||||||
|
.dark .nova-tenancy-state__icon--empty {
|
||||||
|
color: #c4b5fd;
|
||||||
|
background: rgb(91 33 182 / 0.3);
|
||||||
|
}
|
||||||
|
|
||||||
|
.dark .nova-tenancy-card__logo,
|
||||||
|
.dark .nova-tenancy-badge,
|
||||||
|
.dark .nova-tenancy-select--selected {
|
||||||
|
background: color-mix(in srgb, var(--tenant-accent) 22%, #0f172a);
|
||||||
|
}
|
||||||
|
|
||||||
|
.dark .nova-tenancy-card--skeleton span {
|
||||||
|
background: linear-gradient(90deg, #1e293b, #334155, #1e293b);
|
||||||
|
background-size: 220% 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 700px) {
|
||||||
|
.nova-tenancy-hero {
|
||||||
|
align-items: stretch;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-current {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-brand__title {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-reduced-motion: reduce) {
|
||||||
|
.nova-tenancy-card,
|
||||||
|
.nova-tenancy-select,
|
||||||
|
.nova-tenancy-button,
|
||||||
|
.nova-tenancy-pagination button {
|
||||||
|
transition: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nova-tenancy-spinner,
|
||||||
|
.nova-tenancy-card--skeleton span {
|
||||||
|
animation: none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes nova-tenancy-spin { to { transform: rotate(360deg); } }
|
||||||
|
@keyframes nova-tenancy-shimmer { to { background-position: -220% 0; } }
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
<template>
|
||||||
|
<span
|
||||||
|
ref="brand"
|
||||||
|
class="nova-tenancy-brand"
|
||||||
|
:title="title"
|
||||||
|
@click.stop.prevent="visitBrandDestination"
|
||||||
|
>
|
||||||
|
<img
|
||||||
|
v-if="showLogo"
|
||||||
|
:key="logoUrl"
|
||||||
|
:src="logoUrl"
|
||||||
|
class="nova-tenancy-brand__logo"
|
||||||
|
alt=""
|
||||||
|
/>
|
||||||
|
|
||||||
|
<span v-if="showTitle" class="nova-tenancy-brand__title">
|
||||||
|
{{ title }}
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
|
||||||
|
import { currentLogoUrl } from '../tenantState'
|
||||||
|
|
||||||
|
const configuration = Nova.config('novaTenancy') || {}
|
||||||
|
const brand = ref(null)
|
||||||
|
const current = ref(configuration.current || null)
|
||||||
|
const revision = ref(Date.now())
|
||||||
|
let parentLink = null
|
||||||
|
|
||||||
|
const showLogo = computed(() => configuration.branding?.show_logo !== false)
|
||||||
|
const showTitle = computed(() => configuration.branding?.show_title !== false)
|
||||||
|
const title = computed(
|
||||||
|
() => current.value?.title || configuration.copy?.selectTenant || 'Select tenant'
|
||||||
|
)
|
||||||
|
const logoUrl = computed(() => {
|
||||||
|
const apiBase = configuration.apiBase || '/nova-vendor/nova-tenancy'
|
||||||
|
const key = current.value?.key ?? 'none'
|
||||||
|
|
||||||
|
return currentLogoUrl(apiBase, key, revision.value)
|
||||||
|
})
|
||||||
|
|
||||||
|
const updateCurrent = payload => {
|
||||||
|
current.value = payload?.current ?? payload ?? null
|
||||||
|
revision.value = Date.now()
|
||||||
|
}
|
||||||
|
|
||||||
|
const refreshCurrent = async () => {
|
||||||
|
try {
|
||||||
|
const response = await Nova.request().get(
|
||||||
|
`${configuration.apiBase || '/nova-vendor/nova-tenancy'}/current`
|
||||||
|
)
|
||||||
|
updateCurrent(response.data)
|
||||||
|
} catch {
|
||||||
|
// Keep the server-provided bootstrap state if the refresh cannot complete.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const brandDestination = () => {
|
||||||
|
const destination = configuration.branding?.link
|
||||||
|
|
||||||
|
if (destination === 'home') {
|
||||||
|
return '/'
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof destination === 'string' && destination.startsWith('/')) {
|
||||||
|
return destination
|
||||||
|
}
|
||||||
|
|
||||||
|
return configuration.selectorPath || '/nova-tenancy'
|
||||||
|
}
|
||||||
|
|
||||||
|
const visitBrandDestination = () => {
|
||||||
|
Nova.visit(brandDestination())
|
||||||
|
}
|
||||||
|
|
||||||
|
const interceptParentLink = event => {
|
||||||
|
event.preventDefault()
|
||||||
|
event.stopImmediatePropagation()
|
||||||
|
visitBrandDestination()
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
Nova.$on('nova-tenancy:changed', updateCurrent)
|
||||||
|
parentLink = brand.value?.closest('a') || null
|
||||||
|
parentLink?.addEventListener('click', interceptParentLink, true)
|
||||||
|
refreshCurrent()
|
||||||
|
})
|
||||||
|
|
||||||
|
onBeforeUnmount(() => {
|
||||||
|
Nova.$off('nova-tenancy:changed', updateCurrent)
|
||||||
|
parentLink?.removeEventListener('click', interceptParentLink, true)
|
||||||
|
})
|
||||||
|
</script>
|
||||||
@@ -0,0 +1,233 @@
|
|||||||
|
<template>
|
||||||
|
<div class="nova-tenancy-page">
|
||||||
|
<Head :title="copy.pageTitle" />
|
||||||
|
|
||||||
|
<section class="nova-tenancy-hero">
|
||||||
|
<div class="nova-tenancy-hero__glow" aria-hidden="true"></div>
|
||||||
|
|
||||||
|
<div class="nova-tenancy-hero__content">
|
||||||
|
<p class="nova-tenancy-kicker">{{ copy.pageKicker }}</p>
|
||||||
|
<h1 class="nova-tenancy-title">{{ copy.pageTitle }}</h1>
|
||||||
|
<p class="nova-tenancy-description">{{ copy.pageDescription }}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-if="activeTenant" class="nova-tenancy-current">
|
||||||
|
<div class="nova-tenancy-current__logo">
|
||||||
|
<img
|
||||||
|
v-if="activeTenant.logoUrl && !failedImages.has(String(activeTenant.key))"
|
||||||
|
:src="activeTenant.logoUrl"
|
||||||
|
alt=""
|
||||||
|
@error="markImageFailed(activeTenant.key)"
|
||||||
|
/>
|
||||||
|
<span v-else>{{ initial(activeTenant.title) }}</span>
|
||||||
|
</div>
|
||||||
|
<div class="nova-tenancy-current__copy">
|
||||||
|
<span>{{ copy.currentTenant }}</span>
|
||||||
|
<strong>{{ activeTenant.title }}</strong>
|
||||||
|
</div>
|
||||||
|
<span class="nova-tenancy-pulse" aria-hidden="true"></span>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section class="nova-tenancy-toolbar" :aria-label="copy.search">
|
||||||
|
<label class="nova-tenancy-search">
|
||||||
|
<svg viewBox="0 0 24 24" aria-hidden="true">
|
||||||
|
<path d="m21 21-4.35-4.35m2.35-5.65a8 8 0 1 1-16 0 8 8 0 0 1 16 0Z" />
|
||||||
|
</svg>
|
||||||
|
<span class="nova-tenancy-sr-only">{{ copy.search }}</span>
|
||||||
|
<input
|
||||||
|
v-model="search"
|
||||||
|
type="search"
|
||||||
|
:placeholder="copy.searchPlaceholder"
|
||||||
|
autocomplete="off"
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<span v-if="!loading" class="nova-tenancy-count">
|
||||||
|
{{ meta.total.toLocaleString() }}
|
||||||
|
</span>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<div v-if="loading" class="nova-tenancy-grid" aria-busy="true">
|
||||||
|
<div v-for="index in 6" :key="index" class="nova-tenancy-card nova-tenancy-card--skeleton">
|
||||||
|
<span></span><span></span><span></span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-else-if="error" class="nova-tenancy-state" role="alert">
|
||||||
|
<div class="nova-tenancy-state__icon">!</div>
|
||||||
|
<h2>{{ copy.error }}</h2>
|
||||||
|
<button type="button" class="nova-tenancy-button" @click="loadTenants">
|
||||||
|
{{ copy.retry }}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-else-if="tenants.length === 0" class="nova-tenancy-state">
|
||||||
|
<div class="nova-tenancy-state__icon nova-tenancy-state__icon--empty">
|
||||||
|
<svg viewBox="0 0 24 24" aria-hidden="true">
|
||||||
|
<path d="M3 21h18M5 21V7l7-4 7 4v14M9 10h.01M9 14h.01M9 18h.01M15 10h.01M15 14h.01M15 18h.01" />
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<h2>{{ copy.emptyTitle }}</h2>
|
||||||
|
<p>{{ copy.emptyDescription }}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-else class="nova-tenancy-grid">
|
||||||
|
<article
|
||||||
|
v-for="tenant in tenants"
|
||||||
|
:key="tenant.key"
|
||||||
|
class="nova-tenancy-card"
|
||||||
|
:class="{ 'nova-tenancy-card--selected': isSelected(tenant) }"
|
||||||
|
:style="tenantStyle(tenant)"
|
||||||
|
:aria-current="isSelected(tenant) ? 'true' : undefined"
|
||||||
|
>
|
||||||
|
<div class="nova-tenancy-card__accent" aria-hidden="true"></div>
|
||||||
|
<div class="nova-tenancy-card__header">
|
||||||
|
<div class="nova-tenancy-card__logo">
|
||||||
|
<img
|
||||||
|
v-if="tenant.logoUrl && !failedImages.has(String(tenant.key))"
|
||||||
|
:src="tenant.logoUrl"
|
||||||
|
alt=""
|
||||||
|
loading="lazy"
|
||||||
|
@error="markImageFailed(tenant.key)"
|
||||||
|
/>
|
||||||
|
<span v-else>{{ initial(tenant.title) }}</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<span v-if="tenant.badge" class="nova-tenancy-badge">{{ tenant.badge }}</span>
|
||||||
|
<span v-else-if="isSelected(tenant)" class="nova-tenancy-check" :title="copy.selected">
|
||||||
|
<svg viewBox="0 0 20 20" aria-hidden="true"><path d="m5 10 3 3 7-7" /></svg>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="nova-tenancy-card__body">
|
||||||
|
<h2>{{ tenant.title }}</h2>
|
||||||
|
<p v-if="tenant.description">{{ tenant.description }}</p>
|
||||||
|
<p v-else class="nova-tenancy-card__key">#{{ tenant.key }}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="nova-tenancy-select"
|
||||||
|
:class="{ 'nova-tenancy-select--selected': isSelected(tenant) }"
|
||||||
|
:disabled="workingKey !== null || isSelected(tenant)"
|
||||||
|
:aria-busy="workingKey === String(tenant.key)"
|
||||||
|
@click="selectTenant(tenant)"
|
||||||
|
>
|
||||||
|
<span v-if="workingKey === String(tenant.key)" class="nova-tenancy-spinner" aria-hidden="true"></span>
|
||||||
|
<span>{{ isSelected(tenant) ? copy.selected : copy.select }}</span>
|
||||||
|
<svg v-if="!isSelected(tenant)" viewBox="0 0 20 20" aria-hidden="true">
|
||||||
|
<path d="M4 10h12m-4-4 4 4-4 4" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
</article>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<nav v-if="meta.lastPage > 1" class="nova-tenancy-pagination" aria-label="Pagination">
|
||||||
|
<button type="button" :disabled="meta.currentPage <= 1 || loading" @click="goToPage(meta.currentPage - 1)">
|
||||||
|
{{ copy.previous }}
|
||||||
|
</button>
|
||||||
|
<span>{{ meta.currentPage }} / {{ meta.lastPage }}</span>
|
||||||
|
<button type="button" :disabled="meta.currentPage >= meta.lastPage || loading" @click="goToPage(meta.currentPage + 1)">
|
||||||
|
{{ copy.next }}
|
||||||
|
</button>
|
||||||
|
</nav>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import { computed, onBeforeUnmount, onMounted, reactive, ref, watch } from 'vue'
|
||||||
|
import { sameTenant, tenantInitial, tenantStyle as resolveTenantStyle } from '../tenantState'
|
||||||
|
|
||||||
|
const configuration = Nova.config('novaTenancy') || {}
|
||||||
|
const copy = configuration.copy || {}
|
||||||
|
const tenants = ref([])
|
||||||
|
const currentKey = ref(configuration.current?.key ?? null)
|
||||||
|
const search = ref('')
|
||||||
|
const loading = ref(true)
|
||||||
|
const error = ref(false)
|
||||||
|
const workingKey = ref(null)
|
||||||
|
const failedImages = reactive(new Set())
|
||||||
|
const meta = reactive({
|
||||||
|
currentPage: 1,
|
||||||
|
lastPage: 1,
|
||||||
|
perPage: Number(configuration.perPage || 12),
|
||||||
|
total: 0,
|
||||||
|
})
|
||||||
|
let debounceTimer
|
||||||
|
let requestSequence = 0
|
||||||
|
|
||||||
|
const activeTenant = computed(() => {
|
||||||
|
const visible = tenants.value.find(tenant => isSelected(tenant))
|
||||||
|
return (
|
||||||
|
visible ||
|
||||||
|
(sameTenant(configuration.current?.key ?? null, currentKey.value)
|
||||||
|
? configuration.current
|
||||||
|
: null)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
const loadTenants = async () => {
|
||||||
|
const sequence = ++requestSequence
|
||||||
|
loading.value = true
|
||||||
|
error.value = false
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await Nova.request().get(
|
||||||
|
`${configuration.apiBase || '/nova-vendor/nova-tenancy'}/tenants`,
|
||||||
|
{ params: { search: search.value || undefined, page: meta.currentPage, perPage: meta.perPage } }
|
||||||
|
)
|
||||||
|
|
||||||
|
if (sequence !== requestSequence) return
|
||||||
|
|
||||||
|
tenants.value = response.data.data
|
||||||
|
currentKey.value = response.data.currentKey
|
||||||
|
Object.assign(meta, response.data.meta)
|
||||||
|
} catch {
|
||||||
|
if (sequence === requestSequence) error.value = true
|
||||||
|
} finally {
|
||||||
|
if (sequence === requestSequence) loading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const selectTenant = async tenant => {
|
||||||
|
if (workingKey.value !== null || isSelected(tenant)) return
|
||||||
|
|
||||||
|
workingKey.value = String(tenant.key)
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await Nova.request().post(
|
||||||
|
`${configuration.apiBase || '/nova-vendor/nova-tenancy'}/current`,
|
||||||
|
{ tenant: String(tenant.key) }
|
||||||
|
)
|
||||||
|
currentKey.value = response.data.current.key
|
||||||
|
Nova.$emit('nova-tenancy:changed', response.data)
|
||||||
|
Nova.visit(response.data.redirectTo || '/')
|
||||||
|
} catch {
|
||||||
|
Nova.$emit('error', copy.error || 'Unable to select tenant.')
|
||||||
|
} finally {
|
||||||
|
workingKey.value = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const goToPage = page => {
|
||||||
|
meta.currentPage = page
|
||||||
|
loadTenants()
|
||||||
|
}
|
||||||
|
|
||||||
|
const isSelected = tenant => sameTenant(tenant.key, currentKey.value)
|
||||||
|
const initial = tenantInitial
|
||||||
|
const tenantStyle = resolveTenantStyle
|
||||||
|
const markImageFailed = key => failedImages.add(String(key))
|
||||||
|
|
||||||
|
watch(search, () => {
|
||||||
|
clearTimeout(debounceTimer)
|
||||||
|
debounceTimer = setTimeout(() => {
|
||||||
|
meta.currentPage = 1
|
||||||
|
loadTenants()
|
||||||
|
}, Number(configuration.searchDebounce || 300))
|
||||||
|
})
|
||||||
|
|
||||||
|
onMounted(loadTenants)
|
||||||
|
onBeforeUnmount(() => clearTimeout(debounceTimer))
|
||||||
|
</script>
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
export const sameTenant = (left, right) =>
|
||||||
|
left !== null && right !== null && String(left) === String(right)
|
||||||
|
|
||||||
|
export const tenantInitial = title =>
|
||||||
|
String(title || 'T').trim().charAt(0).toLocaleUpperCase()
|
||||||
|
|
||||||
|
export const tenantStyle = tenant => ({
|
||||||
|
'--tenant-accent': tenant.accent || '#7c3aed',
|
||||||
|
})
|
||||||
|
|
||||||
|
export const currentLogoUrl = (apiBase, tenantKey, revision) =>
|
||||||
|
`${apiBase}/current/logo?tenant=${encodeURIComponent(tenantKey ?? 'none')}&v=${revision}`
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import NovaTenancy from './pages/NovaTenancy.vue'
|
||||||
|
import TenantAppLogo from './components/TenantAppLogo.vue'
|
||||||
|
|
||||||
|
Nova.inertia('NovaTenancy', NovaTenancy)
|
||||||
|
|
||||||
|
Nova.booting(app => {
|
||||||
|
if (Nova.config('novaTenancy')?.branding?.enabled !== false) {
|
||||||
|
app.component('AppLogo', TenantAppLogo)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
Nova.request().interceptors.response.use(
|
||||||
|
response => response,
|
||||||
|
error => {
|
||||||
|
if (
|
||||||
|
error.response?.status === 409 &&
|
||||||
|
error.response?.data?.code === 'tenant_selection_required'
|
||||||
|
) {
|
||||||
|
const selectorPath = Nova.config('novaTenancy')?.selectorPath || '/nova-tenancy'
|
||||||
|
Nova.visit(selectorPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.reject(error)
|
||||||
|
}
|
||||||
|
)
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Route;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Http\Controllers\CurrentTenantController;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Http\Controllers\CurrentTenantLogoController;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Http\Controllers\TenantIndexController;
|
||||||
|
|
||||||
|
Route::get('/tenants', TenantIndexController::class)->name('tenants.index');
|
||||||
|
Route::get('/current', [CurrentTenantController::class, 'show'])->name('current.show');
|
||||||
|
Route::post('/current', [CurrentTenantController::class, 'store'])->name('current.store');
|
||||||
|
Route::delete('/current', [CurrentTenantController::class, 'destroy'])->name('current.destroy');
|
||||||
|
Route::get('/current/logo', CurrentTenantLogoController::class)->name('current.logo');
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Route;
|
||||||
|
use Inertia\Inertia;
|
||||||
|
|
||||||
|
Route::get('/', fn () => Inertia::render('NovaTenancy'))->name('nova-tenancy.index');
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Concerns;
|
||||||
|
|
||||||
|
trait HasNovaTenantPresentation
|
||||||
|
{
|
||||||
|
public function novaTenantKey(): int|string
|
||||||
|
{
|
||||||
|
return $this->getAttribute($this->novaTenantKeyColumn());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function novaTenantTitle(): string
|
||||||
|
{
|
||||||
|
return (string) $this->getAttribute((string) config('nova-tenancy.model.title_attribute', 'name'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function novaTenantLogoUrl(): ?string
|
||||||
|
{
|
||||||
|
$value = $this->getAttribute((string) config('nova-tenancy.model.logo_attribute', 'logo_url'));
|
||||||
|
|
||||||
|
return filled($value) ? (string) $value : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function novaTenantMeta(): array
|
||||||
|
{
|
||||||
|
return array_filter([
|
||||||
|
'description' => $this->novaTenantMetaAttribute('description_attribute'),
|
||||||
|
'badge' => $this->novaTenantMetaAttribute('badge_attribute'),
|
||||||
|
'accent' => $this->novaTenantMetaAttribute('accent_attribute'),
|
||||||
|
], static fn (mixed $value): bool => $value !== null && $value !== '');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function novaTenantKeyColumn(): string
|
||||||
|
{
|
||||||
|
return (string) (config('nova-tenancy.model.key_column') ?: $this->getRouteKeyName());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function novaTenantMetaAttribute(string $configurationKey): ?string
|
||||||
|
{
|
||||||
|
$attribute = config("nova-tenancy.model.{$configurationKey}");
|
||||||
|
|
||||||
|
if (! is_string($attribute) || $attribute === '') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$value = $this->getAttribute($attribute);
|
||||||
|
|
||||||
|
return filled($value) ? (string) $value : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Concerns;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\CurrentTenantContext;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\NovaTenant;
|
||||||
|
|
||||||
|
trait InteractsWithCurrentTenant
|
||||||
|
{
|
||||||
|
public static function currentTenantContext(): CurrentTenantContext
|
||||||
|
{
|
||||||
|
return app(CurrentTenantContext::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function currentTenantId(): int|string|null
|
||||||
|
{
|
||||||
|
return static::currentTenantContext()->id();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return (Model&NovaTenant)|null */
|
||||||
|
public static function currentTenantModel(): ?Model
|
||||||
|
{
|
||||||
|
return static::currentTenantContext()->model();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function scopeToCurrentTenant(
|
||||||
|
Builder $query,
|
||||||
|
string $column = 'tenant_id',
|
||||||
|
bool $required = true,
|
||||||
|
): Builder {
|
||||||
|
$id = $required
|
||||||
|
? static::currentTenantContext()->requireId()
|
||||||
|
: static::currentTenantContext()->id();
|
||||||
|
|
||||||
|
if ($id === null) {
|
||||||
|
return $query->whereRaw('1 = 0');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query->where($query->getModel()->qualifyColumn($column), $id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Contracts;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
interface CurrentTenantContext
|
||||||
|
{
|
||||||
|
public function id(): int|string|null;
|
||||||
|
|
||||||
|
/** @return (Model&NovaTenant)|null */
|
||||||
|
public function model(): ?Model;
|
||||||
|
|
||||||
|
public function hasTenant(): bool;
|
||||||
|
|
||||||
|
public function requireId(): int|string;
|
||||||
|
|
||||||
|
/** @return Model&NovaTenant */
|
||||||
|
public function requireModel(): Model;
|
||||||
|
|
||||||
|
/** @param Model&NovaTenant $tenant */
|
||||||
|
public function activate(Model $tenant): void;
|
||||||
|
|
||||||
|
public function forget(): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @template TReturn
|
||||||
|
*
|
||||||
|
* @param Model&NovaTenant $tenant
|
||||||
|
* @param Closure(): TReturn $callback
|
||||||
|
* @return TReturn
|
||||||
|
*/
|
||||||
|
public function run(Model $tenant, Closure $callback): mixed;
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Contracts;
|
||||||
|
|
||||||
|
interface NovaTenant
|
||||||
|
{
|
||||||
|
public function novaTenantKey(): int|string;
|
||||||
|
|
||||||
|
public function novaTenantTitle(): string;
|
||||||
|
|
||||||
|
public function novaTenantLogoUrl(): ?string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{description?: string|null, badge?: string|null, accent?: string|null}
|
||||||
|
*/
|
||||||
|
public function novaTenantMeta(): array;
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Contracts;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
interface ProvidesNovaTenants
|
||||||
|
{
|
||||||
|
public function novaTenantQuery(Request $request): Builder;
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Contracts;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
interface TenantProvider
|
||||||
|
{
|
||||||
|
public function query(Request $request): Builder;
|
||||||
|
|
||||||
|
public function search(Request $request, Builder $query, string $search): Builder;
|
||||||
|
|
||||||
|
public function find(Request $request, int|string $key): ?Model;
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\CurrentTenantContext;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\NovaTenant;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\TenantProvider;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Exceptions\TenantNotSelected;
|
||||||
|
|
||||||
|
class CurrentTenant implements CurrentTenantContext
|
||||||
|
{
|
||||||
|
protected bool $resolved = false;
|
||||||
|
|
||||||
|
/** @var (Model&NovaTenant)|null */
|
||||||
|
protected ?Model $tenant = null;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
protected TenantProvider $provider,
|
||||||
|
protected Request $request,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function id(): int|string|null
|
||||||
|
{
|
||||||
|
return $this->model()?->novaTenantKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function model(): ?Model
|
||||||
|
{
|
||||||
|
if ($this->resolved) {
|
||||||
|
return $this->tenant;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->resolved = true;
|
||||||
|
$key = $this->request->cookie((string) config('nova-tenancy.cookie.name', 'nova_tenant'));
|
||||||
|
|
||||||
|
if ($key === null || $key === '') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$tenant = $this->provider->find($this->request, $key);
|
||||||
|
|
||||||
|
if ($tenant === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertTenant($tenant);
|
||||||
|
|
||||||
|
return $this->tenant = $tenant;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasTenant(): bool
|
||||||
|
{
|
||||||
|
return $this->model() !== null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function requireId(): int|string
|
||||||
|
{
|
||||||
|
return $this->requireModel()->novaTenantKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function requireModel(): Model
|
||||||
|
{
|
||||||
|
return $this->model() ?? throw new TenantNotSelected;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function activate(Model $tenant): void
|
||||||
|
{
|
||||||
|
$this->assertTenant($tenant);
|
||||||
|
$this->tenant = $tenant;
|
||||||
|
$this->resolved = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function forget(): void
|
||||||
|
{
|
||||||
|
$this->tenant = null;
|
||||||
|
$this->resolved = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function run(Model $tenant, Closure $callback): mixed
|
||||||
|
{
|
||||||
|
$previousTenant = $this->tenant;
|
||||||
|
$wasResolved = $this->resolved;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->activate($tenant);
|
||||||
|
|
||||||
|
return $callback();
|
||||||
|
} finally {
|
||||||
|
$this->tenant = $previousTenant;
|
||||||
|
$this->resolved = $wasResolved;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function assertTenant(Model $tenant): void
|
||||||
|
{
|
||||||
|
if (! $tenant instanceof NovaTenant) {
|
||||||
|
throw new InvalidArgumentException(sprintf(
|
||||||
|
'Tenant model [%s] must implement [%s].',
|
||||||
|
$tenant::class,
|
||||||
|
NovaTenant::class,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Events;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Auth\Authenticatable;
|
||||||
|
use Illuminate\Foundation\Events\Dispatchable;
|
||||||
|
|
||||||
|
class TenantCleared
|
||||||
|
{
|
||||||
|
use Dispatchable;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
public Authenticatable $user,
|
||||||
|
public int|string|null $previousTenantId,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Events;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Auth\Authenticatable;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Foundation\Events\Dispatchable;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\NovaTenant;
|
||||||
|
|
||||||
|
class TenantSelected
|
||||||
|
{
|
||||||
|
use Dispatchable;
|
||||||
|
|
||||||
|
/** @param Model&NovaTenant $tenant */
|
||||||
|
public function __construct(
|
||||||
|
public Authenticatable $user,
|
||||||
|
public Model $tenant,
|
||||||
|
public int|string|null $previousTenantId,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Exceptions;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Support\Responsable;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Laravel\Nova\Nova;
|
||||||
|
use RuntimeException;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
class TenantNotSelected extends RuntimeException implements Responsable
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct(__('nova-tenancy::nova-tenancy.errors.selection_required'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toResponse($request): Response
|
||||||
|
{
|
||||||
|
if ($request instanceof Request && $request->expectsJson()) {
|
||||||
|
return response()->json([
|
||||||
|
'message' => $this->getMessage(),
|
||||||
|
'code' => 'tenant_selection_required',
|
||||||
|
], 409);
|
||||||
|
}
|
||||||
|
|
||||||
|
$novaPath = '/'.trim(Nova::path(), '/');
|
||||||
|
$toolPath = trim((string) config('nova-tenancy.routes.uri', 'nova-tenancy'), '/');
|
||||||
|
|
||||||
|
return redirect()->to("{$novaPath}/{$toolPath}");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Http\Controllers;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Contracts\Auth\Authenticatable;
|
||||||
|
use Illuminate\Cookie\CookieJar;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Routing\Controller;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\CurrentTenantContext;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\NovaTenant;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\TenantProvider;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Events\TenantCleared;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Events\TenantSelected;
|
||||||
|
use MeghdadFadaee\NovaTenancy\NovaTenancy;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Support\TenantPayload;
|
||||||
|
|
||||||
|
class CurrentTenantController extends Controller
|
||||||
|
{
|
||||||
|
public function show(
|
||||||
|
Request $request,
|
||||||
|
CurrentTenantContext $currentTenant,
|
||||||
|
CookieJar $cookies,
|
||||||
|
): JsonResponse {
|
||||||
|
$tenant = $currentTenant->model();
|
||||||
|
$response = response()->json([
|
||||||
|
'current' => $tenant === null ? null : TenantPayload::fromModel($tenant),
|
||||||
|
'logoUrl' => $this->logoEndpoint(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($tenant === null && $request->hasCookie($this->cookieName())) {
|
||||||
|
$response->withCookie($cookies->forget(
|
||||||
|
$this->cookieName(),
|
||||||
|
(string) config('nova-tenancy.cookie.path', '/'),
|
||||||
|
config('nova-tenancy.cookie.domain'),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(
|
||||||
|
Request $request,
|
||||||
|
TenantProvider $provider,
|
||||||
|
CurrentTenantContext $currentTenant,
|
||||||
|
CookieJar $cookies,
|
||||||
|
): JsonResponse {
|
||||||
|
$validated = $request->validate([
|
||||||
|
'tenant' => [
|
||||||
|
'required',
|
||||||
|
function (string $attribute, mixed $value, Closure $fail): void {
|
||||||
|
if ((! is_string($value) && ! is_int($value)) || strlen((string) $value) > 255) {
|
||||||
|
$fail("The {$attribute} field must be a valid tenant key.");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$tenant = $provider->find($request, $validated['tenant']);
|
||||||
|
abort_unless($tenant instanceof NovaTenant, 404);
|
||||||
|
|
||||||
|
$user = $request->user();
|
||||||
|
abort_unless($user instanceof Authenticatable, 401);
|
||||||
|
|
||||||
|
$previousTenantId = $currentTenant->id();
|
||||||
|
$currentTenant->activate($tenant);
|
||||||
|
|
||||||
|
TenantSelected::dispatch($user, $tenant, $previousTenantId);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'current' => TenantPayload::fromModel($tenant),
|
||||||
|
'logoUrl' => $this->logoEndpoint(),
|
||||||
|
'redirectTo' => NovaTenancy::landingPath($request, $tenant),
|
||||||
|
])->withCookie($cookies->make(
|
||||||
|
$this->cookieName(),
|
||||||
|
(string) $tenant->novaTenantKey(),
|
||||||
|
(int) config('nova-tenancy.cookie.minutes', 525600),
|
||||||
|
(string) config('nova-tenancy.cookie.path', '/'),
|
||||||
|
config('nova-tenancy.cookie.domain'),
|
||||||
|
config('nova-tenancy.cookie.secure'),
|
||||||
|
(bool) config('nova-tenancy.cookie.http_only', true),
|
||||||
|
false,
|
||||||
|
(string) config('nova-tenancy.cookie.same_site', 'lax'),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy(
|
||||||
|
Request $request,
|
||||||
|
CurrentTenantContext $currentTenant,
|
||||||
|
CookieJar $cookies,
|
||||||
|
): JsonResponse {
|
||||||
|
$user = $request->user();
|
||||||
|
abort_unless($user instanceof Authenticatable, 401);
|
||||||
|
|
||||||
|
$previousTenantId = $currentTenant->id();
|
||||||
|
$currentTenant->forget();
|
||||||
|
TenantCleared::dispatch($user, $previousTenantId);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'current' => null,
|
||||||
|
'redirectTo' => '/'.trim((string) config('nova-tenancy.routes.uri', 'nova-tenancy'), '/'),
|
||||||
|
])->withCookie($cookies->forget(
|
||||||
|
$this->cookieName(),
|
||||||
|
(string) config('nova-tenancy.cookie.path', '/'),
|
||||||
|
config('nova-tenancy.cookie.domain'),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function cookieName(): string
|
||||||
|
{
|
||||||
|
return (string) config('nova-tenancy.cookie.name', 'nova_tenant');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function logoEndpoint(): string
|
||||||
|
{
|
||||||
|
return '/'.trim((string) config('nova-tenancy.routes.api_prefix'), '/').'/current/logo';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Http\Controllers;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Routing\Controller;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\CurrentTenantContext;
|
||||||
|
use MeghdadFadaee\NovaTenancy\NovaTenancy;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Support\LogoUrl;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
class CurrentTenantLogoController extends Controller
|
||||||
|
{
|
||||||
|
public function __invoke(Request $request, CurrentTenantContext $currentTenant): Response
|
||||||
|
{
|
||||||
|
$tenant = $currentTenant->model();
|
||||||
|
|
||||||
|
if ($tenant !== null && ($response = NovaTenancy::logoResponse($request, $tenant)) !== null) {
|
||||||
|
return $this->withoutCaching($response);
|
||||||
|
}
|
||||||
|
|
||||||
|
$logoUrl = $tenant?->novaTenantLogoUrl() ?: config('nova-tenancy.branding.fallback_logo');
|
||||||
|
|
||||||
|
if (($logoUrl = LogoUrl::normalize($logoUrl)) !== null) {
|
||||||
|
$response = str_starts_with($logoUrl, '/')
|
||||||
|
? redirect()->to($logoUrl)
|
||||||
|
: redirect()->away($logoUrl);
|
||||||
|
|
||||||
|
return $this->withoutCaching($response);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->withoutCaching(response(
|
||||||
|
$this->fallbackSvg($tenant?->novaTenantTitle()),
|
||||||
|
200,
|
||||||
|
['Content-Type' => 'image/svg+xml; charset=UTF-8'],
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function fallbackSvg(?string $title): string
|
||||||
|
{
|
||||||
|
$initial = Str::upper(Str::substr(trim((string) $title), 0, 1));
|
||||||
|
$initial = htmlspecialchars($initial !== '' ? $initial : 'T', ENT_QUOTES | ENT_XML1, 'UTF-8');
|
||||||
|
|
||||||
|
return <<<SVG
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96" role="img">
|
||||||
|
<defs><linearGradient id="g" x1="0" y1="0" x2="1" y2="1"><stop stop-color="#7c3aed"/><stop offset="1" stop-color="#2563eb"/></linearGradient></defs>
|
||||||
|
<rect width="96" height="96" rx="24" fill="url(#g)"/>
|
||||||
|
<text x="48" y="61" text-anchor="middle" font-family="ui-sans-serif,system-ui,sans-serif" font-size="42" font-weight="700" fill="white">{$initial}</text>
|
||||||
|
</svg>
|
||||||
|
SVG;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function withoutCaching(Response $response): Response
|
||||||
|
{
|
||||||
|
$response->headers->set('Cache-Control', 'private, no-store, max-age=0');
|
||||||
|
$response->headers->set('Pragma', 'no-cache');
|
||||||
|
$response->headers->set('X-Content-Type-Options', 'nosniff');
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Http\Controllers;
|
||||||
|
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Routing\Controller;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\CurrentTenantContext;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\TenantProvider;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Support\TenantPayload;
|
||||||
|
|
||||||
|
class TenantIndexController extends Controller
|
||||||
|
{
|
||||||
|
public function __invoke(
|
||||||
|
Request $request,
|
||||||
|
TenantProvider $provider,
|
||||||
|
CurrentTenantContext $currentTenant,
|
||||||
|
): JsonResponse {
|
||||||
|
$maximum = max(1, (int) config('nova-tenancy.selection.max_per_page', 48));
|
||||||
|
$validated = $request->validate([
|
||||||
|
'search' => ['nullable', 'string', 'max:200'],
|
||||||
|
'page' => ['nullable', 'integer', 'min:1'],
|
||||||
|
'perPage' => ['nullable', 'integer', 'min:1', "max:{$maximum}"],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$search = trim((string) ($validated['search'] ?? ''));
|
||||||
|
$perPage = min(
|
||||||
|
$maximum,
|
||||||
|
max(1, (int) ($validated['perPage'] ?? config('nova-tenancy.selection.per_page', 12))),
|
||||||
|
);
|
||||||
|
$query = $provider->query($request);
|
||||||
|
|
||||||
|
if ($search !== '') {
|
||||||
|
$query = $provider->search($request, $query, $search);
|
||||||
|
}
|
||||||
|
|
||||||
|
$paginator = $query->paginate($perPage)->through(TenantPayload::fromModel(...));
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'data' => $paginator->items(),
|
||||||
|
'meta' => [
|
||||||
|
'currentPage' => $paginator->currentPage(),
|
||||||
|
'lastPage' => $paginator->lastPage(),
|
||||||
|
'perPage' => $paginator->perPage(),
|
||||||
|
'total' => $paginator->total(),
|
||||||
|
],
|
||||||
|
'currentKey' => $currentTenant->id(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Http\Middleware;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Laravel\Nova\Nova;
|
||||||
|
use Laravel\Nova\Tool;
|
||||||
|
use MeghdadFadaee\NovaTenancy\NovaTenancyTool;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
class Authorize
|
||||||
|
{
|
||||||
|
/** @param Closure(Request): Response $next */
|
||||||
|
public function handle(Request $request, Closure $next): Response
|
||||||
|
{
|
||||||
|
$tool = collect(Nova::registeredTools())->first($this->matchesTool(...));
|
||||||
|
|
||||||
|
abort_if($tool === null, 404);
|
||||||
|
abort_unless($tool->authorize($request), 403);
|
||||||
|
|
||||||
|
return $next($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function matchesTool(Tool $tool): bool
|
||||||
|
{
|
||||||
|
return $tool instanceof NovaTenancyTool;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Http\Middleware;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\CurrentTenantContext;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
class RequireTenant
|
||||||
|
{
|
||||||
|
public function __construct(protected CurrentTenantContext $currentTenant) {}
|
||||||
|
|
||||||
|
/** @param Closure(Request): Response $next */
|
||||||
|
public function handle(Request $request, Closure $next): Response
|
||||||
|
{
|
||||||
|
$this->currentTenant->requireModel();
|
||||||
|
|
||||||
|
return $next($request);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Nova;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Laravel\Nova\Http\Requests\NovaRequest;
|
||||||
|
use Laravel\Nova\Resource;
|
||||||
|
use Laravel\Scout\Builder as ScoutBuilder;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Concerns\InteractsWithCurrentTenant;
|
||||||
|
|
||||||
|
abstract class TenantResource extends Resource
|
||||||
|
{
|
||||||
|
use InteractsWithCurrentTenant;
|
||||||
|
|
||||||
|
final public static function indexQuery(NovaRequest $request, Builder $query): Builder
|
||||||
|
{
|
||||||
|
return static::afterTenantScope($request, static::tenantScope(parent::indexQuery($request, $query)), 'index');
|
||||||
|
}
|
||||||
|
|
||||||
|
final public static function detailQuery(NovaRequest $request, Builder $query): Builder
|
||||||
|
{
|
||||||
|
return static::afterTenantScope($request, static::tenantScope(parent::detailQuery($request, $query)), 'detail');
|
||||||
|
}
|
||||||
|
|
||||||
|
final public static function editQuery(NovaRequest $request, Builder $query): Builder
|
||||||
|
{
|
||||||
|
return static::afterTenantScope($request, static::tenantScope(parent::editQuery($request, $query)), 'edit');
|
||||||
|
}
|
||||||
|
|
||||||
|
final public static function replicateQuery(NovaRequest $request, Builder $query): Builder
|
||||||
|
{
|
||||||
|
return static::afterTenantScope($request, static::tenantScope(parent::replicateQuery($request, $query)), 'replicate');
|
||||||
|
}
|
||||||
|
|
||||||
|
final public static function relatableQuery(NovaRequest $request, Builder $query): Builder
|
||||||
|
{
|
||||||
|
return static::afterTenantScope($request, static::tenantScope(parent::relatableQuery($request, $query)), 'relatable');
|
||||||
|
}
|
||||||
|
|
||||||
|
final public static function scoutQuery(NovaRequest $request, ScoutBuilder $query): ScoutBuilder
|
||||||
|
{
|
||||||
|
return static::afterTenantScoutScope(
|
||||||
|
$request,
|
||||||
|
parent::scoutQuery($request, $query)->where(static::tenantColumn(), static::currentTenantContext()->requireId()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final public static function fill(NovaRequest $request, $model): array
|
||||||
|
{
|
||||||
|
[$model, $callbacks] = parent::fill($request, $model);
|
||||||
|
$model->setAttribute(static::tenantColumn(), static::currentTenantContext()->requireId());
|
||||||
|
|
||||||
|
return static::afterTenantFill($request, $model, $callbacks, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
final public static function fillForUpdate(NovaRequest $request, $model): array
|
||||||
|
{
|
||||||
|
[$model, $callbacks] = parent::fillForUpdate($request, $model);
|
||||||
|
$model->setAttribute(static::tenantColumn(), static::currentTenantContext()->requireId());
|
||||||
|
|
||||||
|
return static::afterTenantFill($request, $model, $callbacks, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function tenantColumn(): string
|
||||||
|
{
|
||||||
|
return (string) config('nova-tenancy.resources.tenant_column', 'tenant_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function tenantScope(Builder $query): Builder
|
||||||
|
{
|
||||||
|
return static::scopeToCurrentTenant($query, static::tenantColumn());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function afterTenantScope(
|
||||||
|
NovaRequest $request,
|
||||||
|
Builder $query,
|
||||||
|
string $operation,
|
||||||
|
): Builder {
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function afterTenantScoutScope(NovaRequest $request, ScoutBuilder $query): ScoutBuilder
|
||||||
|
{
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<int, callable> $callbacks
|
||||||
|
* @return array{Model, array<int, callable>}
|
||||||
|
*/
|
||||||
|
protected static function afterTenantFill(
|
||||||
|
NovaRequest $request,
|
||||||
|
$model,
|
||||||
|
array $callbacks,
|
||||||
|
bool $updating,
|
||||||
|
): array {
|
||||||
|
return [$model, $callbacks];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Laravel\Nova\Menu\MenuItem;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\CurrentTenantContext;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\NovaTenant;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
final class NovaTenancy
|
||||||
|
{
|
||||||
|
/** @var (Closure(Request): bool)|null */
|
||||||
|
protected static ?Closure $authorizationCallback = null;
|
||||||
|
|
||||||
|
/** @var (Closure(Request, Model&NovaTenant): string)|null */
|
||||||
|
protected static ?Closure $landingCallback = null;
|
||||||
|
|
||||||
|
/** @var (Closure(Request, Model&NovaTenant): Response)|null */
|
||||||
|
protected static ?Closure $logoResponseCallback = null;
|
||||||
|
|
||||||
|
public static function current(): CurrentTenantContext
|
||||||
|
{
|
||||||
|
return app(CurrentTenantContext::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function id(): int|string|null
|
||||||
|
{
|
||||||
|
return self::current()->id();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return (Model&NovaTenant)|null */
|
||||||
|
public static function tenant(): ?Model
|
||||||
|
{
|
||||||
|
return self::current()->model();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param Closure(Request): bool $callback */
|
||||||
|
public static function authorizeUsing(Closure $callback): void
|
||||||
|
{
|
||||||
|
self::$authorizationCallback = $callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function authorizedToSee(Request $request): bool
|
||||||
|
{
|
||||||
|
return self::$authorizationCallback === null
|
||||||
|
? $request->user() !== null
|
||||||
|
: (bool) (self::$authorizationCallback)($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param Closure(Request, Model&NovaTenant): string $callback */
|
||||||
|
public static function landingUsing(Closure $callback): void
|
||||||
|
{
|
||||||
|
self::$landingCallback = $callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param Model&NovaTenant $tenant */
|
||||||
|
public static function landingPath(Request $request, Model $tenant): string
|
||||||
|
{
|
||||||
|
$path = self::$landingCallback === null
|
||||||
|
? (string) config('nova-tenancy.selection.landing_path', '/')
|
||||||
|
: (string) (self::$landingCallback)($request, $tenant);
|
||||||
|
|
||||||
|
if ($path === '' || str_starts_with($path, '//') || filter_var($path, FILTER_VALIDATE_URL)) {
|
||||||
|
return '/';
|
||||||
|
}
|
||||||
|
|
||||||
|
return '/'.ltrim($path, '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param Closure(Request, Model&NovaTenant): Response $callback */
|
||||||
|
public static function logoResponseUsing(Closure $callback): void
|
||||||
|
{
|
||||||
|
self::$logoResponseCallback = $callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param Model&NovaTenant $tenant */
|
||||||
|
public static function logoResponse(Request $request, Model $tenant): ?Response
|
||||||
|
{
|
||||||
|
return self::$logoResponseCallback === null
|
||||||
|
? null
|
||||||
|
: (self::$logoResponseCallback)($request, $tenant);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function userMenuItem(): MenuItem
|
||||||
|
{
|
||||||
|
$path = '/'.trim((string) config('nova-tenancy.routes.uri', 'nova-tenancy'), '/');
|
||||||
|
|
||||||
|
return MenuItem::link(self::menuLabel(), $path)
|
||||||
|
->canSee(fn (Request $request): bool => self::authorizedToSee($request));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function menuLabel(): string
|
||||||
|
{
|
||||||
|
$label = config('nova-tenancy.menu.label');
|
||||||
|
|
||||||
|
return is_string($label) && $label !== ''
|
||||||
|
? __($label)
|
||||||
|
: __('nova-tenancy::nova-tenancy.menu.label');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function flushState(): void
|
||||||
|
{
|
||||||
|
self::$authorizationCallback = null;
|
||||||
|
self::$landingCallback = null;
|
||||||
|
self::$logoResponseCallback = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,103 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Route;
|
||||||
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use Laravel\Nova\Events\ServingNova;
|
||||||
|
use Laravel\Nova\Nova;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\CurrentTenantContext;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\TenantProvider;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Http\Middleware\Authorize;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Support\TenantPayload;
|
||||||
|
|
||||||
|
class NovaTenancyServiceProvider extends ServiceProvider
|
||||||
|
{
|
||||||
|
public function register(): void
|
||||||
|
{
|
||||||
|
$this->mergeConfigFrom(__DIR__.'/../config/nova-tenancy.php', 'nova-tenancy');
|
||||||
|
|
||||||
|
$this->app->scoped(TenantProvider::class, function ($app): TenantProvider {
|
||||||
|
$provider = $app->make((string) config('nova-tenancy.provider'));
|
||||||
|
|
||||||
|
if (! $provider instanceof TenantProvider) {
|
||||||
|
throw new InvalidArgumentException('The configured Nova tenancy provider must implement '.TenantProvider::class.'.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $provider;
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->app->scoped(CurrentTenant::class, fn ($app): CurrentTenant => new CurrentTenant(
|
||||||
|
$app->make(TenantProvider::class),
|
||||||
|
$app->make('request'),
|
||||||
|
));
|
||||||
|
$this->app->alias(CurrentTenant::class, CurrentTenantContext::class);
|
||||||
|
|
||||||
|
$this->app->scoped('CurrentTenantId', fn ($app): int|string|null => $app->make(CurrentTenantContext::class)->id());
|
||||||
|
$this->app->scoped('CurrentTenantModel', fn ($app) => $app->make(CurrentTenantContext::class)->model());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function boot(): void
|
||||||
|
{
|
||||||
|
$this->loadTranslationsFrom(__DIR__.'/../lang', 'nova-tenancy');
|
||||||
|
|
||||||
|
$this->publishes([
|
||||||
|
__DIR__.'/../config/nova-tenancy.php' => config_path('nova-tenancy.php'),
|
||||||
|
], 'nova-tenancy-config');
|
||||||
|
$this->publishes([
|
||||||
|
__DIR__.'/../lang' => lang_path('vendor/nova-tenancy'),
|
||||||
|
], 'nova-tenancy-translations');
|
||||||
|
|
||||||
|
Nova::tools([new NovaTenancyTool]);
|
||||||
|
|
||||||
|
$this->app->booted(fn () => $this->routes());
|
||||||
|
|
||||||
|
Nova::serving(function (ServingNova $event): void {
|
||||||
|
$tenant = app(CurrentTenantContext::class)->model();
|
||||||
|
|
||||||
|
Nova::provideToScript([
|
||||||
|
'novaTenancy' => [
|
||||||
|
'current' => $tenant === null ? null : TenantPayload::fromModel($tenant),
|
||||||
|
'apiBase' => '/'.trim((string) config('nova-tenancy.routes.api_prefix'), '/'),
|
||||||
|
'selectorPath' => '/'.trim((string) config('nova-tenancy.routes.uri'), '/'),
|
||||||
|
'searchDebounce' => (int) config('nova-tenancy.selection.search_debounce', 300),
|
||||||
|
'perPage' => (int) config('nova-tenancy.selection.per_page', 12),
|
||||||
|
'branding' => config('nova-tenancy.branding'),
|
||||||
|
'copy' => [
|
||||||
|
'pageTitle' => __('nova-tenancy::nova-tenancy.page.title'),
|
||||||
|
'pageKicker' => __('nova-tenancy::nova-tenancy.page.kicker'),
|
||||||
|
'pageDescription' => __('nova-tenancy::nova-tenancy.page.description'),
|
||||||
|
'currentTenant' => __('nova-tenancy::nova-tenancy.page.current'),
|
||||||
|
'search' => __('nova-tenancy::nova-tenancy.page.search'),
|
||||||
|
'searchPlaceholder' => __('nova-tenancy::nova-tenancy.page.search_placeholder'),
|
||||||
|
'select' => __('nova-tenancy::nova-tenancy.page.select'),
|
||||||
|
'selected' => __('nova-tenancy::nova-tenancy.page.selected'),
|
||||||
|
'emptyTitle' => __('nova-tenancy::nova-tenancy.page.empty_title'),
|
||||||
|
'emptyDescription' => __('nova-tenancy::nova-tenancy.page.empty_description'),
|
||||||
|
'error' => __('nova-tenancy::nova-tenancy.page.error'),
|
||||||
|
'retry' => __('nova-tenancy::nova-tenancy.page.retry'),
|
||||||
|
'previous' => __('nova-tenancy::nova-tenancy.page.previous'),
|
||||||
|
'next' => __('nova-tenancy::nova-tenancy.page.next'),
|
||||||
|
'selectTenant' => __('nova-tenancy::nova-tenancy.page.select_tenant'),
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function routes(): void
|
||||||
|
{
|
||||||
|
if ($this->app->routesAreCached()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Nova::router(['nova', 'nova.auth', Authorize::class], trim((string) config('nova-tenancy.routes.uri'), '/'))
|
||||||
|
->group(__DIR__.'/../routes/inertia.php');
|
||||||
|
|
||||||
|
Route::middleware(['nova', 'nova.auth', Authorize::class])
|
||||||
|
->prefix(trim((string) config('nova-tenancy.routes.api_prefix'), '/'))
|
||||||
|
->as('nova-tenancy.api.')
|
||||||
|
->group(__DIR__.'/../routes/api.php');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Laravel\Nova\Menu\MenuSection;
|
||||||
|
use Laravel\Nova\Nova;
|
||||||
|
use Laravel\Nova\Tool;
|
||||||
|
|
||||||
|
class NovaTenancyTool extends Tool
|
||||||
|
{
|
||||||
|
public function authorize(Request $request): bool
|
||||||
|
{
|
||||||
|
return parent::authorize($request) && NovaTenancy::authorizedToSee($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function boot(): void
|
||||||
|
{
|
||||||
|
Nova::mix('nova-tenancy', __DIR__.'/../dist/mix-manifest.json');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function menu(Request $request): ?MenuSection
|
||||||
|
{
|
||||||
|
if (! config('nova-tenancy.menu.sidebar', false)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return MenuSection::make(NovaTenancy::menuLabel())
|
||||||
|
->path('/'.trim((string) config('nova-tenancy.routes.uri', 'nova-tenancy'), '/'))
|
||||||
|
->icon((string) config('nova-tenancy.menu.icon', 'building-office-2'));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Providers;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\TenantProvider;
|
||||||
|
|
||||||
|
abstract class EloquentTenantProvider implements TenantProvider
|
||||||
|
{
|
||||||
|
public function search(Request $request, Builder $query, string $search): Builder
|
||||||
|
{
|
||||||
|
$columns = $this->searchColumns($request);
|
||||||
|
|
||||||
|
if ($search === '' || $columns === []) {
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query->where(function ($query) use ($columns, $search): void {
|
||||||
|
foreach ($columns as $column) {
|
||||||
|
$query->orWhere($column, 'like', "%{$search}%");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function find(Request $request, int|string $key): ?Model
|
||||||
|
{
|
||||||
|
$query = $this->query($request);
|
||||||
|
$model = $query->getModel();
|
||||||
|
|
||||||
|
return $query->where(
|
||||||
|
$model->qualifyColumn($this->keyColumn($request, $model)),
|
||||||
|
$key,
|
||||||
|
)->first();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return array<int, string> */
|
||||||
|
protected function searchColumns(Request $request): array
|
||||||
|
{
|
||||||
|
return array_values(array_filter(
|
||||||
|
(array) config('nova-tenancy.model.search_columns', ['name']),
|
||||||
|
static fn (mixed $column): bool => is_string($column) && $column !== '',
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function keyColumn(Request $request, Model $model): string
|
||||||
|
{
|
||||||
|
return (string) (config('nova-tenancy.model.key_column') ?: $model->getRouteKeyName());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Providers;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use LogicException;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\ProvidesNovaTenants;
|
||||||
|
|
||||||
|
class UserTenantProvider extends EloquentTenantProvider
|
||||||
|
{
|
||||||
|
public function query(Request $request): Builder
|
||||||
|
{
|
||||||
|
$user = $request->user();
|
||||||
|
|
||||||
|
if (! $user instanceof ProvidesNovaTenants) {
|
||||||
|
throw new LogicException(sprintf(
|
||||||
|
'The authenticated user must implement [%s], or configure a custom tenant provider.',
|
||||||
|
ProvidesNovaTenants::class,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $user->novaTenantQuery($request);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Support;
|
||||||
|
|
||||||
|
final class LogoUrl
|
||||||
|
{
|
||||||
|
public static function normalize(mixed $url): ?string
|
||||||
|
{
|
||||||
|
if (! is_string($url) || $url === '') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (str_starts_with($url, '/') && ! str_starts_with($url, '//')) {
|
||||||
|
return $url;
|
||||||
|
}
|
||||||
|
|
||||||
|
return in_array(parse_url($url, PHP_URL_SCHEME), ['http', 'https'], true)
|
||||||
|
? $url
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Support;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\NovaTenant;
|
||||||
|
|
||||||
|
final class TenantPayload
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param Model&NovaTenant $tenant
|
||||||
|
* @return array{key: int|string, title: string, logoUrl: string|null, description: string|null, badge: string|null, accent: string|null}
|
||||||
|
*/
|
||||||
|
public static function fromModel(Model $tenant): array
|
||||||
|
{
|
||||||
|
if (! $tenant instanceof NovaTenant) {
|
||||||
|
throw new InvalidArgumentException(sprintf(
|
||||||
|
'Tenant model [%s] must implement [%s].',
|
||||||
|
$tenant::class,
|
||||||
|
NovaTenant::class,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
$meta = $tenant->novaTenantMeta();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'key' => $tenant->novaTenantKey(),
|
||||||
|
'title' => $tenant->novaTenantTitle(),
|
||||||
|
'logoUrl' => LogoUrl::normalize($tenant->novaTenantLogoUrl()),
|
||||||
|
'description' => self::nullableString($meta['description'] ?? null),
|
||||||
|
'badge' => self::nullableString($meta['badge'] ?? null),
|
||||||
|
'accent' => self::validAccent($meta['accent'] ?? null),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function nullableString(mixed $value): ?string
|
||||||
|
{
|
||||||
|
return is_string($value) && $value !== '' ? $value : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function validAccent(mixed $value): ?string
|
||||||
|
{
|
||||||
|
if (! is_string($value)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return preg_match('/^#[0-9a-f]{6}$/i', $value) === 1 ? $value : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
import { flushPromises, mount } from '@vue/test-utils'
|
||||||
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
|
import NovaTenancyPage from '../resources/js/pages/NovaTenancy.vue'
|
||||||
|
|
||||||
|
describe('NovaTenancy page', () => {
|
||||||
|
let get
|
||||||
|
let post
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
get = vi.fn().mockResolvedValue({
|
||||||
|
data: {
|
||||||
|
data: [
|
||||||
|
{
|
||||||
|
key: 2,
|
||||||
|
title: 'Beta',
|
||||||
|
logoUrl: null,
|
||||||
|
description: 'A focused workspace',
|
||||||
|
badge: 'Pro',
|
||||||
|
accent: '#2563eb',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
currentKey: 1,
|
||||||
|
meta: { currentPage: 1, lastPage: 1, perPage: 12, total: 1 },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
post = vi.fn().mockResolvedValue({
|
||||||
|
data: {
|
||||||
|
current: { key: 2, title: 'Beta' },
|
||||||
|
redirectTo: '/dashboards/main',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
globalThis.Nova = {
|
||||||
|
config: key =>
|
||||||
|
key === 'novaTenancy'
|
||||||
|
? {
|
||||||
|
current: { key: 1, title: 'Alpha' },
|
||||||
|
apiBase: '/nova-vendor/nova-tenancy',
|
||||||
|
searchDebounce: 1,
|
||||||
|
copy: {
|
||||||
|
pageTitle: 'Choose workspace',
|
||||||
|
pageKicker: 'Tenant context',
|
||||||
|
pageDescription: 'Choose where to work.',
|
||||||
|
currentTenant: 'Current',
|
||||||
|
search: 'Search',
|
||||||
|
searchPlaceholder: 'Search…',
|
||||||
|
select: 'Open',
|
||||||
|
selected: 'Selected',
|
||||||
|
emptyTitle: 'Empty',
|
||||||
|
emptyDescription: 'No results',
|
||||||
|
error: 'Error',
|
||||||
|
retry: 'Retry',
|
||||||
|
previous: 'Previous',
|
||||||
|
next: 'Next',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: null,
|
||||||
|
request: () => ({ get, post }),
|
||||||
|
visit: vi.fn(),
|
||||||
|
$emit: vi.fn(),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('loads an authorized page and switches tenant through Nova navigation', async () => {
|
||||||
|
const wrapper = mount(NovaTenancyPage, {
|
||||||
|
global: { stubs: { Head: true } },
|
||||||
|
})
|
||||||
|
await flushPromises()
|
||||||
|
|
||||||
|
expect(wrapper.text()).toContain('Beta')
|
||||||
|
expect(wrapper.text()).toContain('A focused workspace')
|
||||||
|
|
||||||
|
await wrapper.get('.nova-tenancy-select').trigger('click')
|
||||||
|
await flushPromises()
|
||||||
|
|
||||||
|
expect(post).toHaveBeenCalledWith('/nova-vendor/nova-tenancy/current', {
|
||||||
|
tenant: '2',
|
||||||
|
})
|
||||||
|
expect(Nova.$emit).toHaveBeenCalledWith(
|
||||||
|
'nova-tenancy:changed',
|
||||||
|
expect.objectContaining({ current: { key: 2, title: 'Beta' } })
|
||||||
|
)
|
||||||
|
expect(Nova.visit).toHaveBeenCalledWith('/dashboards/main')
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
import { flushPromises, mount } from '@vue/test-utils'
|
||||||
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
|
import TenantAppLogo from '../resources/js/components/TenantAppLogo.vue'
|
||||||
|
|
||||||
|
describe('TenantAppLogo', () => {
|
||||||
|
let listeners
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
listeners = {}
|
||||||
|
globalThis.Nova = {
|
||||||
|
config: key =>
|
||||||
|
key === 'novaTenancy'
|
||||||
|
? {
|
||||||
|
current: { key: 1, title: 'Alpha' },
|
||||||
|
apiBase: '/nova-vendor/nova-tenancy',
|
||||||
|
selectorPath: '/nova-tenancy',
|
||||||
|
branding: { show_logo: true, show_title: true, link: 'selector' },
|
||||||
|
copy: { selectTenant: 'Select tenant' },
|
||||||
|
}
|
||||||
|
: null,
|
||||||
|
request: () => ({
|
||||||
|
get: vi.fn().mockResolvedValue({
|
||||||
|
data: { current: { key: 1, title: 'Alpha' } },
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
visit: vi.fn(),
|
||||||
|
$on: vi.fn((event, callback) => {
|
||||||
|
listeners[event] = callback
|
||||||
|
}),
|
||||||
|
$off: vi.fn(),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('renders and reactively updates the selected tenant brand', async () => {
|
||||||
|
const wrapper = mount(TenantAppLogo)
|
||||||
|
await flushPromises()
|
||||||
|
|
||||||
|
expect(wrapper.text()).toContain('Alpha')
|
||||||
|
expect(wrapper.get('img').attributes('src')).toContain('/current/logo?tenant=1')
|
||||||
|
|
||||||
|
listeners['nova-tenancy:changed']({ current: { key: 2, title: 'Beta' } })
|
||||||
|
await wrapper.vm.$nextTick()
|
||||||
|
|
||||||
|
expect(wrapper.text()).toContain('Beta')
|
||||||
|
expect(wrapper.get('img').attributes('src')).toContain('/current/logo?tenant=2')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('uses Nova SPA navigation when the brand is clicked', async () => {
|
||||||
|
const wrapper = mount(TenantAppLogo)
|
||||||
|
await wrapper.get('.nova-tenancy-brand').trigger('click')
|
||||||
|
|
||||||
|
expect(Nova.visit).toHaveBeenCalledWith('/nova-tenancy')
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import { describe, expect, it } from 'vitest'
|
||||||
|
import {
|
||||||
|
currentLogoUrl,
|
||||||
|
sameTenant,
|
||||||
|
tenantInitial,
|
||||||
|
tenantStyle,
|
||||||
|
} from '../resources/js/tenantState'
|
||||||
|
|
||||||
|
describe('tenant state helpers', () => {
|
||||||
|
it('compares numeric and string tenant keys consistently', () => {
|
||||||
|
expect(sameTenant(12, '12')).toBe(true)
|
||||||
|
expect(sameTenant(null, null)).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('builds safe presentation fallbacks', () => {
|
||||||
|
expect(tenantInitial(' alpha')).toBe('A')
|
||||||
|
expect(tenantStyle({ accent: null })).toEqual({ '--tenant-accent': '#7c3aed' })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('creates a cache-busted fixed logo endpoint', () => {
|
||||||
|
expect(currentLogoUrl('/nova-vendor/nova-tenancy', 'a/b', 42)).toBe(
|
||||||
|
'/nova-vendor/nova-tenancy/current/logo?tenant=a%2Fb&v=42'
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\CurrentTenantContext;
|
||||||
|
use MeghdadFadaee\NovaTenancy\CurrentTenant;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Exceptions\TenantNotSelected;
|
||||||
|
use MeghdadFadaee\NovaTenancy\NovaTenancy;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Providers\UserTenantProvider;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\Tenant;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\User;
|
||||||
|
|
||||||
|
it('resolves only an accessible tenant from the cookie', function (): void {
|
||||||
|
$user = User::query()->create(['name' => 'Ada']);
|
||||||
|
$otherUser = User::query()->create(['name' => 'Grace']);
|
||||||
|
$tenant = Tenant::query()->create(['user_id' => $user->id, 'title' => 'North']);
|
||||||
|
$inaccessible = Tenant::query()->create(['user_id' => $otherUser->id, 'title' => 'South']);
|
||||||
|
|
||||||
|
$request = Request::create('/');
|
||||||
|
$request->setUserResolver(fn () => $user);
|
||||||
|
$request->cookies->set('nova_tenant', (string) $tenant->id);
|
||||||
|
$context = new CurrentTenant(new UserTenantProvider, $request);
|
||||||
|
|
||||||
|
expect($context->id())->toBe($tenant->id)
|
||||||
|
->and($context->model()?->is($tenant))->toBeTrue();
|
||||||
|
|
||||||
|
$request = Request::create('/');
|
||||||
|
$request->setUserResolver(fn () => $user);
|
||||||
|
$request->cookies->set('nova_tenant', (string) $inaccessible->id);
|
||||||
|
|
||||||
|
expect((new CurrentTenant(new UserTenantProvider, $request))->model())->toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('provides scoped string bindings and strict accessors', function (): void {
|
||||||
|
expect(app('CurrentTenantId'))->toBeNull()
|
||||||
|
->and(app('CurrentTenantModel'))->toBeNull()
|
||||||
|
->and(fn () => app(CurrentTenantContext::class)->requireModel())
|
||||||
|
->toThrow(TenantNotSelected::class);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('temporarily activates a tenant and restores the previous context', function (): void {
|
||||||
|
$user = User::query()->create(['name' => 'Ada']);
|
||||||
|
$first = Tenant::query()->create(['user_id' => $user->id, 'title' => 'First']);
|
||||||
|
$second = Tenant::query()->create(['user_id' => $user->id, 'title' => 'Second']);
|
||||||
|
$request = Request::create('/');
|
||||||
|
$request->setUserResolver(fn () => $user);
|
||||||
|
$context = new CurrentTenant(new UserTenantProvider, $request);
|
||||||
|
$context->activate($first);
|
||||||
|
|
||||||
|
$inside = $context->run($second, fn () => $context->id());
|
||||||
|
|
||||||
|
expect($inside)->toBe($second->id)
|
||||||
|
->and($context->id())->toBe($first->id);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('builds an official nova user menu item without replacing the menu callback', function (): void {
|
||||||
|
$item = NovaTenancy::userMenuItem();
|
||||||
|
|
||||||
|
expect($item->path)->toBe('/nova-tenancy')
|
||||||
|
->and((string) $item->name)->toBe('Select tenant');
|
||||||
|
});
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Event;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Events\TenantCleared;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Events\TenantSelected;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\Tenant;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\User;
|
||||||
|
|
||||||
|
it('searches and paginates only the authenticated users tenants', function (): void {
|
||||||
|
$user = User::query()->create(['name' => 'Ada']);
|
||||||
|
$otherUser = User::query()->create(['name' => 'Grace']);
|
||||||
|
Tenant::query()->create(['user_id' => $user->id, 'title' => 'Alpha']);
|
||||||
|
Tenant::query()->create(['user_id' => $user->id, 'title' => 'Beta']);
|
||||||
|
Tenant::query()->create(['user_id' => $otherUser->id, 'title' => 'Alpha Secret']);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->getJson('/testing/tenants?search=Alpha&perPage=1')
|
||||||
|
->assertOk()
|
||||||
|
->assertJsonPath('meta.total', 1)
|
||||||
|
->assertJsonPath('data.0.title', 'Alpha');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('selects an accessible tenant and returns a safe spa destination', function (): void {
|
||||||
|
Event::fake();
|
||||||
|
$user = User::query()->create(['name' => 'Ada']);
|
||||||
|
$tenant = Tenant::query()->create(['user_id' => $user->id, 'title' => 'Alpha']);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->postJson('/testing/current', ['tenant' => $tenant->id])
|
||||||
|
->assertOk()
|
||||||
|
->assertJsonPath('current.key', $tenant->id)
|
||||||
|
->assertJsonPath('redirectTo', '/')
|
||||||
|
->assertCookie('nova_tenant', (string) $tenant->id);
|
||||||
|
|
||||||
|
Event::assertDispatched(TenantSelected::class, fn (TenantSelected $event): bool => $event->tenant->is($tenant));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not reveal or select another users tenant', function (): void {
|
||||||
|
$user = User::query()->create(['name' => 'Ada']);
|
||||||
|
$otherUser = User::query()->create(['name' => 'Grace']);
|
||||||
|
$tenant = Tenant::query()->create(['user_id' => $otherUser->id, 'title' => 'Secret']);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->postJson('/testing/current', ['tenant' => (string) $tenant->id])
|
||||||
|
->assertNotFound()
|
||||||
|
->assertCookieMissing('nova_tenant');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('clears the selection and dispatches an event', function (): void {
|
||||||
|
Event::fake();
|
||||||
|
$user = User::query()->create(['name' => 'Ada']);
|
||||||
|
$tenant = Tenant::query()->create(['user_id' => $user->id, 'title' => 'Alpha']);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->withCookie('nova_tenant', (string) $tenant->id)
|
||||||
|
->deleteJson('/testing/current')
|
||||||
|
->assertOk()
|
||||||
|
->assertCookieExpired('nova_tenant');
|
||||||
|
|
||||||
|
Event::assertDispatched(TenantCleared::class);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('redirects safe logos without caching them', function (): void {
|
||||||
|
$user = User::query()->create(['name' => 'Ada']);
|
||||||
|
$tenant = Tenant::query()->create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'title' => 'Alpha',
|
||||||
|
'logo_url' => 'https://cdn.example.com/alpha.png',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->withCookie('nova_tenant', (string) $tenant->id)
|
||||||
|
->get('/testing/current/logo')
|
||||||
|
->assertRedirect('https://cdn.example.com/alpha.png')
|
||||||
|
->assertHeader('Cache-Control', 'max-age=0, no-store, private');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('renders a fallback for unsafe logo schemes', function (): void {
|
||||||
|
$user = User::query()->create(['name' => 'Ada']);
|
||||||
|
$tenant = Tenant::query()->create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'title' => 'Alpha',
|
||||||
|
'logo_url' => 'javascript:alert(1)',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->withCookie('nova_tenant', (string) $tenant->id)
|
||||||
|
->get('/testing/current/logo')
|
||||||
|
->assertOk()
|
||||||
|
->assertHeader('Content-Type', 'image/svg+xml; charset=UTF-8');
|
||||||
|
});
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use MeghdadFadaee\NovaTenancy\Support\TenantPayload;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\Tenant;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\User;
|
||||||
|
|
||||||
|
it('normalizes model presentation and rejects unsafe accent values', function (): void {
|
||||||
|
$user = User::query()->create(['name' => 'Ada']);
|
||||||
|
$tenant = Tenant::query()->create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'title' => 'Alpha',
|
||||||
|
'description' => 'Primary workspace',
|
||||||
|
'logo_url' => 'javascript:alert(1)',
|
||||||
|
'accent' => 'red; background: url(evil)',
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(TenantPayload::fromModel($tenant))->toMatchArray([
|
||||||
|
'key' => $tenant->id,
|
||||||
|
'title' => 'Alpha',
|
||||||
|
'description' => 'Primary workspace',
|
||||||
|
'logoUrl' => null,
|
||||||
|
'accent' => null,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$tenant->accent = '#7c3aed';
|
||||||
|
expect(TenantPayload::fromModel($tenant)['accent'])->toBe('#7c3aed');
|
||||||
|
});
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Laravel\Nova\Http\Requests\NovaRequest;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\CurrentTenantContext;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Exceptions\TenantNotSelected;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\Tenant;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\TenantRecord;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\TenantRecordResource;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\User;
|
||||||
|
|
||||||
|
it('scopes all eloquent resource query entry points', function (): void {
|
||||||
|
$user = User::query()->create(['name' => 'Ada']);
|
||||||
|
$first = Tenant::query()->create(['user_id' => $user->id, 'title' => 'First']);
|
||||||
|
$second = Tenant::query()->create(['user_id' => $user->id, 'title' => 'Second']);
|
||||||
|
$visible = TenantRecord::query()->create(['tenant_id' => $first->id, 'name' => 'Visible']);
|
||||||
|
TenantRecord::query()->create(['tenant_id' => $second->id, 'name' => 'Hidden']);
|
||||||
|
app(CurrentTenantContext::class)->activate($first);
|
||||||
|
$request = NovaRequest::create('/nova-api/tenant-records');
|
||||||
|
|
||||||
|
foreach (['indexQuery', 'detailQuery', 'editQuery', 'replicateQuery', 'relatableQuery'] as $method) {
|
||||||
|
$ids = TenantRecordResource::{$method}($request, TenantRecord::query())->pluck('id')->all();
|
||||||
|
expect($ids)->toBe([$visible->id]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('stamps the selected tenant when creating and updating', function (): void {
|
||||||
|
$user = User::query()->create(['name' => 'Ada']);
|
||||||
|
$first = Tenant::query()->create(['user_id' => $user->id, 'title' => 'First']);
|
||||||
|
$second = Tenant::query()->create(['user_id' => $user->id, 'title' => 'Second']);
|
||||||
|
app(CurrentTenantContext::class)->activate($first);
|
||||||
|
$request = NovaRequest::create('/nova-api/tenant-records', 'POST');
|
||||||
|
|
||||||
|
[$newModel] = TenantRecordResource::fill($request, new TenantRecord(['tenant_id' => $second->id]));
|
||||||
|
expect($newModel->tenant_id)->toBe($first->id);
|
||||||
|
|
||||||
|
$existing = new TenantRecord(['tenant_id' => $second->id]);
|
||||||
|
[$updatedModel] = TenantRecordResource::fillForUpdate($request, $existing);
|
||||||
|
expect($updatedModel->tenant_id)->toBe($first->id);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects tenant aware resource queries without a selection', function (): void {
|
||||||
|
$request = NovaRequest::create('/nova-api/tenant-records');
|
||||||
|
|
||||||
|
expect(fn () => TenantRecordResource::indexQuery($request, TenantRecord::query()))
|
||||||
|
->toThrow(TenantNotSelected::class);
|
||||||
|
});
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Tests\Fixtures;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Concerns\HasNovaTenantPresentation;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\NovaTenant;
|
||||||
|
|
||||||
|
class Tenant extends Model implements NovaTenant
|
||||||
|
{
|
||||||
|
use HasNovaTenantPresentation;
|
||||||
|
|
||||||
|
protected $guarded = [];
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Tests\Fixtures;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class TenantRecord extends Model
|
||||||
|
{
|
||||||
|
protected $guarded = [];
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Tests\Fixtures;
|
||||||
|
|
||||||
|
use Laravel\Nova\Fields\FieldCollection;
|
||||||
|
use Laravel\Nova\Http\Requests\NovaRequest;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Nova\TenantResource;
|
||||||
|
|
||||||
|
class TenantRecordResource extends TenantResource
|
||||||
|
{
|
||||||
|
public static $model = TenantRecord::class;
|
||||||
|
|
||||||
|
public function fields(NovaRequest $request): array
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function creationFields(NovaRequest $request): FieldCollection
|
||||||
|
{
|
||||||
|
return new FieldCollection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updateFields(NovaRequest $request): FieldCollection
|
||||||
|
{
|
||||||
|
return new FieldCollection;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Tests\Fixtures;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Contracts\ProvidesNovaTenants;
|
||||||
|
|
||||||
|
class User extends Authenticatable implements ProvidesNovaTenants
|
||||||
|
{
|
||||||
|
protected $guarded = [];
|
||||||
|
|
||||||
|
public function tenants(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(Tenant::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function novaTenantQuery(Request $request): Builder
|
||||||
|
{
|
||||||
|
return $this->tenants()->getQuery()->orderBy('title');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use MeghdadFadaee\NovaTenancy\Tests\TestCase;
|
||||||
|
|
||||||
|
uses(TestCase::class)->in('Feature');
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MeghdadFadaee\NovaTenancy\Tests;
|
||||||
|
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Route;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Laravel\Nova\NovaCoreServiceProvider;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Http\Controllers\CurrentTenantController;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Http\Controllers\CurrentTenantLogoController;
|
||||||
|
use MeghdadFadaee\NovaTenancy\Http\Controllers\TenantIndexController;
|
||||||
|
use MeghdadFadaee\NovaTenancy\NovaTenancy;
|
||||||
|
use MeghdadFadaee\NovaTenancy\NovaTenancyServiceProvider;
|
||||||
|
use Orchestra\Testbench\TestCase as Orchestra;
|
||||||
|
|
||||||
|
abstract class TestCase extends Orchestra
|
||||||
|
{
|
||||||
|
protected function getPackageProviders($app): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
NovaCoreServiceProvider::class,
|
||||||
|
NovaTenancyServiceProvider::class,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function defineEnvironment($app): void
|
||||||
|
{
|
||||||
|
$app['config']->set('app.key', 'base64:'.base64_encode(str_repeat('n', 32)));
|
||||||
|
$app['config']->set('database.default', 'testing');
|
||||||
|
$app['config']->set('database.connections.testing', [
|
||||||
|
'driver' => 'sqlite',
|
||||||
|
'database' => ':memory:',
|
||||||
|
'prefix' => '',
|
||||||
|
]);
|
||||||
|
$app['config']->set('auth.providers.users.model', Fixtures\User::class);
|
||||||
|
$app['config']->set('nova-tenancy.model.title_attribute', 'title');
|
||||||
|
$app['config']->set('nova-tenancy.model.logo_attribute', 'logo_url');
|
||||||
|
$app['config']->set('nova-tenancy.model.description_attribute', 'description');
|
||||||
|
$app['config']->set('nova-tenancy.model.accent_attribute', 'accent');
|
||||||
|
$app['config']->set('nova-tenancy.model.search_columns', ['title']);
|
||||||
|
$app['config']->set('nova-tenancy.resources.tenant_column', 'tenant_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function defineRoutes($router): void
|
||||||
|
{
|
||||||
|
Route::middleware('web')->group(function (): void {
|
||||||
|
Route::get('/testing/tenants', TenantIndexController::class);
|
||||||
|
Route::get('/testing/current', [CurrentTenantController::class, 'show']);
|
||||||
|
Route::post('/testing/current', [CurrentTenantController::class, 'store']);
|
||||||
|
Route::delete('/testing/current', [CurrentTenantController::class, 'destroy']);
|
||||||
|
Route::get('/testing/current/logo', CurrentTenantLogoController::class);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
NovaTenancy::flushState();
|
||||||
|
|
||||||
|
Schema::create('users', function (Blueprint $table): void {
|
||||||
|
$table->id();
|
||||||
|
$table->string('name');
|
||||||
|
$table->rememberToken();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
Schema::create('tenants', function (Blueprint $table): void {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('user_id');
|
||||||
|
$table->string('title');
|
||||||
|
$table->string('logo_url')->nullable();
|
||||||
|
$table->string('description')->nullable();
|
||||||
|
$table->string('accent')->nullable();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
Schema::create('tenant_records', function (Blueprint $table): void {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('tenant_id');
|
||||||
|
$table->string('name');
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tearDown(): void
|
||||||
|
{
|
||||||
|
NovaTenancy::flushState();
|
||||||
|
parent::tearDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import { defineConfig } from 'vitest/config'
|
||||||
|
import vue from '@vitejs/plugin-vue'
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
plugins: [vue()],
|
||||||
|
test: {
|
||||||
|
environment: 'jsdom',
|
||||||
|
globals: true,
|
||||||
|
include: ['tests-js/**/*.test.js'],
|
||||||
|
},
|
||||||
|
})
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
const mix = require('laravel-mix')
|
||||||
|
const NovaExtension = require('laravel-nova-devtool')
|
||||||
|
|
||||||
|
mix.extend('nova', new NovaExtension())
|
||||||
|
|
||||||
|
mix
|
||||||
|
.setPublicPath('dist')
|
||||||
|
.js('resources/js/tool.js', 'js')
|
||||||
|
.vue({ version: 3 })
|
||||||
|
.css('resources/css/tool.css', 'css')
|
||||||
|
.nova('meghdadfadaee/nova-tenancy')
|
||||||
|
.version()
|
||||||
Reference in new issue
Block a user