Added anonymous/private logic
This commit is contained in:
@ -2,12 +2,15 @@
|
||||
|
||||
namespace NoccyLabs\Mercureact\Broker;
|
||||
|
||||
use NoccyLabs\SimpleJWT\JWTToken;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use React\Stream\WritableStreamInterface;
|
||||
|
||||
class SseSubscriber implements SubscriberInterface
|
||||
{
|
||||
public function __construct(
|
||||
private WritableStreamInterface $stream
|
||||
private WritableStreamInterface $stream,
|
||||
private ServerRequestInterface $request,
|
||||
)
|
||||
{
|
||||
}
|
||||
@ -17,8 +20,8 @@ class SseSubscriber implements SubscriberInterface
|
||||
$this->stream->write($message->toString());
|
||||
}
|
||||
|
||||
public function isAuthorized(string $topics): bool
|
||||
public function isAuthorized(): bool
|
||||
{
|
||||
return true;
|
||||
return $this->request->getAttribute('authorization') instanceof JWTToken;
|
||||
}
|
||||
}
|
@ -6,5 +6,5 @@ interface SubscriberInterface
|
||||
{
|
||||
public function deliver(Message $message): void;
|
||||
|
||||
public function isAuthorized(string $topics): bool;
|
||||
public function isAuthorized(): bool;
|
||||
}
|
@ -29,9 +29,17 @@ class Topic
|
||||
public function publish(Message $message)
|
||||
{
|
||||
// TODO check if message id has already been published
|
||||
if (isset($this->messages[$message->id])) return;
|
||||
|
||||
$this->messages[$message->id] = $message;
|
||||
|
||||
|
||||
foreach ($this->subscribers as $subscriber) {
|
||||
// Deliver to all subscribers
|
||||
// Skip sending private messages to unauthorized subscribers
|
||||
if ($message->private && !$subscriber->isAuthorized()) {
|
||||
continue;
|
||||
}
|
||||
// Deliver to the subscriber
|
||||
$subscriber->deliver($message);
|
||||
}
|
||||
}
|
||||
|
@ -34,8 +34,7 @@ class TopicManager
|
||||
public function subscribe(SubscriberInterface $subscriber, array $topics): void
|
||||
{
|
||||
foreach ($topics as $topic) {
|
||||
if ($subscriber->isAuthorized($topic))
|
||||
$this->getTopic($topic)->addSubscriber($subscriber);
|
||||
$this->getTopic($topic)->addSubscriber($subscriber);
|
||||
}
|
||||
$this->subscribers->attach($subscriber);
|
||||
}
|
||||
|
Reference in New Issue
Block a user