abstract class BaseNotificationHandler implements NotificationHandler
{
- public function __construct(
- protected PermissionApplicator $permissionApplicator
- ) {
- }
-
/**
* @param class-string<BaseActivityNotification> $notification
* @param int[] $userIds
}
// Prevent sending if the user does not have access to the related content
- if (!$this->permissionApplicator->checkOwnableUserAccess($relatedModel, 'view')) {
+ $permissions = new PermissionApplicator($user);
+ if (!$permissions->checkOwnableUserAccess($relatedModel, 'view')) {
continue;
}
$handlersToRun = $this->handlers[$activityType] ?? [];
foreach ($handlersToRun as $handlerClass) {
/** @var NotificationHandler $handler */
- $handler = app()->make($handlerClass);
+ $handler = new $handlerClass();
$handler->handle($activity, $detail, $user);
}
}
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Page;
use BookStack\Exceptions\BookStackExceptionHandlerPage;
+use BookStack\Permissions\PermissionApplicator;
use BookStack\Settings\SettingService;
use BookStack\Util\CspService;
use GuzzleHttp\Client;
'timeout' => 3,
]);
});
+
+ $this->app->singleton(PermissionApplicator::class, function ($app) {
+ return new PermissionApplicator(null);
+ });
}
}
use BookStack\Permissions\Models\EntityPermission;
use BookStack\Users\Models\HasCreatorAndUpdater;
use BookStack\Users\Models\HasOwner;
-use BookStack\Users\Models\Role;
use BookStack\Users\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Builder as QueryBuilder;
class PermissionApplicator
{
+ public function __construct(
+ protected ?User $user = null
+ ) {
+ }
+
/**
* Checks if an entity has a restriction set upon it.
*
*/
protected function currentUser(): User
{
- return user();
+ return $this->user ?? user();
}
/**
&& str_contains($mailContent, 'Created By: ' . $admin->name);
});
}
+
+ public function test_notifications_not_sent_if_lacking_view_permission_for_related_item()
+ {
+ $notifications = Notification::fake();
+ $editor = $this->users->editor();
+ $page = $this->entities->page();
+
+ $watches = new UserEntityWatchOptions($editor, $page);
+ $watches->updateWatchLevel('comments');
+ $this->permissions->disableEntityInheritedPermissions($page);
+
+ $this->asAdmin()->post("/comment/{$page->id}", [
+ 'text' => 'My new comment response',
+ ])->assertOk();
+
+ $notifications->assertNothingSentTo($editor);
+ }
}