generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from bramr94/multi-tenancy
Multi-tenancy support
- Loading branch information
Showing
11 changed files
with
242 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace DutchCodingCompany\FilamentSocialite\Tests\Fixtures; | ||
|
||
use Illuminate\Contracts\Auth\Authenticatable; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class TestTeam extends Model | ||
{ | ||
protected $table = 'teams'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace DutchCodingCompany\FilamentSocialite\Tests\Fixtures; | ||
|
||
use Filament\Models\Contracts\HasTenants; | ||
use Filament\Panel; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsToMany; | ||
use Illuminate\Support\Collection; | ||
|
||
/** | ||
* @property Collection<\DutchCodingCompany\FilamentSocialite\Tests\Fixtures\TestTeam> $teams | ||
*/ | ||
class TestTenantUser extends TestUser implements HasTenants | ||
{ | ||
public function canAccessTenant(Model $tenant): bool | ||
{ | ||
return $this->teams->contains($tenant); | ||
} | ||
|
||
/** | ||
* @return \Illuminate\Support\Collection<int, \DutchCodingCompany\FilamentSocialite\Tests\Fixtures\TestTeam> | ||
*/ | ||
public function getTenants(Panel $panel): Collection | ||
{ | ||
return $this->teams; | ||
} | ||
|
||
/** | ||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<\DutchCodingCompany\FilamentSocialite\Tests\Fixtures\TestTeam> | ||
*/ | ||
public function teams(): BelongsToMany | ||
{ | ||
return $this->belongsToMany(TestTeam::class, 'team_user', 'user_id', 'team_id'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class() extends Migration { | ||
public function up(): void | ||
{ | ||
Schema::create('team_user', function (Blueprint $table) { | ||
$table->integer('team_id'); | ||
$table->integer('user_id'); | ||
|
||
$table->unique(['team_id', 'user_id']); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::dropIfExists('team_user'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class() extends Migration { | ||
public function up(): void | ||
{ | ||
Schema::create('teams', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('name'); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::dropIfExists('teams'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace DutchCodingCompany\FilamentSocialite\Tests; | ||
|
||
use DutchCodingCompany\FilamentSocialite\Facades\FilamentSocialite; | ||
use DutchCodingCompany\FilamentSocialite\Models\Contracts\FilamentSocialiteUser as FilamentSocialiteUserContract; | ||
use DutchCodingCompany\FilamentSocialite\Models\SocialiteUser; | ||
use DutchCodingCompany\FilamentSocialite\Tests\Fixtures\TestSocialiteUser; | ||
use DutchCodingCompany\FilamentSocialite\Tests\Fixtures\TestTeam; | ||
use DutchCodingCompany\FilamentSocialite\Tests\Fixtures\TestTenantUser; | ||
use Filament\Facades\Filament; | ||
use Filament\Panel; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Support\Facades\Crypt; | ||
use Laravel\Socialite\Contracts\Provider; | ||
use Laravel\Socialite\Facades\Socialite; | ||
use Mockery; | ||
|
||
class SocialiteTenantLoginTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
protected string $userModelClass = TestTenantUser::class; | ||
|
||
protected array $tenantArguments = [ | ||
TestTeam::class, | ||
]; | ||
|
||
public function testTenantLogin(): void | ||
{ | ||
FilamentSocialite::setLoginRedirectCallback(function (string $provider, FilamentSocialiteUserContract $socialiteUser) { | ||
assert($socialiteUser instanceof SocialiteUser); | ||
|
||
$this->assertEquals($this->panelName, Filament::getCurrentPanel()->getId()); | ||
$this->assertEquals('github', $provider); | ||
$this->assertEquals('github', $socialiteUser->provider); | ||
$this->assertEquals('test-socialite-user-id', $socialiteUser->provider_id); | ||
|
||
return redirect()->to('/some-tenant-url'); | ||
}); | ||
|
||
$response = $this | ||
->getJson("/$this->panelName/oauth/github") | ||
->assertStatus(302); | ||
|
||
$state = session()->get('state'); | ||
|
||
Socialite::shouldReceive('driver') | ||
->with('github') | ||
->andReturn( | ||
Mockery::mock(Provider::class) | ||
->shouldReceive('user') | ||
->andReturn(new TestSocialiteUser()) | ||
->getMock() | ||
); | ||
|
||
// Fake oauth response. | ||
$response = $this | ||
->getJson("/oauth/callback/github?state=$state") | ||
->assertStatus(302); | ||
|
||
$this->assertStringContainsString('/some-tenant-url', $response->headers->get('Location')); | ||
|
||
$this->assertDatabaseHas('socialite_users', [ | ||
'provider' => 'github', | ||
'provider_id' => 'test-socialite-user-id', | ||
]); | ||
|
||
$this->assertDatabaseHas('users', [ | ||
'name' => 'test-socialite-user-name', | ||
'email' => '[email protected]', | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters