Browse Source

added mobile app settings cleanup for tenant deletion

pull/10591/head
dashevchenko 2 years ago
parent
commit
dc5f59254d
  1. 6
      application/src/main/data/upgrade/3.6.4/schema_update.sql
  2. 13
      application/src/main/java/org/thingsboard/server/controller/MobileApplicationController.java
  3. 10
      application/src/test/java/org/thingsboard/server/controller/MobileApplicationControllerTest.java
  4. 2
      common/data/src/main/java/org/thingsboard/server/common/data/mobile/MobileAppSettings.java
  5. 12
      dao/src/main/java/org/thingsboard/server/dao/mobile/BaseMobileAppSettingsService.java
  6. 2
      dao/src/main/java/org/thingsboard/server/dao/mobile/MobileAppSettingsService.java
  7. 2
      dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java
  8. 8
      dao/src/main/java/org/thingsboard/server/dao/model/sql/MobileAppSettingsEntity.java
  9. 6
      dao/src/main/java/org/thingsboard/server/dao/tenant/TenantServiceImpl.java
  10. 2
      dao/src/main/resources/sql/schema-entities.sql

6
application/src/main/data/upgrade/3.6.4/schema_update.sql

@ -135,14 +135,14 @@ DELETE FROM asset_profile WHERE name ='TbServiceQueue';
-- QUEUE STATS UPDATE END
-- MOBILE APPS TABLE CREATE START
-- MOBILE APP SETTINGS TABLE CREATE START
CREATE TABLE IF NOT EXISTS mobile_app_settings (
tenant_id UUID NOT NULL,
use_default boolean,
use_default_app boolean,
android_config VARCHAR(1000),
ios_config VARCHAR(1000),
qr_code_config VARCHAR(100000)
);
-- MOBILE APPS TABLE CREATE END
-- MOBILE APP SETTINGS TABLE CREATE END

13
application/src/main/java/org/thingsboard/server/controller/MobileApplicationController.java

