mirror of https://gitee.com/topiam/eiam
⚡ 优化
parent
855ebeb82e
commit
b9f4599f3b
|
@ -21,9 +21,10 @@ import java.io.Serial;
|
|||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.annotations.JdbcTypeCode;
|
||||
import org.hibernate.annotations.SQLDelete;
|
||||
import org.hibernate.annotations.Type;
|
||||
import org.hibernate.annotations.Where;
|
||||
import org.hibernate.type.SqlTypes;
|
||||
|
||||
import cn.topiam.employee.audit.enums.EventStatus;
|
||||
import cn.topiam.employee.audit.event.type.EventType;
|
||||
|
@ -36,7 +37,6 @@ import lombok.Setter;
|
|||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import io.hypersistence.utils.hibernate.type.json.JsonType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Table;
|
||||
|
@ -61,92 +61,98 @@ import static cn.topiam.employee.support.repository.domain.LogicDeleteEntity.SOF
|
|||
public class AuditEntity extends LogicDeleteEntity<Long> {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = -3119319193111206582L;
|
||||
private static final long serialVersionUID = -3119319193111206582L;
|
||||
|
||||
public static final String EVENT_TYPE_FIELD_NAME = "eventType";
|
||||
|
||||
public static final String ACTOR_ID_FIELD_NAME = "actorId";
|
||||
|
||||
public static final String EVENT_TIME_FIELD_NAME = "eventTime";
|
||||
|
||||
/**
|
||||
* Request Id
|
||||
*/
|
||||
@Column(name = "request_id")
|
||||
private String requestId;
|
||||
private String requestId;
|
||||
|
||||
/**
|
||||
* Session Id
|
||||
*/
|
||||
@Column(name = "session_id")
|
||||
private String sessionId;
|
||||
private String sessionId;
|
||||
|
||||
/**
|
||||
* 操作目标
|
||||
*/
|
||||
@Type(JsonType.class)
|
||||
@JdbcTypeCode(SqlTypes.JSON)
|
||||
@Column(name = "target_")
|
||||
private List<Target> targets;
|
||||
private List<Target> targets;
|
||||
|
||||
/**
|
||||
* UserAgent
|
||||
*/
|
||||
@Type(JsonType.class)
|
||||
@JdbcTypeCode(SqlTypes.JSON)
|
||||
@Column(name = "user_agent")
|
||||
private UserAgent userAgent;
|
||||
private UserAgent userAgent;
|
||||
|
||||
/**
|
||||
* 地理位置
|
||||
*/
|
||||
@Type(JsonType.class)
|
||||
@JdbcTypeCode(SqlTypes.JSON)
|
||||
@Column(name = "geo_location")
|
||||
private GeoLocation geoLocation;
|
||||
private GeoLocation geoLocation;
|
||||
|
||||
/**
|
||||
* 审计事件类型
|
||||
*/
|
||||
@Column(name = "event_type")
|
||||
private EventType eventType;
|
||||
private EventType eventType;
|
||||
|
||||
/**
|
||||
* 参数
|
||||
*/
|
||||
@Column(name = "event_param")
|
||||
private String eventParam;
|
||||
private String eventParam;
|
||||
|
||||
/**
|
||||
* 事件内容
|
||||
*/
|
||||
@Column(name = "event_content")
|
||||
private String eventContent;
|
||||
private String eventContent;
|
||||
|
||||
/**
|
||||
* 事件结果
|
||||
*/
|
||||
@Column(name = "event_result")
|
||||
private String eventResult;
|
||||
private String eventResult;
|
||||
|
||||
/**
|
||||
* 事件时间
|
||||
*/
|
||||
@Column(name = "event_time")
|
||||
private LocalDateTime eventTime;
|
||||
private LocalDateTime eventTime;
|
||||
|
||||
/**
|
||||
* 事件状态
|
||||
*/
|
||||
@Column(name = "event_status")
|
||||
private EventStatus eventStatus;
|
||||
private EventStatus eventStatus;
|
||||
|
||||
/**
|
||||
* 操作者ID
|
||||
*/
|
||||
@Column(name = "actor_id")
|
||||
private String actorId;
|
||||
private String actorId;
|
||||
|
||||
/**
|
||||
* 操作人类型
|
||||
*/
|
||||
@Column(name = "actor_type")
|
||||
private UserType actorType;
|
||||
private UserType actorType;
|
||||
|
||||
/**
|
||||
* 身份验证类型
|
||||
*/
|
||||
@Column(name = "actor_auth_type")
|
||||
private String actorAuthType;
|
||||
private String actorAuthType;
|
||||
}
|
||||
|
|
|
@ -303,7 +303,7 @@ public class AuditEventPublish {
|
|||
.ip(geoLocation.getIp())
|
||||
.provider(geoLocation.getProvider())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
GeoPoint geoPoint = null;
|
||||
if (!Objects.isNull(geoLocation.getLatitude()) && !Objects.isNull(geoLocation.getLongitude())) {
|
||||
geoPoint = new GeoPoint(geoLocation.getLatitude(), geoLocation.getLongitude());
|
||||
|
|
|
@ -20,8 +20,8 @@ package cn.topiam.employee.audit.repository;
|
|||
import java.time.LocalDateTime;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
|
@ -36,7 +36,7 @@ import cn.topiam.employee.support.repository.LogicDeleteRepository;
|
|||
*/
|
||||
@Repository
|
||||
public interface AuditRepository extends LogicDeleteRepository<AuditEntity, Long>,
|
||||
QuerydslPredicateExecutor<AuditEntity>, AuditCustomizedRepository {
|
||||
AuditCustomizedRepository, JpaSpecificationExecutor<AuditEntity> {
|
||||
|
||||
/**
|
||||
* 统计指定时间范围内用户登录失败次数
|
||||
|
|
|
@ -23,16 +23,13 @@ import java.util.Objects;
|
|||
import java.util.Optional;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.querydsl.core.types.ExpressionUtils;
|
||||
import com.querydsl.core.types.Predicate;
|
||||
|
||||
import cn.topiam.employee.audit.controller.pojo.AuditListQuery;
|
||||
import cn.topiam.employee.audit.controller.pojo.AuditListResult;
|
||||
import cn.topiam.employee.audit.entity.AuditEntity;
|
||||
import cn.topiam.employee.audit.entity.QAuditEntity;
|
||||
import cn.topiam.employee.audit.entity.Target;
|
||||
import cn.topiam.employee.audit.enums.TargetType;
|
||||
import cn.topiam.employee.common.entity.account.OrganizationEntity;
|
||||
|
@ -49,13 +46,17 @@ import cn.topiam.employee.common.repository.account.UserRepository;
|
|||
import cn.topiam.employee.common.repository.app.AppRepository;
|
||||
import cn.topiam.employee.common.repository.authentication.IdentityProviderRepository;
|
||||
import cn.topiam.employee.common.repository.identitysource.IdentitySourceRepository;
|
||||
import cn.topiam.employee.common.repository.setting.*;
|
||||
import cn.topiam.employee.common.repository.setting.AdministratorRepository;
|
||||
import cn.topiam.employee.common.repository.setting.MailTemplateRepository;
|
||||
import cn.topiam.employee.support.context.ApplicationContextHelp;
|
||||
import cn.topiam.employee.support.repository.page.domain.Page;
|
||||
import cn.topiam.employee.support.repository.page.domain.PageModel;
|
||||
import cn.topiam.employee.support.security.userdetails.UserType;
|
||||
import cn.topiam.employee.support.security.util.SecurityUtils;
|
||||
|
||||
import jakarta.persistence.criteria.Order;
|
||||
import jakarta.persistence.criteria.Predicate;
|
||||
|
||||
/**
|
||||
* 审计数据转换
|
||||
*
|
||||
|
@ -129,8 +130,7 @@ public interface AuditDataConverter {
|
|||
if (UserType.USER.equals(actorType)) {
|
||||
UserRepository repository = ApplicationContextHelp.getBean(UserRepository.class);
|
||||
UserEntity user = repository.findById(Long.valueOf(actorId)).orElse(new UserEntity());
|
||||
return org.apache.commons.lang3.StringUtils.defaultString(user.getFullName(),
|
||||
user.getUsername());
|
||||
return Objects.toString(user.getFullName(), user.getUsername());
|
||||
}
|
||||
if (UserType.ADMIN.equals(actorType)) {
|
||||
AdministratorRepository repository = ApplicationContextHelp
|
||||
|
@ -148,67 +148,82 @@ public interface AuditDataConverter {
|
|||
* @param query {@link AuditListQuery}
|
||||
* @return {@link Predicate}
|
||||
*/
|
||||
default Predicate auditListRequestConvertToPredicate(AuditListQuery query) {
|
||||
QAuditEntity auditEntity = QAuditEntity.auditEntity;
|
||||
Predicate predicate = ExpressionUtils.and(auditEntity.isNotNull(),
|
||||
auditEntity.deleted.eq(Boolean.FALSE));
|
||||
UserType userType = SecurityUtils.getCurrentUser().getUserType();
|
||||
// 登录角色 管理员
|
||||
if (UserType.ADMIN.equals(userType)) {
|
||||
if (StringUtils.hasText(query.getUsername())) {
|
||||
String actorId = "";
|
||||
if (UserType.USER.getType().equals(query.getUserType())) {
|
||||
UserRepository userRepository = ApplicationContextHelp
|
||||
.getBean(UserRepository.class);
|
||||
UserEntity user = userRepository.findByUsername(query.getUsername());
|
||||
if (!Objects.isNull(user)) {
|
||||
actorId = user.getId().toString();
|
||||
default Specification<AuditEntity> auditListRequestConvertToSpecification(AuditListQuery query,
|
||||
PageModel page) {
|
||||
return (root, criteriaQuery, criteriaBuilder) -> {
|
||||
ArrayList<Predicate> predicates = new ArrayList<>();
|
||||
ArrayList<Order> orders = new ArrayList<>();
|
||||
UserType userType = SecurityUtils.getCurrentUser().getUserType();
|
||||
// 登录角色 管理员
|
||||
if (UserType.ADMIN.equals(userType)) {
|
||||
if (StringUtils.hasText(query.getUsername())) {
|
||||
String actorId = "";
|
||||
if (UserType.USER.getType().equals(query.getUserType())) {
|
||||
UserRepository userRepository = ApplicationContextHelp
|
||||
.getBean(UserRepository.class);
|
||||
UserEntity user = userRepository.findByUsername(query.getUsername());
|
||||
if (!Objects.isNull(user)) {
|
||||
actorId = user.getId().toString();
|
||||
}
|
||||
}
|
||||
if (UserType.ADMIN.getType().equals(query.getUserType())) {
|
||||
AdministratorRepository administratorRepository = ApplicationContextHelp
|
||||
.getBean(AdministratorRepository.class);
|
||||
Optional<AdministratorEntity> optional = administratorRepository
|
||||
.findByUsername(query.getUsername());
|
||||
if (optional.isPresent()) {
|
||||
actorId = optional.get().getId().toString();
|
||||
}
|
||||
}
|
||||
if (StringUtils.hasText(actorId)) {
|
||||
predicates.add(criteriaBuilder.equal(root.get("actorId"), actorId));
|
||||
}
|
||||
}
|
||||
// 用户类型
|
||||
if (UserType.USER.getType().equals(query.getUserType())) {
|
||||
predicates.add(criteriaBuilder.equal(root.get("actorType"), UserType.USER));
|
||||
}
|
||||
if (UserType.ADMIN.getType().equals(query.getUserType())) {
|
||||
AdministratorRepository administratorRepository = ApplicationContextHelp
|
||||
.getBean(AdministratorRepository.class);
|
||||
Optional<AdministratorEntity> optional = administratorRepository
|
||||
.findByUsername(query.getUsername());
|
||||
if (optional.isPresent()) {
|
||||
actorId = optional.get().getId().toString();
|
||||
predicates.add(criteriaBuilder.equal(root.get("actorType"), UserType.ADMIN));
|
||||
}
|
||||
}
|
||||
// 登录角色 用户
|
||||
if (UserType.USER.equals(userType)) {
|
||||
predicates.add(criteriaBuilder.equal(root.get("actorId"),
|
||||
SecurityUtils.getCurrentUser().getId()));
|
||||
// 用户类型
|
||||
predicates.add(criteriaBuilder.equal(root.get("actorType"), UserType.USER));
|
||||
}
|
||||
// 事件类型
|
||||
if (!CollectionUtils.isEmpty(query.getEventType())) {
|
||||
predicates
|
||||
.add(criteriaBuilder.in(root.get("eventType")).value(query.getEventType()));
|
||||
}
|
||||
// 事件状态
|
||||
if (Objects.nonNull(query.getEventStatus())) {
|
||||
predicates
|
||||
.add(criteriaBuilder.equal(root.get("eventStatus"), query.getEventStatus()));
|
||||
}
|
||||
// 事件时间
|
||||
if (!Objects.isNull(query.getStartEventTime())
|
||||
&& !Objects.isNull(query.getEndEventTime())) {
|
||||
predicates.add(criteriaBuilder.between(root.get("eventTime"),
|
||||
query.getStartEventTime(), query.getEndEventTime()));
|
||||
}
|
||||
//默认desc
|
||||
criteriaQuery.orderBy(criteriaBuilder.desc(root.get("eventTime")));
|
||||
for (PageModel.Sort sort : page.getSorts()) {
|
||||
if (org.apache.commons.lang3.StringUtils.equals(sort.getSorter(),
|
||||
SORT_EVENT_TIME)) {
|
||||
if (sort.getAsc()) {
|
||||
orders.add(criteriaBuilder.asc(root.get("eventTime")));
|
||||
}
|
||||
}
|
||||
predicate = ExpressionUtils.and(predicate, auditEntity.actorId.eq(actorId));
|
||||
}
|
||||
// 用户类型
|
||||
if (UserType.USER.getType().equals(query.getUserType())) {
|
||||
predicate = ExpressionUtils.and(predicate, auditEntity.actorType.eq(UserType.USER));
|
||||
}
|
||||
if (UserType.ADMIN.getType().equals(query.getUserType())) {
|
||||
predicate = ExpressionUtils.and(predicate,
|
||||
auditEntity.actorType.eq(UserType.ADMIN));
|
||||
}
|
||||
}
|
||||
// 登录角色 管理员
|
||||
if (UserType.USER.equals(userType)) {
|
||||
predicate = ExpressionUtils.and(predicate,
|
||||
auditEntity.actorId.eq(SecurityUtils.getCurrentUser().getId()));
|
||||
// 用户类型
|
||||
predicate = ExpressionUtils.and(predicate, auditEntity.actorType.eq(UserType.USER));
|
||||
}
|
||||
// 事件类型
|
||||
if (!CollectionUtils.isEmpty(query.getEventType())) {
|
||||
predicate = ExpressionUtils.and(predicate,
|
||||
auditEntity.eventType.in(query.getEventType()));
|
||||
}
|
||||
// 事件状态
|
||||
if (Objects.nonNull(query.getEventStatus())) {
|
||||
predicate = ExpressionUtils.and(predicate,
|
||||
auditEntity.eventStatus.in(query.getEventStatus()));
|
||||
}
|
||||
// 事件时间
|
||||
if (!Objects.isNull(query.getStartEventTime())
|
||||
&& !Objects.isNull(query.getEndEventTime())) {
|
||||
predicate = ExpressionUtils.and(predicate,
|
||||
auditEntity.eventTime.between(query.getStartEventTime(), query.getEndEventTime()));
|
||||
}
|
||||
return predicate;
|
||||
criteriaQuery.orderBy(orders);
|
||||
criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[0])));
|
||||
return criteriaQuery.getRestriction();
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -227,7 +242,7 @@ public interface AuditDataConverter {
|
|||
Optional<UserEntity> user = userRepository.findByIdContainsDeleted(Long.valueOf(id));
|
||||
if (user.isPresent()) {
|
||||
UserEntity entity = user.get();
|
||||
name = org.apache.commons.lang3.StringUtils.defaultString(entity.getFullName(),
|
||||
name = Objects.toString(entity.getFullName(),
|
||||
entity.getUsername());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue