Introduce findTenantByName

This commit is contained in:
Andrii Landiak 2025-05-30 12:24:17 +03:00
parent f12ded5f9b
commit 5fb317c57d
6 changed files with 28 additions and 36 deletions

View File

@ -20,7 +20,6 @@ import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@ -37,7 +36,6 @@ import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.oauth2.OAuth2Client;
import org.thingsboard.server.common.data.oauth2.OAuth2MapperConfig;
import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageDataIterable;
import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.common.data.security.Authority;
import org.thingsboard.server.common.data.security.UserCredentials;
@ -49,7 +47,6 @@ import org.thingsboard.server.dao.tenant.TenantService;
import org.thingsboard.server.dao.user.UserService;
import org.thingsboard.server.service.entitiy.tenant.TbTenantService;
import org.thingsboard.server.service.entitiy.user.TbUserService;
import org.thingsboard.server.service.install.InstallScripts;
import org.thingsboard.server.service.security.model.SecurityUser;
import org.thingsboard.server.service.security.model.UserPrincipal;
@ -80,18 +77,12 @@ public abstract class AbstractOAuth2ClientMapper {
@Autowired
private DashboardService dashboardService;
@Autowired
private InstallScripts installScripts;
@Autowired
private TbUserService tbUserService;
@Autowired
protected TbTenantProfileCache tenantProfileCache;
@Autowired
private ApplicationEventPublisher eventPublisher;
@Value("${edges.enabled}")
@Getter
private boolean edgesEnabled;
@ -121,8 +112,7 @@ public abstract class AbstractOAuth2ClientMapper {
} else {
user.setAuthority(Authority.CUSTOMER_USER);
}
TenantId tenantId = oauth2User.getTenantId() != null ?
oauth2User.getTenantId() : getTenantId(oauth2User.getTenantName());
TenantId tenantId = oauth2User.getTenantId() != null ? oauth2User.getTenantId() : getTenantId(oauth2User.getTenantName());
user.setTenantId(tenantId);
CustomerId customerId = oauth2User.getCustomerId() != null ?
oauth2User.getCustomerId() : getCustomerId(user.getTenantId(), oauth2User.getCustomerName());
@ -174,15 +164,13 @@ public abstract class AbstractOAuth2ClientMapper {
}
}
private TenantId getTenantId(String tenantName) throws Exception {
PageDataIterable<Tenant> tenantIterator = new PageDataIterable<>(tenantService::findTenants, 1024);
for (Tenant tenant : tenantIterator) {
if (tenant.getTitle().equals(tenantName)) {
return tenant.getId();
}
private TenantId getTenantId(String name) throws Exception {
Tenant tenant = tenantService.findTenantByName(name);
if (tenant != null) {
return tenant.getId();
}
Tenant tenant = new Tenant();
tenant.setTitle(tenantName);
tenant = new Tenant();
tenant.setTitle(name);
tenant = tbTenantService.save(tenant);
return tenant.getId();
}

View File

@ -49,7 +49,10 @@ public interface TenantService extends EntityDaoService {
List<TenantId> findTenantIdsByTenantProfileId(TenantProfileId tenantProfileId);
Tenant findTenantByName(String name);
void deleteTenants();
PageData<TenantId> findTenantsIds(PageLink pageLink);
}

View File

@ -38,10 +38,6 @@ import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
/**
* Created by Valerii Sosliuk on 4/30/2017.
*/
@Component
@SqlDao
public class JpaTenantDao extends JpaAbstractDao<TenantEntity, Tenant> implements TenantDao {
@ -97,8 +93,14 @@ public class JpaTenantDao extends JpaAbstractDao<TenantEntity, Tenant> implement
.collect(Collectors.toList());
}
@Override
public Tenant findTenantByName(TenantId tenantId, String name) {
return DaoUtil.getData(tenantRepository.findTenantByTitle(name));
}
@Override
public List<TenantFields> findNextBatch(UUID id, int batchSize) {
return tenantRepository.findNextBatch(id, Limit.of(batchSize));
}
}

View File

@ -48,7 +48,7 @@ public interface TenantRepository extends JpaRepository<TenantEntity, UUID> {
"LEFT JOIN TenantProfileEntity p on p.id = t.tenantProfileId " +
"WHERE (:textSearch IS NULL OR ilike(t.title, CONCAT('%', :textSearch, '%')) = true)")
Page<TenantInfoEntity> findTenantInfosNextPage(@Param("textSearch") String textSearch,
Pageable pageable);
Pageable pageable);
@Query("SELECT t.id FROM TenantEntity t")
Page<UUID> findTenantsIds(Pageable pageable);
@ -59,4 +59,7 @@ public interface TenantRepository extends JpaRepository<TenantEntity, UUID> {
@Query("SELECT new org.thingsboard.server.common.data.edqs.fields.TenantFields(t.id, t.createdTime, t.title, t.version," +
"t.additionalInfo, t.country, t.state, t.city, t.address, t.address2, t.zip, t.phone, t.email, t.region) FROM TenantEntity t WHERE t.id > :id ORDER BY t.id")
List<TenantFields> findNextBatch(@Param("id") UUID id, Limit limit);
TenantEntity findTenantByTitle(String name);
}

View File

@ -30,20 +30,8 @@ public interface TenantDao extends Dao<Tenant> {
TenantInfo findTenantInfoById(TenantId tenantId, UUID id);
/**
* Save or update tenant object
*
* @param tenant the tenant object
* @return saved tenant object
*/
Tenant save(TenantId tenantId, Tenant tenant);
/**
* Find tenants by page link.
*
* @param pageLink the page link
* @return the list of tenant objects
*/
PageData<Tenant> findTenants(TenantId tenantId, PageLink pageLink);
PageData<TenantInfo> findTenantInfos(TenantId tenantId, PageLink pageLink);
@ -52,4 +40,6 @@ public interface TenantDao extends Dao<Tenant> {
List<TenantId> findTenantIdsByTenantProfileId(TenantProfileId tenantProfileId);
Tenant findTenantByName(TenantId tenantId, String name);
}

View File

@ -206,6 +206,12 @@ public class TenantServiceImpl extends AbstractCachedEntityService<TenantId, Ten
return tenantDao.findTenantIdsByTenantProfileId(tenantProfileId);
}
@Override
public Tenant findTenantByName(String name) {
log.trace("Executing findTenantByName [{}]", name);
return tenantDao.findTenantByName(TenantId.SYS_TENANT_ID, name);
}
@Override
public void deleteTenants() {
log.trace("Executing deleteTenants");