Skip to content

Commit

Permalink
- Added 'currentTenant' function (#19)
Browse files Browse the repository at this point in the history
- Added Tests for Multitenancy 'currentTenant' function
- Updated README
  • Loading branch information
joriskuijpers authored and bradenkeith committed Aug 5, 2019
1 parent ee3d2e2 commit 8d5b94d
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ class Product extends Model
> **Hint**
> If the user is assigned `Super Administrator` access, they will be able to access the `admin` subdomain and the tenant scope will not register. This allows you to manage the data across all instances without needing individual access to each Tenant's account.
### Get Current Tenant

You can get the current tenant model:

```php
app('multitenancy')->currentTenant();
```

### Providing Access to Admin Domain

In order to access the `admin.example.com` subdomain, a user will need the `access admin` permission. This package relies on [Spatie's Laravel Permission](https://github.com/spatie/laravel-permission) package and is automatically included as a dependency when installing this package. We also provide a `Super Administrator` role on migration that has the relevant permission already associated with it. Assign the `Super Administrator` role to an admin user to provide the access they need. See the [Laravel Permission](https://github.com/spatie/laravel-permission) documentation for more on adding users to the appropriate role and permission.
Expand Down
10 changes: 10 additions & 0 deletions src/Multitenancy.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ public function setTenant(Tenant $tenant)
return $this;
}

/**
* Returns the current Tenant.
*
* @return \RomegaDigital\Multitenancy\Contracts\Tenant
*/
public function currentTenant(): Tenant
{
return $this->tenant ?? $this->receiveTenantFromRequest();
}

/**
* Applies applicable tenant scopes to model or if not booted yet
* store for deferment.
Expand Down
88 changes: 88 additions & 0 deletions tests/Feature/MultitenancyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace RomegaDigital\Multitenancy\Tests\Feature;

use Illuminate\Http\Response;
use RomegaDigital\Multitenancy\Exceptions\TenantDoesNotExist;
use RomegaDigital\Multitenancy\Middleware\TenantMiddleware;
use RomegaDigital\Multitenancy\Tests\TestCase;

class MultitenancyTest extends TestCase
{
protected $tenantMiddleware;

/**
* Define environment setup.
*
* @param Illuminate\Foundation\Application $app
*
* @return void
*/
protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);

$app['router']->get('/login', function () {
return 'login';
})->name(config('multitenancy.redirect_route'));
}

public function setUp(): void
{
parent::setUp();

$this->tenantMiddleware = new TenantMiddleware(app('auth'), app('multitenancy'));
}

protected function buildRequest($domain)
{
app('request')->headers->set('HOST', $domain.'.example.com');

return $this->tenantMiddleware->handle(app('request'), function () {
return (new Response())->setContent('<html></html>');
});
}

/** @test */
public function it_returns_the_current_tenant_when_set_by_middleware()
{
$this->actingAs($this->testUser);

$this->testTenant->users()->sync($this->testUser);

$this->buildRequest($this->testTenant->domain);

$this->assertEquals($this->testTenant->domain, app('multitenancy')->currentTenant()->domain);
}

/** @test */
public function it_throws_exception_when_tenant_not_set()
{
$this->actingAs($this->testUser);

$this->testTenant->users()->sync($this->testUser);

try {
$this->buildRequest('testdomain');
app('multitenancy')->currentTenant();
$this->fail('Expected exception not thrown');
} catch (TenantDoesNotExist $e) {
$this->assertEquals('There is no tenant at domain `testdomain`.', $e->getMessage());
}
}

/** @test */
public function it_throws_exception_when_tenant_not_set_never_touched_middleware()
{
$this->actingAs($this->testUser);

$this->testTenant->users()->sync($this->testUser);

try {
app('multitenancy')->currentTenant();
$this->fail('Expected exception not thrown');
} catch (TenantDoesNotExist $e) {
$this->assertEquals('There is no tenant at domain ``.', $e->getMessage());
}
}
}

0 comments on commit 8d5b94d

Please sign in to comment.