Skip to content
Merged
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
35 changes: 35 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Tests

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest

strategy:
fail-fast: true
matrix:
php: [8.2, 8.3, 8.4]

name: PHP ${{ matrix.php }}

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite
coverage: none

- name: Install dependencies
run: composer install --prefer-dist --no-interaction --no-progress

- name: Run tests
run: vendor/bin/pest
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor
.idea
.idea
.phpunit.cache
9 changes: 9 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ The `accessToken` can be set:
1. **Globally** via `config/filament-userback.php` - applies to all panels
2. **Per-panel** via `->accessToken()` method - overrides global config

The `only_authenticated` setting controls whether the widget is shown only to authenticated users:
1. **Globally** via `config/filament-userback.php` (default: `true`)
2. **Per-panel** via `->onlyAuthenticated()` method - overrides global config

The `guard` setting specifies which authentication guard to use:
1. **Per-panel** via `->guard()` method - highest priority
2. **Globally** via `config/filament-userback.php`
3. **Fallback** to the panel's configured auth guard (`$panel->getAuthGuard()`)

## Common Tasks

### Testing the plugin locally
Expand Down
18 changes: 18 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,36 @@
"illuminate/contracts": "^11.0|^12.0",
"spatie/laravel-package-tools": "^1.16"
},
"require-dev": {
"orchestra/testbench": "^9.0|^10.0",
"pestphp/pest": "^3.0",
"pestphp/pest-plugin-laravel": "^3.0"
},
"autoload": {
"psr-4": {
"CoddinWeb\\FilamentUserback\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"CoddinWeb\\FilamentUserback\\Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
"CoddinWeb\\FilamentUserback\\FilamentUserbackServiceProvider"
]
}
},
"scripts": {
"test": "vendor/bin/pest"
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
28 changes: 28 additions & 0 deletions config/filament-userback.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,32 @@
*/

'access_token' => env('USERBACK_ACCESS_TOKEN'),

/*
|--------------------------------------------------------------------------
| Only Authenticated Users
|--------------------------------------------------------------------------
|
| When set to true, the Userback widget will only be rendered for
| authenticated users. Set to false to show the widget to all visitors.
|
| This can be overridden per-panel using the ->onlyAuthenticated() method.
|
*/

'only_authenticated' => env('USERBACK_ONLY_AUTHENTICATED', true),

/*
|--------------------------------------------------------------------------
| Authentication Guard
|--------------------------------------------------------------------------
|
| The authentication guard to use when checking if a user is authenticated.
| If not set, it will fall back to the panel's configured auth guard.
|
| This can be overridden per-panel using the ->guard() method.
|
*/

'guard' => env('USERBACK_GUARD'),
];
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
cacheDirectory=".phpunit.cache"
>
<testsuites>
<testsuite name="Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
2 changes: 1 addition & 1 deletion resources/views/userback.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@if(\is_string($accessToken))
@if(\is_string($accessToken) && (!$onlyAuthenticated || auth($guard)->check()))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
<script>
window.Userback = window.Userback || {};
Userback.access_token = @js($accessToken);
Expand Down
57 changes: 57 additions & 0 deletions src/UserbackPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class UserbackPlugin implements Plugin
{
protected ?string $accessToken = null;

protected ?bool $onlyAuthenticated = null;

protected ?string $guard = null;

protected ?Closure $userDataUsing = null;

public static function make(): static
Expand All @@ -42,6 +46,20 @@ public function userDataUsing(?Closure $callback): static
return $this;
}

public function onlyAuthenticated(bool $onlyAuthenticated = true): static
{
$this->onlyAuthenticated = $onlyAuthenticated;

return $this;
}

public function guard(?string $guard): static
{
$this->guard = $guard;

return $this;
}

public function register(Panel $panel): void
{
}
Expand All @@ -63,6 +81,8 @@ public function boot(Panel $panel): void
[
'accessToken' => $this->getAccessToken(),
'userData' => $this->getUserData(),
'onlyAuthenticated' => $this->getOnlyAuthenticated(),
'guard' => $this->getGuard($panel),
],
),
);
Expand All @@ -89,6 +109,43 @@ protected function getAccessToken(): ?string
return null;
}

protected function getOnlyAuthenticated(): bool
{
if (\is_bool($this->onlyAuthenticated)) {
return $this->onlyAuthenticated;
}

$configValue = Config::get('filament-userback.only_authenticated');

return \is_bool($configValue) ? $configValue : true;
}

protected function getGuard(Panel $panel): ?string
{
$guard = null;

if (\is_string($this->guard)) {
$guard = $this->guard;
} elseif (\is_string($configGuard = Config::get('filament-userback.guard'))) {
$guard = $configGuard;
} else {
$guard = $panel->getAuthGuard();
}

return $this->isValidGuard($guard) ? $guard : null;
}

protected function isValidGuard(?string $guard): bool
{
if ($guard === null) {
return true;
}

$guards = Config::get('auth.guards', []);

return \is_array($guards) && \array_key_exists($guard, $guards);
}

/**
* @return array<string, mixed>|null
*/
Expand Down
Loading