Skip to content

Commit

Permalink
Merge pull request #122 from DutchCodingCompany/feature/disable-provi…
Browse files Browse the repository at this point in the history
…ders

Add option to hide providers
  • Loading branch information
dododedodonl authored Nov 29, 2024
2 parents 0d4f985 + 99d4c84 commit c9a801e
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 13 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
- '**.php'
- '.github/workflows/phpstan.yml'
- 'phpstan.neon.dist'
- 'phpstan.laravel-10.neon.dist'
pull_request:

jobs:
Expand All @@ -15,14 +16,16 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
php: [8.3, 8.2, 8.1]
php: [8.4, 8.3, 8.2, 8.1]
laravel: [10.*, 11.*]
stability: [prefer-stable]
include:
- laravel: 10.*
testbench: 8.*
phpstan_neon: phpstan.laravel-10.neon.dist
- laravel: 11.*
testbench: 9.*
phpstan_neon: phpstan.neon.dist
exclude:
- laravel: 11.*
php: 8.1
Expand All @@ -44,9 +47,8 @@ jobs:
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
- name: List Installed Dependencies
run: composer show -D

- name: Run PHPStan
run: vendor/bin/phpstan --error-format=github
run: vendor/bin/phpstan --error-format=github --configuration=${{ matrix.phpstan_neon }}
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,20 @@ FilamentSocialitePlugin::make()
]),
]),
```
## Visibility

You can set the visibility of a provider, if it is not visible, buttons will not be rendered. All functionality will still be enabled.

```php
use DutchCodingCompany\FilamentSocialite\FilamentSocialitePlugin;
use DutchCodingCompany\FilamentSocialite\Provider;

