mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 12:24:04 +00:00
Convert domains to lowercase, persist current domain model on resolver
This commit is contained in:
parent
cd1381eae9
commit
02e049b59e
6 changed files with 42 additions and 6 deletions
|
|
@ -1,14 +1,11 @@
|
||||||
# Contributing
|
# Contributing
|
||||||
|
|
||||||
## Code style
|
## Code style
|
||||||
|
|
||||||
StyleCI will flag code style violations in your pull requests.
|
StyleCI will flag code style violations in your pull requests.
|
||||||
|
|
||||||
## Running tests
|
## Running tests
|
||||||
|
|
||||||
### With Docker
|
Run `docker-compose up-d` to start the containers. Then run `./test` to run the tests.
|
||||||
If you have Docker installed, simply run ./fulltest. When you're done testing, run docker-compose down to shut down the containers.
|
|
||||||
|
|
||||||
### Without Docker
|
When you're done testing, run `docker-compose down` to shut down the containers.
|
||||||
If you run the tests of this package, please make sure you don't store anything in Redis @ 127.0.0.1:6379 db#14. The contents of this database are flushed everytime the tests are run.
|
|
||||||
|
|
||||||
Some tests are run only if the `CONTINUOUS_INTEGRATION` or `DOCKER` environment variables are set to true. This is to avoid things like bloating your MySQL instance with test databases.
|
|
||||||
|
|
|
||||||
13
src/Database/Concerns/ConvertsDomainsToLowercase.php
Normal file
13
src/Database/Concerns/ConvertsDomainsToLowercase.php
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Database\Concerns;
|
||||||
|
|
||||||
|
trait ConvertsDomainsToLowercase
|
||||||
|
{
|
||||||
|
public static function bootConvertsDomainsToLowercase()
|
||||||
|
{
|
||||||
|
static::saving(function ($model) {
|
||||||
|
$model->domain = strtolower($model->domain);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -20,6 +20,7 @@ class Domain extends Model implements Contracts\Domain
|
||||||
{
|
{
|
||||||
use Concerns\CentralConnection,
|
use Concerns\CentralConnection,
|
||||||
Concerns\EnsuresDomainIsNotOccupied,
|
Concerns\EnsuresDomainIsNotOccupied,
|
||||||
|
Concerns\ConvertsDomainsToLowercase,
|
||||||
Concerns\InvalidatesTenantsResolverCache;
|
Concerns\InvalidatesTenantsResolverCache;
|
||||||
|
|
||||||
protected $guarded = [];
|
protected $guarded = [];
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,13 @@ use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
|
||||||
|
|
||||||
class DomainTenantResolver extends Contracts\CachedTenantResolver
|
class DomainTenantResolver extends Contracts\CachedTenantResolver
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* The model representing the domain that the tenant was identified on.
|
||||||
|
*
|
||||||
|
* @var Domain
|
||||||
|
*/
|
||||||
|
public static $currentDomain;
|
||||||
|
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
public static $shouldCache = false;
|
public static $shouldCache = false;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ use Illuminate\Cache\CacheManager;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Contracts\Tenant;
|
use Stancl\Tenancy\Contracts\Tenant;
|
||||||
|
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
|
||||||
|
|
||||||
class TenancyServiceProvider extends ServiceProvider
|
class TenancyServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
|
|
@ -39,6 +40,10 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
return $app[Tenancy::class]->tenant;
|
return $app[Tenancy::class]->tenant;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$this->app->bind(Domain::class, function () {
|
||||||
|
return DomainTenantResolver::$currentDomain;
|
||||||
|
});
|
||||||
|
|
||||||
// Make sure bootstrappers are stateful (singletons).
|
// Make sure bootstrappers are stateful (singletons).
|
||||||
foreach ($this->app['config']['tenancy.bootstrappers'] ?? [] as $bootstrapper) {
|
foreach ($this->app['config']['tenancy.bootstrappers'] ?? [] as $bootstrapper) {
|
||||||
if (method_exists($bootstrapper, '__constructStatic')) {
|
if (method_exists($bootstrapper, '__constructStatic')) {
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ namespace Stancl\Tenancy\Tests;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use Stancl\Tenancy\Database\Concerns\HasDomains;
|
use Stancl\Tenancy\Database\Concerns\HasDomains;
|
||||||
use Stancl\Tenancy\Database\Models;
|
use Stancl\Tenancy\Database\Models;
|
||||||
|
use Stancl\Tenancy\Database\Models\Domain;
|
||||||
use Stancl\Tenancy\Exceptions\DomainOccupiedByOtherTenantException;
|
use Stancl\Tenancy\Exceptions\DomainOccupiedByOtherTenantException;
|
||||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
|
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
|
||||||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
||||||
|
|
@ -103,6 +104,18 @@ class DomainTest extends TestCase
|
||||||
->get('http://foo.localhost/foo/abc/xyz')
|
->get('http://foo.localhost/foo/abc/xyz')
|
||||||
->assertSee('foo');
|
->assertSee('foo');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function domains_are_always_lowercase()
|
||||||
|
{
|
||||||
|
$tenant = DomainTenant::create();
|
||||||
|
|
||||||
|
$tenant->domains()->create([
|
||||||
|
'domain' => 'CAPITALS',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertSame('capitals', Domain::first()->domain);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class DomainTenant extends Models\Tenant
|
class DomainTenant extends Models\Tenant
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue