Skip to content

Commit

Permalink
feat: support seeding default superuser via application.properties
Browse files Browse the repository at this point in the history
  • Loading branch information
maranmaran committed May 3, 2024
1 parent ff923bf commit d9c0308
Showing 1 changed file with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class PulsarApplicationListener implements ApplicationListener<ContextRef

private final PulsarAdminService pulsarAdminService;

private final UsersRepository usersRepository;

@Value("${default.environment.name}")
private String defaultEnvironmentName;

Expand All @@ -47,20 +49,66 @@ public class PulsarApplicationListener implements ApplicationListener<ContextRef
@Value("${default.environment.bookie_url}")
private String defaultEnvironmentBookieUrl;

@Value("${default.superuser.name}")
private String defaultSuperuserName;

@Value("${default.superuser.password}")
private String defaultSuperuserPassword;

@Value("${default.superuser.email}")
private String defaultSuperuserEmail;

@Autowired
public PulsarApplicationListener(EnvironmentsRepository environmentsRepository, PulsarAdminService pulsarAdminService) {
public PulsarApplicationListener(
EnvironmentsRepository environmentsRepository,
PulsarAdminService pulsarAdminService,
UsersRepository usersRepository
) {
this.environmentsRepository = environmentsRepository;
this.pulsarAdminService = pulsarAdminService;
this.usersRepository = usersRepository;
}

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
log.info("Start onApplicationEvent");
Page<EnvironmentEntity> environmentEntities = environmentsRepository
.getEnvironmentsList(1, 1);

seedDefaultSuperuser();
seedDefaultEnvironment();
}

private void seedDefaultSuperuser() {
UserInfoEntity userInfoEntity = new UserInfoEntity();

Map<String, String> 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<UserInfoEntity> 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<EnvironmentEntity> environmentEntities = environmentsRepository.getEnvironmentsList(1, 1);

if (environmentEntities.getResult().size() <= 0) {
Optional<EnvironmentEntity> environmentEntityOptional = environmentsRepository
.findByName(defaultEnvironmentName);
Optional<EnvironmentEntity> environmentEntityOptional = environmentsRepository.findByName(defaultEnvironmentName);

if (defaultEnvironmentName != null
&& defaultEnvironmentServiceUrl != null
&& defaultEnvironmentName.length() > 0
Expand Down Expand Up @@ -89,6 +137,7 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
log.warn("The default environment already exists.");
}
}

log.debug("Environments already exist.");
}
}

0 comments on commit d9c0308

Please sign in to comment.