Commit a1d9ce6e45a709104328cc4724590c872e18749c

Authored by YevhenBondarenko
1 parent 4eccf7f9

created validateNumberOfEntitiesPerTenant

Showing 22 changed files with 68 additions and 80 deletions
  1 +package org.thingsboard.server.dao;
  2 +
  3 +import org.thingsboard.server.common.data.id.TenantId;
  4 +
  5 +public interface TenantEntityDao {
  6 +
  7 + Long countByTenantId(TenantId tenantId);
  8 +}
@@ -23,6 +23,7 @@ import org.thingsboard.server.common.data.id.TenantId; @@ -23,6 +23,7 @@ import org.thingsboard.server.common.data.id.TenantId;
23 import org.thingsboard.server.common.data.page.PageData; 23 import org.thingsboard.server.common.data.page.PageData;
24 import org.thingsboard.server.common.data.page.PageLink; 24 import org.thingsboard.server.common.data.page.PageLink;
25 import org.thingsboard.server.dao.Dao; 25 import org.thingsboard.server.dao.Dao;
  26 +import org.thingsboard.server.dao.TenantEntityDao;
26 27
27 import java.util.List; 28 import java.util.List;
28 import java.util.Optional; 29 import java.util.Optional;
@@ -32,7 +33,7 @@ import java.util.UUID; @@ -32,7 +33,7 @@ import java.util.UUID;
32 * The Interface AssetDao. 33 * The Interface AssetDao.
33 * 34 *
34 */ 35 */
35 -public interface AssetDao extends Dao<Asset> { 36 +public interface AssetDao extends Dao<Asset>, TenantEntityDao {
36 37
37 /** 38 /**
38 * Find asset info by id. 39 * Find asset info by id.
@@ -166,6 +167,4 @@ public interface AssetDao extends Dao<Asset> { @@ -166,6 +167,4 @@ public interface AssetDao extends Dao<Asset> {
166 */ 167 */
167 ListenableFuture<List<EntitySubtype>> findTenantAssetTypesAsync(UUID tenantId); 168 ListenableFuture<List<EntitySubtype>> findTenantAssetTypesAsync(UUID tenantId);
168 169
169 - Long countAssetsByTenantId(TenantId tenantId);  
170 -  
171 } 170 }
@@ -330,12 +330,7 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ @@ -330,12 +330,7 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ
330 DefaultTenantProfileConfiguration profileConfiguration = 330 DefaultTenantProfileConfiguration profileConfiguration =
331 (DefaultTenantProfileConfiguration)tenantProfileCache.get(tenantId).getProfileData().getConfiguration(); 331 (DefaultTenantProfileConfiguration)tenantProfileCache.get(tenantId).getProfileData().getConfiguration();
332 long maxAssets = profileConfiguration.getMaxAssets(); 332 long maxAssets = profileConfiguration.getMaxAssets();
333 - if (maxAssets > 0) {  
334 - long currentAssetsCount = assetDao.countAssetsByTenantId(tenantId);  
335 - if (currentAssetsCount >= maxAssets) {  
336 - throw new DataValidationException("Can't create assets more then " + maxAssets);  
337 - }  
338 - } 333 + validateNumberOfEntitiesPerTenant(tenantId, assetDao, maxAssets, EntityType.ASSET);
339 } 334 }
340 335
341 @Override 336 @Override
@@ -20,6 +20,7 @@ import org.thingsboard.server.common.data.id.TenantId; @@ -20,6 +20,7 @@ import org.thingsboard.server.common.data.id.TenantId;
20 import org.thingsboard.server.common.data.page.PageData; 20 import org.thingsboard.server.common.data.page.PageData;
21 import org.thingsboard.server.common.data.page.PageLink; 21 import org.thingsboard.server.common.data.page.PageLink;
22 import org.thingsboard.server.dao.Dao; 22 import org.thingsboard.server.dao.Dao;
  23 +import org.thingsboard.server.dao.TenantEntityDao;
