Complete log creation

pull/146/head
johnniang 2019-04-22 11:23:01 +08:00
parent 8e64b9acd6
commit 2ae70e1760
4 changed files with 67 additions and 24 deletions

View File

@ -7,13 +7,15 @@ package run.halo.app.model.enums;
*/
public enum LogType implements ValueEnum<Integer> {
POST_PUBLISHED(0),
POST_EDITED(1),
POST_DELETED(5),
LOGGED_IN(2),
LOGGED_OUT(3),
LOGIN_FAILED(4),
;
BLOG_INITIALIZED(0),
POST_PUBLISHED(5),
POST_EDITED(15),
POST_DELETED(20),
LOGGED_IN(25),
LOGGED_OUT(30),
LOGIN_FAILED(35),
PASSWORD_UPDATED(40),
PROFILE_UPDATED(45);
private final Integer value;

View File

@ -413,7 +413,12 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
log.debug("Removed post categories: [{}]", postCategories);
return super.removeById(postId);
Post deletedPost = super.removeById(postId);
// Log it
eventPublisher.publishEvent(new LogEvent(this, postId.toString(), LogType.POST_DELETED, deletedPost.getTitle()));
return deletedPost;
}
@Override

View File

@ -1,9 +1,18 @@
package run.halo.app.service.impl;
import cn.hutool.core.lang.Validator;
import cn.hutool.crypto.digest.BCrypt;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import run.halo.app.cache.StringCacheStore;
import run.halo.app.event.LogEvent;
import run.halo.app.exception.BadRequestException;
import run.halo.app.exception.NotFoundException;
import run.halo.app.model.entity.User;
import run.halo.app.model.enums.LogType;
import run.halo.app.model.params.UserParam;
import run.halo.app.repository.UserRepository;
import run.halo.app.security.context.SecurityContextHolder;
@ -13,18 +22,6 @@ import run.halo.app.service.UserService;
import run.halo.app.service.base.AbstractCrudService;
import run.halo.app.utils.DateUtils;
import run.halo.app.utils.HaloUtils;
import cn.hutool.core.lang.Validator;
import cn.hutool.crypto.digest.BCrypt;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import run.halo.app.exception.BadRequestException;
import run.halo.app.exception.NotFoundException;
import run.halo.app.repository.UserRepository;
import run.halo.app.security.context.SecurityContextHolder;
import run.halo.app.security.support.UserDetail;
import run.halo.app.service.base.AbstractCrudService;
import javax.servlet.http.HttpSession;
import java.util.Date;
@ -45,11 +42,15 @@ public class UserServiceImpl extends AbstractCrudService<User, Integer> implemen
private final StringCacheStore stringCacheStore;
private final ApplicationEventPublisher eventPublisher;
public UserServiceImpl(UserRepository userRepository,
StringCacheStore stringCacheStore) {
StringCacheStore stringCacheStore,
ApplicationEventPublisher eventPublisher) {
super(userRepository);
this.userRepository = userRepository;
this.stringCacheStore = stringCacheStore;
this.eventPublisher = eventPublisher;
}
@Override
@ -142,6 +143,9 @@ public class UserServiceImpl extends AbstractCrudService<User, Integer> implemen
String errorMessage = String.format("Username or password incorrect, you%shave %s", remainder <= 0 ? "" : " still ", HaloUtils.pluralize(remainder, "chance", "chances"));
// Lot it
eventPublisher.publishEvent(new LogEvent(this, key, LogType.LOGIN_FAILED, password));
throw new BadRequestException(errorMessage);
}
@ -151,6 +155,9 @@ public class UserServiceImpl extends AbstractCrudService<User, Integer> implemen
// Set session
httpSession.setAttribute(AdminAuthenticationFilter.ADMIN_SESSION_KEY, new UserDetail(user));
// Log it
eventPublisher.publishEvent(new LogEvent(this, user.getId().toString(), LogType.LOGGED_IN, user.getUsername()));
return user;
}
@ -176,7 +183,12 @@ public class UserServiceImpl extends AbstractCrudService<User, Integer> implemen
setPassword(newPassword, user);
// Update this user
return update(user);
User updatedUser = update(user);
// Log it
eventPublisher.publishEvent(new LogEvent(this, updatedUser.getId().toString(), LogType.PASSWORD_UPDATED, oldPassword));
return updatedUser;
}
@Override
@ -191,6 +203,16 @@ public class UserServiceImpl extends AbstractCrudService<User, Integer> implemen
return create(user);
}
@Override
public User update(User user) {
User updatedUser = super.update(user);
// Log it
eventPublisher.publishEvent(new LogEvent(this, user.getId().toString(), LogType.PROFILE_UPDATED, user.getUsername()));
return updatedUser;
}
private void setPassword(@NonNull String plainPassword, @NonNull User user) {
Assert.hasText(plainPassword, "Plain password must not be blank");
Assert.notNull(user, "User must not be null");

View File

@ -3,15 +3,18 @@ package run.halo.app.web.controller.core;
import cn.hutool.core.util.StrUtil;
import freemarker.template.Configuration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import run.halo.app.event.LogEvent;
import run.halo.app.exception.BadRequestException;
import run.halo.app.model.entity.*;
import run.halo.app.model.enums.AttachmentType;
import run.halo.app.model.enums.LogType;
import run.halo.app.model.params.InstallParam;
import run.halo.app.model.properties.*;
import run.halo.app.model.support.BaseResponse;
@ -48,13 +51,16 @@ public class InstallController {
private final Configuration configuration;
private final ApplicationEventPublisher eventPublisher;
public InstallController(UserService userService,
CategoryService categoryService,
PostService postService,
CommentService commentService,
OptionService optionService,
MenuService menuService,
Configuration configuration) {
Configuration configuration,
ApplicationEventPublisher eventPublisher) {
this.userService = userService;
this.categoryService = categoryService;
this.postService = postService;
@ -62,6 +68,7 @@ public class InstallController {
this.optionService = optionService;
this.menuService = menuService;
this.configuration = configuration;
this.eventPublisher = eventPublisher;
}
/**
@ -92,6 +99,7 @@ public class InstallController {
boolean isInstalled = Boolean.parseBoolean(optionService.getByProperty(PrimaryProperties.IS_INSTALLED).orElse(Boolean.FALSE.toString()));
if (isInstalled) {
// TODO i18n
throw new BadRequestException("该博客已初始化,不能再次安装!");
}
@ -115,6 +123,12 @@ public class InstallController {
// TODO Handle option cache
// TODO i18n
eventPublisher.publishEvent(
new LogEvent(this, user.getId().toString(), LogType.BLOG_INITIALIZED, "博客已成功初始化")
);
// TODO i18n
return BaseResponse.ok("安装完成!");
}
@ -127,6 +141,7 @@ public class InstallController {
menuService.create(menuIndex);
Menu menuArchive = new Menu();
// TODO i18n
menuArchive.setName("归档");
menuArchive.setUrl("/archives");
menuArchive.setSort(2);
@ -148,7 +163,6 @@ public class InstallController {
Category category = new Category();
// TODO Multi level category
category.setName("未分类");
category.setSlugName("default");
category.setDescription("未分类");