use of org.openremote.manager.security.ManagerKeycloakIdentityProvider.KEYCLOAK_USER_ATTRIBUTE_EMAIL_NOTIFICATIONS_DISABLED in project openremote by openremote.
the class EmailNotificationHandler method getTargets.
@Override
public List<Notification.Target> getTargets(Notification.Source source, String sourceId, List<Notification.Target> targets, AbstractNotificationMessage message) {
List<Notification.Target> mappedTargets = new ArrayList<>();
if (targets != null) {
targets.forEach(target -> {
Notification.TargetType targetType = target.getType();
String targetId = target.getId();
switch(targetType) {
case TENANT:
case USER:
// Find all users in this tenant or by id
User[] users = targetType == Notification.TargetType.TENANT ? managerIdentityService.getIdentityProvider().queryUsers(new UserQuery().tenant(new TenantPredicate(targetId))) : managerIdentityService.getIdentityProvider().queryUsers(new UserQuery().ids(targetId));
if (users.length == 0) {
if (targetType == Notification.TargetType.USER) {
LOG.info("User not found: " + targetId);
} else {
LOG.info("No users found in target realm: " + targetId);
}
return;
}
mappedTargets.addAll(Arrays.stream(users).filter(user -> !Boolean.parseBoolean(user.getAttributes().getOrDefault(KEYCLOAK_USER_ATTRIBUTE_EMAIL_NOTIFICATIONS_DISABLED, Collections.singletonList("false")).get(0))).map(user -> {
Notification.Target userAssetTarget = new Notification.Target(Notification.TargetType.USER, user.getId());
userAssetTarget.setData(new EmailNotificationMessage.Recipient(user.getFullName(), user.getEmail()));
return userAssetTarget;
}).collect(Collectors.toList()));
break;
case CUSTOM:
// Nothing to do here
mappedTargets.add(new Notification.Target(targetType, targetId));
break;
case ASSET:
// Find descendant assets with email attribute
List<Asset<?>> assets = assetStorageService.findAll(new AssetQuery().select(new AssetQuery.Select().attributes(Asset.EMAIL.getName())).paths(new PathPredicate(targetId)).attributes(new AttributePredicate(new StringPredicate(Asset.EMAIL.getName()), new ValueEmptyPredicate().negate(true))));
if (assets.isEmpty()) {
LOG.fine("No assets with email attribute descendants of target asset");
return;
}
mappedTargets.addAll(assets.stream().map(asset -> {
Notification.Target assetTarget = new Notification.Target(Notification.TargetType.ASSET, asset.getId());
assetTarget.setData(new EmailNotificationMessage.Recipient(asset.getName(), asset.getEmail().orElse(null)));
return assetTarget;
}).collect(Collectors.toList()));
break;
}
});
}
EmailNotificationMessage email = (EmailNotificationMessage) message;
// Map to/cc/bcc into a custom target for traceability in sent notifications
List<String> addresses = new ArrayList<>();
if (email.getTo() != null) {
addresses.addAll(email.getTo().stream().map(EmailNotificationMessage.Recipient::getAddress).map(address -> "to:" + address).collect(Collectors.toList()));
email.setTo((List<EmailNotificationMessage.Recipient>) null);
}
if (email.getCc() != null) {
addresses.addAll(email.getCc().stream().map(EmailNotificationMessage.Recipient::getAddress).map(address -> "cc:" + address).collect(Collectors.toList()));
email.setCc((List<EmailNotificationMessage.Recipient>) null);
}
if (email.getBcc() != null) {
addresses.addAll(email.getBcc().stream().map(EmailNotificationMessage.Recipient::getAddress).map(address -> "bcc:" + address).collect(Collectors.toList()));
email.setBcc((List<EmailNotificationMessage.Recipient>) null);
}
if (!addresses.isEmpty()) {
mappedTargets.add(new Notification.Target(Notification.TargetType.CUSTOM, String.join(";", addresses)));
}
return mappedTargets;
}
Aggregations