From 30e0009b94f4de8fb738f5c376852fbe3d889cdc Mon Sep 17 00:00:00 2001 From: Bozhidar Date: Fri, 26 Apr 2024 18:22:47 +0300 Subject: [PATCH] impersonate function on customers --- .../Filament/CustomerPanelProvider.php | 4 +- .../Filament/Resources/CustomerResource.php | 14 ++- web/app/Models/Customer.php | 10 +- web/app/Models/User.php | 14 ++- web/composer.json | 1 + web/composer.lock | 111 +++++++++++++++++- web/config/auth.php | 14 +++ web/routes/web.php | 4 + 8 files changed, 164 insertions(+), 8 deletions(-) diff --git a/web/Modules/Customer/App/Providers/Filament/CustomerPanelProvider.php b/web/Modules/Customer/App/Providers/Filament/CustomerPanelProvider.php index d11977a..265735a 100644 --- a/web/Modules/Customer/App/Providers/Filament/CustomerPanelProvider.php +++ b/web/Modules/Customer/App/Providers/Filament/CustomerPanelProvider.php @@ -17,6 +17,7 @@ use Illuminate\Routing\Middleware\SubstituteBindings; use Illuminate\Session\Middleware\AuthenticateSession; use Illuminate\Session\Middleware\StartSession; use Illuminate\View\Middleware\ShareErrorsFromSession; +use Modules\Customer\App\Http\Middleware\CustomerAuthenticate; class CustomerPanelProvider extends PanelProvider { @@ -44,7 +45,7 @@ class CustomerPanelProvider extends PanelProvider return $panel ->id('customer::admin') ->path('customer') - + ->login() ->font('Albert Sans') ->sidebarWidth('15rem') // ->brandLogo(fn () => view('filament.admin.logo')) @@ -74,6 +75,7 @@ class CustomerPanelProvider extends PanelProvider DisableBladeIconComponents::class, DispatchServingFilamentEvent::class, ]) + ->authGuard('web_customer') ->authMiddleware([ Authenticate::class, ]); diff --git a/web/app/Filament/Resources/CustomerResource.php b/web/app/Filament/Resources/CustomerResource.php index 7190e35..618364e 100644 --- a/web/app/Filament/Resources/CustomerResource.php +++ b/web/app/Filament/Resources/CustomerResource.php @@ -10,6 +10,7 @@ use Filament\Forms\Form; use Filament\Resources\Resource; use Filament\Tables; use Filament\Tables\Table; +use STS\FilamentImpersonate\Tables\Actions\Impersonate; class CustomerResource extends Resource { @@ -113,19 +114,26 @@ class CustomerResource extends Resource ->label('Customer Name') ->searchable() ->sortable(), - Tables\Columns\TextColumn::make('username') - ->searchable() - ->sortable(), +// Tables\Columns\TextColumn::make('username') +// ->searchable() +// ->sortable(), Tables\Columns\TextColumn::make('email') ->searchable() ->sortable(), + + ]) ->defaultSort('id', 'desc') ->filters([ // ]) ->actions([ + + Impersonate::make('impersonate') + ->guard('web_customer') + ->redirectTo(route('filament.customer::admin.pages.dashboard')), + Tables\Actions\EditAction::make(), Tables\Actions\DeleteAction::make(), ]) diff --git a/web/app/Models/Customer.php b/web/app/Models/Customer.php index d5aa9f9..f4ef0d1 100644 --- a/web/app/Models/Customer.php +++ b/web/app/Models/Customer.php @@ -4,9 +4,9 @@ namespace App\Models; use App\ApiSDK\PhyreApiSDK; use Illuminate\Database\Eloquent\Factories\HasFactory; -use Illuminate\Database\Eloquent\Model; +use Illuminate\Foundation\Auth\User as Authenticatable; -class Customer extends Model +class Customer extends Authenticatable { use HasFactory; @@ -69,4 +69,10 @@ class Customer extends Model { return $this->hasMany(HostingSubscription::class); } + + public function canBeImpersonated() + { + return true; + } + } diff --git a/web/app/Models/User.php b/web/app/Models/User.php index 4d7f70f..107a953 100644 --- a/web/app/Models/User.php +++ b/web/app/Models/User.php @@ -3,12 +3,14 @@ namespace App\Models; // use Illuminate\Contracts\Auth\MustVerifyEmail; +use Filament\Models\Contracts\FilamentUser; +use Filament\Panel; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; -class User extends Authenticatable +class User extends Authenticatable implements FilamentUser { use HasApiTokens, HasFactory, Notifiable; @@ -42,4 +44,14 @@ class User extends Authenticatable 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; + + public function canAccessPanel(Panel $panel): bool + { + return true; + } + + public function canImpersonate() + { + return true; + } } diff --git a/web/composer.json b/web/composer.json index 8fa66b7..b892c16 100644 --- a/web/composer.json +++ b/web/composer.json @@ -26,6 +26,7 @@ "phpseclib/phpseclib": "^3.0", "postare/blade-mdi": "^1.1", "spatie/ssh": "^1.10", + "stechstudio/filament-impersonate": "^3.8", "symfony/process": "^6.3", "symfony/yaml": "^7.0", "torann/geoip": "^3.0", diff --git a/web/composer.lock b/web/composer.lock index b7afbd7..931f3d3 100644 --- a/web/composer.lock +++ b/web/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "24ce03daacd794976d8e4ad932a3acc2", + "content-hash": "24c114639ffad26d47dac674a59e408e", "packages": [ { "name": "acmephp/core", @@ -2723,6 +2723,73 @@ }, "time": "2024-04-09T00:35:30+00:00" }, + { + "name": "lab404/laravel-impersonate", + "version": "1.7.5", + "source": { + "type": "git", + "url": "https://github.com/404labfr/laravel-impersonate.git", + "reference": "82cad73700a8699d63de169bb41abd5ae283e9a8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/404labfr/laravel-impersonate/zipball/82cad73700a8699d63de169bb41abd5ae283e9a8", + "reference": "82cad73700a8699d63de169bb41abd5ae283e9a8", + "shasum": "" + }, + "require": { + "laravel/framework": "^6.0 | ^7.0 | ^8.0 | ^9.0 | ^10.0 | ^11.0", + "php": "^7.2 | ^8.0" + }, + "require-dev": { + "mockery/mockery": "^1.3.3", + "orchestra/testbench": "^4.0 | ^5.0 | ^6.0 | ^7.0 | ^8.0 | ^9.0", + "phpunit/phpunit": "^7.5 | ^8.0 | ^9.0 | ^10.0" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Lab404\\Impersonate\\ImpersonateServiceProvider" + ] + } + }, + "autoload": { + "files": [ + "src/helpers.php" + ], + "psr-4": { + "Lab404\\Impersonate\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Marceau Casals", + "email": "marceau@casals.fr" + } + ], + "description": "Laravel Impersonate is a plugin that allows to you to authenticate as your users.", + "keywords": [ + "auth", + "impersonate", + "impersonation", + "laravel", + "laravel-package", + "laravel-plugin", + "package", + "plugin", + "user" + ], + "support": { + "issues": "https://github.com/404labfr/laravel-impersonate/issues", + "source": "https://github.com/404labfr/laravel-impersonate/tree/1.7.5" + }, + "time": "2024-03-11T14:26:14+00:00" + }, { "name": "laravel/framework", "version": "v10.48.8", @@ -6587,6 +6654,48 @@ ], "time": "2024-04-05T09:15:00+00:00" }, + { + "name": "stechstudio/filament-impersonate", + "version": "3.8", + "source": { + "type": "git", + "url": "https://github.com/stechstudio/filament-impersonate.git", + "reference": "d24a9ffc1ef2f87940d151ca1cb2c2d2e5e524a8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/stechstudio/filament-impersonate/zipball/d24a9ffc1ef2f87940d151ca1cb2c2d2e5e524a8", + "reference": "d24a9ffc1ef2f87940d151ca1cb2c2d2e5e524a8", + "shasum": "" + }, + "require": { + "filament/filament": "^3.0", + "lab404/laravel-impersonate": "^1.7" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "STS\\FilamentImpersonate\\FilamentImpersonateServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "STS\\FilamentImpersonate\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "A Filament package to impersonate your users.", + "support": { + "issues": "https://github.com/stechstudio/filament-impersonate/issues", + "source": "https://github.com/stechstudio/filament-impersonate/tree/3.8" + }, + "time": "2024-03-25T03:05:55+00:00" + }, { "name": "swagger-api/swagger-ui", "version": "v5.16.0", diff --git a/web/config/auth.php b/web/config/auth.php index 9548c15..62d73c7 100644 --- a/web/config/auth.php +++ b/web/config/auth.php @@ -40,6 +40,10 @@ return [ 'driver' => 'session', 'provider' => 'users', ], + 'web_customer' => [ + 'driver' => 'session', + 'provider' => 'customers', + ], ], /* @@ -64,6 +68,10 @@ return [ 'driver' => 'eloquent', 'model' => App\Models\User::class, ], + 'customers' => [ + 'driver' => 'eloquent', + 'model' => App\Models\Customer::class, + ], // 'users' => [ // 'driver' => 'database', @@ -97,6 +105,12 @@ return [ 'expire' => 60, 'throttle' => 60, ], + 'customers' => [ + 'provider' => 'customers', + 'table' => 'password_reset_tokens', + 'expire' => 60, + 'throttle' => 60, + ], ], /* diff --git a/web/routes/web.php b/web/routes/web.php index 616268b..a954a6f 100644 --- a/web/routes/web.php +++ b/web/routes/web.php @@ -14,6 +14,10 @@ use phpseclib3\Net\SSH2; | */ +Route::get('/login', function () { + return redirect(route('filament.admin.auth.login')); +})->name('login'); + if (!file_exists(storage_path('installed'))) { Route::get('/', \App\Livewire\Installer::class);