23 24
24 import java.util.Optional; 25 import java.util.Optional;
25 import java.util.UUID; 26 import java.util.UUID;
@@ -27,7 +28,7 @@ import java.util.UUID; @@ -27,7 +28,7 @@ import java.util.UUID;
27 /** 28 /**
28 * The Interface CustomerDao. 29 * The Interface CustomerDao.
29 */ 30 */
30 -public interface CustomerDao extends Dao<Customer> { 31 +public interface CustomerDao extends Dao<Customer>, TenantEntityDao {
31 32
32 /** 33 /**
33 * Save or update customer object 34 * Save or update customer object
@@ -55,6 +56,4 @@ public interface CustomerDao extends Dao<Customer> { @@ -55,6 +56,4 @@ public interface CustomerDao extends Dao<Customer> {
55 */ 56 */
56 Optional<Customer> findCustomersByTenantIdAndTitle(UUID tenantId, String title); 57 Optional<Customer> findCustomersByTenantIdAndTitle(UUID tenantId, String title);
57 58
58 - Long countCustomersByTenantId(TenantId tenantId);  
59 -  
60 } 59 }
@@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired; @@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.context.annotation.Lazy; 24 import org.springframework.context.annotation.Lazy;
25 import org.springframework.stereotype.Service; 25 import org.springframework.stereotype.Service;
26 import org.thingsboard.server.common.data.Customer; 26 import org.thingsboard.server.common.data.Customer;
  27 +import org.thingsboard.server.common.data.EntityType;
27 import org.thingsboard.server.common.data.Tenant; 28 import org.thingsboard.server.common.data.Tenant;
28 import org.thingsboard.server.common.data.id.CustomerId; 29 import org.thingsboard.server.common.data.id.CustomerId;
29 import org.thingsboard.server.common.data.id.TenantId; 30 import org.thingsboard.server.common.data.id.TenantId;
@@ -172,12 +173,8 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom @@ -172,12 +173,8 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom
172 DefaultTenantProfileConfiguration profileConfiguration = 173 DefaultTenantProfileConfiguration profileConfiguration =
173 (DefaultTenantProfileConfiguration)tenantProfileCache.get(tenantId).getProfileData().getConfiguration(); 174 (DefaultTenantProfileConfiguration)tenantProfileCache.get(tenantId).getProfileData().getConfiguration();
174 long maxCustomers = profileConfiguration.getMaxCustomers(); 175 long maxCustomers = profileConfiguration.getMaxCustomers();
175 - if (maxCustomers > 0) {  
176 - long currentCustomersCount = customerDao.countCustomersByTenantId(tenantId);  
177 - if (currentCustomersCount >= maxCustomers) {  
178 - throw new DataValidationException("Can't create customers more then " + maxCustomers);  
179 - }  
180 - } 176 +
  177 + validateNumberOfEntitiesPerTenant(tenantId, customerDao, maxCustomers, EntityType.CUSTOMER);
