mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 10:54: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
|
||||
|
||||
## Code style
|
||||
|
||||
StyleCI will flag code style violations in your pull requests.
|
||||
|
||||
## Running tests
|
||||
|
||||
### With Docker
|
||||
If you have Docker installed, simply run ./fulltest. When you're done testing, run docker-compose down to shut down the containers.
|
||||
Run `docker-compose up-d` to start the containers. Then run `./test` to run the tests.
|
||||
|
||||
### Without Docker
|
||||
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.
|
||||
When you're done testing, run `docker-compose down` to shut down the containers.
|
||||
|
|
|
|||
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,
|
||||
Concerns\EnsuresDomainIsNotOccupied,
|
||||
Concerns\ConvertsDomainsToLowercase,
|
||||
Concerns\InvalidatesTenantsResolverCache;
|
||||
|
||||
protected $guarded = [];
|
||||
|
|
|
|||
|
|
@ -10,6 +10,13 @@ use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
|
|||
|
||||
class DomainTenantResolver extends Contracts\CachedTenantResolver
|
||||
{
|
||||
/**
|
||||
* The model representing the domain that the tenant was identified on.
|
||||
*
|
||||
* @var Domain
|
||||
*/
|
||||
public static $currentDomain;
|
||||
|
||||
/** @var bool */
|
||||
public static $shouldCache = false;
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ use Illuminate\Cache\CacheManager;
|
|||
use Illuminate\Support\ServiceProvider;
|
||||
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
|
||||
|
||||
class TenancyServiceProvider extends ServiceProvider
|
||||
{
|
||||
|
|
@ -39,6 +40,10 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
return $app[Tenancy::class]->tenant;
|
||||
});
|
||||
|
||||
$this->app->bind(Domain::class, function () {
|
||||
return DomainTenantResolver::$currentDomain;
|
||||
});
|
||||
|
||||
// Make sure bootstrappers are stateful (singletons).
|
||||
foreach ($this->app['config']['tenancy.bootstrappers'] ?? [] as $bootstrapper) {
|
||||
if (method_exists($bootstrapper, '__constructStatic')) {
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ namespace Stancl\Tenancy\Tests;
|
|||
use Illuminate\Support\Facades\Route;
|
||||
use Stancl\Tenancy\Database\Concerns\HasDomains;
|
||||
use Stancl\Tenancy\Database\Models;
|
||||
use Stancl\Tenancy\Database\Models\Domain;
|
||||
use Stancl\Tenancy\Exceptions\DomainOccupiedByOtherTenantException;
|
||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
||||
|
|
@ -103,6 +104,18 @@ class DomainTest extends TestCase
|
|||
->get('http://foo.localhost/foo/abc/xyz')
|
||||
->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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue