Skip to content

Commit

Permalink
完善后台机制
Browse files Browse the repository at this point in the history
  • Loading branch information
Smith-Cruise committed Feb 17, 2018
1 parent 6e8c780 commit 6df64d6
Show file tree
Hide file tree
Showing 18 changed files with 539 additions and 156 deletions.
25 changes: 25 additions & 0 deletions eagle-oj-mail/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>eagle-oj</artifactId>
<groupId>com.eagleoj</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>eagle-oj-mail</artifactId>

<dependencies>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
</project>
27 changes: 27 additions & 0 deletions eagle-oj-mail/src/main/java/com/eagleoj/mail/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.eagleoj.mail;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;

/**
* @author Smith
**/
public class Main {
public static void main(String[] args) throws Exception {
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost("smtpdm.aliyun.com");
sender.setPort(80);
sender.setUsername("mail@eagleoj.com");
sender.setPassword("v2l8CRlTPtLvz6ocp2Bv");
sender.testConnection();
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setTo("chendingchao1@126.com");
helper.setText("Thank you for ordering!");
helper.setSubject("xx");
helper.setFrom("mail@eagleoj.com");
sender.send(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class CacheController {

private static Cache<Integer, Object> leaderboardCache;

private static Cache<String, String> settingCache;

static {
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(true);
authCache = cacheManager
Expand All @@ -52,6 +54,16 @@ public class CacheController {
ResourcePoolsBuilder.newResourcePoolsBuilder().heap(20, MemoryUnit.MB))
.withExpiry(Expirations.timeToLiveExpiration(Duration.of(DefaultConfig.LEADERBOARD_REFRESH_TIME, TimeUnit.MINUTES)))
.build());

settingCache = cacheManager
.createCache("settingCache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(
String.class,
String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10, MemoryUnit.MB))
.build()

);
}

public static Cache<String, String> getAuthCache() {
Expand All @@ -63,4 +75,8 @@ public static Cache<String, String> getAuthCache() {
public static Cache<Integer, Object> getLeaderboard() {
return leaderboardCache;
}

public static Cache<String, String> getSettingCache() {
return settingCache;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public class IndexController {
public void getAvatar(@RequestParam("aid") int aid,
HttpServletResponse response) throws IOException {
AttachmentEntity entity = attachmentService.getAvatar(aid);
String OSS_URL = settingService.getSetting(SettingKeyMapper.OSS_URL);
response.sendRedirect(OSS_URL+entity.getUrl());
//String OSS_URL = settingService.getSetting(SettingKeyMapper.OSS_URL);
//response.sendRedirect(OSS_URL+entity.getUrl());
}

@RequestMapping("/401")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package com.eagleoj.web.controller;

import com.eagleoj.judge.LanguageEnum;
import com.eagleoj.web.cache.CacheController;
import com.eagleoj.web.setting.SettingKeyMapper;
import com.eagleoj.web.controller.format.admin.UpdateGlobalSettingFormat;
import com.eagleoj.web.controller.format.admin.UpdateStorageSettingFormat;
import com.eagleoj.web.setting.SettingEnum;
import com.eagleoj.web.setting.SettingService;
import com.eagleoj.web.util.FileUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import com.eagleoj.web.controller.exception.UnauthorizedException;
import com.eagleoj.web.controller.exception.WebErrorException;
import com.eagleoj.web.controller.format.index.AddSettingFormat;
import com.eagleoj.web.controller.format.index.UpdateSettingFormat;
import com.eagleoj.web.entity.ResponseEntity;
import org.ehcache.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand All @@ -33,39 +36,136 @@ public class SettingController {
@Autowired
private SettingService settingService;

@Autowired
private FileUtil fileUtil;

@GetMapping
public ResponseEntity getSetting(@RequestParam(name = "is_detail", required = false, defaultValue = "false") boolean isDetail) {
public ResponseEntity getSetting() {
Map<String, Object> settingMap = new HashMap<>();
// 网站未安装
// is website installed
if (! settingService.isInstalled()) {
settingMap.put(SettingKeyMapper.IS_INSTALLED, false);
settingMap.put(SettingEnum.IS_INSTALLED.getName(), false);
return new ResponseEntity(settingMap);
} else {
settingMap.put(SettingKeyMapper.IS_INSTALLED, true);
settingMap.put(SettingEnum.IS_INSTALLED.getName(), true);
}

// lang map
Map<LanguageEnum, String> langMap = new HashMap<>();
for (LanguageEnum languageEnum: LanguageEnum.values()) {
langMap.put(languageEnum, languageEnum.getName());
}
settingMap.put("lang", langMap);

// get setting
String webTitle = settingService.getSetting(SettingEnum.WEB_TITLE);
settingMap.put(SettingEnum.WEB_TITLE.getName(), webTitle);
if (settingService.isOpenStorage()) {
settingMap.put(SettingEnum.IS_OPEN_STORAGE.getName(), true);
settingMap.put(SettingEnum.OSS_URL.getName(), settingService.getSetting(SettingEnum.OSS_URL));
} else {
settingMap.put(SettingEnum.IS_OPEN_STORAGE.getName(), false);
}
return new ResponseEntity(settingMap);
}

settingMap.put(SettingKeyMapper.WEB_TITLE, settingService.getSetting(SettingKeyMapper.WEB_TITLE));
settingMap.put(SettingKeyMapper.OSS_URL, settingService.getSetting(SettingKeyMapper.OSS_URL));
settingMap.put(SettingKeyMapper.LANG, settingService.getLangMap());
if (isDetail) {
Subject subject = SecurityUtils.getSubject();
if (! (subject.isAuthenticated() && subject.hasRole("9"))) {
throw new UnauthorizedException();
@ApiOperation("获取全局设置")
@RequiresRoles("9")
@GetMapping("/global")
public ResponseEntity getGlobalSetting() {
Map<String, String> map = new HashMap<>();
map.put(SettingEnum.WEB_TITLE.getName(), settingService.getSetting(SettingEnum.WEB_TITLE));
return new ResponseEntity(map);
}

@ApiOperation("更新全局设置")
@RequiresRoles("9")
@PostMapping("/global")
public ResponseEntity updateGlobalSetting(@RequestBody @Valid UpdateGlobalSettingFormat format) {
settingService.updateSetting(SettingEnum.WEB_TITLE, format.getTitle());
return new ResponseEntity("更新设置成功");
}

@ApiOperation("获取存储设置")
@RequiresRoles("9")
@GetMapping("/storage")
public ResponseEntity getStorageSetting() {
Map<String, String> map = new HashMap<>();
if (settingService.isOpenStorage()) {
map.put(SettingEnum.IS_OPEN_STORAGE.getName(), "true");
List<SettingEnum> keys = new ArrayList<>(5);
keys.add(SettingEnum.OSS_ACCESS_KEY);
keys.add(SettingEnum.OSS_SECRET_KEY);
keys.add(SettingEnum.OSS_END_POINT);
keys.add(SettingEnum.OSS_BUCKET);
keys.add(SettingEnum.OSS_URL);
List<String> values = settingService.listSettings(keys);
for (int i=0; i<values.size(); i++) {
map.put(keys.get(i).getName(), values.get(i));
}
} else {
map.put(SettingEnum.IS_OPEN_STORAGE.getName(), "false");
}
return new ResponseEntity(map);
}

@ApiOperation("更新存储设置")
@RequiresRoles("9")
@PostMapping("/storage")
public ResponseEntity updateStorageSetting(@RequestBody @Valid UpdateStorageSettingFormat format) {
if (format.getOpenStorage()) {
if (format.getAccessKey() == null || format.getSecretKey() == null || format.getEndPoint() == null ||
format.getBucket() == null || format.getUrl() == null) {
throw new WebErrorException("参数不得为空");
}

// 进行客户端测试
if (! fileUtil.test(format.getAccessKey(), format.getSecretKey(), format.getEndPoint(), format.getBucket())) {
throw new WebErrorException("你填写的参数无效");
}

settingMap.put(SettingKeyMapper.OSS_ACCESS_KEY, settingService.getSetting(SettingKeyMapper.OSS_ACCESS_KEY));
settingMap.put(SettingKeyMapper.OSS_SECRET_KEY, settingService.getSetting(SettingKeyMapper.OSS_SECRET_KEY));
settingMap.put(SettingKeyMapper.OSS_END_POINT, settingService.getSetting(SettingKeyMapper.OSS_END_POINT));
settingMap.put(SettingKeyMapper.OSS_BUCKET, settingService.getSetting(SettingKeyMapper.OSS_BUCKET));
boolean isAlready = true;
try {
settingService.getSetting(SettingEnum.OSS_BUCKET);
} catch (Exception e) {
isAlready = false;
}
List<SettingEnum> keys = new ArrayList<>(6);
List<String> values = new ArrayList<>(6);
keys.add(SettingEnum.IS_OPEN_STORAGE);
values.add("true");
keys.add(SettingEnum.OSS_ACCESS_KEY);
values.add(format.getAccessKey());
keys.add(SettingEnum.OSS_SECRET_KEY);
values.add(format.getSecretKey());
keys.add(SettingEnum.OSS_END_POINT);
values.add(format.getEndPoint());
keys.add(SettingEnum.OSS_BUCKET);
values.add(format.getBucket());
keys.add(SettingEnum.OSS_URL);
values.add(format.getUrl());
if (isAlready) {
// update
settingService.updateSettings(keys, values);
} else {
// save
settingService.saveSettings(keys, values);
}
fileUtil.refresh();
} else {
try {
settingService.updateSetting(SettingEnum.IS_OPEN_STORAGE, "false");
} catch (Exception e) {
settingService.saveSetting(SettingEnum.IS_OPEN_STORAGE, "false");
}
}
return new ResponseEntity(settingMap);
return new ResponseEntity("更新设置成功");
}

@RequiresRoles("9")
@PutMapping
public ResponseEntity updateSetting(@RequestBody @Valid UpdateSettingFormat format) {
String key = format.getKey();
/*String key = format.getKey();
String value = format.getValue();
switch (key) {
case "title":
Expand All @@ -89,20 +189,15 @@ public ResponseEntity updateSetting(@RequestBody @Valid UpdateSettingFormat form
default:
throw new WebErrorException("不存在此key");
}
settingService.refresh();
settingService.refresh();*/
return new ResponseEntity("网站配置更新成功");
}

@PostMapping
@PostMapping("/install")
public ResponseEntity install(@RequestBody @Valid AddSettingFormat format) {
if (settingService.isInstalled()) {
throw new WebErrorException("系统已经安装过了");
}

settingService.installWeb(format.getEmail(), format.getNickname(), format.getPassword(),
format.getTitle(), format.getAccessKey(), format.getSecretKey(), format.getEndPoint(),
format.getBucket(), format.getUrl());
settingService.refresh();
format.getTitle());
return new ResponseEntity("安装成功");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.eagleoj.web.controller.format.user.UpdateUserFormat;
import com.eagleoj.web.controller.format.user.UpdateUserPasswordFormat;
import com.eagleoj.web.controller.format.user.UpdateUserProfileFormat;
import com.eagleoj.web.setting.SettingService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageRowBounds;
Expand Down Expand Up @@ -54,6 +55,9 @@ public class UserController {
@Autowired
private ContestUserService contestUserService;

@Autowired
private SettingService settingService;

@ApiOperation("获取当前用户的所有信息")
@GetMapping
@RequiresAuthentication
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.eagleoj.web.controller.format.admin;

import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotNull;

/**
* @author Smith
**/
public class UpdateGlobalSettingFormat {

@NotNull
@Length(min = 1, max = 20)
private String title;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}
Loading

0 comments on commit 6df64d6

Please sign in to comment.