Skip to content

Commit

Permalink
abort 404 if tenant not found without using macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Takeshi Yu committed Apr 23, 2023
1 parent 56402f5 commit 86bf423
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/Exceptions/TenantNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace TakeshiYu\Tenantify\Exceptions;

use Exception;

class TenantNotFoundException extends Exception
{
public function __construct($message = '')
{
parent::__construct($message ?: 'Tenant is not found.');
}
}
4 changes: 3 additions & 1 deletion src/TenancyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use TakeshiYu\Tenantify\Exceptions\TenancyNotInitializedException;
use TakeshiYu\Tenantify\Exceptions\TenantNotFoundException;

class TenancyManager
{
Expand Down Expand Up @@ -72,6 +73,7 @@ protected function resolveTenantFromSubdomain(Request $request): null|Model
{
$subdomain = explode('.', $request->getHost())[0];

return $this->config['tenant_model']::where($this->config['tenant_slug'], $subdomain)->first();
return $this->config['tenant_model']::where($this->config['tenant_slug'], $subdomain)
->firstOr(fn () => abort(404, 'Tenant is not found'));
}
}
5 changes: 5 additions & 0 deletions tests/Feature/ResolveTenantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@
$this->assertEquals($tenant->id, Tenancy::id());
$this->assertEquals($tenant->slug, Tenancy::slug());
});

test('Teannt not found when using tenancy macro', function () {
$response = $this->get('http://foo.tenantify.test/home');
$response->assertStatus(404);
});

0 comments on commit 86bf423

Please sign in to comment.