Skip to content

Commit 946d915

Browse files
committed
Add option for a custom model callback
1 parent 029dc28 commit 946d915

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,27 @@ FilamentDeveloperLoginsPlugin::make()
103103
->modelClass(Admin::class)
104104
```
105105

106+
### Override query
107+
108+
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.
109+
This method accepts a closure and provides the plugin and should return an instance of `Illuminate\Database\Eloquent\Builder`.
110+
111+
Example:
112+
113+
```php
114+
use DutchCodingCompany\FilamentDeveloperLogins\FilamentDeveloperLoginsPlugin;
115+
use Illuminate\Database\Eloquent\Builder;;
116+
117+
FilamentDeveloperLoginsPlugin::make()
118+
->modelCallback(
119+
fn (FilamentDeveloperLoginsPlugin $plugin, string $credentials): Builder
120+
=> (new $plugin->getModelClass())
121+
->where($plugin->getColumn(), $credentials)
122+
// Above is the default behavior. For example if you are using a global scope you can remove it here.
123+
->withoutGlobalScopes()
124+
)
125+
```
126+
106127
### redirectTo()
107128

108129
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.

src/FilamentDeveloperLoginsPlugin.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Filament\Panel;
1111
use Filament\Schemas\Concerns\HasColumns;
1212
use Filament\Support\Concerns\EvaluatesClosures;
13+
use Illuminate\Database\Eloquent\Builder;
1314

1415
class FilamentDeveloperLoginsPlugin implements Plugin
1516
{
@@ -22,6 +23,8 @@ class FilamentDeveloperLoginsPlugin implements Plugin
2223

2324
public Closure | bool $enabled = false;
2425

26+
public ?Closure $modelCallback = null;
27+
2528
public Closure | bool $switchable = true;
2629

2730
/**
@@ -96,6 +99,28 @@ public function getEnabled(): bool
9699
return $this->evaluate($this->enabled);
97100
}
98101

102+
public function modelCallback(Closure $callback): static
103+
{
104+
$this->modelCallback = $callback;
105+
106+
return $this;
107+
}
108+
109+
/**
110+
* @param FilamentDeveloperLoginsPlugin $plugin
111+
* @param string $credentials
112+
* @return Builder<\Illuminate\Database\Eloquent\Model&\Illuminate\Contracts\Auth\Authenticatable>
113+
*/
114+
public function getModelCallback(self $plugin, string $credentials): Builder
115+
{
116+
return $this->evaluate(
117+
value: $this->modelCallback
118+
?? static fn (): Builder => (new ($plugin->getModelClass()))
119+
->where($plugin->getColumn(), $credentials),
120+
namedInjections: ['plugin' => $plugin, 'credentials' => $credentials],
121+
);
122+
}
123+
99124
public function switchable(Closure | bool $value): static
100125
{
101126
$this->switchable = $value;

src/FilamentDevelopersLogin.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ public function login(Panel $panel, FilamentDeveloperLoginsPlugin $plugin, strin
2525
}
2626

2727
/** @var ?\Illuminate\Contracts\Auth\Authenticatable $model */
28-
$model = (new ($plugin->getModelClass()))
29-
->where($plugin->getColumn(), $credentials)
30-
->first();
28+
$model = $plugin->getModelCallback($plugin, $credentials)->first();
29+
30+
// $model = (new ($plugin->getModelClass()))
31+
// ->where($plugin->getColumn(), $credentials)
32+
// ->first();
3133

3234
if (! $model) {
3335
throw ValidationException::withMessages([

0 commit comments

Comments
 (0)