181 customerDao.findCustomersByTenantIdAndTitle(customer.getTenantId().getId(), customer.getTitle()).ifPresent( 178 customerDao.findCustomersByTenantIdAndTitle(customer.getTenantId().getId(), customer.getTitle()).ifPresent(
182 c -> { 179 c -> {
183 throw new DataValidationException("Customer with such title already exists!"); 180 throw new DataValidationException("Customer with such title already exists!");
@@ -18,11 +18,12 @@ package org.thingsboard.server.dao.dashboard; @@ -18,11 +18,12 @@ package org.thingsboard.server.dao.dashboard;
18 import org.thingsboard.server.common.data.Dashboard; 18 import org.thingsboard.server.common.data.Dashboard;
19 import org.thingsboard.server.common.data.id.TenantId; 19 import org.thingsboard.server.common.data.id.TenantId;
20 import org.thingsboard.server.dao.Dao; 20 import org.thingsboard.server.dao.Dao;
  21 +import org.thingsboard.server.dao.TenantEntityDao;
21 22
22 /** 23 /**
23 * The Interface DashboardDao. 24 * The Interface DashboardDao.
24 */ 25 */
25 -public interface DashboardDao extends Dao<Dashboard> { 26 +public interface DashboardDao extends Dao<Dashboard>, TenantEntityDao {
26 27
27 /** 28 /**
28 * Save or update dashboard object 29 * Save or update dashboard object
@@ -31,6 +32,4 @@ public interface DashboardDao extends Dao<Dashboard> { @@ -31,6 +32,4 @@ public interface DashboardDao extends Dao<Dashboard> {
31 * @return saved dashboard object 32 * @return saved dashboard object
32 */ 33 */
33 Dashboard save(TenantId tenantId, Dashboard dashboard); 34 Dashboard save(TenantId tenantId, Dashboard dashboard);
34 -  
35 - Long countDashboardsByTenantId(TenantId tenantId);  
36 } 35 }
@@ -24,6 +24,7 @@ import org.springframework.stereotype.Service; @@ -24,6 +24,7 @@ import org.springframework.stereotype.Service;
24 import org.thingsboard.server.common.data.Customer; 24 import org.thingsboard.server.common.data.Customer;
25 import org.thingsboard.server.common.data.Dashboard; 25 import org.thingsboard.server.common.data.Dashboard;
26 import org.thingsboard.server.common.data.DashboardInfo; 26 import org.thingsboard.server.common.data.DashboardInfo;
  27 +import org.thingsboard.server.common.data.EntityType;
27 import org.thingsboard.server.common.data.Tenant; 28 import org.thingsboard.server.common.data.Tenant;
28 import org.thingsboard.server.common.data.id.CustomerId; 29 import org.thingsboard.server.common.data.id.CustomerId;
29 import org.thingsboard.server.common.data.id.DashboardId; 30 import org.thingsboard.server.common.data.id.DashboardId;
@@ -226,12 +227,7 @@ public class DashboardServiceImpl extends AbstractEntityService implements Dashb @@ -226,12 +227,7 @@ public class DashboardServiceImpl extends AbstractEntityService implements Dashb
226 DefaultTenantProfileConfiguration profileConfiguration = 227 DefaultTenantProfileConfiguration profileConfiguration =
227 (DefaultTenantProfileConfiguration)tenantProfileCache.get(tenantId).getProfileData().getConfiguration(); 228 (DefaultTenantProfileConfiguration)tenantProfileCache.get(tenantId).getProfileData().getConfiguration();
228 long maxDashboards = profileConfiguration.getMaxDashboards(); 229 long maxDashboards = profileConfiguration.getMaxDashboards();
229 - if (maxDashboards > 0) {  
230 - long currentDashboardsCount = dashboardDao.countDashboardsByTenantId(tenantId);  
231 - if (currentDashboardsCount >= maxDashboards) {  
232 - throw new DataValidationException("Can't create dashboards more then " + maxDashboards);  
233 - }  
234 - } 230 + validateNumberOfEntitiesPerTenant(tenantId, dashboardDao, maxDashboards, EntityType.DASHBOARD);
235 } 231 }
236 232
237 @Override 233 @Override
@@ -23,6 +23,7 @@ import org.thingsboard.server.common.data.id.TenantId; @@ -23,6 +23,7 @@ import org.thingsboard.server.common.data.id.TenantId;
23 import org.thingsboard.server.common.data.page.PageData; 23 import org.thingsboard.server.common.data.page.PageData;
24 import org.thingsboard.server.common.data.page.PageLink; 24 import org.thingsboard.server.common.data.page.PageLink;
25 import org.thingsboard.server.dao.Dao; 25 import org.thingsboard.server.dao.Dao;
  26 +import org.thingsboard.server.dao.TenantEntityDao;
26 27
27 import java.util.List; 28 import java.util.List;
28 import java.util.Optional; 29 import java.util.Optional;
@@ -32,7 +33,7 @@ import java.util.UUID; @@ -32,7 +33,7 @@ import java.util.UUID;
32 * The Interface DeviceDao. 33 * The Interface DeviceDao.
33 * 34 *
34 */ 35 */
35 -public interface DeviceDao extends Dao<Device> { 36 +public interface DeviceDao extends Dao<Device>, TenantEntityDao {
36 37
37 /** 38 /**
38 * Find device info by id. 39 * Find device info by id.
@@ -203,8 +204,6 @@ public interface DeviceDao extends Dao<Device> { @@ -203,8 +204,6 @@ public interface DeviceDao extends Dao<Device> {
203 */ 204 */
204 ListenableFuture<Device> findDeviceByTenantIdAndIdAsync(TenantId tenantId, UUID id); 205 ListenableFuture<Device> findDeviceByTenantIdAndIdAsync(TenantId tenantId, UUID id);
205 206
206 - Long countDevicesByTenantId(TenantId tenantId);  
207 -  
208 Long countDevicesByDeviceProfileId(TenantId tenantId, UUID deviceProfileId); 207 Long countDevicesByDeviceProfileId(TenantId tenantId, UUID deviceProfileId);
209 208
210 /** 209 /**
@@ -530,12 +530,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe @@ -530,12 +530,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
530 DefaultTenantProfileConfiguration profileConfiguration = 530 DefaultTenantProfileConfiguration profileConfiguration =
531 (DefaultTenantProfileConfiguration)tenantProfileCache.get(tenantId).getProfileData().getConfiguration(); 531 (DefaultTenantProfileConfiguration)tenantProfileCache.get(tenantId).getProfileData().getConfiguration();
532 long maxDevices = profileConfiguration.getMaxDevices(); 532 long maxDevices = profileConfiguration.getMaxDevices();
533 - if (maxDevices > 0) {  
534 - long currentDevicesCount = deviceDao.countDevicesByTenantId(tenantId);  
535 - if (currentDevicesCount >= maxDevices) {  
536 - throw new DataValidationException("Can't create devices more then " + maxDevices);  
537 - }  
538 - } 533 + validateNumberOfEntitiesPerTenant(tenantId, deviceDao, maxDevices, EntityType.DEVICE);
539 } 534 }
540 535
541 @Override 536 @Override
@@ -37,12 +37,11 @@ public abstract class AbstractEntityService { @@ -37,12 +37,11 @@ public abstract class AbstractEntityService {
37 37
38 protected Optional<ConstraintViolationException> extractConstraintViolationException(Exception t) { 38 protected Optional<ConstraintViolationException> extractConstraintViolationException(Exception t) {
39 if (t instanceof ConstraintViolationException) { 39 if (t instanceof ConstraintViolationException) {
40 - return Optional.of ((ConstraintViolationException) t); 40 + return Optional.of((ConstraintViolationException) t);
41 } else if (t.getCause() instanceof ConstraintViolationException) { 41 } else if (t.getCause() instanceof ConstraintViolationException) {
42 - return Optional.of ((ConstraintViolationException) (t.getCause())); 42 + return Optional.of((ConstraintViolationException) (t.getCause()));
43 } else { 43 } else {
44 return Optional.empty(); 44 return Optional.empty();
45 } 45 }
46 } 46 }
47 -  
48 } 47 }
@@ -592,12 +592,7 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC @@ -592,12 +592,7 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC
592 DefaultTenantProfileConfiguration profileConfiguration = 592 DefaultTenantProfileConfiguration profileConfiguration =
593 (DefaultTenantProfileConfiguration)tenantProfileCache.get(tenantId).getProfileData().getConfiguration(); 593 (DefaultTenantProfileConfiguration)tenantProfileCache.get(tenantId).getProfileData().getConfiguration();
594 long maxRuleChains = profileConfiguration.getMaxRuleChains(); 594 long maxRuleChains = profileConfiguration.getMaxRuleChains();
595 - if (maxRuleChains > 0) {  
596 - long currentRuleChainsCount = ruleChainDao.countRuleChainsByTenantId(tenantId);  
597 - if (currentRuleChainsCount >= maxRuleChains) {  
598 - throw new DataValidationException("Can't create rule chains more then " + maxRuleChains);  
599 - }  
600 - } 595 + validateNumberOfEntitiesPerTenant(tenantId, ruleChainDao, maxRuleChains, EntityType.RULE_CHAIN);
601 } 596 }
602 597
603 @Override 598 @Override
@@ -15,18 +15,18 @@ @@ -15,18 +15,18 @@
15 */ 15 */
16 package org.thingsboard.server.dao.rule; 16 package org.thingsboard.server.dao.rule;
17 17
18 -import org.thingsboard.server.common.data.id.TenantId;  
19 import org.thingsboard.server.common.data.page.PageData; 18 import org.thingsboard.server.common.data.page.PageData;
20 import org.thingsboard.server.common.data.page.PageLink; 19 import org.thingsboard.server.common.data.page.PageLink;
21 import org.thingsboard.server.common.data.rule.RuleChain; 20 import org.thingsboard.server.common.data.rule.RuleChain;
22 import org.thingsboard.server.dao.Dao; 21 import org.thingsboard.server.dao.Dao;
  22 +import org.thingsboard.server.dao.TenantEntityDao;
23 23
24 import java.util.UUID; 24 import java.util.UUID;
25 25
26 /** 26 /**
27 * Created by igor on 3/12/18. 27 * Created by igor on 3/12/18.
28 */ 28 */
29 -public interface RuleChainDao extends Dao<RuleChain> { 29 +public interface RuleChainDao extends Dao<RuleChain>, TenantEntityDao {
30 30
31 /** 31 /**
32 * Find rule chains by tenantId and page link. 32 * Find rule chains by tenantId and page link.
@@ -36,6 +36,4 @@ public interface RuleChainDao extends Dao<RuleChain> { @@ -36,6 +36,4 @@ public interface RuleChainDao extends Dao<RuleChain> {
36 * @return the list of rule chain objects 36 * @return the list of rule chain objects
37 */ 37 */
38 PageData<RuleChain> findRuleChainsByTenantId(UUID tenantId, PageLink pageLink); 38 PageData<RuleChain> findRuleChainsByTenantId(UUID tenantId, PageLink pageLink);
39 -  
40 - Long countRuleChainsByTenantId(TenantId tenantId);  
41 } 39 }
@@ -18,7 +18,9 @@ package org.thingsboard.server.dao.service; @@ -18,7 +18,9 @@ package org.thingsboard.server.dao.service;
18 import com.fasterxml.jackson.databind.JsonNode; 18 import com.fasterxml.jackson.databind.JsonNode;
19 import lombok.extern.slf4j.Slf4j; 19 import lombok.extern.slf4j.Slf4j;
20 import org.thingsboard.server.common.data.BaseData; 20 import org.thingsboard.server.common.data.BaseData;
  21 +import org.thingsboard.server.common.data.EntityType;
21 import org.thingsboard.server.common.data.id.TenantId; 22 import org.thingsboard.server.common.data.id.TenantId;
  23 +import org.thingsboard.server.dao.TenantEntityDao;
22 import org.thingsboard.server.dao.exception.DataValidationException; 24 import org.thingsboard.server.dao.exception.DataValidationException;
23 25
24 import java.util.HashSet; 26 import java.util.HashSet;
@@ -79,6 +81,19 @@ public abstract class DataValidator<D extends BaseData<?>> { @@ -79,6 +81,19 @@ public abstract class DataValidator<D extends BaseData<?>> {
79 return emailMatcher.matches(); 81 return emailMatcher.matches();
80 } 82 }
81 83
  84 + protected void validateNumberOfEntitiesPerTenant(TenantId tenantId,
  85 + TenantEntityDao tenantEntityDao,
  86 + long maxEntities,
  87 + EntityType entityType) {
  88 + if (maxEntities > 0) {
  89 + long currentEntitiesCount = tenantEntityDao.countByTenantId(tenantId);
  90 + if (currentEntitiesCount >= maxEntities) {
  91 + throw new DataValidationException(String.format("Can't create more then %d %ss!",
  92 + maxEntities, entityType.name().toLowerCase().replaceAll("_", " ")));
  93 + }
  94 + }
  95 + }
  96 +
82 protected static void validateJsonStructure(JsonNode expectedNode, JsonNode actualNode) { 97 protected static void validateJsonStructure(JsonNode expectedNode, JsonNode actualNode) {
83 Set<String> expectedFields = new HashSet<>(); 98 Set<String> expectedFields = new HashSet<>();
84 Iterator<String> fieldsIterator = expectedNode.fieldNames(); 99 Iterator<String> fieldsIterator = expectedNode.fieldNames();
@@ -178,8 +178,7 @@ public class JpaAssetDao extends JpaAbstractSearchTextDao<AssetEntity, Asset> im @@ -178,8 +178,7 @@ public class JpaAssetDao extends JpaAbstractSearchTextDao<AssetEntity, Asset> im
178 } 178 }
179 179
180 @Override 180 @Override
181 - public Long countAssetsByTenantId(TenantId tenantId) { 181 + public Long countByTenantId(TenantId tenantId) {
182 return assetRepository.countByTenantId(tenantId.getId()); 182 return assetRepository.countByTenantId(tenantId.getId());
183 -  
184 } 183 }
185 } 184 }
@@ -65,7 +65,7 @@ public class JpaCustomerDao extends JpaAbstractSearchTextDao<CustomerEntity, Cus @@ -65,7 +65,7 @@ public class JpaCustomerDao extends JpaAbstractSearchTextDao<CustomerEntity, Cus
65 } 65 }
66 66
67 @Override 67 @Override
68 - public Long countCustomersByTenantId(TenantId tenantId) { 68 + public Long countByTenantId(TenantId tenantId) {
69 return customerRepository.countByTenantId(tenantId.getId()); 69 return customerRepository.countByTenantId(tenantId.getId());
70 } 70 }
71 } 71 }
@@ -46,7 +46,7 @@ public class JpaDashboardDao extends JpaAbstractSearchTextDao<DashboardEntity, D @@ -46,7 +46,7 @@ public class JpaDashboardDao extends JpaAbstractSearchTextDao<DashboardEntity, D
46 } 46 }
47 47
48 @Override 48 @Override
49 - public Long countDashboardsByTenantId(TenantId tenantId) { 49 + public Long countByTenantId(TenantId tenantId) {
50 return dashboardRepository.countByTenantId(tenantId.getId()); 50 return dashboardRepository.countByTenantId(tenantId.getId());
51 } 51 }
52 } 52 }
@@ -50,9 +50,9 @@ public interface DeviceRepository extends PagingAndSortingRepository<DeviceEntit @@ -50,9 +50,9 @@ public interface DeviceRepository extends PagingAndSortingRepository<DeviceEntit
50 "AND d.deviceProfileId = :profileId " + 50 "AND d.deviceProfileId = :profileId " +
51 "AND LOWER(d.searchText) LIKE LOWER(CONCAT(:searchText, '%'))") 51 "AND LOWER(d.searchText) LIKE LOWER(CONCAT(:searchText, '%'))")
52 Page<DeviceEntity> findByTenantIdAndProfileId(@Param("tenantId") UUID tenantId, 52 Page<DeviceEntity> findByTenantIdAndProfileId(@Param("tenantId") UUID tenantId,
53 - @Param("profileId") UUID profileId,  
54 - @Param("searchText") String searchText,  
55 - Pageable pageable); 53 + @Param("profileId") UUID profileId,
  54 + @Param("searchText") String searchText,
  55 + Pageable pageable);
