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

View File

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

View File

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

View File

@ -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," + @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") "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); 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); 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); 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<Tenant> findTenants(TenantId tenantId, PageLink pageLink);
PageData<TenantInfo> findTenantInfos(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); 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); return tenantDao.findTenantIdsByTenantProfileId(tenantProfileId);
} }
@Override
public Tenant findTenantByName(String name) {
log.trace("Executing findTenantByName [{}]", name);
return tenantDao.findTenantByName(TenantId.SYS_TENANT_ID, name);
}
@Override @Override
public void deleteTenants() { public void deleteTenants() {
log.trace("Executing deleteTenants"); log.trace("Executing deleteTenants");