Kaynağa Gözat

授权管理

xiatian 16 saat önce
ebeveyn
işleme
9230ed85f0

+ 97 - 0
stmms-biz/src/main/java/cn/com/qmth/stmms/biz/exam/bean/SystemAuthVo.java

@@ -0,0 +1,97 @@
+package cn.com.qmth.stmms.biz.exam.bean;
+
+import java.util.Date;
+
+import io.swagger.annotations.ApiModelProperty;
+
+public class SystemAuthVo {
+
+    @ApiModelProperty("版本号")
+    private String version;
+
+    @ApiModelProperty("过期时间")
+    private Date expireTime;
+
+    @ApiModelProperty("当前信息")
+    private String authText;
+
+    @ApiModelProperty("授权模式")
+    private String typeText;
+
+    @ApiModelProperty("双评轨迹复核")
+    private String doubleTrackText;
+
+    @ApiModelProperty("分组删除警告")
+    private String groupDeleteWarnText;
+
+    @ApiModelProperty("试评卷模式")
+    private String trialModeText;
+
+    @ApiModelProperty("是否支持客观题卡")
+    private String objectiveEnableText;
+
+    public Date getExpireTime() {
+        return expireTime;
+    }
+
+    public void setExpireTime(Date expireTime) {
+        this.expireTime = expireTime;
+    }
+
+    public String getAuthText() {
+        return authText;
+    }
+
+    public void setAuthText(String authText) {
+        this.authText = authText;
+    }
+
+    public String getTypeText() {
+        return typeText;
+    }
+
+    public void setTypeText(String typeText) {
+        this.typeText = typeText;
+    }
+
+    public String getVersion() {
+        return version;
+    }
+
+    public void setVersion(String version) {
+        this.version = version;
+    }
+
+    public String getDoubleTrackText() {
+        return doubleTrackText;
+    }
+
+    public void setDoubleTrackText(String doubleTrackText) {
+        this.doubleTrackText = doubleTrackText;
+    }
+
+    public String getGroupDeleteWarnText() {
+        return groupDeleteWarnText;
+    }
+
+    public void setGroupDeleteWarnText(String groupDeleteWarnText) {
+        this.groupDeleteWarnText = groupDeleteWarnText;
+    }
+
+    public String getTrialModeText() {
+        return trialModeText;
+    }
+
+    public void setTrialModeText(String trialModeText) {
+        this.trialModeText = trialModeText;
+    }
+
+    public String getObjectiveEnableText() {
+        return objectiveEnableText;
+    }
+
+    public void setObjectiveEnableText(String objectiveEnableText) {
+        this.objectiveEnableText = objectiveEnableText;
+    }
+
+}

+ 166 - 0
stmms-web/src/main/java/cn/com/qmth/stmms/api/controller/admin/SystemAuthController.java

