Explorar el Código

Merge branch 'westt/deal_todo' of rongrunxiang/OnlineJudge into master

rongrunxiang hace 1 año
padre
commit
f4a2e0481a

+ 12 - 9
src/main/java/com/example/onlinejudge/controller/QuestionController.java

@@ -1,10 +1,12 @@
 package com.example.onlinejudge.controller;
 
+import com.alibaba.druid.wall.violation.ErrorCode;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 
 import com.example.onlinejudge.common.DeleteRequest;
 
 
+import com.example.onlinejudge.exception.BusinessException;
 import com.example.onlinejudge.model.VO.QuestionVO;
 import com.example.onlinejudge.model.dto.question.*;
 import com.example.onlinejudge.model.entity.Question;
@@ -66,9 +68,9 @@ public class QuestionController {
             question.setJudgeConfig(GSON.toJson(judgeConfig));
         }
         questionService.validQuestion(question, true);
-        //TODO:记录谁创建了这个题目
-//        User loginUser = userService.getLoginUser(request);
-//        question.setUserId(loginUser.getId());
+        //TODO:记录谁创建了这个题目 Done
+        Long loginUserId = userService.getLoginUserId();
+        question.setUserId(loginUserId);
         boolean result = questionService.save(question);
         if(!result){
             return Result.error(ResultCode.PARAM_IS_INVALID);
@@ -98,8 +100,8 @@ public class QuestionController {
         }
         //ThrowUtils.throwIf(oldQuestion == null, ErrorCode.NOT_FOUND_ERROR);
         // 仅本人或管理员可删除
-        //TODO:判断是否是管理员
-        if (false) {
+        //TODO:判断是否是管理员 DONE
+        if (userService.isAdmin(userService.getLoginUserId())) {
             return Result.error(ResultCode.PERMISSION_DENIED);
         }
         boolean b = questionService.removeById(id);
@@ -122,12 +124,13 @@ public class QuestionController {
         if (question == null) {
             return Result.error(ResultCode.NOT_FOUND_ERROR);
         }
-        //TODO:判断是否是管理员
+        //TODO:判断是否是管理员 DONE
 //        User loginUser = userService.getLoginUser(request);
+        Long loginUserId = userService.getLoginUserId();
 //        // 不是本人或管理员,不能直接获取所有信息
-//        if (!question.getUserId().equals(loginUser.getId()) && !userService.isAdmin(loginUser)) {
-//            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
-//        }
+        if (!question.getUserId().equals(loginUserId) && !userService.isAdmin(loginUserId)) {
+            throw new BusinessException(ResultCode.PERMISSION_DENIED);
+        }
         return Result.success(question);
     }
 

+ 8 - 9
src/main/java/com/example/onlinejudge/controller/QuestionSubmitController.java

@@ -1,6 +1,7 @@
 package com.example.onlinejudge.controller;
 
 
+import cn.dev33.satoken.stp.StpUtil;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 
 import com.example.onlinejudge.model.VO.QuestionSubmitVO;
@@ -51,11 +52,10 @@ public class QuestionSubmitController {
         if (questionSubmitAddRequest == null || questionSubmitAddRequest.getQuestionId() <= 0) {
             return new Result(ResultCode.PARAM_IS_INVALID.getCode(),ResultCode.PARAM_IS_INVALID.getMessage());
         }
-        // TODO: 获取登录id
-//        final User loginUser = userService.getLoginUser(request);
-//        long questionSubmitId = questionSubmitService.doQuestionSubmit(questionSubmitAddRequest, loginUser);
-//        return Result.success(questionSubmitId);
-        return null;
+        // TODO: 获取登录id DONE
+        long logInId = StpUtil.getLoginIdAsLong();
+        long questionSubmitId = questionSubmitService.doQuestionSubmit(questionSubmitAddRequest, logInId);
+        return Result.success(questionSubmitId);
     }
 
     /**
@@ -77,10 +77,9 @@ public class QuestionSubmitController {
         // 从数据库中查询原始的题目提交分页信息
         Page<QuestionSubmit> questionSubmitPage = questionSubmitService.page(new Page<>(current, size),
                 questionSubmitService.getQueryWrapper(questionSubmitQueryRequest));
-//        final User loginUser = userService.getLoginUser(request);
-        // TODO 返回脱敏信息
-//        return ResultUtils.success(questionSubmitService.getQuestionSubmitVOPage(questionSubmitPage, loginUser));
-        return null;
+        final long logInId = StpUtil.getLoginIdAsLong();
+        // TODO 返回脱敏信息 DONE
+        return Result.success(questionSubmitService.getQuestionSubmitVOPage(questionSubmitPage, logInId));
     }
 
 

+ 0 - 3
src/main/java/com/example/onlinejudge/mapper/UserMapper.java

@@ -6,7 +6,4 @@ import org.apache.ibatis.annotations.Mapper;
 
 @Mapper
 public interface UserMapper extends BaseMapper<User> {
-    User selectUserById(Integer id);
-    User selectUserByUsernameAndPassword(String username, String password);
-    boolean insertUser(String username, String password);
 }

+ 17 - 0
src/main/java/com/example/onlinejudge/model/VO/UserVO.java

@@ -1,5 +1,7 @@
 package com.example.onlinejudge.model.VO;
 
+import cn.hutool.core.bean.BeanUtil;
+import com.example.onlinejudge.model.entity.User;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
@@ -30,4 +32,19 @@ public class UserVO implements Serializable {
     private Date createTime;
 
     private static final long serialVersionUID = 1L;
+
+    /** TODO: 没有对userProfile和userAvatar、createTime属性进行处理
+     * User对象转UserVO包装类
+     * @param user User对象
+     * @return UserVo
+     */
+    public static UserVO objToVo(User user) {
+        if (user == null)
+            return null;
+        UserVO userVO = new UserVO();
+        userVO.setUserName(user.getUsername());
+        userVO.setUserRole(user.getRole());
+        userVO.setId(user.getId());
+        return userVO;
+    }
 }

+ 2 - 2
src/main/java/com/example/onlinejudge/model/dto/user/UserLoginRequest.java

@@ -10,12 +10,12 @@ import java.io.Serializable;
 @Data
 public class UserLoginRequest implements Serializable {
 
-    @ApiModelProperty("用户ID")
-    private static final long serialVersionUID = 3191241716373120793L;
 
     @ApiModelProperty("用户名")
     private String userName;
 
     @ApiModelProperty("密码")
     private String password;
+
+    private static final long serialVersionUID = 3191241716373120793L;
 }

+ 2 - 0
src/main/java/com/example/onlinejudge/model/entity/User.java

@@ -29,4 +29,6 @@ public class User {
 
     @ApiModelProperty("密码")
     private String password;
+    @ApiModelProperty("用户权限")
+    private String role;
 }

+ 6 - 6
src/main/java/com/example/onlinejudge/service/QuestionSubmitService.java

@@ -16,10 +16,10 @@ public interface QuestionSubmitService extends IService<QuestionSubmit> {
      * 题目提交
      *
      * @param questionSubmitAddRequest 题目提交信息
-     * @param loginUser
+     * @param loginUserId
      * @return
      */
-    long doQuestionSubmit(QuestionSubmitAddRequest questionSubmitAddRequest, User loginUser);
+    long doQuestionSubmit(QuestionSubmitAddRequest questionSubmitAddRequest, Long loginUserId);
 
     /**
      * 获取查询条件
@@ -33,17 +33,17 @@ public interface QuestionSubmitService extends IService<QuestionSubmit> {
      * 获取题目封装
      *
      * @param questionSubmit
-     * @param loginUser
+     * @param loginUserId
      * @return
      */
-    QuestionSubmitVO getQuestionSubmitVO(QuestionSubmit questionSubmit, User loginUser);
+    QuestionSubmitVO getQuestionSubmitVO(QuestionSubmit questionSubmit, Long loginUserId);
 
     /**
      * 分页获取题目封装
      *
      * @param questionSubmitPage
-     * @param loginUser
+     * @param loginUserId
      * @return
      */
-    Page<QuestionSubmitVO> getQuestionSubmitVOPage(Page<QuestionSubmit> questionSubmitPage, User loginUser);
+    Page<QuestionSubmitVO> getQuestionSubmitVOPage(Page<QuestionSubmit> questionSubmitPage, Long loginUserId);
 }

+ 13 - 1
src/main/java/com/example/onlinejudge/service/UserService.java

@@ -1,10 +1,22 @@
 package com.example.onlinejudge.service;
 
 import cn.dev33.satoken.util.SaResult;
+import com.example.onlinejudge.model.VO.UserVO;
+import com.example.onlinejudge.model.entity.User;
 import com.example.onlinejudge.model.entity.result.Result;
 
+import javax.servlet.http.HttpServletRequest;
+
 public interface UserService {
     Result login(String username, String password);
 
-    String getLoginUser();
+    User getLoginUser(HttpServletRequest request);
+
+    Boolean isAdmin(Long userId);
+
+    Long getLoginUserId();
+
+    User getUserById(Long id);
+
+    UserVO getUserVO(User user);
 }

+ 4 - 3
src/main/java/com/example/onlinejudge/service/impl/QuestionServiceImpl.java

@@ -140,12 +140,13 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question>
         List<QuestionVO> questionVOList = questionList.stream().map(question -> {
             QuestionVO questionVO = QuestionVO.objToVo(question);
             Long userId = question.getUserId();
-            User user = null;
-            //TODO 填充userID
+            User user = userService.getUserById(userId);
+            //TODO 填充userID Done
+
 //            if (userIdUserListMap.containsKey(userId)) {
 //                user = userIdUserListMap.get(userId).get(0);
 //            }
-//            questionVO.setUserVO(userService.getUserVO(user));
+            questionVO.setUserVO(userService.getUserVO(user));
             return questionVO;
         }).collect(Collectors.toList());
         questionVOPage.setRecords(questionVOList);

+ 15 - 15
src/main/java/com/example/onlinejudge/service/impl/QuestionSubmitServiceImpl.java

@@ -24,12 +24,13 @@ import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.context.annotation.Lazy;
+import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
 import java.util.List;
 import java.util.concurrent.CompletableFuture;
 import java.util.stream.Collectors;
-
+@Service
 public class QuestionSubmitServiceImpl extends ServiceImpl<QuestionSubmitMapper, QuestionSubmit>
         implements QuestionSubmitService {
 
@@ -47,11 +48,11 @@ public class QuestionSubmitServiceImpl extends ServiceImpl<QuestionSubmitMapper,
      * 提交题目
      *
      * @param questionSubmitAddRequest
-     * @param loginUser
+     * @param loginUserId
      * @return
      */
     @Override
-    public long doQuestionSubmit(QuestionSubmitAddRequest questionSubmitAddRequest, User loginUser) {
+    public long doQuestionSubmit(QuestionSubmitAddRequest questionSubmitAddRequest, Long loginUserId) {
         // 校验编程语言是否合法
         String language = questionSubmitAddRequest.getLanguage();
         QuestionSubmitLanguageEnum languageEnum = QuestionSubmitLanguageEnum.getEnumByValue(language);
@@ -65,10 +66,10 @@ public class QuestionSubmitServiceImpl extends ServiceImpl<QuestionSubmitMapper,
             throw new BusinessException(ResultCode.NOT_FOUND_ERROR);
         }
         // 是否已提交题目
-        long userId = loginUser.getId();
+
         // 每个用户串行提交题目
         QuestionSubmit questionSubmit = new QuestionSubmit();
-        questionSubmit.setUserId(userId);
+        questionSubmit.setUserId(loginUserId);
         questionSubmit.setQuestionId(questionId);
         questionSubmit.setCode(questionSubmitAddRequest.getCode());
         questionSubmit.setLanguage(language);
@@ -109,37 +110,36 @@ public class QuestionSubmitServiceImpl extends ServiceImpl<QuestionSubmitMapper,
 
         // 拼接查询条件
         queryWrapper.eq(StringUtils.isNotBlank(language), "language", language);
-        queryWrapper.eq(ObjectUtils.isNotEmpty(userId), "userId", userId);
-        queryWrapper.eq(ObjectUtils.isNotEmpty(questionId), "questionId", questionId);
+        queryWrapper.eq(ObjectUtils.isNotEmpty(userId), "user_id", userId);
+        queryWrapper.eq(ObjectUtils.isNotEmpty(questionId), "question_id", questionId);
         queryWrapper.eq(QuestionSubmitStatusEnum.getEnumByValue(status) != null, "status", status);
-        queryWrapper.eq("isDelete", false);
+        queryWrapper.eq("is_delete", 0);
         queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                 sortField);
         return queryWrapper;
     }
 
     @Override
-    public QuestionSubmitVO getQuestionSubmitVO(QuestionSubmit questionSubmit, User loginUser) {
+    public QuestionSubmitVO getQuestionSubmitVO(QuestionSubmit questionSubmit, Long userId) {
         QuestionSubmitVO questionSubmitVO = QuestionSubmitVO.objToVo(questionSubmit);
         // 脱敏:仅本人和管理员能看见自己(提交 userId 和登录用户 id 不同)提交的代码
-        long userId = loginUser.getId();
-        // 处理脱敏
-        //TODO  && !userService.isAdmin(loginUser)
-        if (userId != questionSubmit.getUserId() ) {
+
+        //TODO 处理脱敏 DONE
+        if (userId != questionSubmit.getUserId() && !userService.isAdmin(userId)) {
             questionSubmitVO.setCode(null);
         }
         return questionSubmitVO;
     }
 
     @Override
-    public Page<QuestionSubmitVO> getQuestionSubmitVOPage(Page<QuestionSubmit> questionSubmitPage, User loginUser) {
+    public Page<QuestionSubmitVO> getQuestionSubmitVOPage(Page<QuestionSubmit> questionSubmitPage, Long loginUserId) {
         List<QuestionSubmit> questionSubmitList = questionSubmitPage.getRecords();
         Page<QuestionSubmitVO> questionSubmitVOPage = new Page<>(questionSubmitPage.getCurrent(), questionSubmitPage.getSize(), questionSubmitPage.getTotal());
         if (CollectionUtils.isEmpty(questionSubmitList)) {
             return questionSubmitVOPage;
         }
         List<QuestionSubmitVO> questionSubmitVOList = questionSubmitList.stream()
-                .map(questionSubmit -> getQuestionSubmitVO(questionSubmit, loginUser))
+                .map(questionSubmit -> getQuestionSubmitVO(questionSubmit, loginUserId))
                 .collect(Collectors.toList());
         questionSubmitVOPage.setRecords(questionSubmitVOList);
         return questionSubmitVOPage;

+ 44 - 0
src/main/java/com/example/onlinejudge/service/impl/StpInterfaceImpl.java

@@ -0,0 +1,44 @@
+package com.example.onlinejudge.service.impl;
+
+import cn.dev33.satoken.stp.StpInterface;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.example.onlinejudge.mapper.UserMapper;
+import com.example.onlinejudge.model.entity.User;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 自定义权限加载接口实现类
+ */
+@Component    // 保证此类被 SpringBoot 扫描,完成 Sa-Token 的自定义权限验证扩展
+@Slf4j
+public class StpInterfaceImpl implements StpInterface {
+    @Autowired
+    private UserMapper userMapper;
+
+    /**
+     * 返回一个账号所拥有的权限码集合
+     */
+    @Override
+    public List<String> getPermissionList(Object loginId, String loginType) {
+        return null;
+    }
+
+    /**
+     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
+     */
+    @Override
+    public List<String> getRoleList(Object loginId, String loginType) {
+        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
+        queryWrapper.eq("id", loginId);
+        User user = userMapper.selectOne(queryWrapper);
+        List<String> list = new ArrayList<>();
+        list.add(user.getRole());
+        return list;
+    }
+
+}

+ 24 - 2
src/main/java/com/example/onlinejudge/service/impl/UserServiceImpl.java

@@ -1,10 +1,12 @@
 package com.example.onlinejudge.service.impl;
 
+import cn.dev33.satoken.exception.NotRoleException;
 import cn.dev33.satoken.stp.SaTokenInfo;
 import cn.dev33.satoken.stp.StpUtil;
 import cn.dev33.satoken.util.SaResult;
 import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.example.onlinejudge.model.VO.UserVO;
 import com.example.onlinejudge.model.entity.User;
 import com.example.onlinejudge.mapper.UserMapper;
 import com.example.onlinejudge.model.entity.result.Result;
@@ -14,6 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import sun.security.krb5.internal.PAData;
 
+import javax.servlet.http.HttpServletRequest;
 import java.util.LinkedHashMap;
 
 /**
@@ -24,6 +27,16 @@ public class UserServiceImpl implements UserService {
     @Autowired
     private UserMapper userMapper;
 
+    @Override
+    public Long getLoginUserId() {
+        return StpUtil.getLoginIdAsLong();
+    }
+
+    @Override
+    public Boolean isAdmin(Long userId) {
+        return StpUtil.hasRole("admin");
+    }
+
     /**
      * 用户登录服务
      *
@@ -44,8 +57,17 @@ public class UserServiceImpl implements UserService {
     }
 
     @Override
-    public String getLoginUser() {
-        return StpUtil.getLoginId().toString();
+    public User getLoginUser(HttpServletRequest request) {
+        return userMapper.selectById(StpUtil.getLoginIdAsLong());
     }
 
+    @Override
+    public User getUserById(Long id) {
+        return userMapper.selectById(id);
+    }
+
+    @Override
+    public UserVO getUserVO(User user) {
+        return null;
+    }
 }