56 56
57 @Query("SELECT new org.thingsboard.server.dao.model.sql.DeviceInfoEntity(d, c.title, c.additionalInfo, p.name) " + 57 @Query("SELECT new org.thingsboard.server.dao.model.sql.DeviceInfoEntity(d, c.title, c.additionalInfo, p.name) " +
58 "FROM DeviceEntity d " + 58 "FROM DeviceEntity d " +
@@ -62,9 +62,9 @@ public interface DeviceRepository extends PagingAndSortingRepository<DeviceEntit @@ -62,9 +62,9 @@ public interface DeviceRepository extends PagingAndSortingRepository<DeviceEntit
62 "AND d.customerId = :customerId " + 62 "AND d.customerId = :customerId " +
63 "AND LOWER(d.searchText) LIKE LOWER(CONCAT(:searchText, '%'))") 63 "AND LOWER(d.searchText) LIKE LOWER(CONCAT(:searchText, '%'))")
64 Page<DeviceInfoEntity> findDeviceInfosByTenantIdAndCustomerId(@Param("tenantId") UUID tenantId, 64 Page<DeviceInfoEntity> findDeviceInfosByTenantIdAndCustomerId(@Param("tenantId") UUID tenantId,
65 - @Param("customerId") UUID customerId,  
66 - @Param("searchText") String searchText,  
67 - Pageable pageable); 65 + @Param("customerId") UUID customerId,
  66 + @Param("searchText") String searchText,
  67 + Pageable pageable);
