From 946d915b6d71ed81a0971aa74afe895aef109ea9 Mon Sep 17 00:00:00 2001 From: Bram Raaijmakers Date: Thu, 4 Sep 2025 14:40:38 +0200 Subject: [PATCH 1/2] Add option for a custom model callback --- README.md | 21 +++++++++++++++++++++ src/FilamentDeveloperLoginsPlugin.php | 25 +++++++++++++++++++++++++ src/FilamentDevelopersLogin.php | 8 +++++--- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ab2c720..a5f1b0f 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,27 @@ FilamentDeveloperLoginsPlugin::make() ->modelClass(Admin::class) ``` +### Override query + +Default the plugin will retrieve the user by searching the provided model using the specified column. If you want to implement your own logic to retrieve the user, you can use the `modelCallback()` method. +This method accepts a closure and provides the plugin and should return an instance of `Illuminate\Database\Eloquent\Builder`. + +Example: + +```php +use DutchCodingCompany\FilamentDeveloperLogins\FilamentDeveloperLoginsPlugin; +use Illuminate\Database\Eloquent\Builder;; + +FilamentDeveloperLoginsPlugin::make() + ->modelCallback( + fn (FilamentDeveloperLoginsPlugin $plugin, string $credentials): Builder + => (new $plugin->getModelClass()) + ->where($plugin->getColumn(), $credentials) + // Above is the default behavior. For example if you are using a global scope you can remove it here. + ->withoutGlobalScopes() + ) +``` + ### redirectTo() By default, the user will be redirected using the `Filament::getUrl()` method, which directs them to the dashboard. In the case of multi-tenancy, the user will also be redirected to the correct tenant. If you prefer to use a different url, you can utilize the redirectTo() method. diff --git a/src/FilamentDeveloperLoginsPlugin.php b/src/FilamentDeveloperLoginsPlugin.php index 272c760..c037c6c 100644 --- a/src/FilamentDeveloperLoginsPlugin.php +++ b/src/FilamentDeveloperLoginsPlugin.php @@ -10,6 +10,7 @@ use Filament\Panel; use Filament\Schemas\Concerns\HasColumns; use Filament\Support\Concerns\EvaluatesClosures; +use Illuminate\Database\Eloquent\Builder; class FilamentDeveloperLoginsPlugin implements Plugin { @@ -22,6 +23,8 @@ class FilamentDeveloperLoginsPlugin implements Plugin public Closure | bool $enabled = false; + public ?Closure $modelCallback = null; + public Closure | bool $switchable = true; /** @@ -96,6 +99,28 @@ public function getEnabled(): bool return $this->evaluate($this->enabled); } + public function modelCallback(Closure $callback): static + { + $this->modelCallback = $callback; + + return $this; + } + + /** + * @param FilamentDeveloperLoginsPlugin $plugin + * @param string $credentials + * @return Builder<\Illuminate\Database\Eloquent\Model&\Illuminate\Contracts\Auth\Authenticatable> + */ + public function getModelCallback(self $plugin, string $credentials): Builder + { + return $this->evaluate( + value: $this->modelCallback + ?? static fn (): Builder => (new ($plugin->getModelClass())) + ->where($plugin->getColumn(), $credentials), + namedInjections: ['plugin' => $plugin, 'credentials' => $credentials], + ); + } + public function switchable(Closure | bool $value): static { $this->switchable = $value; diff --git a/src/FilamentDevelopersLogin.php b/src/FilamentDevelopersLogin.php index a366ad9..b2eba33 100644 --- a/src/FilamentDevelopersLogin.php +++ b/src/FilamentDevelopersLogin.php @@ -25,9 +25,11 @@ public function login(Panel $panel, FilamentDeveloperLoginsPlugin $plugin, strin } /** @var ?\Illuminate\Contracts\Auth\Authenticatable $model */ - $model = (new ($plugin->getModelClass())) - ->where($plugin->getColumn(), $credentials) - ->first(); + $model = $plugin->getModelCallback($plugin, $credentials)->first(); + +// $model = (new ($plugin->getModelClass())) +// ->where($plugin->getColumn(), $credentials) +// ->first(); if (! $model) { throw ValidationException::withMessages([ From 92811fe1d7bf0440d34b7f7416e9516b210b473f Mon Sep 17 00:00:00 2001 From: Bram Raaijmakers Date: Thu, 4 Sep 2025 14:44:02 +0200 Subject: [PATCH 2/2] Remove commented out code. --- src/FilamentDevelopersLogin.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/FilamentDevelopersLogin.php b/src/FilamentDevelopersLogin.php index b2eba33..14e5e89 100644 --- a/src/FilamentDevelopersLogin.php +++ b/src/FilamentDevelopersLogin.php @@ -27,10 +27,6 @@ public function login(Panel $panel, FilamentDeveloperLoginsPlugin $plugin, strin /** @var ?\Illuminate\Contracts\Auth\Authenticatable $model */ $model = $plugin->getModelCallback($plugin, $credentials)->first(); -// $model = (new ($plugin->getModelClass())) -// ->where($plugin->getColumn(), $credentials) -// ->first(); - if (! $model) { throw ValidationException::withMessages([ 'developer-logins-failed' => __('filament-developer-logins::auth.messages.failed_not_found'),