package com.rongyichuang.common.util;
|
|
import com.rongyichuang.judge.entity.Judge;
|
import com.rongyichuang.judge.repository.JudgeRepository;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.stereotype.Component;
|
|
import java.util.Optional;
|
|
/**
|
* 用户上下文工具类
|
* 用于获取当前登录用户信息和关联的评委信息
|
*/
|
@Component
|
public class UserContextUtil {
|
|
private static final Logger logger = LoggerFactory.getLogger(UserContextUtil.class);
|
|
@Autowired
|
private JudgeRepository judgeRepository;
|
|
/**
|
* 获取当前登录用户ID
|
* 注意:当前系统暂时使用固定用户ID,后续需要根据实际认证机制修改
|
*
|
* @return 用户ID
|
*/
|
public Long getCurrentUserId() {
|
try {
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
if (authentication != null && authentication.isAuthenticated() &&
|
!"anonymousUser".equals(authentication.getPrincipal())) {
|
// TODO: 从认证信息中获取真实的用户ID
|
// 这里需要根据实际的认证机制来实现
|
// 例如:从JWT token中解析用户ID,或从UserDetails中获取
|
logger.debug("获取到认证用户: {}", authentication.getName());
|
return 1L; // 临时返回固定用户ID
|
}
|
} catch (Exception e) {
|
logger.warn("获取当前用户ID时发生异常: {}", e.getMessage());
|
}
|
|
// 如果没有认证信息,返回默认用户ID(开发阶段使用)
|
logger.debug("未找到认证信息,使用默认用户ID");
|
return 1L;
|
}
|
|
/**
|
* 获取当前用户关联的评委信息
|
*
|
* @return 评委信息,如果当前用户不是评委则返回空
|
*/
|
public Optional<Judge> getCurrentJudge() {
|
Long userId = getCurrentUserId();
|
if (userId == null) {
|
logger.warn("无法获取当前用户ID");
|
return Optional.empty();
|
}
|
|
try {
|
Optional<Judge> judge = judgeRepository.findByUserId(userId);
|
if (judge.isPresent()) {
|
logger.debug("找到当前用户关联的评委: {}", judge.get().getName());
|
} else {
|
logger.debug("当前用户(ID: {})不是评委", userId);
|
}
|
return judge;
|
} catch (Exception e) {
|
logger.error("查询评委信息时发生异常: {}", e.getMessage(), e);
|
return Optional.empty();
|
}
|
}
|
|
/**
|
* 获取当前用户关联的评委ID
|
*
|
* @return 评委ID,如果当前用户不是评委则返回null
|
*/
|
public Long getCurrentJudgeId() {
|
return getCurrentJudge().map(Judge::getId).orElse(null);
|
}
|
|
/**
|
* 检查当前用户是否为评委
|
*
|
* @return true如果当前用户是评委,否则false
|
*/
|
public boolean isCurrentUserJudge() {
|
return getCurrentJudge().isPresent();
|
}
|
|
/**
|
* 检查当前用户是否为指定活动的评委
|
*
|
* @param activityId 活动ID
|
* @return true如果当前用户是该活动的评委,否则false
|
*/
|
public boolean isCurrentUserJudgeForActivity(Long activityId) {
|
Optional<Judge> judge = getCurrentJudge();
|
if (judge.isEmpty()) {
|
return false;
|
}
|
|
try {
|
// 通过ActivityJudge表检查当前评委是否参与指定活动
|
return judgeRepository.existsByIdAndActivityId(judge.get().getId(), activityId);
|
} catch (Exception e) {
|
logger.error("检查评委活动权限时发生异常: {}", e.getMessage(), e);
|
return false;
|
}
|
}
|
|
/**
|
* 获取当前用户名称(用于日志记录)
|
*
|
* @return 用户名称
|
*/
|
public String getCurrentUserName() {
|
try {
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
if (authentication != null && authentication.isAuthenticated()) {
|
return authentication.getName();
|
}
|
} catch (Exception e) {
|
logger.warn("获取当前用户名称时发生异常: {}", e.getMessage());
|
}
|
return "未知用户";
|
}
|
}
|