68 68
69 @Query("SELECT d FROM DeviceEntity d WHERE d.tenantId = :tenantId") 69 @Query("SELECT d FROM DeviceEntity d WHERE d.tenantId = :tenantId")
70 Page<DeviceEntity> findByTenantId(@Param("tenantId") UUID tenantId, 70 Page<DeviceEntity> findByTenantId(@Param("tenantId") UUID tenantId,
@@ -102,9 +102,9 @@ public interface DeviceRepository extends PagingAndSortingRepository<DeviceEntit @@ -102,9 +102,9 @@ public interface DeviceRepository extends PagingAndSortingRepository<DeviceEntit
102 "AND d.type = :type " + 102 "AND d.type = :type " +
103 "AND LOWER(d.searchText) LIKE LOWER(CONCAT(:textSearch, '%'))") 103 "AND LOWER(d.searchText) LIKE LOWER(CONCAT(:textSearch, '%'))")
104 Page<DeviceInfoEntity> findDeviceInfosByTenantIdAndType(@Param("tenantId") UUID tenantId, 104 Page<DeviceInfoEntity> findDeviceInfosByTenantIdAndType(@Param("tenantId") UUID tenantId,
105 - @Param("type") String type,  
106 - @Param("textSearch") String textSearch,  
107 - Pageable pageable); 105 + @Param("type") String type,
  106 + @Param("textSearch") String textSearch,
  107 + Pageable pageable);