@@ -0,0 +1,166 @@
+package cn.com.qmth.stmms.api.controller.admin;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.servlet.mvc.support.RedirectAttributes;
+
+import com.qmth.boot.tools.models.ByteArray;
+
+import cn.com.qmth.stmms.api.controller.BaseApiController;
+import cn.com.qmth.stmms.biz.config.model.SystemAuth;
+import cn.com.qmth.stmms.biz.config.service.SystemAuthService;
+import cn.com.qmth.stmms.biz.config.service.impl.SystemCache;
+import cn.com.qmth.stmms.biz.exam.bean.ResultMessage;
+import cn.com.qmth.stmms.biz.exam.bean.SystemAuthVo;
+import cn.com.qmth.stmms.biz.exception.StatusException;
+import cn.com.qmth.stmms.biz.school.service.SchoolService;
+import cn.com.qmth.stmms.biz.utils.AppLicenseUtil;
+import cn.com.qmth.stmms.biz.utils.SolarHttpUtil;
+import cn.com.qmth.stmms.common.annotation.Logging;
+import cn.com.qmth.stmms.common.enums.LogType;
+import cn.com.qmth.stmms.common.enums.SystemAuthType;
+import cn.com.qmth.stmms.common.enums.TrialMode;
+import cn.com.qmth.stmms.common.utils.AesUtils;
+import cn.com.qmth.stmms.common.utils.Encodes;
+import cn.com.qmth.stmms.common.utils.VersionInfo;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import net.sf.json.JSONObject;
+
+@Api(tags = "授权管理")
+@Controller("adminSystemAuthController")
+@RequestMapping("/api/admin/sys/auth")
+public class SystemAuthController extends BaseApiController {
+
+    private static Logger log = LoggerFactory.getLogger(SystemAuthController.class);
+
+    @Autowired
+    private SystemAuthService authService;
+
+    @Autowired
+    private SchoolService schoolService;
+
+    @Autowired
+    private SystemCache authCache;
+
+    @Value("${qmth.solar.host}")
+    private String host;
+
+    @Value("${qmth.solar.app.uri}")
+    private String uri;
+
+    @ApiOperation(value = "授权模式")
+    @RequestMapping(value = "types", method = RequestMethod.POST)
+    @ResponseBody
+    public List<SystemAuthType> systemAuthTypeList() {
+        return Arrays.asList(SystemAuthType.values());
+    }
+
+    @Logging(menu = "查询授权信息", type = LogType.QUERY)
+    @ApiOperation(value = "授权信息")
+    @RequestMapping(value = "info", method = RequestMethod.POST)
+    @ResponseBody
+    public SystemAuthVo info() {
+        SystemAuthVo ret = new SystemAuthVo();
+        SystemAuth systemAuth = authService.findOne();
+        if (authCache.isAuth()) {
+            ret.setAuthText("已授权");
+            ret.setTypeText(systemAuth.getType().getName());
+            ret.setExpireTime(authCache.getExpireTime() == null ? null : new Date(authCache.getExpireTime()));
+            ret.setDoubleTrackText(authCache.isDoubleTrack() ? "启用" : "未启用");
+            ret.setGroupDeleteWarnText(authCache.isGroupDeleteWarn() ? "启用" : "未启用");
+            ret.setTrialModeText(
+                    authCache.getTrialMode() == null ? TrialMode.SHARE.getName() : authCache.getTrialMode().getName());
+            ret.setObjectiveEnableText(authCache.isYjsObjectiveEnable() ? "支持" : "不支持");
+        } else {
+            ret.setAuthText("未授权");
+        }
+        ret.setVersion("v" + VersionInfo.NAME + " build" + VersionInfo.DATE);
+        return ret;
+    }
+
+    @Logging(menu = "修改授权配置", type = LogType.UPDATE)
+    @ApiOperation(value = "修改授权配置")
+    @RequestMapping(value = "update", method = RequestMethod.POST)
+    @ResponseBody
+    public ResultMessage update(SystemAuth systemAuth, MultipartFile file) {
+        if (SystemAuthType.ONLINE.equals(systemAuth.getType())) {
+            SolarHttpUtil httpUtil = new SolarHttpUtil(systemAuth.getAccessKey(), systemAuth.getAccessSecret(), host,
+                    uri);
+            systemAuth.setAccessKey(AesUtils.encrypt(systemAuth.getAccessKey()));
+            systemAuth.setAccessSecret(AesUtils.encrypt(systemAuth.getAccessSecret()));
+            authService.save(systemAuth);
+            if (httpUtil.getAPP()) {
+                authCache.setAuth(true);
+                authCache.setDoubleTrack(true);
+                authCache.setExpireTime(null);
+                authCache.setTrialMode(TrialMode.SHARE);
+                authCache.setYjsObjectiveEnable(false);
+                schoolService.updateOrg();
+            } else {
+                authCache.setAuth(false);
+                authCache.setDoubleTrack(false);
+                authCache.setExpireTime(null);
+                authCache.setTrialMode(TrialMode.SHARE);
+                authCache.setYjsObjectiveEnable(false);
+                throw new StatusException("授权验证失败");
+            }
+        } else {
+            try {
+                systemAuth.setDescription(ByteArray.fromInputStream(file.getInputStream()).toBase64());
+                JSONObject json = AppLicenseUtil.parseLicense(ByteArray.fromInputStream(file.getInputStream()).value());
+                authCache.parseJson(json);
+                authService.save(systemAuth);
+                if (json == null) {
+                    throw new StatusException("授权文件解析失败");
+                }
+            } catch (IOException e) {
+                log.error("parseLicense error", e);
+                throw new StatusException("授权文件解析失败");
+            }
+        }
+        return resultOk();
+    }
+
+    @Logging(menu = "导出硬件信息", type = LogType.EXPORT)
+    @ApiOperation(value = "导出硬件信息")
+    @RequestMapping(value = "export", method = RequestMethod.POST)
+    @ResponseBody
+    public void exportInfo(HttpServletResponse response) {
+        try {
+            download(response, AppLicenseUtil.buildDeviceInfo().value());
+        } catch (Exception e) {
+            log.error("export device.info error", e);
+            throw new StatusException("导出失败", e);
+        }
+    }
+
+    private void download(HttpServletResponse response, byte[] data) throws Exception {
+        response.setHeader("Content-Disposition", "attachment; filename=" + Encodes.urlEncode("device.info"));
+        IOUtils.copy(new ByteArrayInputStream(data), response.getOutputStream());
+    }
+
+    protected void addMessage(RedirectAttributes redirectAttributes, String... messages) {
+        StringBuilder sb = new StringBuilder();
+        for (String message : messages) {
+            sb.append(message).append(messages.length > 1 ? "<br/>" : "");
+        }
+        redirectAttributes.addFlashAttribute("message", sb.toString());
+    }
+}