Browse Source

Firebase settings only for sysadmin

pull/8522/head
ViacheslavKlimov 3 years ago
parent
commit
bb14dd611e
  1. 2
      application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationCenter.java
  2. 10
      application/src/main/java/org/thingsboard/server/service/notification/NotificationProcessingContext.java
  3. 4
      application/src/main/java/org/thingsboard/server/service/notification/channels/MobileAppNotificationChannel.java
  4. 4
      dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationSettingsService.java

2
application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationCenter.java

@ -154,6 +154,7 @@ public class DefaultNotificationCenter extends AbstractSubscriptionService imple
} }
} }
NotificationSettings settings = notificationSettingsService.findNotificationSettings(tenantId); NotificationSettings settings = notificationSettingsService.findNotificationSettings(tenantId);
NotificationSettings systemSettings = tenantId.isSysTenantId() ? settings : notificationSettingsService.findNotificationSettings(TenantId.SYS_TENANT_ID);
log.debug("Processing notification request (tenantId: {}, targets: {})", tenantId, request.getTargets()); log.debug("Processing notification request (tenantId: {}, targets: {})", tenantId, request.getTargets());
request.setStatus(NotificationRequestStatus.PROCESSING); request.setStatus(NotificationRequestStatus.PROCESSING);
@ -165,6 +166,7 @@ public class DefaultNotificationCenter extends AbstractSubscriptionService imple
.deliveryMethods(deliveryMethods) .deliveryMethods(deliveryMethods)
.template(notificationTemplate) .template(notificationTemplate)
.settings(settings) .settings(settings)
.systemSettings(systemSettings)
.build(); .build();
processNotificationRequestAsync(ctx, targets, callback); processNotificationRequestAsync(ctx, targets, callback);

10
application/src/main/java/org/thingsboard/server/service/notification/NotificationProcessingContext.java

@ -43,6 +43,7 @@ public class NotificationProcessingContext {
@Getter @Getter
private final TenantId tenantId; private final TenantId tenantId;
private final NotificationSettings settings; private final NotificationSettings settings;
private final NotificationSettings systemSettings;
@Getter @Getter
private final NotificationRequest request; private final NotificationRequest request;
@Getter @Getter
@ -58,11 +59,12 @@ public class NotificationProcessingContext {
@Builder @Builder
public NotificationProcessingContext(TenantId tenantId, NotificationRequest request, Set<NotificationDeliveryMethod> deliveryMethods, public NotificationProcessingContext(TenantId tenantId, NotificationRequest request, Set<NotificationDeliveryMethod> deliveryMethods,
NotificationTemplate template, NotificationSettings settings) { NotificationTemplate template, NotificationSettings settings, NotificationSettings systemSettings) {
this.tenantId = tenantId; this.tenantId = tenantId;
this.request = request; this.request = request;
this.deliveryMethods = deliveryMethods; this.deliveryMethods = deliveryMethods;
this.settings = settings; this.settings = settings;
this.systemSettings = systemSettings;
this.notificationTemplate = template; this.notificationTemplate = template;
this.notificationType = template.getNotificationType(); this.notificationType = template.getNotificationType();
this.templates = new EnumMap<>(NotificationDeliveryMethod.class); this.templates = new EnumMap<>(NotificationDeliveryMethod.class);
@ -81,6 +83,12 @@ public class NotificationProcessingContext {
} }
public <C extends NotificationDeliveryMethodConfig> C getDeliveryMethodConfig(NotificationDeliveryMethod deliveryMethod) { public <C extends NotificationDeliveryMethodConfig> C getDeliveryMethodConfig(NotificationDeliveryMethod deliveryMethod) {
NotificationSettings settings;
if (deliveryMethod == NotificationDeliveryMethod.MOBILE_APP) {
settings = this.systemSettings;
} else {
settings = this.settings;
}
return (C) settings.getDeliveryMethodsConfigs().get(deliveryMethod); return (C) settings.getDeliveryMethodsConfigs().get(deliveryMethod);
} }

4
application/src/main/java/org/thingsboard/server/service/notification/channels/MobileAppNotificationChannel.java

@ -66,8 +66,8 @@ public class MobileAppNotificationChannel implements NotificationChannel<User, M
@Override @Override
public void check(TenantId tenantId) throws Exception { public void check(TenantId tenantId) throws Exception {
NotificationSettings settings = notificationSettingsService.findNotificationSettings(tenantId); NotificationSettings systemSettings = notificationSettingsService.findNotificationSettings(TenantId.SYS_TENANT_ID);
if (!settings.getDeliveryMethodsConfigs().containsKey(NotificationDeliveryMethod.MOBILE_APP)) { if (!systemSettings.getDeliveryMethodsConfigs().containsKey(NotificationDeliveryMethod.MOBILE_APP)) {
throw new RuntimeException("Push-notifications to mobile are not configured"); throw new RuntimeException("Push-notifications to mobile are not configured");
} }
} }

4
dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationSettingsService.java

@ -27,6 +27,7 @@ import org.thingsboard.server.common.data.AdminSettings;
import org.thingsboard.server.common.data.CacheConstants; import org.thingsboard.server.common.data.CacheConstants;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.UserId; import org.thingsboard.server.common.data.id.UserId;
import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod;
import org.thingsboard.server.common.data.notification.NotificationType; import org.thingsboard.server.common.data.notification.NotificationType;
import org.thingsboard.server.common.data.notification.settings.NotificationSettings; import org.thingsboard.server.common.data.notification.settings.NotificationSettings;
import org.thingsboard.server.common.data.notification.settings.UserNotificationSettings; import org.thingsboard.server.common.data.notification.settings.UserNotificationSettings;
@ -70,6 +71,9 @@ public class DefaultNotificationSettingsService implements NotificationSettingsS
@CacheEvict(cacheNames = CacheConstants.NOTIFICATION_SETTINGS_CACHE, key = "#tenantId") @CacheEvict(cacheNames = CacheConstants.NOTIFICATION_SETTINGS_CACHE, key = "#tenantId")
@Override @Override
public void saveNotificationSettings(TenantId tenantId, NotificationSettings settings) { public void saveNotificationSettings(TenantId tenantId, NotificationSettings settings) {
if (!tenantId.isSysTenantId() && settings.getDeliveryMethodsConfigs().containsKey(NotificationDeliveryMethod.MOBILE_APP)) {
throw new IllegalArgumentException("Mobile settings can only be configured by system administrator");
}
AdminSettings adminSettings = Optional.ofNullable(adminSettingsService.findAdminSettingsByTenantIdAndKey(tenantId, SETTINGS_KEY)) AdminSettings adminSettings = Optional.ofNullable(adminSettingsService.findAdminSettingsByTenantIdAndKey(tenantId, SETTINGS_KEY))
.orElseGet(() -> { .orElseGet(() -> {
AdminSettings newAdminSettings = new AdminSettings(); AdminSettings newAdminSettings = new AdminSettings();

Loading…
Cancel
Save