108 108
109 @Query("SELECT new org.thingsboard.server.dao.model.sql.DeviceInfoEntity(d, c.title, c.additionalInfo, p.name) " + 109 @Query("SELECT new org.thingsboard.server.dao.model.sql.DeviceInfoEntity(d, c.title, c.additionalInfo, p.name) " +
110 "FROM DeviceEntity d " + 110 "FROM DeviceEntity d " +
@@ -137,10 +137,10 @@ public interface DeviceRepository extends PagingAndSortingRepository<DeviceEntit @@ -137,10 +137,10 @@ public interface DeviceRepository extends PagingAndSortingRepository<DeviceEntit
137 "AND d.type = :type " + 137 "AND d.type = :type " +
138 "AND LOWER(d.searchText) LIKE LOWER(CONCAT(:textSearch, '%'))") 138 "AND LOWER(d.searchText) LIKE LOWER(CONCAT(:textSearch, '%'))")
139 Page<DeviceInfoEntity> findDeviceInfosByTenantIdAndCustomerIdAndType(@Param("tenantId") UUID tenantId, 139 Page<DeviceInfoEntity> findDeviceInfosByTenantIdAndCustomerIdAndType(@Param("tenantId") UUID tenantId,
140 - @Param("customerId") UUID customerId,  
141 - @Param("type") String type,  
142 - @Param("textSearch") String textSearch,  
143 - Pageable pageable); 140 + @Param("customerId") UUID customerId,
  141 + @Param("type") String type,
  142 + @Param("textSearch") String textSearch,
  143 + Pageable pageable);
