Merge pull request #13480 from AndriiLandiak/fix-mapper
Fix tenant id resolving in OAuth2 mapper
This commit is contained in:
commit
b87c8bbde6
@ -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;
|
||||||
@ -34,8 +33,8 @@ import org.thingsboard.server.common.data.id.CustomerId;
|
|||||||
import org.thingsboard.server.common.data.id.DashboardId;
|
import org.thingsboard.server.common.data.id.DashboardId;
|
||||||
import org.thingsboard.server.common.data.id.IdBased;
|
import org.thingsboard.server.common.data.id.IdBased;
|
||||||
import org.thingsboard.server.common.data.id.TenantId;
|
import org.thingsboard.server.common.data.id.TenantId;
|
||||||
import org.thingsboard.server.common.data.oauth2.OAuth2MapperConfig;
|
|
||||||
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.page.PageData;
|
import org.thingsboard.server.common.data.page.PageData;
|
||||||
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;
|
||||||
@ -48,17 +47,16 @@ 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;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public abstract class AbstractOAuth2ClientMapper {
|
public abstract class AbstractOAuth2ClientMapper {
|
||||||
|
|
||||||
private static final int DASHBOARDS_REQUEST_LIMIT = 10;
|
private static final int DASHBOARDS_REQUEST_LIMIT = 10;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -79,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;
|
||||||
@ -120,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());
|
||||||
@ -173,17 +164,15 @@ public abstract class AbstractOAuth2ClientMapper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private TenantId getTenantId(String tenantName) throws Exception {
|
private TenantId getTenantId(String name) throws Exception {
|
||||||
List<Tenant> tenants = tenantService.findTenants(new PageLink(1, 0, tenantName)).getData();
|
Tenant tenant = tenantService.findTenantByName(name);
|
||||||
Tenant tenant;
|
if (tenant != null) {
|
||||||
if (tenants == null || tenants.isEmpty()) {
|
return tenant.getId();
|
||||||
tenant = new Tenant();
|
|
||||||
tenant.setTitle(tenantName);
|
|
||||||
tenant = tbTenantService.save(tenant);
|
|
||||||
} else {
|
|
||||||
tenant = tenants.get(0);
|
|
||||||
}
|
}
|
||||||
return tenant.getTenantId();
|
tenant = new Tenant();
|
||||||
|
tenant.setTitle(name);
|
||||||
|
tenant = tbTenantService.save(tenant);
|
||||||
|
return tenant.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
private CustomerId getCustomerId(TenantId tenantId, String customerName) {
|
private CustomerId getCustomerId(TenantId tenantId, String customerName) {
|
||||||
@ -220,4 +209,5 @@ public abstract class AbstractOAuth2ClientMapper {
|
|||||||
} while (dashboardsPage.hasNext());
|
} while (dashboardsPage.hasNext());
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -48,7 +48,7 @@ public interface TenantRepository extends JpaRepository<TenantEntity, UUID> {
|
|||||||
"LEFT JOIN TenantProfileEntity p on p.id = t.tenantProfileId " +
|
"LEFT JOIN TenantProfileEntity p on p.id = t.tenantProfileId " +
|
||||||
"WHERE (:textSearch IS NULL OR ilike(t.title, CONCAT('%', :textSearch, '%')) = true)")
|
"WHERE (:textSearch IS NULL OR ilike(t.title, CONCAT('%', :textSearch, '%')) = true)")
|
||||||
Page<TenantInfoEntity> findTenantInfosNextPage(@Param("textSearch") String textSearch,
|
Page<TenantInfoEntity> findTenantInfosNextPage(@Param("textSearch") String textSearch,
|
||||||
Pageable pageable);
|
Pageable pageable);
|
||||||
|
|
||||||
@Query("SELECT t.id FROM TenantEntity t")
|
@Query("SELECT t.id FROM TenantEntity t")
|
||||||
Page<UUID> findTenantsIds(Pageable pageable);
|
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," +
|
@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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user