Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
By 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`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This method accepts a closure and provides the plugin and should return an instance of `Illuminate\Database\Eloquent\Builder`.
This method accepts a closure. The closure should return an instance of `Illuminate\Database\Eloquent\Builder`.


Example:

```php
use DutchCodingCompany\FilamentDeveloperLogins\FilamentDeveloperLoginsPlugin;
use Illuminate\Database\Eloquent\Builder;;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use Illuminate\Database\Eloquent\Builder;;
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.
Expand Down
25 changes: 25 additions & 0 deletions src/FilamentDeveloperLoginsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -22,6 +23,8 @@ class FilamentDeveloperLoginsPlugin implements Plugin

public Closure | bool $enabled = false;

public ?Closure $modelCallback = null;

public Closure | bool $switchable = true;

/**
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't get the model callback, but the user query builder (via the model callback)

{
return $this->evaluate(
value: $this->modelCallback
?? static fn (): Builder => (new ($plugin->getModelClass()))
->where($plugin->getColumn(), $credentials),
Comment on lines +117 to +119
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

            value: $this->modelCallback
                ?? static fn (self $plugin, string $credentials): Builder => (new ($plugin->getModelClass()))->where($plugin->getColumn(), $credentials),

namedInjections: ['plugin' => $plugin, 'credentials' => $credentials],
);
}

public function switchable(Closure | bool $value): static
{
$this->switchable = $value;
Expand Down
4 changes: 1 addition & 3 deletions src/FilamentDevelopersLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ 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();

if (! $model) {
throw ValidationException::withMessages([
Expand Down