144 144
145 @Query("SELECT new org.thingsboard.server.dao.model.sql.DeviceInfoEntity(d, c.title, c.additionalInfo, p.name) " + 145 @Query("SELECT new org.thingsboard.server.dao.model.sql.DeviceInfoEntity(d, c.title, c.additionalInfo, p.name) " +
146 "FROM DeviceEntity d " + 146 "FROM DeviceEntity d " +
@@ -220,7 +220,7 @@ public class JpaDeviceDao extends JpaAbstractSearchTextDao<DeviceEntity, Device> @@ -220,7 +220,7 @@ public class JpaDeviceDao extends JpaAbstractSearchTextDao<DeviceEntity, Device>
220 } 220 }
221 221
222 @Override 222 @Override
223 - public Long countDevicesByTenantId(TenantId tenantId) { 223 + public Long countByTenantId(TenantId tenantId) {
224 return deviceRepository.countByTenantId(tenantId.getId()); 224 return deviceRepository.countByTenantId(tenantId.getId());
225 } 225 }
226 226
@@ -58,7 +58,7 @@ public class JpaRuleChainDao extends JpaAbstractSearchTextDao<RuleChainEntity, R @@ -58,7 +58,7 @@ public class JpaRuleChainDao extends JpaAbstractSearchTextDao<RuleChainEntity, R
58 } 58 }
59 59
60 @Override 60 @Override
61 - public Long countRuleChainsByTenantId(TenantId tenantId) { 61 + public Long countByTenantId(TenantId tenantId) {
62 return ruleChainRepository.countByTenantId(tenantId.getId()); 62 return ruleChainRepository.countByTenantId(tenantId.getId());
63 } 63 }
64 } 64 }
@@ -93,7 +93,7 @@ public class JpaUserDao extends JpaAbstractSearchTextDao<UserEntity, User> imple @@ -93,7 +93,7 @@ public class JpaUserDao extends JpaAbstractSearchTextDao<UserEntity, User> imple
93 } 93 }
94 94
95 @Override 95 @Override
96 - public Long countUsersByTenantId(TenantId tenantId) { 96 + public Long countByTenantId(TenantId tenantId) {
97 return userRepository.countByTenantId(tenantId.getId()); 97 return userRepository.countByTenantId(tenantId.getId());
98 } 98 }
99 } 99 }
@@ -20,10 +20,11 @@ import org.thingsboard.server.common.data.id.TenantId; @@ -20,10 +20,11 @@ import org.thingsboard.server.common.data.id.TenantId;
20 import org.thingsboard.server.common.data.page.PageData; 20 import org.thingsboard.server.common.data.page.PageData;
21 import org.thingsboard.server.common.data.page.PageLink; 21 import org.thingsboard.server.common.data.page.PageLink;
22 import org.thingsboard.server.dao.Dao; 22 import org.thingsboard.server.dao.Dao;
  23 +import org.thingsboard.server.dao.TenantEntityDao;
