13
13
*/
14
14
package org .apache .pulsar .manager ;
15
15
16
- import com .github .pagehelper .Page ;
17
- import lombok .extern .slf4j .Slf4j ;
16
+ import java .util .Map ;
17
+ import java .util .Optional ;
18
+
19
+ import org .apache .commons .codec .digest .DigestUtils ;
20
+ import org .apache .commons .lang .StringUtils ;
18
21
import org .apache .pulsar .client .admin .PulsarAdminException ;
19
22
import org .apache .pulsar .manager .entity .EnvironmentEntity ;
20
23
import org .apache .pulsar .manager .entity .EnvironmentsRepository ;
24
+ import org .apache .pulsar .manager .entity .UserInfoEntity ;
25
+ import org .apache .pulsar .manager .entity .UsersRepository ;
21
26
import org .apache .pulsar .manager .service .PulsarAdminService ;
27
+ import org .apache .pulsar .manager .service .UsersService ;
22
28
import org .springframework .beans .factory .annotation .Autowired ;
23
29
import org .springframework .beans .factory .annotation .Value ;
24
30
import org .springframework .context .ApplicationListener ;
25
31
import org .springframework .context .event .ContextRefreshedEvent ;
26
32
import org .springframework .stereotype .Component ;
27
33
28
- import java .util .Optional ;
34
+ import com .github .pagehelper .Page ;
35
+
36
+ import lombok .extern .slf4j .Slf4j ;
37
+
29
38
30
39
/**
31
40
* PulsarApplicationListener do something after the spring framework initialization is complete.
@@ -38,6 +47,10 @@ public class PulsarApplicationListener implements ApplicationListener<ContextRef
38
47
39
48
private final PulsarAdminService pulsarAdminService ;
40
49
50
+ private final UsersRepository usersRepository ;
51
+
52
+ private final UsersService usersService ;
53
+
41
54
@ Value ("${default.environment.name}" )
42
55
private String defaultEnvironmentName ;
43
56
@@ -47,20 +60,79 @@ public class PulsarApplicationListener implements ApplicationListener<ContextRef
47
60
@ Value ("${default.environment.bookie_url}" )
48
61
private String defaultEnvironmentBookieUrl ;
49
62
63
+ @ Value ("${default.superuser.enable}" )
64
+ private Boolean defaultSuperuserEnable = false ;
65
+
66
+ @ Value ("${default.superuser.name}" )
67
+ private String defaultSuperuserName ;
68
+
69
+ @ Value ("${default.superuser.email}" )
70
+ private String defaultSuperuserEmail ;
71
+
72
+ @ Value ("${default.superuser.password}" )
73
+ private String defaultSuperuserPassword ;
74
+
50
75
@ Autowired
51
- public PulsarApplicationListener (EnvironmentsRepository environmentsRepository , PulsarAdminService pulsarAdminService ) {
76
+ public PulsarApplicationListener (
77
+ EnvironmentsRepository environmentsRepository ,
78
+ PulsarAdminService pulsarAdminService ,
79
+ UsersRepository usersRepository ,
80
+ UsersService usersService
81
+ ) {
52
82
this .environmentsRepository = environmentsRepository ;
53
83
this .pulsarAdminService = pulsarAdminService ;
84
+ this .usersRepository = usersRepository ;
85
+ this .usersService = usersService ;
54
86
}
55
87
56
88
@ Override
57
89
public void onApplicationEvent (ContextRefreshedEvent event ) {
58
90
log .info ("Start onApplicationEvent" );
59
- Page <EnvironmentEntity > environmentEntities = environmentsRepository
60
- .getEnvironmentsList (1 , 1 );
91
+
92
+ seedDefaultSuperuser ();
93
+ seedDefaultEnvironment ();
94
+ }
95
+
96
+ private void seedDefaultSuperuser () {
97
+ if (defaultSuperuserEnable == false ) {
98
+ log .debug ("Superuser seed disabled" );
99
+ return ;
100
+ }
101
+
102
+ UserInfoEntity userInfoEntity = new UserInfoEntity ();
103
+ userInfoEntity .setName (defaultSuperuserName );
104
+ userInfoEntity .setEmail (defaultSuperuserEmail );
105
+ userInfoEntity .setPassword (defaultSuperuserPassword );
106
+
107
+ Map <String , String > userValidateResult = usersService .validateUserInfo (userInfoEntity );
108
+ if (userValidateResult .get ("error" ) != null ) {
109
+ log .error ("Superuser seed failed." , userValidateResult .get ("error" ));
110
+ System .exit (-1 );
111
+ }
112
+ if (StringUtils .isBlank (userInfoEntity .getPassword ())) {
113
+ log .error ("Superuser seed failed. Password is required." );
114
+ System .exit (-1 );
115
+ }
116
+
117
+ Optional <UserInfoEntity > optionalUserEntity = usersRepository .findByUserName (userInfoEntity .getName ());
118
+ if (optionalUserEntity .isPresent ()) {
119
+ log .warn ("Superuser already exists." );
120
+ return ;
121
+ }
122
+
123
+ userInfoEntity .setPassword (DigestUtils .sha256Hex (userInfoEntity .getPassword ()));
124
+ usersRepository .save (userInfoEntity );
125
+
126
+ log .info ("Successfully added a default superuser: name = {}, email = {}, password = {}." ,
127
+ defaultSuperuserName , defaultSuperuserEmail , defaultSuperuserPassword );
128
+ }
129
+
130
+ private void seedDefaultEnvironment () {
131
+ Page <EnvironmentEntity > environmentEntities = environmentsRepository .getEnvironmentsList (1 , 1 );
132
+
61
133
if (environmentEntities .getResult ().size () <= 0 ) {
62
- Optional <EnvironmentEntity > environmentEntityOptional = environmentsRepository
63
- . findByName ( defaultEnvironmentName );
134
+ Optional <EnvironmentEntity > environmentEntityOptional = environmentsRepository . findByName ( defaultEnvironmentName );
135
+
64
136
if (defaultEnvironmentName != null
65
137
&& defaultEnvironmentServiceUrl != null
66
138
&& defaultEnvironmentName .length () > 0
@@ -89,6 +161,7 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
89
161
log .warn ("The default environment already exists." );
90
162
}
91
163
}
164
+
92
165
log .debug ("Environments already exist." );
93
166
}
94
167
}
0 commit comments