diff --git a/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java b/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java index 9f3941af..ed2fb873 100644 --- a/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java +++ b/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java @@ -38,6 +38,8 @@ public class PulsarApplicationListener implements ApplicationListener environmentEntities = environmentsRepository - .getEnvironmentsList(1, 1); + + seedDefaultSuperuser(); + seedDefaultEnvironment(); + } + + private void seedDefaultSuperuser() { + UserInfoEntity userInfoEntity = new UserInfoEntity(); + + Map userValidateResult = usersService.validateUserInfo(userInfoEntity); + if (userValidateResult.get("error") != null) { + log.error("Superuser seed failed.", userValidateResult.get("error")); + System.exit(-1); + } + if (StringUtils.isBlank(userInfoEntity.getPassword())) { + log.error("Superuser seed failed. Password is required."); + System.exit(-1); + } + + Optional optionalUserEntity = usersRepository.findByUserName(userInfoEntity.getName()); + if (optionalUserEntity.isPresent()) { + log.error("Superuser already exists."); + return; + } + + userInfoEntity.setPassword(DigestUtils.sha256Hex(userInfoEntity.getPassword())); + usersRepository.save(userInfoEntity); + + log.info("Successfully added a default superuser: name = {}, email = {}, password = {}.", + defaultSuperuserName, defaultSuperuserEmail, defaultSuperuserPassword); + } + + private void seedDefaultEnvironment() { + Page environmentEntities = environmentsRepository.getEnvironmentsList(1, 1); + if (environmentEntities.getResult().size() <= 0) { - Optional environmentEntityOptional = environmentsRepository - .findByName(defaultEnvironmentName); + Optional environmentEntityOptional = environmentsRepository.findByName(defaultEnvironmentName); + if (defaultEnvironmentName != null && defaultEnvironmentServiceUrl != null && defaultEnvironmentName.length() > 0 @@ -89,6 +137,7 @@ public void onApplicationEvent(ContextRefreshedEvent event) { log.warn("The default environment already exists."); } } + log.debug("Environments already exist."); } }