Skip to content

Commit

Permalink
simplify socialite user to interface + move query functions to our ow…
Browse files Browse the repository at this point in the history
…n implementation
  • Loading branch information
bert-w committed Feb 27, 2024
1 parent 4267c2e commit 7d6e4d2
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 30 deletions.
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ parameters:
count: 1
path: tests/SocialiteLoginTest.php

-
message: "#^Cannot call method run\\(\\) on Illuminate\\\\Testing\\\\PendingCommand\\|int\\.$#"
count: 1
path: tests/TestCase.php

-
message: "#^Parameter \\#1 \\$callback of static method Illuminate\\\\Database\\\\Eloquent\\\\Factories\\\\Factory\\<Illuminate\\\\Database\\\\Eloquent\\\\Model\\>\\:\\:guessFactoryNamesUsing\\(\\) expects callable\\(class\\-string\\<Illuminate\\\\Database\\\\Eloquent\\\\Model\\>\\)\\: class\\-string\\<Illuminate\\\\Database\\\\Eloquent\\\\Factories\\\\Factory\\>, Closure\\(string\\)\\: non\\-falsy\\-string given\\.$#"
count: 1
Expand Down
18 changes: 4 additions & 14 deletions src/FilamentSocialite.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Illuminate\Contracts\Auth\Factory;
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Database\Eloquent\Model;
use Laravel\Socialite\Contracts\User as SocialiteUserContract;

class FilamentSocialite
Expand Down Expand Up @@ -115,14 +114,14 @@ public function getUserResolver(): Closure
}

/**
* @return class-string<\Illuminate\Database\Eloquent\Model&\DutchCodingCompany\FilamentSocialite\Models\Contracts\FilamentSocialiteUser>
* @return class-string<\DutchCodingCompany\FilamentSocialite\Models\Contracts\FilamentSocialiteUser>
*/
public function getSocialiteUserModelClass(): string
{
return $this->getPlugin()->getSocialiteUserModelClass();
}

public function getSocialiteUserModel(): Model & FilamentSocialiteUser
public function getSocialiteUserModel(): FilamentSocialiteUser
{
return new ($this->getSocialiteUserModelClass());
}
Expand Down Expand Up @@ -157,21 +156,12 @@ public function setUserResolver(Closure $callback = null): static
*/
public function getCreateSocialiteUserCallback(): Closure
{
return function (
return $this->createSocialiteUserCallback ?? function (
string $provider,
SocialiteUserContract $oauthUser,
Authenticatable $user,
) {
/**
* @var \Illuminate\Database\Eloquent\Builder<\DutchCodingCompany\FilamentSocialite\Models\SocialiteUser> $query
*/
$query = $this->getSocialiteUserModel()->query();

return $query->create([
'user_id' => $user->getKey(),
'provider' => $provider,
'provider_id' => $oauthUser->getId(),
]);
return $this->getSocialiteUserModel()::createForProvider($provider, $oauthUser, $user);
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/FilamentSocialitePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class FilamentSocialitePlugin implements Plugin
protected string $userModelClass = User::class;

/**
* @var class-string<\Illuminate\Database\Eloquent\Model&\DutchCodingCompany\FilamentSocialite\Models\Contracts\FilamentSocialiteUser>
* @var class-string<\DutchCodingCompany\FilamentSocialite\Models\Contracts\FilamentSocialiteUser>
*/
protected string $socialiteUserModelClass = SocialiteUser::class;

Expand Down Expand Up @@ -199,7 +199,7 @@ public function getUserModelClass(): string
}

/**
* @param class-string<\Illuminate\Database\Eloquent\Model&\DutchCodingCompany\FilamentSocialite\Models\Contracts\FilamentSocialiteUser> $value
* @param class-string<\DutchCodingCompany\FilamentSocialite\Models\Contracts\FilamentSocialiteUser> $value
*/
public function setSocialiteUserModelClass(string $value): static
{
Expand All @@ -213,7 +213,7 @@ public function setSocialiteUserModelClass(string $value): static
}

/**
* @return class-string<\Illuminate\Database\Eloquent\Model&\DutchCodingCompany\FilamentSocialite\Models\Contracts\FilamentSocialiteUser>
* @return class-string<\DutchCodingCompany\FilamentSocialite\Models\Contracts\FilamentSocialiteUser>
*/
public function getSocialiteUserModelClass(): string
{
Expand Down
12 changes: 3 additions & 9 deletions src/Http/Controllers/SocialiteLoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,9 @@ protected function retrieveOauthUser(string $provider): ?SocialiteUserContract
return null;
}

protected function retrieveSocialiteUser(string $provider, SocialiteUserContract $user): ?FilamentSocialiteUserContract
protected function retrieveSocialiteUser(string $provider, SocialiteUserContract $oauthUser): ?FilamentSocialiteUserContract
{
/** @var ?\DutchCodingCompany\FilamentSocialite\Models\Contracts\FilamentSocialiteUser $model */
$model = $this->socialite->getSocialiteUserModelClass()::query()
->where('provider', $provider)
->where('provider_id', $user->getId())
->first();

return $model;
return $this->socialite->getSocialiteUserModel()::findForProvider($provider, $oauthUser);
}

protected function redirectToLogin(string $message): RedirectResponse
Expand All @@ -95,7 +89,7 @@ protected function isUserAllowed(SocialiteUserContract $user): bool
->__toString();

// See if everything after @ is in the domains array
return (bool) (in_array($emailDomain, $domains));
return in_array($emailDomain, $domains);
}

protected function loginUser(FilamentSocialiteUserContract $socialiteUser): RedirectResponse
Expand Down
8 changes: 6 additions & 2 deletions src/Models/Contracts/FilamentSocialiteUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
namespace DutchCodingCompany\FilamentSocialite\Models\Contracts;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Laravel\Socialite\Contracts\User as SocialiteUserContract;

interface FilamentSocialiteUser
{
public function getUser(): Model & Authenticatable;
public function getUser(): Authenticatable;

public static function findForProvider(string $provider, SocialiteUserContract $oauthUser): ?self;

public static function createForProvider(string $provider, SocialiteUserContract $oauthUser, Authenticatable $user): self;
}
21 changes: 20 additions & 1 deletion src/Models/SocialiteUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Laravel\Socialite\Contracts\User as SocialiteUserContract;

/**
* @property int $user_id
Expand All @@ -32,8 +33,26 @@ public function user(): BelongsTo
return $this->belongsTo(FilamentSocialite::getUserModelClass());
}

public function getUser(): Model & Authenticatable
public function getUser(): Authenticatable
{
return $this->user;
}

public static function findForProvider(string $provider, SocialiteUserContract $oauthUser): ?self
{
return self::query()
->where('provider', $provider)
->where('provider_id', $oauthUser->getId())
->first();
}

public static function createForProvider(string $provider, SocialiteUserContract $oauthUser, Authenticatable $user): self
{
return self::query()
->create([
'user_id' => $user->getKey(),
'provider' => $provider,
'provider_id' => $oauthUser->getId(),
]);
}
}
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected function defineDatabaseMigrations()
$this->loadLaravelMigrations();
$this->loadMigrationsFrom(__DIR__.'/Fixtures');

Artisan::call('migrate', ['--database' => 'testing']);
$this->artisan('migrate', ['--database' => 'testing'])->run();

Schema::table('users', static function (Blueprint $table): void {
$table->string('password')->nullable()->change();
Expand Down

0 comments on commit 7d6e4d2

Please sign in to comment.