1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 23:14:03 +00:00

Convert domains to lowercase, persist current domain model on resolver

This commit is contained in:
Samuel Štancl 2020-06-28 11:45:08 +02:00
parent cd1381eae9
commit 02e049b59e
6 changed files with 42 additions and 6 deletions

View file

@ -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.

View 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);
});
}
}

View file

@ -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 = [];

View file

@ -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;

View file

@ -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')) {

View file

@ -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