From 30210c70794067539b9b6ea7c3df229d6c479336 Mon Sep 17 00:00:00 2001 From: BohdanSmetanyuk <37596731+BohdanSmetanyuk@users.noreply.github.com> Date: Tue, 21 Jul 2020 15:04:24 +0300 Subject: [PATCH] Bug/several credentials for device (#3089) * fix for device credentials * fix in validateCreate --- .../dao/device/DeviceCredentialsServiceImpl.java | 16 +++++++++++++--- .../main/resources/sql/schema-entities-hsql.sql | 3 ++- dao/src/main/resources/sql/schema-entities.sql | 3 ++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceCredentialsServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceCredentialsServiceImpl.java index e5b8458a91..ac247667bc 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceCredentialsServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceCredentialsServiceImpl.java @@ -84,7 +84,8 @@ public class DeviceCredentialsServiceImpl extends AbstractEntityService implemen return deviceCredentialsDao.save(tenantId, deviceCredentials); } catch (Exception t) { ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); - if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_credentials_id_unq_key")) { + if (e != null && e.getConstraintName() != null + && (e.getConstraintName().equalsIgnoreCase("device_credentials_id_unq_key") || e.getConstraintName().equalsIgnoreCase("device_credentials_device_id_unq_key"))) { throw new DataValidationException("Specified credentials are already registered!"); } else { throw t; @@ -111,14 +112,23 @@ public class DeviceCredentialsServiceImpl extends AbstractEntityService implemen @Override protected void validateCreate(TenantId tenantId, DeviceCredentials deviceCredentials) { + if (deviceCredentialsDao.findByDeviceId(tenantId, deviceCredentials.getDeviceId().getId()) != null) { + throw new DataValidationException("Credentials for this device are already specified!"); + } + if (deviceCredentialsDao.findByCredentialsId(tenantId, deviceCredentials.getCredentialsId()) != null) { + throw new DataValidationException("Device credentials are already assigned to another device!"); + } } @Override protected void validateUpdate(TenantId tenantId, DeviceCredentials deviceCredentials) { - DeviceCredentials existingCredentials = deviceCredentialsDao.findById(tenantId, deviceCredentials.getUuidId()); - if (existingCredentials == null) { + if (deviceCredentialsDao.findById(tenantId, deviceCredentials.getUuidId()) == null) { throw new DataValidationException("Unable to update non-existent device credentials!"); } + DeviceCredentials existingCredentials = deviceCredentialsDao.findByCredentialsId(tenantId, deviceCredentials.getCredentialsId()); + if (existingCredentials != null && !existingCredentials.getId().equals(deviceCredentials.getId())) { + throw new DataValidationException("Device credentials are already assigned to another device!"); + } } @Override diff --git a/dao/src/main/resources/sql/schema-entities-hsql.sql b/dao/src/main/resources/sql/schema-entities-hsql.sql index 1a1f5b02ed..f634590598 100644 --- a/dao/src/main/resources/sql/schema-entities-hsql.sql +++ b/dao/src/main/resources/sql/schema-entities-hsql.sql @@ -142,7 +142,8 @@ CREATE TABLE IF NOT EXISTS device_credentials ( credentials_type varchar(255), credentials_value varchar, device_id uuid, - CONSTRAINT device_credentials_id_unq_key UNIQUE (credentials_id) + CONSTRAINT device_credentials_id_unq_key UNIQUE (credentials_id), + CONSTRAINT device_credentials_device_id_unq_key UNIQUE (device_id) ); CREATE TABLE IF NOT EXISTS event ( diff --git a/dao/src/main/resources/sql/schema-entities.sql b/dao/src/main/resources/sql/schema-entities.sql index e01c422b6c..af4d5d158c 100644 --- a/dao/src/main/resources/sql/schema-entities.sql +++ b/dao/src/main/resources/sql/schema-entities.sql @@ -142,7 +142,8 @@ CREATE TABLE IF NOT EXISTS device_credentials ( credentials_type varchar(255), credentials_value varchar, device_id uuid, - CONSTRAINT device_credentials_id_unq_key UNIQUE (credentials_id) + CONSTRAINT device_credentials_id_unq_key UNIQUE (credentials_id), + CONSTRAINT device_credentials_device_id_unq_key UNIQUE (device_id) ); CREATE TABLE IF NOT EXISTS event (