1
|
package eu.etaxonomy.cdm.api.service.security;
|
2
|
|
3
|
import java.util.Arrays;
|
4
|
import java.util.List;
|
5
|
import java.util.UUID;
|
6
|
|
7
|
import org.apache.log4j.Logger;
|
8
|
import org.junit.Assert;
|
9
|
import org.junit.Before;
|
10
|
import org.junit.Test;
|
11
|
import org.springframework.core.io.ClassPathResource;
|
12
|
import org.springframework.core.io.Resource;
|
13
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
14
|
import org.springframework.security.core.Authentication;
|
15
|
import org.springframework.security.core.GrantedAuthority;
|
16
|
import org.springframework.security.core.context.SecurityContextHolder;
|
17
|
import org.springframework.security.core.context.SecurityContextImpl;
|
18
|
import org.springframework.security.core.userdetails.UserDetails;
|
19
|
|
20
|
import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
|
21
|
import eu.etaxonomy.cdm.api.application.ICdmApplicationRemoteConfiguration;
|
22
|
import eu.etaxonomy.cdm.api.service.IGroupService;
|
23
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
24
|
import eu.etaxonomy.cdm.api.service.IUserService;
|
25
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
26
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
27
|
|
28
|
public class UserAdministration {
|
29
|
|
30
|
private ICdmApplicationRemoteConfiguration appCtr;
|
31
|
private static final Logger logger = Logger.getLogger(UserAdministration.class);
|
32
|
|
33
|
@Before
|
34
|
public void setUp() {
|
35
|
Resource applicationContextResource = new ClassPathResource("/eu/etaxonomy/cdm/remotingApplicationContext.xml");
|
36
|
appCtr = CdmApplicationRemoteController.NewInstance(applicationContextResource, null);
|
37
|
|
38
|
String user = "admin"; // ROLE_ADMIN, ROLE_USER
|
39
|
// String user = "developer"; // ROLE_USER1, ROLE_USER2
|
40
|
String pwd = "0000";
|
41
|
|
42
|
Authentication auth = new UsernamePasswordAuthenticationToken(user, pwd);
|
43
|
SecurityContextImpl sc = new SecurityContextImpl();
|
44
|
sc.setAuthentication(auth);
|
45
|
SecurityContextHolder.setContext(sc);
|
46
|
logger.info("setting up security context");
|
47
|
}
|
48
|
|
49
|
@Test
|
50
|
public void testSaveOrUpdateSample() {
|
51
|
ITaxonService taxonService = appCtr.getTaxonService();
|
52
|
UUID uuid = UUID.fromString("7267e704-e0e8-41ee-8112-5aa22c61dea9");
|
53
|
TaxonBase taxon = taxonService.load(uuid, TAXON_INIT_STRATEGY);
|
54
|
logger.info("taxonName=" + taxon.getName());
|
55
|
|
56
|
TaxonNameBase name = taxon.getName();
|
57
|
|
58
|
String nameString = "Malvaceae";
|
59
|
name.setTitleCache(nameString);
|
60
|
|
61
|
taxonService.saveOrUpdate(taxon);
|
62
|
|
63
|
TaxonBase taxon2 = taxonService.load(uuid, TAXON_INIT_STRATEGY);
|
64
|
logger.info("taxonName=" + taxon2.getName());
|
65
|
|
66
|
Assert.assertEquals(nameString, taxon2.getName().getTitleCache());
|
67
|
}
|
68
|
|
69
|
@Test
|
70
|
public void testCreateNewGroupWithPresentGrantedAuthorities() {
|
71
|
IGroupService groupService = appCtr.getGroupService();
|
72
|
String newGroupName = "GROUP_TEST";
|
73
|
List<GrantedAuthority> authoritiesList = groupService.findGroupAuthorities("GROUP_ADMIN");
|
74
|
groupService.createGroup(newGroupName, authoritiesList);
|
75
|
}
|
76
|
|
77
|
// @Test
|
78
|
// public void testCreateUser() {
|
79
|
// IUserService userService = appCtr.getUserService();
|
80
|
// String username = "developer";
|
81
|
// String pwd = "0000";
|
82
|
// UserDetails user = User.NewInstance(username, pwd);
|
83
|
// userService.createUser(user);
|
84
|
// }
|
85
|
|
86
|
// @Test
|
87
|
// public void testCreateGroupUserWithNewAuthorities() {
|
88
|
// IGroupService groupService = appCtr.getGroupService();
|
89
|
// String groupName = "GROUP_USER";
|
90
|
// GrantedAuthorityImpl grantedAuthorityAdmin = GrantedAuthorityImpl.NewInstance();
|
91
|
// GrantedAuthorityImpl grantedAuthorityUser = (GrantedAuthorityImpl.NewInstance());
|
92
|
// grantedAuthorityAdmin.setAuthority("ROLE_USER1");
|
93
|
// grantedAuthorityUser.setAuthority("ROLE_USER2");
|
94
|
// List<GrantedAuthority> grantedAuthoritiesList = new ArrayList<GrantedAuthority>();
|
95
|
// grantedAuthoritiesList.add(grantedAuthorityAdmin);
|
96
|
// grantedAuthoritiesList.add(grantedAuthorityUser);
|
97
|
// groupService.createGroup(groupName, grantedAuthoritiesList);
|
98
|
// }
|
99
|
|
100
|
// @Test
|
101
|
// public void testCreateGroupAdminWithNewAuthorities() {
|
102
|
// IGroupService groupService = appCtr.getGroupService();
|
103
|
// String groupName = "GROUP_ADMIN";
|
104
|
// GrantedAuthorityImpl grantedAuthorityAdmin = GrantedAuthorityImpl.NewInstance();
|
105
|
// GrantedAuthorityImpl grantedAuthorityUser = (GrantedAuthorityImpl.NewInstance());
|
106
|
// grantedAuthorityAdmin.setAuthority("ROLE_ADMIN");
|
107
|
// grantedAuthorityUser.setAuthority("ROLE_USER");
|
108
|
// List<GrantedAuthority> grantedAuthoritiesList = new ArrayList<GrantedAuthority>();
|
109
|
// grantedAuthoritiesList.add(grantedAuthorityAdmin);
|
110
|
// grantedAuthoritiesList.add(grantedAuthorityUser);
|
111
|
// groupService.createGroup(groupName, grantedAuthoritiesList);
|
112
|
// }
|
113
|
|
114
|
@Test
|
115
|
public void testAddUserToUserGroup() {
|
116
|
IGroupService groupService = appCtr.getGroupService();
|
117
|
String groupName = "GROUP_TEST";
|
118
|
String userName = "developer";
|
119
|
groupService.addUserToGroup(userName, groupName);
|
120
|
}
|
121
|
|
122
|
@Test
|
123
|
public void testRemoveUserFromUserGroup() {
|
124
|
IGroupService groupService = appCtr.getGroupService();
|
125
|
String groupName = "GROUP_TEST";
|
126
|
String userName = "developer";
|
127
|
groupService.removeUserFromGroup(userName, groupName);
|
128
|
}
|
129
|
|
130
|
// @Test
|
131
|
// public void testAddUserToAdminGroup() {
|
132
|
// IGroupService groupService = appCtr.getGroupService();
|
133
|
// String groupName = "GROUP_ADMIN";
|
134
|
// String userName = "admin";
|
135
|
// groupService.addUserToGroup(userName, groupName);
|
136
|
// }
|
137
|
|
138
|
@Test
|
139
|
public void testUserService() {
|
140
|
IUserService userService = appCtr.getUserService();
|
141
|
UserDetails userDetails = userService.loadUserByUsername("developer");
|
142
|
String username = userDetails.getUsername();
|
143
|
String pwd = userDetails.getPassword();
|
144
|
logger.info("userDetails for " + username + ": '" + pwd + "'");
|
145
|
}
|
146
|
|
147
|
private static final List<String> TAXON_INIT_STRATEGY = Arrays.asList(new String []{
|
148
|
"*",
|
149
|
// taxon relations
|
150
|
"relationsToThisName.fromTaxon.name",
|
151
|
// the name
|
152
|
"name.$",
|
153
|
"name.rank.representations",
|
154
|
"name.status.type.representations",
|
155
|
|
156
|
// taxon descriptions
|
157
|
"descriptions.elements.area.$",
|
158
|
"descriptions.elements.multilanguageText",
|
159
|
"descriptions.elements.media.representations.parts",
|
160
|
"descriptions.elements.media.title",
|
161
|
// supplemental
|
162
|
"annotations"
|
163
|
});
|
164
|
|
165
|
}
|