/
ConfigurationStatusChecker.java
94 lines (85 loc) · 4.63 KB
/
ConfigurationStatusChecker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* This file is part of alf.io.
*
* alf.io is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* alf.io is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with alf.io. If not, see <http://www.gnu.org/licenses/>.
*/
package alfio.config;
import alfio.manager.system.ConfigurationManager;
import alfio.manager.system.DataMigrator;
import alfio.manager.user.UserManager;
import alfio.model.system.ConfigurationKeys;
import alfio.model.user.Role;
import alfio.model.user.User;
import alfio.repository.user.AuthorityRepository;
import alfio.repository.user.UserRepository;
import alfio.util.PasswordGenerator;
import lombok.extern.log4j.Log4j2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import static alfio.model.system.ConfigurationKeys.*;
import static java.util.Optional.ofNullable;
@Component
public class ConfigurationStatusChecker implements ApplicationListener<ContextRefreshedEvent> {
private static final Logger log = LoggerFactory.getLogger(ConfigurationStatusChecker.class);
private final ConfigurationManager configurationManager;
private final UserRepository userRepository;
private final AuthorityRepository authorityRepository;
private final PasswordEncoder passwordEncoder;
private final String version;
private final DataMigrator dataMigrator;
public ConfigurationStatusChecker(ConfigurationManager configurationManager,
UserRepository userRepository,
AuthorityRepository authorityRepository,
PasswordEncoder passwordEncoder,
@Value("${alfio.version}") String version,
DataMigrator dataMigrator) {
this.configurationManager = configurationManager;
this.authorityRepository = authorityRepository;
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
this.version = version;
this.dataMigrator = dataMigrator;
}
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
boolean initCompleted = configurationManager.getForSystem(ConfigurationKeys.INIT_COMPLETED).getValueAsBooleanOrDefault();
if (!initCompleted) {
String adminPassword = PasswordGenerator.generateRandomPassword();
userRepository.create(UserManager.ADMIN_USERNAME, passwordEncoder.encode(adminPassword), "The", "Administrator", "admin@localhost", true, User.Type.INTERNAL, null, null);
authorityRepository.create(UserManager.ADMIN_USERNAME, Role.ADMIN.getRoleName());
log.info("*******************************************************");
log.info(" This is the first time you're running alf.io");
log.info(" here the generated admin credentials:");
log.info(" {} ", adminPassword);
log.info("*******************************************************");
configurationManager.saveSystemConfiguration(INIT_COMPLETED, "true");
ofNullable(System.getProperty("maps.clientApiKey")).ifPresent(clientApiKey -> configurationManager.saveSystemConfiguration(MAPS_CLIENT_API_KEY, clientApiKey));
ofNullable(System.getProperty("recaptcha.apiKey")).ifPresent(clientApiKey -> configurationManager.saveSystemConfiguration(RECAPTCHA_API_KEY, clientApiKey));
ofNullable(System.getProperty("recaptcha.secret")).ifPresent(clientApiKey -> configurationManager.saveSystemConfiguration(RECAPTCHA_SECRET, clientApiKey));
}
log.info("performing migration from previous version, if any");
try {
dataMigrator.migrateEventsToCurrentVersion();
log.info("done.");
log.info("initialized alf.io version {} ", version);
} catch (Exception e) {
log.error("unable to perform data migration. Please report this issue.", e);
}
}
}