Refactored

This commit is contained in:
imbeacon 2023-03-30 09:52:04 +03:00
parent 2bd6a29ca4
commit c99368d885
6 changed files with 24 additions and 14 deletions

View File

@ -60,6 +60,7 @@ import org.thingsboard.server.common.data.security.UserCredentials;
import org.thingsboard.server.common.data.security.UserSettings; import org.thingsboard.server.common.data.security.UserSettings;
import org.thingsboard.server.common.data.security.event.UserCredentialsInvalidationEvent; import org.thingsboard.server.common.data.security.event.UserCredentialsInvalidationEvent;
import org.thingsboard.server.common.data.security.model.JwtPair; import org.thingsboard.server.common.data.security.model.JwtPair;
import org.thingsboard.server.dao.entity.EntityService;
import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.queue.util.TbCoreComponent;
import org.thingsboard.server.service.entitiy.user.TbUserService; import org.thingsboard.server.service.entitiy.user.TbUserService;
import org.thingsboard.server.service.query.EntityQueryService; import org.thingsboard.server.service.query.EntityQueryService;
@ -71,7 +72,9 @@ import org.thingsboard.server.service.security.permission.Resource;
import org.thingsboard.server.service.security.system.SystemSecurityService; import org.thingsboard.server.service.security.system.SystemSecurityService;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -121,6 +124,9 @@ public class UserController extends BaseController {
@Autowired @Autowired
private EntityQueryService entityQueryService; private EntityQueryService entityQueryService;
@Autowired
private EntityService entityService;
@ApiOperation(value = "Get User (getUserById)", @ApiOperation(value = "Get User (getUserById)",
notes = "Fetch the User object based on the provided User Id. " + notes = "Fetch the User object based on the provided User Id. " +
"If the user has the authority of 'SYS_ADMIN', the server does not perform additional checks. " + "If the user has the authority of 'SYS_ADMIN', the server does not perform additional checks. " +
@ -466,13 +472,18 @@ public class UserController extends BaseController {
Alarm alarm = checkAlarmId(alarmEntityId, Operation.READ); Alarm alarm = checkAlarmId(alarmEntityId, Operation.READ);
SecurityUser currentUser = getCurrentUser(); SecurityUser currentUser = getCurrentUser();
TenantId tenantId = currentUser.getTenantId(); TenantId tenantId = currentUser.getTenantId();
CustomerId originatorCustomerId = entityService.fetchEntityCustomerId(tenantId, alarm.getOriginator()).get();
PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder); PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder);
PageData<User> pageData; PageData<User> pageData;
if (Authority.TENANT_ADMIN.equals(currentUser.getAuthority())) { if (Authority.TENANT_ADMIN.equals(currentUser.getAuthority())) {
if (alarm.getCustomerId() == null) { if (alarm.getCustomerId() == null) {
pageData = userService.findTenantAdmins(tenantId, pageLink); pageData = userService.findTenantAdmins(tenantId, pageLink);
} else { } else {
pageData = userService.findTenantAndCustomerUsers(tenantId, alarm.getCustomerId(), pageLink); ArrayList<CustomerId> customerIds = new ArrayList<>(Collections.singletonList(new CustomerId(CustomerId.NULL_UUID)));
if (!CustomerId.NULL_UUID.equals(originatorCustomerId.getId())) {
customerIds.add(originatorCustomerId);
}
pageData = userService.findUsersByCustomerIds(tenantId, customerIds, pageLink);
} }
} else { } else {
pageData = userService.findCustomerUsers(tenantId, alarm.getCustomerId(), pageLink); pageData = userService.findCustomerUsers(tenantId, alarm.getCustomerId(), pageLink);

View File

@ -77,7 +77,7 @@ public interface UserService extends EntityDaoService {
PageData<User> findCustomerUsers(TenantId tenantId, CustomerId customerId, PageLink pageLink); PageData<User> findCustomerUsers(TenantId tenantId, CustomerId customerId, PageLink pageLink);
PageData<User> findTenantAndCustomerUsers(TenantId tenantId, CustomerId customerId, PageLink pageLink); PageData<User> findUsersByCustomerIds(TenantId tenantId, List<CustomerId> customerIds, PageLink pageLink);
void deleteCustomerUsers(TenantId tenantId, CustomerId customerId); void deleteCustomerUsers(TenantId tenantId, CustomerId customerId);

View File

@ -20,6 +20,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.User; import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.TenantProfileId; import org.thingsboard.server.common.data.id.TenantProfileId;
import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageData;
@ -103,16 +104,14 @@ public class JpaUserDao extends JpaAbstractSearchTextDao<UserEntity, User> imple
} }
@Override @Override
public PageData<User> findTenantAndCustomerUsers(UUID tenantId, UUID customerId, PageLink pageLink) { public PageData<User> findUsersByCustomerIds(UUID tenantId, List<CustomerId> customerIds, PageLink pageLink) {
return DaoUtil.toPageData( return DaoUtil.toPageData(
userRepository userRepository
.findTenantAndCustomerUsers( .findTenantAndCustomerUsers(
tenantId, tenantId,
customerId, DaoUtil.toUUIDs(customerIds),
Objects.toString(pageLink.getTextSearch(), ""), Objects.toString(pageLink.getTextSearch(), ""),
NULL_UUID,
DaoUtil.toPageable(pageLink))); DaoUtil.toPageable(pageLink)));
} }
@Override @Override

View File

@ -45,12 +45,11 @@ public interface UserRepository extends JpaRepository<UserEntity, UUID> {
Pageable pageable); Pageable pageable);
@Query("SELECT u FROM UserEntity u WHERE u.tenantId = :tenantId " + @Query("SELECT u FROM UserEntity u WHERE u.tenantId = :tenantId " +
"AND (:customerId IS NULL OR u.customerId IN (:customerId, :nullCustomerId)) " + "AND u.customerId IN (:customerIds) " +
"AND LOWER(u.searchText) LIKE LOWER(CONCAT('%', :searchText, '%'))") "AND LOWER(u.searchText) LIKE LOWER(CONCAT('%', :searchText, '%'))")
Page<UserEntity> findTenantAndCustomerUsers(@Param("tenantId") UUID tenantId, Page<UserEntity> findTenantAndCustomerUsers(@Param("tenantId") UUID tenantId,
@Param("customerId") UUID customerId, @Param("customerIds") Collection<UUID> customerIds,
@Param("searchText") String searchText, @Param("searchText") String searchText,
@Param("nullCustomerId") UUID nullCustomerId,
Pageable pageable); Pageable pageable);
@Query("SELECT u FROM UserEntity u WHERE u.tenantId = :tenantId " + @Query("SELECT u FROM UserEntity u WHERE u.tenantId = :tenantId " +

View File

@ -16,6 +16,7 @@
package org.thingsboard.server.dao.user; package org.thingsboard.server.dao.user;
import org.thingsboard.server.common.data.User; import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.TenantProfileId; import org.thingsboard.server.common.data.id.TenantProfileId;
import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageData;
@ -90,7 +91,7 @@ public interface UserDao extends Dao<User>, TenantEntityDao {
* @param pageLink the page link * @param pageLink the page link
* @return the list of user entities * @return the list of user entities
*/ */
PageData<User> findTenantAndCustomerUsers(UUID tenantId, UUID customerId, PageLink pageLink); PageData<User> findUsersByCustomerIds(UUID tenantId, List<CustomerId> customerIds, PageLink pageLink);
PageData<User> findAll(PageLink pageLink); PageData<User> findAll(PageLink pageLink);

View File

@ -294,12 +294,12 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic
} }
@Override @Override
public PageData<User> findTenantAndCustomerUsers(TenantId tenantId, CustomerId customerId, PageLink pageLink) { public PageData<User> findUsersByCustomerIds(TenantId tenantId, List<CustomerId> customerIds, PageLink pageLink) {
log.trace("Executing findTenantAndCustomerUsers, tenantId [{}], customerId [{}], pageLink [{}]", tenantId, customerId, pageLink); log.trace("Executing findTenantAndCustomerUsers, tenantId [{}], customerIds [{}], pageLink [{}]", tenantId, customerIds, pageLink);
validateId(tenantId, INCORRECT_TENANT_ID + tenantId); validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
validatePageLink(pageLink); validatePageLink(pageLink);
validateId(customerId, "Incorrect customerId " + customerId); customerIds.forEach(customerId -> {validateId(customerId, "Incorrect customerId " + customerId);});
return userDao.findTenantAndCustomerUsers(tenantId.getId(), customerId.getId(), pageLink); return userDao.findUsersByCustomerIds(tenantId.getId(), customerIds, pageLink);
} }
@Override @Override