Skip to content

Commit

Permalink
🦄 fix otale#11 close otale#11
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Feb 25, 2017
1 parent 394ae30 commit 4d982c6
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/tale/controller/BaseController.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public Users user() {
return TaleUtils.getLoginUser();
}

public Integer getUid(){
return this.user().getUid();
}

public String render_404() {
return "/comm/error_404";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import com.blade.mvc.http.Request;
import com.blade.mvc.view.RestResponse;
import com.tale.controller.BaseController;
import com.tale.dto.LogActions;
import com.tale.dto.Types;
import com.tale.exception.TipException;
import com.tale.model.Contents;
import com.tale.model.Metas;
import com.tale.model.Users;
import com.tale.service.ContentsService;
import com.tale.service.LogService;
import com.tale.service.MetasService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -35,6 +37,9 @@ public class ArticleController extends BaseController {
@Inject
private MetasService metasService;

@Inject
private LogService logService;

@Route(value = "", method = HttpMethod.GET)
public String index(@QueryParam(value = "page", defaultValue = "1") int page,
@QueryParam(value = "limit", defaultValue = "15") int limit, Request request) {
Expand Down Expand Up @@ -149,9 +154,10 @@ public RestResponse modifyArticle(@QueryParam Integer cid, @QueryParam String ti

@Route(value = "delete")
@JSON
public RestResponse delete(@QueryParam int cid) {
public RestResponse delete(@QueryParam int cid, Request request) {
try {
contentsService.delete(cid);
logService.save(LogActions.DEL_ARTICLE, cid+"", request.address(), this.getUid());
} catch (Exception e) {
String msg = "文章删除失败";
if (e instanceof TipException) {
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/tale/controller/admin/AttachController.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
import com.blade.mvc.multipart.FileItem;
import com.blade.mvc.view.RestResponse;
import com.tale.controller.BaseController;
import com.tale.dto.LogActions;
import com.tale.dto.Types;
import com.tale.exception.TipException;
import com.tale.model.Attach;
import com.tale.model.Users;
import com.tale.service.AttachService;
import com.tale.service.LogService;
import com.tale.utils.TaleUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -44,6 +46,9 @@ public class AttachController extends BaseController {
@Inject
private AttachService attachService;

@Inject
private LogService logService;

@Route(value = "", method = HttpMethod.GET)
public String index(Request request, @QueryParam(value = "page", defaultValue = "1") int page,
@QueryParam(value = "limit", defaultValue = "12") int limit) {
Expand Down Expand Up @@ -95,7 +100,7 @@ public RestResponse upload(Request request) {

@Route(value = "delete")
@JSON
public RestResponse delete(@QueryParam int id) {
public RestResponse delete(@QueryParam Integer id, Request request) {
try {
Attach attach = attachService.byId(id);
if(null == attach){
Expand All @@ -104,7 +109,7 @@ public RestResponse delete(@QueryParam int id) {
attachService.delete(id);
String upDir = CLASSPATH.substring(0, CLASSPATH.length() - 1);
FileKit.delete(upDir + attach.getFkey());
LOGGER.info("DELETE ATTACH {}", attach.getFkey());
logService.save(LogActions.DEL_ARTICLE, attach.getFkey(), request.address(), this.getUid());
} catch (Exception e) {
String msg = "附件删除失败";
if (e instanceof TipException) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/tale/controller/admin/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
import com.blade.mvc.annotation.QueryParam;
import com.blade.mvc.annotation.Route;
import com.blade.mvc.http.HttpMethod;
import com.blade.mvc.http.Request;
import com.blade.mvc.http.Response;
import com.blade.mvc.http.wrapper.Session;
import com.blade.mvc.view.RestResponse;
import com.tale.dto.LogActions;
import com.tale.exception.TipException;
import com.tale.init.TaleConst;
import com.tale.model.Users;
import com.tale.service.LogService;
import com.tale.service.UsersService;
import com.tale.utils.TaleUtils;
import org.slf4j.Logger;
Expand All @@ -29,6 +32,9 @@ public class AuthController {
@Inject
private UsersService usersService;

@Inject
private LogService logService;

@Route(value = "login", method = HttpMethod.GET)
public String login() {
return "admin/login";
Expand All @@ -39,13 +45,15 @@ public String login() {
public RestResponse doLogin(@QueryParam String username,
@QueryParam String password,
@QueryParam String remeber_me,
Request request,
Session session, Response response) {
try {
Users user = usersService.login(username, password);
session.attribute(TaleConst.LOGIN_SESSION_KEY, user);
if (StringKit.isNotBlank(remeber_me)) {
TaleUtils.setCookie(response, user.getUid());
}
logService.save(LogActions.LOGIN, null, request.address(), user.getUid());
} catch (Exception e) {
String msg = "登录失败";
if (e instanceof TipException) {
Expand Down
44 changes: 35 additions & 9 deletions src/main/java/com/tale/controller/admin/IndexController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.blade.kit.StringKit;
import com.blade.kit.Tools;
import com.blade.kit.base.Config;
import com.blade.kit.json.JSONKit;
import com.blade.mvc.annotation.Controller;
import com.blade.mvc.annotation.JSON;
import com.blade.mvc.annotation.QueryParam;
Expand All @@ -14,12 +15,15 @@
import com.blade.mvc.view.RestResponse;
import com.tale.controller.BaseController;
import com.tale.dto.BackResponse;
import com.tale.dto.LogActions;
import com.tale.dto.Statistics;
import com.tale.exception.TipException;
import com.tale.init.TaleConst;
import com.tale.model.Comments;
import com.tale.model.Contents;
import com.tale.model.Logs;
import com.tale.model.Users;
import com.tale.service.LogService;
import com.tale.service.OptionsService;
import com.tale.service.SiteService;
import com.tale.service.UsersService;
Expand Down Expand Up @@ -47,6 +51,9 @@ public class IndexController extends BaseController {
@Inject
private UsersService usersService;

@Inject
private LogService logService;

/**
* 仪表盘
*/
Expand All @@ -55,9 +62,13 @@ public String index(Request request) {
List<Comments> comments = siteService.recentComments(5);
List<Contents> contents = siteService.recentContents(5);
Statistics statistics = siteService.getStatistics();
// 取最新的20条日志
List<Logs> logs = logService.getLogs(1, 20);

request.attribute("comments", comments);
request.attribute("articles", contents);
request.attribute("statistics", statistics);
request.attribute("logs", logs);
return "admin/index";
}

Expand Down Expand Up @@ -88,6 +99,7 @@ public RestResponse saveSetting(@QueryParam String site_theme, Request request)
if (StringKit.isNotBlank(site_theme)) {
BaseController.THEME = "themes/" + site_theme;
}
logService.save(LogActions.SYS_SETTING, JSONKit.toJSONString(querys), request.address(), this.getUid());
return RestResponse.ok();
} catch (Exception e) {
String msg = "保存设置失败";
Expand All @@ -113,14 +125,15 @@ public String profile() {
*/
@Route(value = "profile", method = HttpMethod.POST)
@JSON
public RestResponse saveProfile(@QueryParam String screen_name, @QueryParam String email) {
public RestResponse saveProfile(@QueryParam String screen_name, @QueryParam String email, Request request) {
Users users = this.user();
if (StringKit.isNotBlank(screen_name) && StringKit.isNotBlank(email)) {
Users temp = new Users();
temp.setUid(users.getUid());
temp.setScreen_name(screen_name);
temp.setEmail(email);
usersService.update(temp);
logService.save(LogActions.UP_INFO, JSONKit.toJSONString(temp), request.address(), this.getUid());
}
return RestResponse.ok();
}
Expand All @@ -130,7 +143,7 @@ public RestResponse saveProfile(@QueryParam String screen_name, @QueryParam Stri
*/
@Route(value = "password", method = HttpMethod.POST)
@JSON
public RestResponse upPwd(@QueryParam String old_password, @QueryParam String password) {
public RestResponse upPwd(@QueryParam String old_password, @QueryParam String password, Request request) {
Users users = this.user();
if (StringKit.isBlank(old_password) || StringKit.isBlank(password)) {
return RestResponse.fail("请确认信息输入完整");
Expand All @@ -143,12 +156,23 @@ public RestResponse upPwd(@QueryParam String old_password, @QueryParam String pa
return RestResponse.fail("请输入6-14位密码");
}

Users temp = new Users();
temp.setUid(users.getUid());
String pwd = Tools.md5(users.getUsername() + password);
temp.setPassword(pwd);
usersService.update(temp);
return RestResponse.ok();
try {
Users temp = new Users();
temp.setUid(users.getUid());
String pwd = Tools.md5(users.getUsername() + password);
temp.setPassword(pwd);
usersService.update(temp);
logService.save(LogActions.UP_PWD, null, request.address(), this.getUid());
return RestResponse.ok();
} catch (Exception e){
String msg = "密码修改失败";
if (e instanceof TipException) {
msg = e.getMessage();
} else {
LOGGER.error(msg, e);
}
return RestResponse.fail(msg);
}
}

/**
Expand All @@ -157,12 +181,14 @@ public RestResponse upPwd(@QueryParam String old_password, @QueryParam String pa
*/
@Route(value = "backup", method = HttpMethod.POST)
@JSON
public RestResponse backup(@QueryParam String bk_type, @QueryParam String bk_path, Response response) {
public RestResponse backup(@QueryParam String bk_type, @QueryParam String bk_path,
Request request) {
if (StringKit.isBlank(bk_type)) {
return RestResponse.fail("请确认信息输入完整");
}
try {
BackResponse backResponse = siteService.backup(bk_type, bk_path, "yyyyMMddHHmm");
logService.save(LogActions.SYS_BACKUP, null, request.address(), this.getUid());
return RestResponse.ok(backResponse);
} catch (Exception e){
String msg = "备份失败";
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/tale/controller/admin/PageController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import com.blade.mvc.http.Request;
import com.blade.mvc.view.RestResponse;
import com.tale.controller.BaseController;
import com.tale.dto.LogActions;
import com.tale.dto.Types;
import com.tale.exception.TipException;
import com.tale.init.TaleConst;
import com.tale.model.Contents;
import com.tale.model.Users;
import com.tale.service.ContentsService;
import com.tale.service.LogService;
import com.tale.service.MetasService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -32,6 +34,9 @@ public class PageController extends BaseController {
@Inject
private MetasService metasService;

@Inject
private LogService logService;

@Route(value = "", method = HttpMethod.GET)
public String index(Request request) {
Paginator<Contents> contentsPaginator = contentsService.getArticles(new Take(Contents.class).eq("type", Types.PAGE).page(1, TaleConst.MAX_POSTS, "created desc"));
Expand Down Expand Up @@ -124,9 +129,10 @@ public RestResponse modifyArticle(@QueryParam Integer cid, @QueryParam String ti

@Route(value = "delete")
@JSON
public RestResponse delete(@QueryParam int cid) {
public RestResponse delete(@QueryParam int cid, Request request) {
try {
contentsService.delete(cid);
logService.save(LogActions.DEL_PAGE, cid+"", request.address(), this.getUid());
} catch (Exception e) {
String msg = "页面删除失败";
if (e instanceof TipException) {
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/tale/dto/LogActions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.tale.dto;

/**
* 日志操作常量
*
* Created by biezhi on 2017/2/26.
*/
public interface LogActions {

String LOGIN = "登录后台";

String UP_PWD = "修改密码";

String UP_INFO = "修改个人信息";

String DEL_ARTICLE = "删除文章";

String DEL_PAGE = "删除页面";

String SYS_BACKUP = "系统备份";

String SYS_SETTING = "保存系统设置";

String INIT_SITE = "初始化站点";

}
Loading

0 comments on commit 4d982c6

Please sign in to comment.