Skip to content

Commit

Permalink
bugfix: readSubjects were not added to SendClaimMessage, thus it coul…
Browse files Browse the repository at this point in the history
…d not be routed to subscribers with read permissions

Signed-off-by: Daniel Fesenmeyer <daniel.fesenmeyer@bosch-si.com>
  • Loading branch information
danielFesenmeyer committed May 3, 2018
1 parent cb90aa9 commit 3afd7d5
Showing 1 changed file with 26 additions and 20 deletions.
Expand Up @@ -53,12 +53,15 @@ private MessageCommandEnforcement(final Context context, final Cache<EntityId, E
@Override
public void enforce(final MessageCommand signal, final ActorRef sender) {
enforcerRetriever.retrieve(entityId(), (idEntry, enforcerEntry) -> {
if (idEntry.exists() && signal instanceof SendClaimMessage) {
publishMessageCommand(signal, sender);
} else if (enforcerEntry.exists()) {
enforceMessageCommand(signal, enforcerEntry.getValue(), sender);
} else {
if (!enforcerEntry.exists()) {
reportNonexistentEnforcer(signal, sender);
} else {
final Enforcer enforcer = enforcerEntry.getValue();
if (signal instanceof SendClaimMessage) {
publishMessageCommand(signal, enforcer, sender);
} else {
enforceMessageCommand(signal, enforcer, sender);
}
}
});
}
Expand Down Expand Up @@ -111,18 +114,27 @@ private void reportNonexistentEnforcer(final MessageCommand command, final Actor

private void enforceMessageCommand(final MessageCommand command, final Enforcer enforcer, final ActorRef sender) {
if (isAuthorized(command, enforcer)) {
final DittoHeaders headersWithReadSubjects = command.getDittoHeaders()
.toBuilder()
.readSubjects(getThingsReadSubjects(command, enforcer))
.build();

final MessageCommand commandWithReadSubjects = command.setDittoHeaders(headersWithReadSubjects);
publishMessageCommand(commandWithReadSubjects, sender);
publishMessageCommand(command, enforcer, sender);
} else {
rejectMessageCommand(command, sender);
}
}

private void publishMessageCommand(final MessageCommand command, final Enforcer enforcer, final ActorRef sender) {
final DittoHeaders headersWithReadSubjects = command.getDittoHeaders()
.toBuilder()
.readSubjects(getThingsReadSubjects(command, enforcer))
.build();

final MessageCommand commandWithReadSubjects = command.setDittoHeaders(headersWithReadSubjects);

publishToMediator(commandWithReadSubjects, sender);

// answer the sender immediately for fire-and-forget message commands.
getResponseForFireAndForgetMessage(commandWithReadSubjects)
.ifPresent(response -> replyToSender(response, sender));
}

private void rejectMessageCommand(final MessageCommand command, final ActorRef sender) {
final MessageSendNotAllowedException error =
MessageSendNotAllowedException.newBuilder(command.getThingId())
Expand All @@ -135,16 +147,10 @@ private void rejectMessageCommand(final MessageCommand command, final ActorRef s
replyToSender(error, sender);
}

private void publishMessageCommand(final MessageCommand command, final ActorRef sender) {
publishToMediator(command, sender);

// answer the sender immediately for fire-and-forget message commands.
getResponseForFireAndForgetMessage(command)
.ifPresent(response -> replyToSender(response, sender));
}

private void publishToMediator(final MessageCommand command, final ActorRef sender) {
// using pub/sub to publish the command to any interested parties (e.g. a Websocket):
log().debug("Publish message to pub-sub: <{}>", command.getTypePrefix());

final DistributedPubSubMediator.Publish publishMessage =
new DistributedPubSubMediator.Publish(command.getTypePrefix(), command, true);
pubSubMediator().tell(publishMessage, sender);
Expand Down

0 comments on commit 3afd7d5

Please sign in to comment.