-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Impersonation action log implementation (#13)
- Loading branch information
Showing
20 changed files
with
383 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Jampire\MoonshineImpersonate\Database\Factories; | ||
|
||
use Illuminate\Support\Facades\Hash; | ||
use Illuminate\Support\Str; | ||
use Jampire\MoonshineImpersonate\Tests\Stubs\Models\NonLogUser; | ||
use Orchestra\Testbench\Factories\UserFactory as TestbenchUserFactory; | ||
|
||
/** | ||
* Class NonLogUserFactory | ||
* | ||
* @author Dzianis Kotau <[email protected]> | ||
*/ | ||
class NonLogUserFactory extends TestbenchUserFactory | ||
{ | ||
protected $model = NonLogUser::class; | ||
|
||
/** | ||
* @return array{name: string, email: string, email_verified_at: \Illuminate\Support\Carbon, password: string, remember_token: string} | ||
*/ | ||
public function definition(): array | ||
{ | ||
return [ | ||
'name' => fake()->name(), | ||
'email' => fake()->unique()->safeEmail(), | ||
'email_verified_at' => now(), | ||
'password' => Hash::make('secret'), | ||
'remember_token' => Str::random(10), | ||
]; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
database/migrations/create_moonshine_change_logs_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateMoonShineChangeLogsTable extends Migration | ||
{ | ||
public function up(): void | ||
{ | ||
Schema::create('moonshine_change_logs', function (Blueprint $table): void { | ||
$table->id(); | ||
$table->foreignId('moonshine_user_id'); | ||
$table->morphs('changelogable'); | ||
$table->longText('states_before'); | ||
$table->longText('states_after'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::dropIfExists('moonshine_change_logs'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ | |
|
||
namespace Jampire\MoonshineImpersonate\Enums; | ||
|
||
/** | ||
* @author Dzianis Kotau <[email protected]> | ||
*/ | ||
enum Permission: string | ||
{ | ||
case IMPERSONATE = 'impersonate'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Jampire\MoonshineImpersonate\Enums; | ||
|
||
/** | ||
* @author Dzianis Kotau <[email protected]> | ||
*/ | ||
enum State: string | ||
{ | ||
case IMPERSONATION_ENTERED = 'impersonation_entered'; | ||
case IMPERSONATION_STOPPED = 'impersonation_stopped'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Jampire\MoonshineImpersonate\Listeners; | ||
|
||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Jampire\MoonshineImpersonate\Enums\State; | ||
use Jampire\MoonshineImpersonate\Events\ImpersonationEntered; | ||
|
||
/** | ||
* Class LogImpersonationEnter | ||
* | ||
* @author Dzianis Kotau <[email protected]> | ||
*/ | ||
class LogImpersonationEnter implements ShouldQueue | ||
{ | ||
public function handle(ImpersonationEntered $event): void | ||
{ | ||
$event->impersonated->changeLogs()->create([ | ||
'moonshine_user_id' => $event->impersonator->getAuthIdentifier(), | ||
'states_before' => State::IMPERSONATION_STOPPED->value, | ||
'states_after' => State::IMPERSONATION_ENTERED->value, | ||
]); | ||
} | ||
|
||
public function shouldQueue(ImpersonationEntered $event): bool | ||
{ | ||
return method_exists($event->impersonated, 'changeLogs'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Jampire\MoonshineImpersonate\Listeners; | ||
|
||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Jampire\MoonshineImpersonate\Enums\State; | ||
use Jampire\MoonshineImpersonate\Events\ImpersonationStopped; | ||
|
||
/** | ||
* Class LogImpersonationStopped | ||
* | ||
* @author Dzianis Kotau <[email protected]> | ||
*/ | ||
class LogImpersonationStopped implements ShouldQueue | ||
{ | ||
public function handle(ImpersonationStopped $event): void | ||
{ | ||
$event->impersonated->changeLogs()->create([ | ||
'moonshine_user_id' => $event->impersonator->getAuthIdentifier(), | ||
'states_before' => State::IMPERSONATION_ENTERED->value, | ||
'states_after' => State::IMPERSONATION_STOPPED->value, | ||
]); | ||
} | ||
|
||
public function shouldQueue(ImpersonationStopped $event): bool | ||
{ | ||
return method_exists($event->impersonated, 'changeLogs'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Jampire\MoonshineImpersonate\Providers; | ||
|
||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; | ||
use Jampire\MoonshineImpersonate\Events\ImpersonationEntered; | ||
use Jampire\MoonshineImpersonate\Events\ImpersonationStopped; | ||
use Jampire\MoonshineImpersonate\Listeners\LogImpersonationEnter; | ||
use Jampire\MoonshineImpersonate\Listeners\LogImpersonationStopped; | ||
|
||
/** | ||
* Class EventServiceProvider | ||
* | ||
* @author Dzianis Kotau <[email protected]> | ||
*/ | ||
class EventServiceProvider extends ServiceProvider | ||
{ | ||
protected $listen = [ | ||
ImpersonationEntered::class => [ | ||
LogImpersonationEnter::class, | ||
], | ||
ImpersonationStopped::class => [ | ||
LogImpersonationStopped::class, | ||
], | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Support\Facades\Event; | ||
use Jampire\MoonshineImpersonate\Enums\State; | ||
use Jampire\MoonshineImpersonate\Events\ImpersonationEntered; | ||
use Jampire\MoonshineImpersonate\Listeners\LogImpersonationEnter; | ||
use Jampire\MoonshineImpersonate\Support\Settings; | ||
use Jampire\MoonshineImpersonate\Tests\Stubs\Models\MoonshineUser; | ||
use Jampire\MoonshineImpersonate\Tests\Stubs\Models\NonLogUser; | ||
use Jampire\MoonshineImpersonate\Tests\Stubs\Models\User; | ||
|
||
use function Pest\Laravel\actingAs; | ||
use function Pest\Laravel\assertDatabaseEmpty; | ||
use function Pest\Laravel\assertDatabaseHas; | ||
use function Pest\Laravel\post; | ||
|
||
it('handles enter impersonation mode', function (): void { | ||
$user = User::factory()->create(); | ||
$moonShineUser = MoonshineUser::factory()->create(); | ||
|
||
assertDatabaseEmpty('moonshine_change_logs'); | ||
|
||
$event = new ImpersonationEntered($moonShineUser, $user); | ||
$listener = new LogImpersonationEnter(); | ||
$listener->handle($event); | ||
|
||
assertDatabaseHas('moonshine_change_logs', [ | ||
'moonshine_user_id' => $moonShineUser->getAuthIdentifier(), | ||
'changelogable_type' => $user::class, | ||
'changelogable_id' => $user->getKey(), | ||
'states_before' => '"'.State::IMPERSONATION_STOPPED->value.'"', | ||
'states_after' => '"'.State::IMPERSONATION_ENTERED->value.'"', | ||
]); | ||
}); | ||
|
||
it('listens enter impersonation event', function (): void { | ||
Event::fake(); | ||
|
||
$user = User::factory()->create(); | ||
$moonShineUser = MoonshineUser::factory()->create(); | ||
|
||
actingAs($moonShineUser, Settings::moonShineGuard()); | ||
post(route_impersonate('enter'), [ | ||
'id' => $user->id, | ||
])->assertSessionHasNoErrors(); | ||
|
||
Event::assertListening( | ||
ImpersonationEntered::class, | ||
LogImpersonationEnter::class | ||
); | ||
}); | ||
|
||
it('cannot log enter impersonation mode without changeLogs relation', function (): void { | ||
config(['auth.providers.users.model' => NonLogUser::class]); | ||
|
||
$user = NonLogUser::factory()->create(); | ||
$moonShineUser = MoonshineUser::factory()->create(); | ||
|
||
$event = new ImpersonationEntered($moonShineUser, $user); | ||
$listener = new LogImpersonationEnter(); | ||
|
||
expect($listener->shouldQueue($event)) | ||
->toBeFalse(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Support\Facades\Event; | ||
use Jampire\MoonshineImpersonate\Enums\State; | ||
use Jampire\MoonshineImpersonate\Events\ImpersonationStopped; | ||
use Jampire\MoonshineImpersonate\Listeners\LogImpersonationStopped; | ||
use Jampire\MoonshineImpersonate\Support\Settings; | ||
use Jampire\MoonshineImpersonate\Tests\Stubs\Models\MoonshineUser; | ||
use Jampire\MoonshineImpersonate\Tests\Stubs\Models\NonLogUser; | ||
use Jampire\MoonshineImpersonate\Tests\Stubs\Models\User; | ||
|
||
use function Pest\Laravel\actingAs; | ||
use function Pest\Laravel\assertDatabaseEmpty; | ||
use function Pest\Laravel\assertDatabaseHas; | ||
use function Pest\Laravel\get; | ||
|
||
it('handles stopped impersonation mode', function (): void { | ||
$user = User::factory()->create(); | ||
$moonShineUser = MoonshineUser::factory()->create(); | ||
|
||
assertDatabaseEmpty('moonshine_change_logs'); | ||
|
||
$event = new ImpersonationStopped($moonShineUser, $user); | ||
$listener = new LogImpersonationStopped(); | ||
$listener->handle($event); | ||
|
||
assertDatabaseHas('moonshine_change_logs', [ | ||
'moonshine_user_id' => $moonShineUser->getAuthIdentifier(), | ||
'changelogable_type' => $user::class, | ||
'changelogable_id' => $user->getKey(), | ||
'states_before' => '"'.State::IMPERSONATION_ENTERED->value.'"', | ||
'states_after' => '"'.State::IMPERSONATION_STOPPED->value.'"', | ||
]); | ||
}); | ||
|
||
it('listens stopped impersonation event', function (): void { | ||
Event::fake(); | ||
|
||
$user = User::factory()->create(); | ||
$moonShineUser = MoonshineUser::factory()->create(); | ||
|
||
actingAs($moonShineUser, Settings::moonShineGuard()) | ||
->withSession([Settings::key() => $user->getKey()]); | ||
|
||
get(route_impersonate('stop')) | ||
->assertSessionHasNoErrors(); | ||
|
||
Event::assertListening( | ||
ImpersonationStopped::class, | ||
LogImpersonationStopped::class | ||
); | ||
}); | ||
|
||
it('cannot log stopped impersonation mode without changeLogs relation', function (): void { | ||
config(['auth.providers.users.model' => NonLogUser::class]); | ||
|
||
$user = NonLogUser::factory()->create(); | ||
$moonShineUser = MoonshineUser::factory()->create(); | ||
|
||
$event = new ImpersonationStopped($moonShineUser, $user); | ||
$listener = new LogImpersonationStopped(); | ||
|
||
expect($listener->shouldQueue($event)) | ||
->toBeFalse(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.