FilamentSocialitePlugin::make()
->providers([
Provider::make('github')
->visible(fn () => true),
]),
```

## Stateless Authentication
You can add `stateless` parameters to the provider configuration in the config/services.php config file, for example:
Expand Down
36 changes: 36 additions & 0 deletions phpstan-baseline.laravel-10.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
parameters:
ignoreErrors:
-
message: "#^Class App\\\\Models\\\\User not found\\.$#"
count: 1
path: src/FilamentSocialitePlugin.php

-
message: "#^Property DutchCodingCompany\\\\FilamentSocialite\\\\FilamentSocialitePlugin\\:\\:\\$userModelClass \\(class\\-string\\<Illuminate\\\\Contracts\\\\Auth\\\\Authenticatable\\>\\) does not accept default value of type string\\.$#"
count: 1
path: src/FilamentSocialitePlugin.php

-
message: "#^Parameter \\#1 \\$value of method DutchCodingCompany\\\\FilamentSocialite\\\\Http\\\\Controllers\\\\SocialiteLoginController\\:\\:evaluate\\(\\) expects bool\\|\\(callable\\(\\)\\: bool\\), bool\\|\\(Closure\\(string, Laravel\\\\Socialite\\\\Contracts\\\\User, Illuminate\\\\Contracts\\\\Auth\\\\Authenticatable\\|null\\)\\: bool\\) given\\.$#"
count: 1
path: src/Http/Controllers/SocialiteLoginController.php

-
message: "#^Generic type Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\BelongsToMany\\<DutchCodingCompany\\\\FilamentSocialite\\\\Tests\\\\Fixtures\\\\TestTeam, static\\(DutchCodingCompany\\\\FilamentSocialite\\\\Tests\\\\Fixtures\\\\TestTenantUser\\)\\> in PHPDoc tag @return specifies 2 template types, but class Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\BelongsToMany supports only 1\\: TRelatedModel$#"
count: 1
path: tests/Fixtures/TestTenantUser.php

-
message: "#^Method DutchCodingCompany\\\\FilamentSocialite\\\\Tests\\\\Fixtures\\\\TestTenantUser\\:\\:teams\\(\\) should return Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\BelongsToMany\\<DutchCodingCompany\\\\FilamentSocialite\\\\Tests\\\\Fixtures\\\\TestTeam, static\\(DutchCodingCompany\\\\FilamentSocialite\\\\Tests\\\\Fixtures\\\\TestTenantUser\\)\\> but returns Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\BelongsToMany\\<DutchCodingCompany\\\\FilamentSocialite\\\\Tests\\\\Fixtures\\\\TestTeam\\>\\.$#"
count: 1
path: tests/Fixtures/TestTenantUser.php

-
message: "#^Call to an undefined method Mockery\\\\ExpectationInterface\\|Mockery\\\\HigherOrderMessage\\:\\:andReturn\\(\\)\\.$#"
count: 3
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
path: tests/TestCase.php
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: src/Http/Controllers/SocialiteLoginController.php

-
message: "#^Method DutchCodingCompany\\\\FilamentSocialite\\\\Tests\\\\Fixtures\\\\TestTenantUser\\:\\:teams\\(\\) should return Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\BelongsToMany\\<DutchCodingCompany\\\\FilamentSocialite\\\\Tests\\\\Fixtures\\\\TestTeam, static\\(DutchCodingCompany\\\\FilamentSocialite\\\\Tests\\\\Fixtures\\\\TestTenantUser\\)\\> but returns Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\BelongsToMany\\<DutchCodingCompany\\\\FilamentSocialite\\\\Tests\\\\Fixtures\\\\TestTeam, \\$this\\(DutchCodingCompany\\\\FilamentSocialite\\\\Tests\\\\Fixtures\\\\TestTenantUser\\)\\>\\.$#"
count: 1
path: tests/Fixtures/TestTenantUser.php

-
message: "#^Call to an undefined method Mockery\\\\ExpectationInterface\\|Mockery\\\\HigherOrderMessage\\:\\:andReturn\\(\\)\\.$#"
count: 3
Expand Down
12 changes: 12 additions & 0 deletions phpstan.laravel-10.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
includes:
- ./vendor/larastan/larastan/extension.neon
- ./phpstan-baseline.laravel-10.neon

parameters:
level: 8

paths:
- config
- database
- src
- tests
6 changes: 3 additions & 3 deletions resources/views/components/buttons.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@endforeach
@endif

@if (count($providers))
@if (count($visibleProviders))
@if($showDivider)
<div class="relative flex items-center justify-center text-center">
<div class="absolute border-t border-gray-200 w-full h-px"></div>
Expand All @@ -15,8 +15,8 @@
</div>
@endif

<div class="grid @if(count($providers) > 1) grid-cols-2 @endif gap-4">
@foreach($providers as $key => $provider)
<div class="grid @if(count($visibleProviders) > 1) grid-cols-2 @endif gap-4">
@foreach($visibleProviders as $key => $provider)
<x-filament::button
:color="$provider->getColor()"
:outlined="$provider->getOutlined()"
Expand Down
2 changes: 2 additions & 0 deletions src/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace DutchCodingCompany\FilamentSocialite;

use Closure;
use DutchCodingCompany\FilamentSocialite\Traits\CanBeHidden;
use Filament\Support\Colors\Color;
use Filament\Support\Concerns\EvaluatesClosures;
use Illuminate\Support\Str;

class Provider
{
use EvaluatesClosures;
use CanBeHidden;

protected string $name;

Expand Down
10 changes: 5 additions & 5 deletions src/Traits/Callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ trait Callbacks
/**
* @param ?\Closure(string $provider, \Laravel\Socialite\Contracts\User $oauthUser, \DutchCodingCompany\FilamentSocialite\FilamentSocialitePlugin $plugin): \Illuminate\Contracts\Auth\Authenticatable $callback
*/
public function createUserUsing(Closure $callback = null): static
public function createUserUsing(?Closure $callback = null): static
{
$this->createUserUsing = $callback;

Expand Down Expand Up @@ -99,9 +99,9 @@ public function getRedirectAfterLoginUsing(): Closure
}

/**
* @param \Closure(string $provider, \Laravel\Socialite\Contracts\User $oauthUser, \DutchCodingCompany\FilamentSocialite\FilamentSocialitePlugin $plugin): ?(\Illuminate\Contracts\Auth\Authenticatable) $callback
* @param ?\Closure(string $provider, \Laravel\Socialite\Contracts\User $oauthUser, \DutchCodingCompany\FilamentSocialite\FilamentSocialitePlugin $plugin): ?(\Illuminate\Contracts\Auth\Authenticatable) $callback
*/
public function resolveUserUsing(Closure $callback = null): static
public function resolveUserUsing(?Closure $callback = null): static
{
$this->resolveUserUsing = $callback;

Expand All @@ -125,9 +125,9 @@ public function getResolveUserUsing(): Closure
}

/**
* @param \Closure(\DutchCodingCompany\FilamentSocialite\FilamentSocialitePlugin $plugin, \Laravel\Socialite\Contracts\User $oauthUser): bool $callback
* @param ?\Closure(\DutchCodingCompany\FilamentSocialite\FilamentSocialitePlugin $plugin, \Laravel\Socialite\Contracts\User $oauthUser): bool $callback
*/
public function authorizeUserUsing(Closure $callback = null): static
public function authorizeUserUsing(?Closure $callback = null): static
{
$this->authorizeUserUsing = $callback;

Expand Down
40 changes: 40 additions & 0 deletions src/Traits/CanBeHidden.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace DutchCodingCompany\FilamentSocialite\Traits;

use Closure;

trait CanBeHidden
{
protected bool | Closure $isHidden = false;

protected bool | Closure $isVisible = true;

public function hidden(bool | Closure $condition = true): static
{
$this->isHidden = $condition;

return $this;
}

public function visible(bool | Closure $condition = true): static
{
$this->isVisible = $condition;

return $this;
}

public function isHidden(): bool
{
if ($this->evaluate($this->isHidden)) {
return true;
}

return ! $this->evaluate($this->isVisible);
}

public function isVisible(): bool
{
return ! $this->isHidden();
}
}
4 changes: 3 additions & 1 deletion src/View/Components/Buttons.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace DutchCodingCompany\FilamentSocialite\View\Components;

use DutchCodingCompany\FilamentSocialite\FilamentSocialitePlugin;
use DutchCodingCompany\FilamentSocialite\Provider;
use Illuminate\Support\MessageBag;
use Illuminate\View\Component;

Expand All @@ -28,7 +29,8 @@ public function render()
}

return view('filament-socialite::components.buttons', [
'providers' => $this->plugin->getProviders(),
'providers' => $providers = $this->plugin->getProviders(),
'visibleProviders' => array_filter($providers, fn (Provider $provider) => $provider->isVisible()),
'socialiteRoute' => $this->plugin->getRoute(),
'messageBag' => $messageBag,
]);
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/TestTenantUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getTenants(Panel $panel): Collection
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<\DutchCodingCompany\FilamentSocialite\Tests\Fixtures\TestTeam>
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<\DutchCodingCompany\FilamentSocialite\Tests\Fixtures\TestTeam, static>
*/
public function teams(): BelongsToMany
{
Expand Down

0 comments on commit c9a801e

Please sign in to comment.