]> BookStack Code Mirror - bookstack/blob - app/Activity/Notifications/Handlers/BaseNotificationHandler.php
b5f339b2ce0ba3f98f680df4339fc56910476d79
[bookstack] / app / Activity / Notifications / Handlers / BaseNotificationHandler.php
1 <?php
2
3 namespace BookStack\Activity\Notifications\Handlers;
4
5 use BookStack\Activity\Models\Loggable;
6 use BookStack\Activity\Notifications\Messages\BaseActivityNotification;
7 use BookStack\Entities\Models\Entity;
8 use BookStack\Permissions\PermissionApplicator;
9 use BookStack\Users\Models\User;
10
11 abstract class BaseNotificationHandler implements NotificationHandler
12 {
13     /**
14      * @param class-string<BaseActivityNotification> $notification
15      * @param int[] $userIds
16      */
17     protected function sendNotificationToUserIds(string $notification, array $userIds, User $initiator, string|Loggable $detail, Entity $relatedModel): void
18     {
19         $users = User::query()->whereIn('id', array_unique($userIds))->get();
20
21         foreach ($users as $user) {
22             // Prevent sending to the user that initiated the activity
23             if ($user->id === $initiator->id) {
24                 continue;
25             }
26
27             // Prevent sending of the user does not have notification permissions
28             if (!$user->can('receive-notifications')) {
29                 continue;
30             }
31
32             // Prevent sending if the user does not have access to the related content
33             $permissions = new PermissionApplicator($user);
34             if (!$permissions->checkOwnableUserAccess($relatedModel, 'view')) {
35                 continue;
36             }
37
38             // Send the notification
39             $user->notify(new $notification($detail, $initiator));
40         }
41     }
42 }