1
|
/**
|
2
|
* Copyright (C) 2012 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.api.application;
|
10
|
|
11
|
import java.util.Arrays;
|
12
|
import java.util.EnumSet;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.List;
|
15
|
import java.util.Set;
|
16
|
import java.util.UUID;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.springframework.beans.factory.annotation.Autowired;
|
20
|
import org.springframework.context.event.ContextRefreshedEvent;
|
21
|
import org.springframework.context.event.ContextStartedEvent;
|
22
|
import org.springframework.security.authentication.AuthenticationProvider;
|
23
|
import org.springframework.security.core.GrantedAuthority;
|
24
|
import org.springframework.transaction.PlatformTransactionManager;
|
25
|
import org.springframework.transaction.TransactionDefinition;
|
26
|
import org.springframework.transaction.TransactionStatus;
|
27
|
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
28
|
|
29
|
import eu.etaxonomy.cdm.api.service.ICommonService;
|
30
|
import eu.etaxonomy.cdm.api.service.IGrantedAuthorityService;
|
31
|
import eu.etaxonomy.cdm.api.service.IGroupService;
|
32
|
import eu.etaxonomy.cdm.api.service.IUserService;
|
33
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
34
|
import eu.etaxonomy.cdm.common.monitor.NullProgressMonitor;
|
35
|
import eu.etaxonomy.cdm.config.Configuration;
|
36
|
import eu.etaxonomy.cdm.model.metadata.CdmMetaData;
|
37
|
import eu.etaxonomy.cdm.model.permission.CRUD;
|
38
|
import eu.etaxonomy.cdm.model.permission.GrantedAuthorityImpl;
|
39
|
import eu.etaxonomy.cdm.model.permission.Group;
|
40
|
import eu.etaxonomy.cdm.model.permission.PermissionClass;
|
41
|
import eu.etaxonomy.cdm.model.permission.User;
|
42
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmAuthority;
|
43
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.Role;
|
44
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
45
|
|
46
|
/**
|
47
|
* The <code>FirstDataInserter</code> is responsible for equipping a new and empty database with
|
48
|
* the initial set of data need by the cdmlib. It operates not only on empty databases,
|
49
|
* its methods are executed everytime the ApplicationContext has been started up, that is listens
|
50
|
* for {@link ContextStartedEvent}s.
|
51
|
* <p>
|
52
|
* responsibilities:
|
53
|
* <ul>
|
54
|
* <li>User 'admin' and role 'ROLE_ADMIN'</li>
|
55
|
* <li>cdm metadata</li>
|
56
|
* <ul>
|
57
|
* <p>
|
58
|
* The <code>runAsAuthenticationProvider</code> must be set in a security application context, eg:
|
59
|
* {@code
|
60
|
<bean id="firstDataInserter" class="eu.etaxonomy.cdm.api.application.FirstDataInserter">
|
61
|
<property name="runAsAuthenticationProvider" ref="runAsAuthenticationProvider"/>
|
62
|
</bean>
|
63
|
}
|
64
|
*
|
65
|
*
|
66
|
*
|
67
|
* @author a.kohlbecker
|
68
|
* @since Oct 12, 2012
|
69
|
*
|
70
|
*/
|
71
|
//@RunAs("ROLE_ADMIN") // seems to be broken in spring see: https://jira.springsource.org/browse/SEC-1671
|
72
|
public class FirstDataInserter extends AbstractDataInserter {
|
73
|
|
74
|
/**
|
75
|
*
|
76
|
*/
|
77
|
private static final EnumSet<CRUD> CREATE_READ = EnumSet.of(CRUD.CREATE, CRUD.READ);
|
78
|
private static final EnumSet<CRUD> UPDATE_DELETE = EnumSet.of(CRUD.UPDATE, CRUD.DELETE);
|
79
|
private static final EnumSet<CRUD> CREATE_READ_UPDATE = EnumSet.of(CRUD.CREATE, CRUD.READ, CRUD.UPDATE);
|
80
|
private static final EnumSet<CRUD> CREATE_READ_UPDATE_DELETE = EnumSet.of(CRUD.CREATE, CRUD.READ, CRUD.UPDATE, CRUD.DELETE);
|
81
|
|
82
|
public static final Logger logger = Logger.getLogger(FirstDataInserter.class);
|
83
|
|
84
|
public static final String[] EDITOR_GROUP_AUTHORITIES = new String[]{
|
85
|
new CdmAuthority(PermissionClass.REFERENCE, CREATE_READ).toString(),
|
86
|
new CdmAuthority(PermissionClass.TAXONNAME, CREATE_READ_UPDATE).toString(),
|
87
|
new CdmAuthority(PermissionClass.TEAMORPERSONBASE, CREATE_READ).toString(),
|
88
|
new CdmAuthority(PermissionClass.TAXONBASE, CREATE_READ_UPDATE_DELETE).toString(),
|
89
|
new CdmAuthority(PermissionClass.DESCRIPTIONBASE, CREATE_READ_UPDATE_DELETE).toString(),
|
90
|
new CdmAuthority(PermissionClass.DESCRIPTIONELEMENTBASE, CREATE_READ_UPDATE_DELETE).toString(),
|
91
|
new CdmAuthority(PermissionClass.SPECIMENOROBSERVATIONBASE, CREATE_READ_UPDATE_DELETE).toString(),
|
92
|
new CdmAuthority(PermissionClass.COLLECTION, CREATE_READ_UPDATE_DELETE).toString(),
|
93
|
};
|
94
|
|
95
|
/**
|
96
|
* This group will in future replace the group Editor, see issue #7150
|
97
|
*/
|
98
|
public static final String[] EDITOR_GROUP_EXTENDED_CREATE_GROUP_AUTHORITIES = new String[]{
|
99
|
new CdmAuthority(PermissionClass.REFERENCE, CREATE_READ).toString(),
|
100
|
new CdmAuthority(PermissionClass.TAXONNAME, CREATE_READ).toString(),
|
101
|
new CdmAuthority(PermissionClass.TEAMORPERSONBASE, CREATE_READ).toString(),
|
102
|
new CdmAuthority(PermissionClass.TAXONBASE, CREATE_READ).toString(),
|
103
|
new CdmAuthority(PermissionClass.DESCRIPTIONBASE, CREATE_READ).toString(),
|
104
|
new CdmAuthority(PermissionClass.DESCRIPTIONELEMENTBASE, CREATE_READ).toString(),
|
105
|
new CdmAuthority(PermissionClass.SPECIMENOROBSERVATIONBASE, CREATE_READ).toString(),
|
106
|
new CdmAuthority(PermissionClass.COLLECTION, CREATE_READ).toString(),
|
107
|
};
|
108
|
|
109
|
public static final String[] PROJECT_MANAGER_GROUP_AUTHORITIES = new String[]{
|
110
|
new CdmAuthority(PermissionClass.REFERENCE, UPDATE_DELETE).toString(),
|
111
|
new CdmAuthority(PermissionClass.TAXONNAME, EnumSet.of(CRUD.DELETE)).toString(),
|
112
|
new CdmAuthority(PermissionClass.TEAMORPERSONBASE, UPDATE_DELETE).toString(),
|
113
|
Role.ROLE_PROJECT_MANAGER.toString(),
|
114
|
};
|
115
|
|
116
|
public static final String[] EDITOR_REFERENCE_GROUP_AUTHORITIES = new String[]{
|
117
|
new CdmAuthority(PermissionClass.REFERENCE, UPDATE_DELETE).toString(),
|
118
|
new CdmAuthority(PermissionClass.TEAMORPERSONBASE, UPDATE_DELETE).toString()
|
119
|
};
|
120
|
|
121
|
public static final String[] EDIT_ALL_TAXA_GROUP_AUTHORITIES = new String[]{
|
122
|
new CdmAuthority(PermissionClass.TAXONNODE, CREATE_READ_UPDATE_DELETE).toString()
|
123
|
};
|
124
|
|
125
|
public static final String[] ADMIN_GROUP_AUTHORITIES = new String[]{
|
126
|
Role.ROLE_ADMIN.toString()
|
127
|
};
|
128
|
|
129
|
@Autowired
|
130
|
private ICommonService commonService;
|
131
|
|
132
|
@Autowired
|
133
|
private IUserService userService;
|
134
|
|
135
|
@Autowired
|
136
|
private IGroupService groupService;
|
137
|
|
138
|
|
139
|
@Autowired
|
140
|
private IGrantedAuthorityService grantedAuthorityService;
|
141
|
|
142
|
// not to be autowired, since the FirstdataInserter must be usable without security
|
143
|
private AuthenticationProvider runAsAuthenticationProvider = null;
|
144
|
|
145
|
protected PlatformTransactionManager transactionManager;
|
146
|
|
147
|
protected DefaultTransactionDefinition txDefinition = new DefaultTransactionDefinition();
|
148
|
|
149
|
private IProgressMonitor progressMonitor = null;
|
150
|
|
151
|
private boolean firstDataInserted = false;
|
152
|
|
153
|
@Autowired
|
154
|
public void setTransactionManager(PlatformTransactionManager transactionManager) {
|
155
|
this.transactionManager = transactionManager;
|
156
|
}
|
157
|
|
158
|
public FirstDataInserter() {
|
159
|
txDefinition.setName("FirstDataInserter.insertFirstData()");
|
160
|
txDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
|
161
|
}
|
162
|
|
163
|
@Override
|
164
|
public void onApplicationEvent(ContextRefreshedEvent event) {
|
165
|
if(event.getApplicationContext() instanceof MonitoredGenericApplicationContext){
|
166
|
progressMonitor = ((MonitoredGenericApplicationContext)event.getApplicationContext()).getCurrentMonitor();
|
167
|
/* TODO set up work amount, currently the amount of work ticks is hard coded
|
168
|
* in {@link CdmApplicationControllersetNewDataSource}, but we need some means to register
|
169
|
* additional ticks.
|
170
|
* see http://dev.e-taxonomy.eu/trac/ticket/3140 (generic way to obtain work ticks of application startup for monitoring)
|
171
|
*
|
172
|
*/
|
173
|
} else {
|
174
|
progressMonitor = new NullProgressMonitor();
|
175
|
}
|
176
|
|
177
|
insertFirstData();
|
178
|
}
|
179
|
|
180
|
|
181
|
private void insertFirstData() {
|
182
|
|
183
|
// this ApplicationListener may be called multiple times in nested
|
184
|
// application contexts like in web applications
|
185
|
if(!firstDataInserted){
|
186
|
|
187
|
runAsAuthentication(Role.ROLE_ADMIN);
|
188
|
|
189
|
TransactionStatus txStatus = transactionManager.getTransaction(txDefinition);
|
190
|
|
191
|
logger.info("inserting first data");
|
192
|
checkAdminUser();
|
193
|
checkDefaultGroups();
|
194
|
assureRole_REMOTING_forEditors();
|
195
|
assureRole(Role.ROLE_USER_MANAGER);
|
196
|
checkMetadata();
|
197
|
firstDataInserted = true;
|
198
|
|
199
|
transactionManager.commit(txStatus);
|
200
|
|
201
|
restoreAuthentication();
|
202
|
|
203
|
} else {
|
204
|
logger.debug("insertFirstData() already executed before, skipping this time");
|
205
|
}
|
206
|
}
|
207
|
|
208
|
|
209
|
private void checkMetadata() {
|
210
|
int metaDataCount = commonService.getCdmMetaData().size();
|
211
|
if (metaDataCount == 0){
|
212
|
progressMonitor.subTask("Creating Meta Data");
|
213
|
createMetadata();
|
214
|
}
|
215
|
}
|
216
|
|
217
|
private void checkAdminUser() {
|
218
|
User admin = findFirstUser();
|
219
|
|
220
|
if (admin == null){
|
221
|
progressMonitor.subTask("Creating Admin User");
|
222
|
admin = createAdminUser();
|
223
|
} else {
|
224
|
logger.info("Assuming first user '" + admin + "' is admin.");
|
225
|
}
|
226
|
|
227
|
checkAdminRole(admin);
|
228
|
progressMonitor.worked(1);
|
229
|
}
|
230
|
|
231
|
private void checkDefaultGroups(){
|
232
|
|
233
|
progressMonitor.subTask("Checking default groups");
|
234
|
checkGroup(Group.GROUP_EDITOR_UUID, Group.GROUP_EDITOR_NAME, EDITOR_GROUP_AUTHORITIES);
|
235
|
checkGroup(Group.GROUP_EDITOR_EXTENDED_CREATE_UUID, Group.GROUP_EDITOR_EXTENDED_CREATE_NAME, EDITOR_GROUP_EXTENDED_CREATE_GROUP_AUTHORITIES);
|
236
|
checkGroup(Group.GROUP_PROJECT_MANAGER_UUID, Group.GROUP_PROJECT_MANAGER_NAME, PROJECT_MANAGER_GROUP_AUTHORITIES);
|
237
|
checkGroup(Group.GROUP_ADMIN_UUID, Group.GROUP_ADMIN_NAME, ADMIN_GROUP_AUTHORITIES);
|
238
|
checkGroup(Group.GROUP_EDITOR_REFERENCE_UUID, Group.GROUP_EDITOR_REFERENCE, EDITOR_REFERENCE_GROUP_AUTHORITIES);
|
239
|
checkGroup(Group.GROUP_ALLOW_ALL_TAXA_UUID, Group.GROUP_ALLOW_ALL_TAXA_NAME, EDIT_ALL_TAXA_GROUP_AUTHORITIES);
|
240
|
progressMonitor.worked(1);
|
241
|
}
|
242
|
|
243
|
/**
|
244
|
* @param newGroups
|
245
|
* @param groupName
|
246
|
* @param requiredAuthorities
|
247
|
*/
|
248
|
private void checkGroup(UUID groupUuid, String groupName, String[] requiredAuthorities) {
|
249
|
Group group = groupService.load(groupUuid);
|
250
|
if(group == null){
|
251
|
group = Group.NewInstance();
|
252
|
group.setUuid(groupUuid);
|
253
|
logger.info("New Group '" + groupName + "' created");
|
254
|
}
|
255
|
group.setName(groupName); // force default name
|
256
|
|
257
|
Set<GrantedAuthority> grantedAuthorities = group.getGrantedAuthorities();
|
258
|
|
259
|
for(String a : requiredAuthorities){
|
260
|
boolean isMissing = true;
|
261
|
for(GrantedAuthority ga : grantedAuthorities){
|
262
|
if(a.equals(ga.getAuthority())){
|
263
|
isMissing = false;
|
264
|
break;
|
265
|
}
|
266
|
}
|
267
|
if(isMissing){
|
268
|
GrantedAuthorityImpl newGa = grantedAuthorityService.findAuthorityString(a);
|
269
|
|
270
|
if (newGa == null){
|
271
|
newGa = GrantedAuthorityImpl.NewInstance(a);
|
272
|
}
|
273
|
|
274
|
group.addGrantedAuthority(newGa);
|
275
|
logger.info("New GrantedAuthority '" + a + "' added to '" + groupName + "'");
|
276
|
}
|
277
|
}
|
278
|
|
279
|
groupService.merge(group, true);
|
280
|
logger.info("Check of group '" + groupName + "' done");
|
281
|
}
|
282
|
|
283
|
/**
|
284
|
* @return
|
285
|
*/
|
286
|
private User findFirstUser() {
|
287
|
User firstUser = null;
|
288
|
List<User> users = userService.list(null, 1, null, Arrays.asList(new OrderHint[]{new OrderHint("id", OrderHint.SortOrder.ASCENDING)}), null);
|
289
|
if(users.size() > 0){
|
290
|
firstUser = users.get(0);
|
291
|
}
|
292
|
return firstUser;
|
293
|
}
|
294
|
|
295
|
private User createAdminUser(){
|
296
|
|
297
|
User admin = User.NewInstance(Configuration.adminLogin, Configuration.adminPassword);
|
298
|
userService.save(admin);
|
299
|
logger.info("user '" + Configuration.adminLogin + "' created.");
|
300
|
return admin;
|
301
|
}
|
302
|
|
303
|
/**
|
304
|
* Assures the {@link Role#ROLE_REMOTING} exists.
|
305
|
* <p>
|
306
|
* If the role is missing in the db it will be created and added to the Groups <code>Editor</code> and <code>EditorExtendedCreate</code>.
|
307
|
* <p>
|
308
|
* The role will however not be added to the editor groups in case the role exist but is missing from one of these groups. This allows removal
|
309
|
* of the role from the editor groups to withdraw the remote editing permission from editors in general for a project.
|
310
|
* <p>
|
311
|
* see https://dev.e-taxonomy.eu/redmine/issues/7972
|
312
|
*/
|
313
|
private void assureRole_REMOTING_forEditors(){
|
314
|
|
315
|
if(!roleExists(Role.ROLE_REMOTING)){
|
316
|
GrantedAuthorityImpl roleRemoting = assureRole(Role.ROLE_REMOTING);
|
317
|
Group groupEditor = groupService.load(Group.GROUP_EDITOR_UUID);
|
318
|
groupEditor.addGrantedAuthority(roleRemoting);
|
319
|
groupService.saveOrUpdate(groupEditor);
|
320
|
Group groupEditorExtendedCreate = groupService.load(Group.GROUP_EDITOR_EXTENDED_CREATE_UUID);
|
321
|
groupEditorExtendedCreate.addGrantedAuthority(roleRemoting);
|
322
|
groupService.saveOrUpdate(groupEditorExtendedCreate);
|
323
|
}
|
324
|
}
|
325
|
|
326
|
private void checkAdminRole(User admin) {
|
327
|
Set<GrantedAuthority> authorities = new HashSet<>();
|
328
|
|
329
|
|
330
|
authorities = (Set<GrantedAuthority>) admin.getAuthorities();
|
331
|
|
332
|
boolean hasRoleAdmin = false;
|
333
|
for(GrantedAuthority grau : authorities){
|
334
|
if(grau.getAuthority().contentEquals(Role.ROLE_ADMIN.getAuthority())){
|
335
|
hasRoleAdmin = true;
|
336
|
break;
|
337
|
}
|
338
|
}
|
339
|
|
340
|
if(!hasRoleAdmin){
|
341
|
authorities.add(assureRole(Role.ROLE_ADMIN));
|
342
|
admin.setGrantedAuthorities(authorities);
|
343
|
progressMonitor.subTask("Creating Admins Role");
|
344
|
userService.saveOrUpdate(admin);
|
345
|
logger.info("Role " + Role.ROLE_ADMIN.getAuthority() + " for user '" + Configuration.adminLogin + "' created and added");
|
346
|
}
|
347
|
}
|
348
|
|
349
|
/**
|
350
|
* @return
|
351
|
*/
|
352
|
private GrantedAuthorityImpl assureRole(Role role) {
|
353
|
GrantedAuthorityImpl roleLoaded = grantedAuthorityService.find(role.getUuid());
|
354
|
if(roleLoaded == null){
|
355
|
roleLoaded = grantedAuthorityService.save(role.asNewGrantedAuthority());
|
356
|
}
|
357
|
return roleLoaded;
|
358
|
}
|
359
|
|
360
|
private boolean roleExists(Role role) {
|
361
|
GrantedAuthorityImpl roleLoaded = grantedAuthorityService.find(role.getUuid());
|
362
|
return roleLoaded != null;
|
363
|
}
|
364
|
|
365
|
private void createMetadata(){
|
366
|
List<CdmMetaData> metaData = CdmMetaData.defaultMetaData();
|
367
|
commonService.saveAllMetaData(metaData);
|
368
|
logger.info("Metadata created.");
|
369
|
}
|
370
|
|
371
|
}
|