mirror of https://github.com/halo-dev/halo
Initialize a test user if absent when startup
parent
dd2caca58d
commit
6139392909
|
@ -1,11 +1,14 @@
|
||||||
package cc.ryanc.halo.listener;
|
package cc.ryanc.halo.listener;
|
||||||
|
|
||||||
import cc.ryanc.halo.config.properties.HaloProperties;
|
import cc.ryanc.halo.config.properties.HaloProperties;
|
||||||
|
import cc.ryanc.halo.model.entity.User;
|
||||||
import cc.ryanc.halo.model.enums.BlogProperties;
|
import cc.ryanc.halo.model.enums.BlogProperties;
|
||||||
|
import cc.ryanc.halo.model.params.UserParam;
|
||||||
import cc.ryanc.halo.model.support.HaloConst;
|
import cc.ryanc.halo.model.support.HaloConst;
|
||||||
import cc.ryanc.halo.model.support.Theme;
|
import cc.ryanc.halo.model.support.Theme;
|
||||||
import cc.ryanc.halo.service.OptionService;
|
import cc.ryanc.halo.service.OptionService;
|
||||||
import cc.ryanc.halo.service.ThemeService;
|
import cc.ryanc.halo.service.ThemeService;
|
||||||
|
import cc.ryanc.halo.service.UserService;
|
||||||
import cc.ryanc.halo.utils.HaloUtils;
|
import cc.ryanc.halo.utils.HaloUtils;
|
||||||
import cc.ryanc.halo.web.controller.content.base.BaseContentController;
|
import cc.ryanc.halo.web.controller.content.base.BaseContentController;
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
@ -58,6 +61,9 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
|
||||||
@Autowired
|
@Autowired
|
||||||
private ThemeService themeService;
|
private ThemeService themeService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onApplicationEvent(ApplicationStartedEvent event) {
|
public void onApplicationEvent(ApplicationStartedEvent event) {
|
||||||
// save halo version to database
|
// save halo version to database
|
||||||
|
@ -66,6 +72,32 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
|
||||||
this.getActiveTheme();
|
this.getActiveTheme();
|
||||||
this.printStartInfo();
|
this.printStartInfo();
|
||||||
this.initThemes();
|
this.initThemes();
|
||||||
|
|
||||||
|
// Init user in development environment
|
||||||
|
if (!haloProperties.getProductionEnv()) {
|
||||||
|
initAnTestUserIfAbsent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize an test user if absent
|
||||||
|
*/
|
||||||
|
private void initAnTestUserIfAbsent() {
|
||||||
|
// Create an user if absent
|
||||||
|
List<User> users = userService.listAll();
|
||||||
|
|
||||||
|
if (users.isEmpty()) {
|
||||||
|
UserParam userParam = new UserParam();
|
||||||
|
userParam.setUsername("test");
|
||||||
|
userParam.setNickname("developer");
|
||||||
|
userParam.setEmail("test@test.com");
|
||||||
|
|
||||||
|
log.debug("Initializing a test user: [{}]", userParam);
|
||||||
|
|
||||||
|
User testUser = userService.createBy(userParam, "opentest");
|
||||||
|
|
||||||
|
log.debug("Initialized a test user: [{}]", testUser);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package cc.ryanc.halo.model.entity;
|
package cc.ryanc.halo.model.entity;
|
||||||
|
|
||||||
|
import cc.ryanc.halo.utils.DateUtils;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
@ -20,7 +21,7 @@ import java.util.Date;
|
||||||
@Table(name = "users")
|
@Table(name = "users")
|
||||||
@SQLDelete(sql = "update users set deleted = true where id = ?")
|
@SQLDelete(sql = "update users set deleted = true where id = ?")
|
||||||
@Where(clause = "deleted = false")
|
@Where(clause = "deleted = false")
|
||||||
@ToString
|
@ToString(callSuper = true)
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
public class User extends BaseEntity {
|
public class User extends BaseEntity {
|
||||||
|
|
||||||
|
@ -76,6 +77,23 @@ public class User extends BaseEntity {
|
||||||
@Override
|
@Override
|
||||||
public void prePersist() {
|
public void prePersist() {
|
||||||
super.prePersist();
|
super.prePersist();
|
||||||
|
|
||||||
id = null;
|
id = null;
|
||||||
|
|
||||||
|
if (email == null) {
|
||||||
|
email = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (avatar == null) {
|
||||||
|
avatar = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (description == null) {
|
||||||
|
description = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (expireTime == null) {
|
||||||
|
expireTime = DateUtils.now();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,3 +53,4 @@ logging:
|
||||||
|
|
||||||
halo:
|
halo:
|
||||||
doc-disabled: false
|
doc-disabled: false
|
||||||
|
production-env: false
|
||||||
|
|
Loading…
Reference in New Issue