From 7f7710ed678705539b54375e6f7a7eb89e03486f Mon Sep 17 00:00:00 2001 From: ruibaby Date: Tue, 25 Jun 2019 15:37:28 +0800 Subject: [PATCH] Log login and logout action. --- .../halo/app/model/support/LogsRecord.java | 36 ------------------- .../halo/app/model/support/QiNiuPutSet.java | 1 + .../app/service/impl/AdminServiceImpl.java | 16 +++++++++ .../templates/common/macro/global_macro.ftl | 23 ++++++++---- 4 files changed, 34 insertions(+), 42 deletions(-) delete mode 100644 src/main/java/run/halo/app/model/support/LogsRecord.java diff --git a/src/main/java/run/halo/app/model/support/LogsRecord.java b/src/main/java/run/halo/app/model/support/LogsRecord.java deleted file mode 100644 index 2e17ae572..000000000 --- a/src/main/java/run/halo/app/model/support/LogsRecord.java +++ /dev/null @@ -1,36 +0,0 @@ -package run.halo.app.model.support; - -/** - *
- *     日志常量
- * 
- * - * @author ryanwang - * @date : 2018/1/19 - */ -public interface LogsRecord { - - String INSTALL = "初始化博客"; - - String LOGIN = "登录后台"; - - String LOGIN_SUCCESS = "登录成功"; - - String LOGIN_ERROR = "登录失败"; - - String LOGOUT = "退出登录"; - - String PUSH_POST = "发表文章"; - - String PUSH_PAGE = "发表页面"; - - String REMOVE_POST = "删除文章"; - - String CHANGE_THEME = "更换主题"; - - String UPLOAD_THEME = "上传主题"; - - String UPLOAD_FILE = "上传附件"; - - String REMOVE_FILE = "移除附件"; -} diff --git a/src/main/java/run/halo/app/model/support/QiNiuPutSet.java b/src/main/java/run/halo/app/model/support/QiNiuPutSet.java index 5ccc07316..cfa015bf2 100644 --- a/src/main/java/run/halo/app/model/support/QiNiuPutSet.java +++ b/src/main/java/run/halo/app/model/support/QiNiuPutSet.java @@ -17,6 +17,7 @@ public class QiNiuPutSet { * 文件hash值 */ public String hash; + /** * 文件名 */ diff --git a/src/main/java/run/halo/app/service/impl/AdminServiceImpl.java b/src/main/java/run/halo/app/service/impl/AdminServiceImpl.java index 4820ce737..ad4610296 100644 --- a/src/main/java/run/halo/app/service/impl/AdminServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/AdminServiceImpl.java @@ -3,6 +3,7 @@ package run.halo.app.service.impl; import cn.hutool.core.lang.Validator; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.http.ResponseEntity; import org.springframework.lang.NonNull; import org.springframework.stereotype.Service; @@ -10,6 +11,7 @@ import org.springframework.util.Assert; import org.springframework.web.client.RestTemplate; import run.halo.app.cache.StringCacheStore; import run.halo.app.config.properties.HaloProperties; +import run.halo.app.event.logger.LogEvent; import run.halo.app.exception.BadRequestException; import run.halo.app.exception.NotFoundException; import run.halo.app.exception.ServiceException; @@ -17,6 +19,7 @@ import run.halo.app.model.dto.EnvironmentDTO; import run.halo.app.model.dto.StatisticDTO; import run.halo.app.model.entity.User; import run.halo.app.model.enums.CommentStatus; +import run.halo.app.model.enums.LogType; import run.halo.app.model.enums.Mode; import run.halo.app.model.enums.PostStatus; import run.halo.app.model.params.LoginParam; @@ -76,6 +79,8 @@ public class AdminServiceImpl implements AdminService { private final HaloProperties haloProperties; + private final ApplicationEventPublisher eventPublisher; + private final String driverClassName; private final String mode; @@ -92,6 +97,7 @@ public class AdminServiceImpl implements AdminService { StringCacheStore cacheStore, RestTemplate restTemplate, HaloProperties haloProperties, + ApplicationEventPublisher eventPublisher, @Value("${spring.datasource.driver-class-name}") String driverClassName, @Value("${spring.profiles.active:prod}") String mode) { this.postService = postService; @@ -106,6 +112,7 @@ public class AdminServiceImpl implements AdminService { this.cacheStore = cacheStore; this.restTemplate = restTemplate; this.haloProperties = haloProperties; + this.eventPublisher = eventPublisher; this.driverClassName = driverClassName; this.mode = mode; } @@ -126,6 +133,8 @@ public class AdminServiceImpl implements AdminService { userService.getByEmailOfNonNull(username) : userService.getByUsernameOfNonNull(username); } catch (NotFoundException e) { log.error("Failed to find user by name: " + username, e); + eventPublisher.publishEvent(new LogEvent(this, "", LogType.LOGIN_FAILED, loginParam.getUsername())); + throw new BadRequestException(mismatchTip); } @@ -133,6 +142,8 @@ public class AdminServiceImpl implements AdminService { if (!userService.passwordMatch(user, loginParam.getPassword())) { // If the password is mismatch + eventPublisher.publishEvent(new LogEvent(this, "", LogType.LOGIN_FAILED, loginParam.getUsername())); + throw new BadRequestException(mismatchTip); } @@ -141,6 +152,9 @@ public class AdminServiceImpl implements AdminService { throw new BadRequestException("您已登录,请不要重复登录"); } + // Log it then login successful + eventPublisher.publishEvent(new LogEvent(this, user.getUsername(), LogType.LOGGED_IN, user.getNickname())); + // Generate new token return buildAuthToken(user); } @@ -170,6 +184,8 @@ public class AdminServiceImpl implements AdminService { cacheStore.delete(SecurityUtils.buildRefreshTokenKey(user)); }); + eventPublisher.publishEvent(new LogEvent(this, user.getUsername(), LogType.LOGGED_OUT, user.getNickname())); + log.info("You have been logged out, looking forward to your next visit!"); } diff --git a/src/main/resources/templates/common/macro/global_macro.ftl b/src/main/resources/templates/common/macro/global_macro.ftl index ba8c13b9f..3fd40ad2d 100644 --- a/src/main/resources/templates/common/macro/global_macro.ftl +++ b/src/main/resources/templates/common/macro/global_macro.ftl @@ -24,16 +24,16 @@ <#-- 站点验证代码 --> <#macro verification> <#if options.seo_verification_google??> - + <#if options.seo_verification_bing??> - + <#if options.seo_verification_baidu??> - + <#if options.seo_verification_qihu??> - + @@ -51,17 +51,28 @@ +<#-- global head --> <#macro head> <#if options.spider_disabled!false> - + - + <@custom_head /> <@verification /> <@favicon /> +<#-- global footer --> <#macro footer> <@footer_info /> <@statistics /> + + +<#-- post comment module --> +<#macro comment post,type> + <#if !post.disallowComment!false> + + + + \ No newline at end of file