1
|
import eu.dnetlib.domain.enabling.SecurityProfile;
|
2
|
import eu.dnetlib.domain.functionality.UserProfile;
|
3
|
import eu.dnetlib.utils.md5.MD5;
|
4
|
import gr.uoa.di.driver.xml.SecurityProfileXmlConverter;
|
5
|
import gr.uoa.di.driver.xml.UserProfileXmlConverter;
|
6
|
|
7
|
import java.io.File;
|
8
|
import java.io.FileReader;
|
9
|
import java.io.FileWriter;
|
10
|
import java.io.FilenameFilter;
|
11
|
import java.io.IOException;
|
12
|
import java.security.NoSuchAlgorithmException;
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.List;
|
15
|
|
16
|
import javax.xml.bind.JAXBException;
|
17
|
|
18
|
public class CleanupSecurityProfiles {
|
19
|
final static File rootDir = new File("/home/antleb/projects/dnet1.1/uoa-is/cnr-test-profiles/src/main/eu/dnetlib/test/profiles/");
|
20
|
final static File userDir = new File(rootDir, "UserDSResources/UserDSResourceType");
|
21
|
final static File secProfDir = new File(rootDir, "SecurityProfileDSResources/SecurityProfileDSResourceType");
|
22
|
|
23
|
public static void cleanupUserProfiles()
|
24
|
throws JAXBException, IOException {
|
25
|
UserProfileXmlConverter conv = new UserProfileXmlConverter();
|
26
|
File[] xmlsFiles = userDir.listFiles(new FilenameFilter() {
|
27
|
|
28
|
@Override
|
29
|
public boolean accept(File dir, String name) {
|
30
|
return name.endsWith(".xml");
|
31
|
}
|
32
|
});
|
33
|
List<String> users = new ArrayList<String>();
|
34
|
|
35
|
for (File xmlFile : xmlsFiles) {
|
36
|
String xml = readXml(xmlFile);
|
37
|
UserProfile user = conv.XmlToObject(xml);
|
38
|
|
39
|
if (users.contains(user.getEmail())) {
|
40
|
System.out.println("user with email '" + user.getEmail() + "' already exists. Removing profile");
|
41
|
xmlFile.delete();
|
42
|
} else {
|
43
|
users.add(user.getEmail());
|
44
|
}
|
45
|
}
|
46
|
}
|
47
|
|
48
|
public static void cleanupSecProfiles() throws JAXBException,
|
49
|
NoSuchAlgorithmException, IOException {
|
50
|
SecurityProfileXmlConverter conv = new SecurityProfileXmlConverter();
|
51
|
File[] xmlsFiles = secProfDir.listFiles(new FilenameFilter() {
|
52
|
|
53
|
@Override
|
54
|
public boolean accept(File dir, String name) {
|
55
|
return name.endsWith(".xml");
|
56
|
}
|
57
|
});
|
58
|
|
59
|
for (File xmlFile : xmlsFiles) {
|
60
|
String xml = readXml(xmlFile);
|
61
|
SecurityProfile secProfile = conv.XmlToObject(xml);
|
62
|
|
63
|
if (secProfile.getDriverResourceId() == null
|
64
|
|| secProfile.getDriverResourceId().trim().equals("")) {
|
65
|
System.out.println("Security profile is orphan: " + secProfile.getResourceId());
|
66
|
xmlFile.delete();
|
67
|
} else if (!new File(userDir, secProfile.getDriverResourceId().split("_")[0] + ".xml").exists()) {
|
68
|
System.out.println("Security profile points to non existing user: " + secProfile.getResourceId());
|
69
|
xmlFile.delete();
|
70
|
|
71
|
} else {
|
72
|
System.out.println("---");
|
73
|
if (secProfile.getPassword() != null) {
|
74
|
String oldPass = secProfile.getPassword();
|
75
|
String newPass = MD5.encrypt2Hex(oldPass);
|
76
|
|
77
|
System.out.println(oldPass + " -> " + newPass);
|
78
|
|
79
|
secProfile.setPassword(newPass);
|
80
|
|
81
|
xml = conv.ObjectToXml(secProfile);
|
82
|
|
83
|
saveXml(xml, xmlFile);
|
84
|
}
|
85
|
}
|
86
|
}
|
87
|
}
|
88
|
|
89
|
private static void saveXml(String xml, File xmlFile) throws IOException {
|
90
|
FileWriter fw = new FileWriter(xmlFile);
|
91
|
|
92
|
fw.append(xml);
|
93
|
|
94
|
fw.close();
|
95
|
}
|
96
|
|
97
|
private static String readXml(File xmlFile) throws IOException {
|
98
|
FileReader fr = new FileReader(xmlFile);
|
99
|
StringBuilder sb = new StringBuilder();
|
100
|
char buffer[] = new char[1024];
|
101
|
while (fr.read(buffer) != -1) {
|
102
|
sb.append(buffer);
|
103
|
|
104
|
buffer = new char[1024];
|
105
|
}
|
106
|
|
107
|
fr.close();
|
108
|
|
109
|
return sb.toString().trim();
|
110
|
}
|
111
|
|
112
|
public static void main(String[] args) throws Exception {
|
113
|
cleanupUserProfiles();
|
114
|
cleanupSecProfiles();
|
115
|
}
|
116
|
}
|