-
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.
- Loading branch information
1 parent
4caf538
commit 6ba7429
Showing
17 changed files
with
253 additions
and
48 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,37 @@ | ||
<?php | ||
|
||
namespace App\Events\Users; | ||
|
||
use App\Models\User; | ||
use Illuminate\Broadcasting\Channel; | ||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Broadcasting\PrivateChannel; | ||
use Illuminate\Contracts\Auth\Authenticatable; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class UserWasCreated | ||
{ | ||
use Dispatchable; | ||
use InteractsWithSockets; | ||
use SerializesModels; | ||
|
||
/** | ||
* Create a new event instance. | ||
* | ||
* @param User|Authenticatable $user | ||
*/ | ||
public function __construct(public User|Authenticatable $user) {} | ||
|
||
/** | ||
* Get the channels the event should broadcast on. | ||
* | ||
* @return array<int, 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
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,41 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Models\User; | ||
use App\Services\AuditItemService; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Queue\Queueable; | ||
|
||
class RecordUserWasCreatedAuditItem implements ShouldQueue | ||
{ | ||
use Queueable; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @param User $actioningUser | ||
* @param User $createdUser | ||
*/ | ||
public function __construct(public User $actioningUser, public User $createdUser) {} | ||
|
||
/** | ||
* Execute the job. | ||
*/ | ||
public function handle(): void | ||
{ | ||
$eventText = ''; | ||
|
||
if ($this->actioningUser->is_admin) { | ||
$eventText .= 'Admin '; | ||
} | ||
|
||
$eventText .= $this->actioningUser->name . ' created a new user ' . $this->createdUser->name . '.'; | ||
|
||
AuditItemService::createAuditItemForEvent( | ||
actioningUser: $this->actioningUser, | ||
model: $this->createdUser, | ||
eventText: $eventText | ||
); | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
|
||
namespace App\Listeners\Users; | ||
|
||
use App\Events\Users\UserWasCreated; | ||
use App\Jobs\RecordUserWasCreatedAuditItem; | ||
use Auth; | ||
use Log; | ||
|
||
class HandleUserWasCreatedEvent | ||
{ | ||
/** | ||
* Create the event listener. | ||
*/ | ||
public function __construct() {} | ||
|
||
/** | ||
* Handle the event. | ||
* | ||
* @param UserWasCreated $event | ||
*/ | ||
public function handle(UserWasCreated $event): void | ||
{ | ||
if (env('APP_ENV') != 'testing') { | ||
dispatch( | ||
new RecordUserWasCreatedAuditItem( | ||
actioningUser: Auth::user(), | ||
createdUser: $event->user | ||
) | ||
); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use App\Models\AuditItem; | ||
use App\Models\User; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class AuditItemService | ||
{ | ||
public static function createAuditItemForEvent(User $actioningUser, Model $model, string $eventText): AuditItem | ||
{ | ||
$teamId = null; | ||
|
||
if ($model->hasAttribute('team_id')) { | ||
$teamId = $model->team_id; | ||
} | ||
|
||
if ($model->hasAttribute('current_team_id')) { | ||
$teamId = $model->current_team_id; | ||
} | ||
|
||
$auditItem = new AuditItem(); | ||
$auditItem->auditable_type = get_class($model); | ||
$auditItem->auditable_id = $model->id; | ||
$auditItem->auditable_text = $eventText; | ||
$auditItem->auditable_team_id = $teamId; | ||
$auditItem->actioning_user_id = $actioningUser->id; | ||
$auditItem->save(); | ||
|
||
return $auditItem; | ||
} | ||
} |
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
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.