PHP code example of ebrook / filament-keycloak-web-guard

1. Go to this page and download the library: Download ebrook/filament-keycloak-web-guard library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ebrook / filament-keycloak-web-guard example snippets


'redirect_url' => '/admin',

'routes' => [
    'login' => 'login',
    'logout' => 'logout',
    'register' => 'register',
    'callback' => 'callback',
]

'scopes' => [],

'scopes' => ['example_scope_1', 'example_scope_2'],

'guards' => [
    'web' => [
        'driver' => 'keycloak-web',
        'provider' => 'users',
    ],

    // ...
],

'providers' => [
    'users' => [
        'driver' => 'keycloak-users',
        'model' => Ebrook\KeycloakWebGuard\Models\KeycloakUser::class,
    ],

    // ...
]

// app/Providers/Filament/AdminPanelProvider.php
use Ebrook\KeycloakWebGuard\Filament\Pages\Auth\KeycloakLogin;
use Ebrook\KeycloakWebGuard\Filament\Http\Middleware\FilamentKeycloakAuthenticate;

$panel
    ->login(KeycloakLogin::class)
    ->authMiddleware([
        FilamentKeycloakAuthenticate::class,
    ]);

// app/Providers/AppServiceProvider.php
use Filament\Auth\Http\Responses\Contracts\LogoutResponse;
use Ebrook\KeycloakWebGuard\Filament\Http\Responses\KeycloakLogoutResponse;

// place inside register() (or boot()) so the binding is applied
public function register(): void
{
    $this->app->bind(LogoutResponse::class, KeycloakLogoutResponse::class);
}

if (Gate::denies('keycloak-web', 'manage-account')) {
  return abort(403);
}

if (Gate::denies('keycloak-web', ['manage-account'])) {
  return abort(403);
}

if (Gate::denies('keycloak-web', 'manage-account', 'another-resource')) {
  return abort(403);
}

$this->middleware('keycloak-web-can:manage-something-cool');

// For multiple roles, separate with '|'
$this->middleware('keycloak-web-can:manage-something-cool|manage-something-nice|manage-my-application');

// On RouteServiceProvider.php for example

Route::prefix('admin')
  ->middleware('keycloak-web')
  ->namespace($this->namespace)
  ->group(base_path('routes/web.php'));

// Or with Route facade in another place

Route::group(['middleware' => 'keycloak-web'], function () {
    Route::get('/admin', 'Controller@admin');
});

// On your EncryptCookies middleware

class EncryptCookies extends Middleware
{
    protected $except = [];

    public function __construct(EncrypterContract $encrypter)
    {
        parent::__construct($encrypter);

        /**
         * This will disable in runtime.
         *
         * If you have a "session.cookie" option or don't care about changing the app name
         * (in another environment, for example), you can only add it to "$except" array on top
         */
        $this->disableFor(config('session.cookie'));
    }
}


php artisan vendor:publish  --provider="Ebrook\KeycloakWebGuard\KeycloakWebGuardServiceProvider"