@ -106,7 +106,6 @@ public class MobileApplicationController extends BaseController {
public ResponseEntity<JsonNode> getAppleAppSiteAssociation() {
MobileAppSettings mobileAppSettings = mobileAppSettingsService.getMobileAppSettings(TenantId.SYS_TENANT_ID);
IosConfig iosConfig = mobileAppSettings.getIosConfig();
if (iosConfig != null && iosConfig.isEnabled() && !iosConfig.getAppId().isBlank()) {
return ResponseEntity.ok(JacksonUtil.toJsonNode(String.format(APPLE_APP_SITE_ASSOCIATION_PATTERN, iosConfig.getAppId())));
} else {
@ -115,7 +114,7 @@ public class MobileApplicationController extends BaseController {
}
@ApiOperation(value = "Create Or Update the Mobile application settings (saveMobileAppSettings)",
notes = "The payload contains associated android and iOS applications and platform qr code widget settings." + SYSTEM_AUTHORITY_PARAGRAPH)
notes = "The request payload contains configuration for android/iOS applications and platform qr code widget settings." + SYSTEM_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority('SYS_ADMIN')")
@PostMapping(value = "/api/mobile/app/settings")
public MobileAppSettings saveMobileAppSettings(@Parameter(description = "A JSON value representing the mobile apps configuration")
@ -128,7 +127,7 @@ public class MobileApplicationController extends BaseController {
}
@ApiOperation(value = "Get Mobile application settings (getMobileAppSettings)",
notes = "The payload contains associated android and iOS applications and platform qr code widget settings." + AVAILABLE_FOR_ANY_AUTHORIZED_USER)
notes = "The response payload contains configuration for android/iOS applications and platform qr code widget settings." + AVAILABLE_FOR_ANY_AUTHORIZED_USER)
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@GetMapping(value = "/api/mobile/app/settings")
public MobileAppSettings getMobileAppSettings() throws ThingsboardException {
@ -139,18 +138,16 @@ public class MobileApplicationController extends BaseController {
}
@ApiOperation(value = "Get the deep link to the associated mobile application (getMobileAppDeepLink)",
notes = "Fetch the url that takes user to associated mobile application " + AVAILABLE_FOR_ANY_AUTHORIZED_USER)
notes = "Fetch the url that takes user to linked mobile application " + AVAILABLE_FOR_ANY_AUTHORIZED_USER)
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@GetMapping(value = "/api/mobile/deepLink", produces = "text/plain")
public String getMobileAppDeepLink(HttpServletRequest request) throws ThingsboardException, URISyntaxException {
SecurityUser currentUser = getCurrentUser();
String secret = mobileAppSecretService.generateMobileAppSecret(currentUser);
String secret = mobileAppSecretService.generateMobileAppSecret(getCurrentUser());
String baseUrl = systemSecurityService.getBaseUrl(TenantId.SYS_TENANT_ID, new CustomerId(EntityId.NULL_UUID), request);
String platformDomain = new URI(baseUrl).getHost();
MobileAppSettings mobileAppSettings = mobileAppSettingsService.getMobileAppSettings(TenantId.SYS_TENANT_ID);
String appDomain;
if (!mobileAppSettings.isUseDefault()) {
if (!mobileAppSettings.isUseDefaultApp()) {
appDomain = platformDomain;
} else {
appDomain = DEFAULT_APP_DOMAIN;

10
application/src/test/java/org/thingsboard/server/controller/MobileApplicationControllerTest.java

@ -52,7 +52,7 @@ public class MobileApplicationControllerTest extends AbstractControllerTest {
qrCodeConfig.setQrCodeLabel(TEST_LABEL);
MobileAppSettings mobileAppSettings = new MobileAppSettings();
mobileAppSettings.setUseDefault(true);
mobileAppSettings.setUseDefaultApp(true);
AndroidConfig androidConfig = AndroidConfig.builder()
.appPackage(ANDROID_PACKAGE_NAME)
.sha256CertFingerprints(ANDROID_APP_SHA256)
@ -75,15 +75,15 @@ public class MobileApplicationControllerTest extends AbstractControllerTest {
loginSysAdmin();
MobileAppSettings mobileAppSettings = doGet("/api/mobile/app/settings", MobileAppSettings.class);
assertThat(mobileAppSettings.getQrCodeConfig().getQrCodeLabel()).isEqualTo(TEST_LABEL);
assertThat(mobileAppSettings.isUseDefault()).isTrue();
assertThat(mobileAppSettings.isUseDefaultApp()).isTrue();
mobileAppSettings.setUseDefault(false);
mobileAppSettings.setUseDefaultApp(false);
doPost("/api/mobile/app/settings", mobileAppSettings)
.andExpect(status().isOk());
MobileAppSettings updatedMobileAppSettings = doGet("/api/mobile/app/settings", MobileAppSettings.class);
assertThat(updatedMobileAppSettings.isUseDefault()).isFalse();
assertThat(updatedMobileAppSettings.isUseDefaultApp()).isFalse();
}
@Test
@ -134,7 +134,7 @@ public class MobileApplicationControllerTest extends AbstractControllerTest {
// update mobile setting to use custom one
loginSysAdmin();
MobileAppSettings mobileAppSettings = doGet("/api/mobile/app/settings", MobileAppSettings.class);
mobileAppSettings.setUseDefault(false);
mobileAppSettings.setUseDefaultApp(false);
doPost("/api/mobile/app/settings", mobileAppSettings);
String customAppDeepLink = doGet("/api/mobile/deepLink", String.class);

2
common/data/src/main/java/org/thingsboard/server/common/data/mobile/MobileAppSettings.java

@ -27,7 +27,7 @@ public class MobileAppSettings implements Serializable {
private static final long serialVersionUID = 2628323657987010348L;
private TenantId tenantId;
boolean useDefault;
private boolean useDefaultApp;
@Valid
private AndroidConfig androidConfig;
@Valid

12
dao/src/main/java/org/thingsboard/server/dao/mobile/BaseMobileAppSettingsService.java

@ -28,11 +28,14 @@ import org.thingsboard.server.common.data.mobile.MobileAppSettings;
import org.thingsboard.server.common.data.mobile.QRCodeConfig;
import org.thingsboard.server.dao.entity.AbstractCachedService;
import static org.thingsboard.server.dao.service.Validator.validateId;
@Service
@Slf4j
@RequiredArgsConstructor
public class BaseMobileAppSettingsService extends AbstractCachedService<TenantId, MobileAppSettings, MobileAppSettingsEvictEvent> implements MobileAppSettingsService {
public static final String INCORRECT_TENANT_ID = "Incorrect tenantId ";
private static final String DEFAULT_QR_CODE_LABEL = "Scan to connect or download mobile app";
private final MobileAppSettingsDao mobileAppSettingsDao;
@ -51,6 +54,13 @@ public class BaseMobileAppSettingsService extends AbstractCachedService<TenantId
return constructMobileAppSettings(mobileAppSettings);
}
@Override
public void deleteByTenantId(TenantId tenantId) {
log.trace("Executing deleteByTenantId, tenantId [{}]", tenantId);
validateId(tenantId, id -> INCORRECT_TENANT_ID + id);
mobileAppSettingsDao.removeByTenantId(tenantId);
}
@TransactionalEventListener(classes = MobileAppSettingsEvictEvent.class)
@Override
public void handleEvictEvent(MobileAppSettingsEvictEvent event) {
@ -60,7 +70,7 @@ public class BaseMobileAppSettingsService extends AbstractCachedService<TenantId
private MobileAppSettings constructMobileAppSettings(MobileAppSettings mobileAppSettings) {
if (mobileAppSettings == null) {
mobileAppSettings = new MobileAppSettings();
mobileAppSettings.setUseDefault(true);
mobileAppSettings.setUseDefaultApp(true);
AndroidConfig androidConfig = AndroidConfig.builder()
.enabled(true)

2
dao/src/main/java/org/thingsboard/server/dao/mobile/MobileAppSettingsService.java

@ -24,4 +24,6 @@ public interface MobileAppSettingsService {
MobileAppSettings getMobileAppSettings(TenantId tenantId);
void deleteByTenantId(TenantId tenantId);
}

2
dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java

@ -663,7 +663,7 @@ public class ModelConstants {
* Mobile application settings constants.
*/
public static final String MOBILE_APP_SETTINGS_TABLE_NAME = "mobile_app_settings";
public static final String MOBILE_APP_SETTINGS_USE_DEFAULT_PROPERTY = "use_default";
public static final String MOBILE_APP_SETTINGS_USE_DEFAULT_APP_PROPERTY = "use_default_app";
public static final String MOBILE_APP_SETTINGS_ANDROID_CONFIG_PROPERTY = "android_config";
public static final String MOBILE_APP_IOS_CONFIG_PROPERTY = "ios_config";
public static final String MOBILE_APP_QR_CODE_CONFIG_PROPERTY = "qr_code_config";

8
dao/src/main/java/org/thingsboard/server/dao/model/sql/MobileAppSettingsEntity.java

@ -46,8 +46,8 @@ public class MobileAppSettingsEntity implements ToData<MobileAppSettings>, Seria
@Column(name = ModelConstants.TENANT_ID_COLUMN, columnDefinition = "uuid")
protected UUID tenantId;
@Column(name = ModelConstants.MOBILE_APP_SETTINGS_USE_DEFAULT_PROPERTY)
private boolean useDefault;
@Column(name = ModelConstants.MOBILE_APP_SETTINGS_USE_DEFAULT_APP_PROPERTY)
private boolean useDefaultApp;
@Convert(converter = JsonConverter.class)
@Column(name = ModelConstants.MOBILE_APP_SETTINGS_ANDROID_CONFIG_PROPERTY)
@ -63,7 +63,7 @@ public class MobileAppSettingsEntity implements ToData<MobileAppSettings>, Seria
public MobileAppSettingsEntity(MobileAppSettings mobileAppSettings) {
this.tenantId = mobileAppSettings.getTenantId().getId();
this.useDefault = mobileAppSettings.isUseDefault();
this.useDefaultApp = mobileAppSettings.isUseDefaultApp();
if (mobileAppSettings.getAndroidConfig() != null) {
this.androidConfig = JacksonUtil.valueToTree(mobileAppSettings.getAndroidConfig());
}
@ -79,7 +79,7 @@ public class MobileAppSettingsEntity implements ToData<MobileAppSettings>, Seria
public MobileAppSettings toData() {
MobileAppSettings mobileAppSettings = new MobileAppSettings();
mobileAppSettings.setTenantId(TenantId.fromUUID(tenantId));
mobileAppSettings.setUseDefault(useDefault);
mobileAppSettings.setUseDefaultApp(useDefaultApp);
if (qrCodeConfig != null) {
mobileAppSettings.setAndroidConfig(JacksonUtil.convertValue(androidConfig, AndroidConfig.class));
}

6
dao/src/main/java/org/thingsboard/server/dao/tenant/TenantServiceImpl.java

@ -42,6 +42,7 @@ import org.thingsboard.server.dao.device.DeviceService;
import org.thingsboard.server.dao.entity.AbstractCachedEntityService;
import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent;
import org.thingsboard.server.dao.eventsourcing.SaveEntityEvent;
import org.thingsboard.server.dao.mobile.MobileAppSettingsService;
import org.thingsboard.server.dao.notification.NotificationRequestService;
import org.thingsboard.server.dao.notification.NotificationRuleService;
import org.thingsboard.server.dao.notification.NotificationSettingsService;
@ -137,6 +138,10 @@ public class TenantServiceImpl extends AbstractCachedEntityService<TenantId, Ten
@Autowired
private QueueStatsService queueStatsService;
@Lazy
@Autowired
private MobileAppSettingsService mobileAppSettingsService;
@Autowired
private AdminSettingsService adminSettingsService;
@ -266,6 +271,7 @@ public class TenantServiceImpl extends AbstractCachedEntityService<TenantId, Ten
relationService.deleteEntityRelations(tenantId, tenantId);
alarmService.deleteEntityAlarmRecordsByTenantId(tenantId);
queueStatsService.deleteByTenantId(tenantId);
mobileAppSettingsService.deleteByTenantId(tenantId);
}
@Override

2
dao/src/main/resources/sql/schema-entities.sql

@ -897,7 +897,7 @@ CREATE TABLE IF NOT EXISTS queue_stats (
CREATE TABLE IF NOT EXISTS mobile_app_settings (
tenant_id UUID NOT NULL,
use_default boolean,
use_default_app boolean,
android_config VARCHAR(1000),
ios_config VARCHAR(1000),
qr_code_config VARCHAR(100000)

Loading…
Cancel
Save