23 24
24 import java.util.UUID; 25 import java.util.UUID;
25 26
26 -public interface UserDao extends Dao<User> { 27 +public interface UserDao extends Dao<User>, TenantEntityDao {
27 28
28 /** 29 /**
29 * Save or update user object 30 * Save or update user object
@@ -49,7 +50,7 @@ public interface UserDao extends Dao<User> { @@ -49,7 +50,7 @@ public interface UserDao extends Dao<User> {
49 * @return the list of user entities 50 * @return the list of user entities
50 */ 51 */
51 PageData<User> findByTenantId(UUID tenantId, PageLink pageLink); 52 PageData<User> findByTenantId(UUID tenantId, PageLink pageLink);
52 - 53 +
53 /** 54 /**
54 * Find tenant admin users by tenantId and page link. 55 * Find tenant admin users by tenantId and page link.
55 * 56 *
@@ -58,7 +59,7 @@ public interface UserDao extends Dao<User> { @@ -58,7 +59,7 @@ public interface UserDao extends Dao<User> {
58 * @return the list of user entities 59 * @return the list of user entities
59 */ 60 */
60 PageData<User> findTenantAdmins(UUID tenantId, PageLink pageLink); 61 PageData<User> findTenantAdmins(UUID tenantId, PageLink pageLink);
61 - 62 +
62 /** 63 /**
63 * Find customer users by tenantId, customerId and page link. 64 * Find customer users by tenantId, customerId and page link.
64 * 65 *
@@ -68,6 +69,4 @@ public interface UserDao extends Dao<User> { @@ -68,6 +69,4 @@ public interface UserDao extends Dao<User> {
68 * @return the list of user entities 69 * @return the list of user entities
69 */ 70 */
70 PageData<User> findCustomerUsers(UUID tenantId, UUID customerId, PageLink pageLink); 71 PageData<User> findCustomerUsers(UUID tenantId, UUID customerId, PageLink pageLink);
71 -  
72 - Long countUsersByTenantId(TenantId tenantId);  
73 } 72 }
@@ -27,6 +27,7 @@ import org.springframework.beans.factory.annotation.Value; @@ -27,6 +27,7 @@ import org.springframework.beans.factory.annotation.Value;
27 import org.springframework.context.annotation.Lazy; 27 import org.springframework.context.annotation.Lazy;
28 import org.springframework.stereotype.Service; 28 import org.springframework.stereotype.Service;
29 import org.thingsboard.server.common.data.Customer; 29 import org.thingsboard.server.common.data.Customer;
  30 +import org.thingsboard.server.common.data.EntityType;
30 import org.thingsboard.server.common.data.Tenant; 31 import org.thingsboard.server.common.data.Tenant;
31 import org.thingsboard.server.common.data.User; 32 import org.thingsboard.server.common.data.User;
32 import org.thingsboard.server.common.data.id.CustomerId; 33 import org.thingsboard.server.common.data.id.CustomerId;
@@ -377,12 +378,7 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic @@ -377,12 +378,7 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic
377 DefaultTenantProfileConfiguration profileConfiguration = 378 DefaultTenantProfileConfiguration profileConfiguration =
378 (DefaultTenantProfileConfiguration) tenantProfileCache.get(tenantId).getProfileData().getConfiguration(); 379 (DefaultTenantProfileConfiguration) tenantProfileCache.get(tenantId).getProfileData().getConfiguration();
379 long maxUsers = profileConfiguration.getMaxUsers(); 380 long maxUsers = profileConfiguration.getMaxUsers();
380 - if (maxUsers > 0) {  
381 - long currentUsersCount = userDao.countUsersByTenantId(tenantId);  
382 - if (currentUsersCount >= maxUsers) {  
383 - throw new DataValidationException("Can't create users more then " + maxUsers);  
384 - }  
385 - } 381 + validateNumberOfEntitiesPerTenant(tenantId, userDao, maxUsers, EntityType.USER);
386 } 382 }
387 } 383 }
388 384