From b2e5668d82d408af8ad5666056b9affc2dc537f3 Mon Sep 17 00:00:00 2001 From: Paula Quispe Date: Tue, 18 Jul 2023 16:48:52 -0400 Subject: [PATCH 1/2] FOUR-9390 --- ProcessMaker/Events/UserRestored.php | 66 ++++++ .../Http/Controllers/Api/UserController.php | 4 + .../Providers/EventServiceProvider.php | 2 + resources/lang/en.json | 1 + tests/Feature/Api/SecurityLogsTest.php | 199 +++++++++++++++++- 5 files changed, 268 insertions(+), 4 deletions(-) create mode 100644 ProcessMaker/Events/UserRestored.php diff --git a/ProcessMaker/Events/UserRestored.php b/ProcessMaker/Events/UserRestored.php new file mode 100644 index 0000000000..6e146ade1e --- /dev/null +++ b/ProcessMaker/Events/UserRestored.php @@ -0,0 +1,66 @@ +user = $data; + } + + /** + * Get specific data related to the event + * + * @return array + */ + public function getData(): array + { + return [ + 'name' => [ + 'label' => $this->user->getAttribute('username'), + 'link' => route('users.edit', $this->user), + ], + 'email' => $this->user->getAttribute('email'), + 'last_modified' => $this->user->getAttribute('updated_at'), + ]; + } + + /** + * Get specific changes without format related to the event + * + * @return array + */ + public function getChanges(): array + { + return [ + 'id' => $this->user->getAttribute('id'), + 'username' => $this->user->getAttribute('username'), + ]; + } + + /** + * Get the Event name with the syntax ‘[Past-test Action] [Object]’ + * + * @return string + */ + public function getEventName(): string + { + return 'UserRestored'; + } +} diff --git a/ProcessMaker/Http/Controllers/Api/UserController.php b/ProcessMaker/Http/Controllers/Api/UserController.php index da1cf4be64..9ed4e57563 100644 --- a/ProcessMaker/Http/Controllers/Api/UserController.php +++ b/ProcessMaker/Http/Controllers/Api/UserController.php @@ -9,6 +9,7 @@ use ProcessMaker\Events\UserCreated; use ProcessMaker\Events\UserDeleted; use ProcessMaker\Events\UserGroupMembershipUpdated; +use ProcessMaker\Events\UserRestored; use ProcessMaker\Events\UserUpdated; use ProcessMaker\Exception\ReferentialIntegrityException; use ProcessMaker\Http\Controllers\Controller; @@ -579,6 +580,9 @@ public function restore(Request $request) if ($user instanceof User) { $user->restore(); + + // Register the Event + UserRestored::dispatch($user); } return response([], 200); diff --git a/ProcessMaker/Providers/EventServiceProvider.php b/ProcessMaker/Providers/EventServiceProvider.php index 57bdaf3dd4..c2ce0bdc4f 100644 --- a/ProcessMaker/Providers/EventServiceProvider.php +++ b/ProcessMaker/Providers/EventServiceProvider.php @@ -55,6 +55,7 @@ use ProcessMaker\Events\UserCreated; use ProcessMaker\Events\UserDeleted; use ProcessMaker\Events\UserGroupMembershipUpdated; +use ProcessMaker\Events\UserRestored; use ProcessMaker\Events\UserUpdated; use ProcessMaker\Listeners\SecurityLogger; @@ -148,6 +149,7 @@ public function boot() $this->app['events']->listen(UserCreated::class, SecurityLogger::class); $this->app['events']->listen(UserDeleted::class, SecurityLogger::class); $this->app['events']->listen(UserGroupMembershipUpdated::class, SecurityLogger::class); + $this->app['events']->listen(UserRestored::class, SecurityLogger::class); $this->app['events']->listen(UserUpdated::class, SecurityLogger::class); } } diff --git a/resources/lang/en.json b/resources/lang/en.json index 68e8e893a7..e98b901511 100644 --- a/resources/lang/en.json +++ b/resources/lang/en.json @@ -1022,6 +1022,7 @@ "UserCreated": "User Created", "UserDeleted": "User Deleted", "UserGroupsUpdated": "User Groups Updated", + "UserRestored": "User Restored", "UserUpdated": "User Updated", "Username": "Username", "User_name": "Username", diff --git a/tests/Feature/Api/SecurityLogsTest.php b/tests/Feature/Api/SecurityLogsTest.php index 4c6af4f780..58e33e327d 100644 --- a/tests/Feature/Api/SecurityLogsTest.php +++ b/tests/Feature/Api/SecurityLogsTest.php @@ -6,14 +6,18 @@ use Faker\Factory as Faker; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Auth; +use ProcessMaker\Events\EnvironmentVariablesCreated; +use ProcessMaker\Events\EnvironmentVariablesDeleted; +use ProcessMaker\Events\EnvironmentVariablesUpdated; use ProcessMaker\Events\SettingsUpdated; -use ProcessMaker\Events\SignalCreated; -use ProcessMaker\Managers\SignalManager; +use ProcessMaker\Events\UserCreated; +use ProcessMaker\Events\UserDeleted; +use ProcessMaker\Events\UserRestored; +use ProcessMaker\Events\UserUpdated; +use ProcessMaker\Models\EnvironmentVariable; use ProcessMaker\Models\Permission; -use ProcessMaker\Models\Process; use ProcessMaker\Models\SecurityLog; use ProcessMaker\Models\Setting; -use ProcessMaker\Models\SignalData; use ProcessMaker\Models\User; use ProcessMaker\Providers\AuthServiceProvider; use Tests\Feature\Shared\RequestHelper; @@ -170,6 +174,85 @@ public function testStore() $this->assertIsObject($securityLog->data); } + /** + * This test Environment Variables Created + */ + public function testEnvironmentVariablesCreated() + { + $fields = [ + 'name' => 'var_1', + 'description' => 'description 1', + 'created_at' => '2019-01-01', + ]; + $vars = EnvironmentVariable::factory()->create($fields); + EnvironmentVariablesCreated::dispatch($fields); + $collection = SecurityLog::get(); + // Check if the variable security_log is enable + if (config('app.security_log')) { + $this->assertCount(1, $collection); + $securityLog = $collection->first(); + $this->assertEquals('EnvironmentVariablesCreated', $securityLog->getAttribute('event')); + $this->assertIsObject($securityLog->getAttribute('data')); + $this->assertArrayHasKey('name', get_object_vars($securityLog->getAttribute('data'))); + $this->assertArrayHasKey('created_at', get_object_vars($securityLog->getAttribute('data'))); + $this->assertIsObject($securityLog->getAttribute('changes')); + } else { + $this->assertCount(0, $collection); + } + } + + /** + * This test Environment Variables Deleted + */ + public function testEnvironmentVariablesDeleted() + { + $vars = EnvironmentVariable::factory()->create([ + 'name' => 'var_2', + ]); + EnvironmentVariablesDeleted::dispatch($vars); + $collection = SecurityLog::get(); + // Check if the variable security_log is enable + if (config('app.security_log')) { + $this->assertCount(1, $collection); + $securityLog = $collection->first(); + $this->assertEquals('EnvironmentVariablesDeleted', $securityLog->getAttribute('event')); + $this->assertIsObject($securityLog->getAttribute('data')); + $this->assertArrayHasKey('name', get_object_vars($securityLog->getAttribute('data'))); + $this->assertArrayHasKey('deleted_at', get_object_vars($securityLog->getAttribute('data'))); + $this->assertIsObject($securityLog->getAttribute('changes')); + } else { + $this->assertCount(0, $collection); + } + } + + /** + * This test Environment Variables Updated + */ + public function testEnvironmentVariablesUpdated() + { + $vars = EnvironmentVariable::factory()->create([ + 'name' => 'var_3', + ]); + $original = $vars->getOriginal(); + $vars->fill(['description' => 'test']); + $vars->save(); + $changes = $vars->getChanges(); + EnvironmentVariablesUpdated::dispatch($vars, $changes, $original); + $collection = SecurityLog::get(); + // Check if the variable security_log is enable + if (config('app.security_log')) { + $this->assertCount(1, $collection); + $securityLog = $collection->first(); + $this->assertEquals('EnvironmentVariablesUpdated', $securityLog->getAttribute('event')); + $this->assertIsObject($securityLog->getAttribute('data')); + $this->assertArrayHasKey('name', get_object_vars($securityLog->getAttribute('data'))); + $this->assertArrayHasKey('last_modified', get_object_vars($securityLog->getAttribute('data'))); + $this->assertIsObject($securityLog->getAttribute('changes')); + } else { + $this->assertCount(0, $collection); + } + } + /** * This test the Setting Update */ @@ -186,6 +269,114 @@ public function testSettingUpdated() $this->assertCount(1, $collection); $securityLog = $collection->first(); $this->assertEquals('SettingsUpdated', $securityLog->getAttribute('event')); + $this->assertIsObject($securityLog->getAttribute('data')); + $this->assertIsObject($securityLog->getAttribute('changes')); + } else { + $this->assertCount(0, $collection); + } + } + + /** + * This test User Created + */ + public function testUserCreated() + { + // Create a user created + $user = User::factory()->create([ + 'status' => 'ACTIVE', + ]); + UserCreated::dispatch($user); + $collection = SecurityLog::get(); + // Check if the variable security_log is enable + if (config('app.security_log')) { + $this->assertCount(1, $collection); + $securityLog = $collection->first(); + $this->assertEquals('UserCreated', $securityLog->getAttribute('event')); + $this->assertIsObject($securityLog->getAttribute('data')); + $this->assertArrayHasKey('name', get_object_vars($securityLog->getAttribute('data'))); + $this->assertArrayHasKey('created_at', get_object_vars($securityLog->getAttribute('data'))); + $this->assertIsObject($securityLog->getAttribute('changes')); + } else { + $this->assertCount(0, $collection); + } + } + + /** + * This test User Deleted + */ + public function testUserDeleted() + { + // Create a user deleted + $user = User::factory()->create([ + 'status' => 'ACTIVE', + ]); + $user->delete(); + UserDeleted::dispatch($user); + $collection = SecurityLog::get(); + // Check if the variable security_log is enable + if (config('app.security_log')) { + $this->assertCount(1, $collection); + $securityLog = $collection->first(); + $this->assertEquals('UserDeleted', $securityLog->getAttribute('event')); + $this->assertIsObject($securityLog->getAttribute('data')); + $this->assertArrayHasKey('name', get_object_vars($securityLog->getAttribute('data'))); + $this->assertArrayHasKey('deleted_at', get_object_vars($securityLog->getAttribute('data'))); + $this->assertIsObject($securityLog->getAttribute('changes')); + } else { + $this->assertCount(0, $collection); + } + } + + /** + * This test User Restored + */ + public function testUserRestored() + { + // Create a user restored + $user = User::factory()->create([ + 'deleted_at' => null, + 'status' => 'ACTIVE', + ]); + UserRestored::dispatch($user); + $collection = SecurityLog::get(); + // Check if the variable security_log is enable + if (config('app.security_log')) { + $this->assertCount(1, $collection); + $securityLog = $collection->first(); + $this->assertEquals('UserRestored', $securityLog->getAttribute('event')); + $this->assertIsObject($securityLog->getAttribute('data')); + $this->assertArrayHasKey('name', get_object_vars($securityLog->getAttribute('data'))); + $this->assertArrayHasKey('last_modified', get_object_vars($securityLog->getAttribute('data'))); + $this->assertIsObject($securityLog->getAttribute('changes')); + } else { + $this->assertCount(0, $collection); + } + } + + /** + * This test User Updated + */ + public function testUserUpdated() + { + // Create a user updated + $user = User::factory()->create([ + 'status' => 'ACTIVE', + ]); + $original = $user->getOriginal(); + $user->fill(['timezone' => 'America/Monterrey']); + $user->saveOrFail(); + $changes = $user->getChanges(); + UserUpdated::dispatch($user, $changes, $original); + $collection = SecurityLog::get(); + // Check if the variable security_log is enable + if (config('app.security_log')) { + $this->assertCount(1, $collection); + $securityLog = $collection->first(); + $this->assertEquals('UserUpdated', $securityLog->getAttribute('event')); + $this->assertIsObject($securityLog->getAttribute('data')); + $this->assertArrayHasKey('name', get_object_vars($securityLog->getAttribute('data'))); + $this->assertArrayHasKey('last_modified', get_object_vars($securityLog->getAttribute('data'))); + $this->assertIsObject($securityLog->getAttribute('changes')); } else { $this->assertCount(0, $collection); } From 58381d9d6ee2c9f5acc15f1e376640e07409876c Mon Sep 17 00:00:00 2001 From: Paula Quispe Date: Thu, 20 Jul 2023 08:22:48 -0400 Subject: [PATCH 2/2] Add test --- tests/Feature/Api/SecurityLogsTest.php | 185 ------------------------- 1 file changed, 185 deletions(-) diff --git a/tests/Feature/Api/SecurityLogsTest.php b/tests/Feature/Api/SecurityLogsTest.php index 58e33e327d..68530713ba 100644 --- a/tests/Feature/Api/SecurityLogsTest.php +++ b/tests/Feature/Api/SecurityLogsTest.php @@ -174,85 +174,6 @@ public function testStore() $this->assertIsObject($securityLog->data); } - /** - * This test Environment Variables Created - */ - public function testEnvironmentVariablesCreated() - { - $fields = [ - 'name' => 'var_1', - 'description' => 'description 1', - 'created_at' => '2019-01-01', - ]; - $vars = EnvironmentVariable::factory()->create($fields); - EnvironmentVariablesCreated::dispatch($fields); - $collection = SecurityLog::get(); - // Check if the variable security_log is enable - if (config('app.security_log')) { - $this->assertCount(1, $collection); - $securityLog = $collection->first(); - $this->assertEquals('EnvironmentVariablesCreated', $securityLog->getAttribute('event')); - $this->assertIsObject($securityLog->getAttribute('data')); - $this->assertArrayHasKey('name', get_object_vars($securityLog->getAttribute('data'))); - $this->assertArrayHasKey('created_at', get_object_vars($securityLog->getAttribute('data'))); - $this->assertIsObject($securityLog->getAttribute('changes')); - } else { - $this->assertCount(0, $collection); - } - } - - /** - * This test Environment Variables Deleted - */ - public function testEnvironmentVariablesDeleted() - { - $vars = EnvironmentVariable::factory()->create([ - 'name' => 'var_2', - ]); - EnvironmentVariablesDeleted::dispatch($vars); - $collection = SecurityLog::get(); - // Check if the variable security_log is enable - if (config('app.security_log')) { - $this->assertCount(1, $collection); - $securityLog = $collection->first(); - $this->assertEquals('EnvironmentVariablesDeleted', $securityLog->getAttribute('event')); - $this->assertIsObject($securityLog->getAttribute('data')); - $this->assertArrayHasKey('name', get_object_vars($securityLog->getAttribute('data'))); - $this->assertArrayHasKey('deleted_at', get_object_vars($securityLog->getAttribute('data'))); - $this->assertIsObject($securityLog->getAttribute('changes')); - } else { - $this->assertCount(0, $collection); - } - } - - /** - * This test Environment Variables Updated - */ - public function testEnvironmentVariablesUpdated() - { - $vars = EnvironmentVariable::factory()->create([ - 'name' => 'var_3', - ]); - $original = $vars->getOriginal(); - $vars->fill(['description' => 'test']); - $vars->save(); - $changes = $vars->getChanges(); - EnvironmentVariablesUpdated::dispatch($vars, $changes, $original); - $collection = SecurityLog::get(); - // Check if the variable security_log is enable - if (config('app.security_log')) { - $this->assertCount(1, $collection); - $securityLog = $collection->first(); - $this->assertEquals('EnvironmentVariablesUpdated', $securityLog->getAttribute('event')); - $this->assertIsObject($securityLog->getAttribute('data')); - $this->assertArrayHasKey('name', get_object_vars($securityLog->getAttribute('data'))); - $this->assertArrayHasKey('last_modified', get_object_vars($securityLog->getAttribute('data'))); - $this->assertIsObject($securityLog->getAttribute('changes')); - } else { - $this->assertCount(0, $collection); - } - } - /** * This test the Setting Update */ @@ -275,110 +196,4 @@ public function testSettingUpdated() $this->assertCount(0, $collection); } } - - /** - * This test User Created - */ - public function testUserCreated() - { - // Create a user created - $user = User::factory()->create([ - 'status' => 'ACTIVE', - ]); - UserCreated::dispatch($user); - $collection = SecurityLog::get(); - // Check if the variable security_log is enable - if (config('app.security_log')) { - $this->assertCount(1, $collection); - $securityLog = $collection->first(); - $this->assertEquals('UserCreated', $securityLog->getAttribute('event')); - $this->assertIsObject($securityLog->getAttribute('data')); - $this->assertArrayHasKey('name', get_object_vars($securityLog->getAttribute('data'))); - $this->assertArrayHasKey('created_at', get_object_vars($securityLog->getAttribute('data'))); - $this->assertIsObject($securityLog->getAttribute('changes')); - } else { - $this->assertCount(0, $collection); - } - } - - /** - * This test User Deleted - */ - public function testUserDeleted() - { - // Create a user deleted - $user = User::factory()->create([ - 'status' => 'ACTIVE', - ]); - $user->delete(); - UserDeleted::dispatch($user); - $collection = SecurityLog::get(); - // Check if the variable security_log is enable - if (config('app.security_log')) { - $this->assertCount(1, $collection); - $securityLog = $collection->first(); - $this->assertEquals('UserDeleted', $securityLog->getAttribute('event')); - $this->assertIsObject($securityLog->getAttribute('data')); - $this->assertArrayHasKey('name', get_object_vars($securityLog->getAttribute('data'))); - $this->assertArrayHasKey('deleted_at', get_object_vars($securityLog->getAttribute('data'))); - $this->assertIsObject($securityLog->getAttribute('changes')); - } else { - $this->assertCount(0, $collection); - } - } - - /** - * This test User Restored - */ - public function testUserRestored() - { - // Create a user restored - $user = User::factory()->create([ - 'deleted_at' => null, - 'status' => 'ACTIVE', - ]); - UserRestored::dispatch($user); - $collection = SecurityLog::get(); - // Check if the variable security_log is enable - if (config('app.security_log')) { - $this->assertCount(1, $collection); - $securityLog = $collection->first(); - $this->assertEquals('UserRestored', $securityLog->getAttribute('event')); - $this->assertIsObject($securityLog->getAttribute('data')); - $this->assertArrayHasKey('name', get_object_vars($securityLog->getAttribute('data'))); - $this->assertArrayHasKey('last_modified', get_object_vars($securityLog->getAttribute('data'))); - $this->assertIsObject($securityLog->getAttribute('changes')); - } else { - $this->assertCount(0, $collection); - } - } - - /** - * This test User Updated - */ - public function testUserUpdated() - { - // Create a user updated - $user = User::factory()->create([ - 'status' => 'ACTIVE', - ]); - $original = $user->getOriginal(); - $user->fill(['timezone' => 'America/Monterrey']); - $user->saveOrFail(); - $changes = $user->getChanges(); - UserUpdated::dispatch($user, $changes, $original); - $collection = SecurityLog::get(); - // Check if the variable security_log is enable - if (config('app.security_log')) { - $this->assertCount(1, $collection); - $securityLog = $collection->first(); - $this->assertEquals('UserUpdated', $securityLog->getAttribute('event')); - $this->assertIsObject($securityLog->getAttribute('data')); - $this->assertArrayHasKey('name', get_object_vars($securityLog->getAttribute('data'))); - $this->assertArrayHasKey('last_modified', get_object_vars($securityLog->getAttribute('data'))); - $this->assertIsObject($securityLog->getAttribute('changes')); - } else { - $this->assertCount(0, $collection); - } - } }