-
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.
Merge pull request #32 from openfoodfoundation/feature/api-token-crea…
…te-event Feature: Added audit events for tokens / teams / team users
- Loading branch information
Showing
18 changed files
with
320 additions
and
5 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
app/Events/PersonalAccessTokens/PersonalAccessTokenWasCreated.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,36 @@ | ||
<?php | ||
|
||
namespace App\Events\PersonalAccessTokens; | ||
|
||
use App\Models\PersonalAccessToken; | ||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Broadcasting\PrivateChannel; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
use Laravel\Sanctum\PersonalAccessToken as SanctumPersonalAccessToken; | ||
|
||
class PersonalAccessTokenWasCreated | ||
{ | ||
use Dispatchable; | ||
use InteractsWithSockets; | ||
use SerializesModels; | ||
|
||
/** | ||
* Create a new event instance. | ||
* | ||
* @param PersonalAccessToken|SanctumPersonalAccessToken $personalAccessToken | ||
*/ | ||
public function __construct(public PersonalAccessToken|SanctumPersonalAccessToken $personalAccessToken) {} | ||
|
||
/** | ||
* Get the channels the event should broadcast on. | ||
* | ||
* @return array<int, \Illuminate\Broadcasting\Channel> | ||
*/ | ||
public function broadcastOn(): array | ||
{ | ||
return [ | ||
new PrivateChannel('channel-name'), | ||
]; | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
namespace App\Events\TeamUsers; | ||
|
||
use App\Models\TeamUser; | ||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Broadcasting\PrivateChannel; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class TeamUserWasCreated | ||
{ | ||
use Dispatchable; | ||
use InteractsWithSockets; | ||
use SerializesModels; | ||
|
||
/** | ||
* Create a new event instance. | ||
* | ||
* @param TeamUser $teamUser | ||
*/ | ||
public function __construct(public TeamUser $teamUser) {} | ||
|
||
/** | ||
* Get the channels the event should broadcast on. | ||
* | ||
* @return array<int, \Illuminate\Broadcasting\Channel> | ||
*/ | ||
public function broadcastOn(): array | ||
{ | ||
return [ | ||
new PrivateChannel('channel-name'), | ||
]; | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
namespace App\Events\Teams; | ||
|
||
use App\Models\Team; | ||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Broadcasting\PrivateChannel; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class TeamWasCreated | ||
{ | ||
use Dispatchable; | ||
use InteractsWithSockets; | ||
use SerializesModels; | ||
|
||
/** | ||
* Create a new event instance. | ||
* | ||
* @param Team $team | ||
*/ | ||
public function __construct(public Team $team) {} | ||
|
||
/** | ||
* Get the channels the event should broadcast on. | ||
* | ||
* @return array<int, \Illuminate\Broadcasting\Channel> | ||
*/ | ||
public function broadcastOn(): array | ||
{ | ||
return [ | ||
new PrivateChannel('channel-name'), | ||
]; | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
app/Jobs/AuditItems/RecordPersonalAccessTokenWasCreated.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,36 @@ | ||
<?php | ||
|
||
namespace App\Jobs\AuditItems; | ||
|
||
use App\Models\PersonalAccessToken; | ||
use App\Models\User; | ||
use App\Services\AuditItemService; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Queue\Queueable; | ||
use Laravel\Sanctum\PersonalAccessToken as SanctumPersonalAccessToken; | ||
|
||
class RecordPersonalAccessTokenWasCreated implements ShouldQueue | ||
{ | ||
use Queueable; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @param PersonalAccessToken|SanctumPersonalAccessToken $personalAccessToken | ||
*/ | ||
public function __construct(public PersonalAccessToken|SanctumPersonalAccessToken $personalAccessToken) {} | ||
|
||
/** | ||
* Execute the job. | ||
*/ | ||
public function handle(): void | ||
{ | ||
$user = User::find($this->personalAccessToken->tokenable_id); | ||
|
||
AuditItemService::createAuditItemForEvent( | ||
model : $this->personalAccessToken, | ||
eventText: 'Access token "' . $this->personalAccessToken->name . '" was created for user ' . $user->name . '.', | ||
teamId : $user->current_team_id, | ||
); | ||
} | ||
} |
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,37 @@ | ||
<?php | ||
|
||
namespace App\Jobs\AuditItems; | ||
|
||
use App\Models\Team; | ||
use App\Models\TeamUser; | ||
use App\Models\User; | ||
use App\Services\AuditItemService; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Queue\Queueable; | ||
|
||
class RecordTeamUserWasCreatedAuditItem implements ShouldQueue | ||
{ | ||
use Queueable; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @param TeamUser $teamUser | ||
*/ | ||
public function __construct(public TeamUser $teamUser) {} | ||
|
||
/** | ||
* Execute the job. | ||
*/ | ||
public function handle(): void | ||
{ | ||
$user = User::find($this->teamUser->user_id); | ||
$team = Team::find($this->teamUser->team_id); | ||
|
||
AuditItemService::createAuditItemForEvent( | ||
model : $this->teamUser, | ||
eventText: 'User ' . $user->name . ' was added to team "' . $team->name . '".', | ||
teamId : $this->teamUser->team_id | ||
); | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace App\Jobs\AuditItems; | ||
|
||
use App\Models\Team; | ||
use App\Services\AuditItemService; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Queue\Queueable; | ||
|
||
class RecordTeamWasCreatedAuditItem implements ShouldQueue | ||
{ | ||
use Queueable; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @param Team $team | ||
*/ | ||
public function __construct(public Team $team) {} | ||
|
||
/** | ||
* Execute the job. | ||
*/ | ||
public function handle(): void | ||
{ | ||
AuditItemService::createAuditItemForEvent( | ||
model : $this->team, | ||
eventText: 'Team ' . $this->team->name . ' was created.', | ||
teamId : $this->team->id | ||
); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
app/Jobs/RecordUserWasCreatedAuditItem.php → ...itItems/RecordUserWasCreatedAuditItem.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
25 changes: 25 additions & 0 deletions
25
app/Listeners/PersonalAccessTokens/HandlePersonalAccessTokenWasCreatedEvent.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,25 @@ | ||
<?php | ||
|
||
namespace App\Listeners\PersonalAccessTokens; | ||
|
||
use App\Events\PersonalAccessTokens\PersonalAccessTokenWasCreated; | ||
use App\Jobs\AuditItems\RecordPersonalAccessTokenWasCreated; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
|
||
class HandlePersonalAccessTokenWasCreatedEvent implements ShouldQueue | ||
{ | ||
/** | ||
* Create the event listener. | ||
*/ | ||
public function __construct() {} | ||
|
||
/** | ||
* Handle the event. | ||
* | ||
* @param PersonalAccessTokenWasCreated $event | ||
*/ | ||
public function handle(PersonalAccessTokenWasCreated $event): void | ||
{ | ||
dispatch(new RecordPersonalAccessTokenWasCreated($event->personalAccessToken)); | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
|
||
namespace App\Listeners\TeamUsers; | ||
|
||
use App\Events\TeamUsers\TeamUserWasCreated; | ||
use App\Jobs\AuditItems\RecordTeamUserWasCreatedAuditItem; | ||
|
||
class HandleTeamUserWasCreatedEvent | ||
{ | ||
/** | ||
* Create the event listener. | ||
*/ | ||
public function __construct() {} | ||
|
||
/** | ||
* Handle the event. | ||
* | ||
* @param TeamUserWasCreated $event | ||
*/ | ||
public function handle(TeamUserWasCreated $event): void | ||
{ | ||
dispatch(new RecordTeamUserWasCreatedAuditItem($event->teamUser)); | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
namespace App\Listeners\Teams; | ||
|
||
use App\Events\Teams\TeamWasCreated; | ||
use App\Jobs\AuditItems\RecordTeamWasCreatedAuditItem; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
|
||
class HandleTeamWasCreatedEvent implements ShouldQueue | ||
{ | ||
/** | ||
* Create the event listener. | ||
*/ | ||
public function __construct() {} | ||
|
||
/** | ||
* Handle the event. | ||
* | ||
* @param TeamWasCreated $event | ||
*/ | ||
public function handle(TeamWasCreated $event): void | ||
{ | ||
dispatch(new RecordTeamWasCreatedAuditItem($event->team)); | ||
} | ||
} |
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
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.