Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Use hibernate filter to add condition to query #121

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package green_buildings.idp.aop;

import commons.springfw.impl.utils.SecurityUtils;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.hibernate.Filter;
import org.hibernate.Session;
import org.springframework.stereotype.Component;
import green_buildings.idp.entity.UserEntity;

import java.util.Objects;
import java.util.UUID;

@Aspect
@Component
@RequiredArgsConstructor
public class UserRepositoryAdvice {

@PersistenceContext
private final EntityManager em;

@Pointcut("execution(* green_buildings.idp.repository.UserRepositoryAdapter.*(..))")
public void userRepositoryMethods() {
// Pointcut for methods in UserRepository have to filter users base on their enterprise
}

@Around(value = "userRepositoryMethods()")
public Object applyEnterpriseFilter(ProceedingJoinPoint joinPoint) throws Throwable {
Session session = em.unwrap(Session.class);
boolean hasSession = Objects.nonNull(session);
if (hasSession) {
Filter filter = session.enableFilter(UserEntity.BELONG_ENTERPRISE_FILTER);
UUID enterpriseId = SecurityUtils.getCurrentUserEnterpriseId().orElseThrow();
filter.setParameter(UserEntity.BELONG_ENTERPRISE_PARAM, enterpriseId);
}

try {
return joinPoint.proceed();
} finally {
if (hasSession) {
session.disableFilter(UserEntity.BELONG_ENTERPRISE_FILTER);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.ParamDef;

import java.util.LinkedHashSet;
import java.util.Set;
import java.util.UUID;

@NamedEntityGraph(
name = UserEntity.WITH_ENTERPRISE_PERMISSIONS_ENTITY_GRAPH,
Expand All @@ -29,6 +33,9 @@
@NamedAttributeNode("buildingPermissions")
}
)
@FilterDef(name = UserEntity.BELONG_ENTERPRISE_FILTER, parameters = @ParamDef(name = UserEntity.BELONG_ENTERPRISE_PARAM, type = UUID.class))
@Filter(name = UserEntity.BELONG_ENTERPRISE_FILTER,
condition = "id IN (SELECT ue.user_id FROM enterprise_users ue WHERE ue.enterprise_id = :" + UserEntity.BELONG_ENTERPRISE_PARAM)
@Entity
@Table(name = "users")
@AllArgsConstructor
Expand All @@ -38,6 +45,8 @@
public class UserEntity extends AbstractAuditableEntity {

public static final String WITH_ENTERPRISE_PERMISSIONS_ENTITY_GRAPH = "user-permissions-entity-graph";
public static final String BELONG_ENTERPRISE_FILTER = "belongEnterpriseFilter";
public static final String BELONG_ENTERPRISE_PARAM = "enterpriseId";

@NotNull
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL, optional = false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package green_buildings.idp.repository;

import green_buildings.idp.entity.UserEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.UUID;

@Component
@RequiredArgsConstructor
public class UserRepositoryAdapter {
private final UserRepository userRepo;

public List<UserEntity> findByIDs(Set<UUID> ids) {
return userRepo.findByIDs(ids);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import green_buildings.idp.entity.UserEntity;
import green_buildings.idp.producers.IdPEventProducer;
import green_buildings.idp.repository.UserRepository;
import green_buildings.idp.repository.UserRepositoryAdapter;
import green_buildings.idp.service.UserService;
import green_buildings.idp.utils.IEmailUtil;
import green_buildings.idp.utils.IMessageUtil;
Expand Down Expand Up @@ -54,6 +55,7 @@
public class UserServiceImpl extends SagaManager implements UserService {

private final UserRepository userRepo;
private final UserRepositoryAdapter userRepositoryAdapter;
private final UserValidator userValidator;
private final PasswordEncoder passwordEncoder;
@Qualifier("signupValidator")
Expand Down Expand Up @@ -174,7 +176,7 @@ public void deleteUsers(Set<UUID> userIds) {
if (CollectionUtils.isEmpty(userIds)) {
throw new BusinessException("userIds", "user.delete.no.ids", Collections.emptyList());
}
var users = userRepo.findByIDs(userIds);
var users = userRepositoryAdapter.findByIDs(userIds);
if (users.size() != userIds.size()) {
userIds.removeAll(users.stream().map(UserEntity::getId).collect(Collectors.toSet()));
throw new BusinessException("userIds", "user.delete.not.found",
Expand Down