Revision 47674
Added by Katerina Iatropoulou almost 7 years ago
modules/uoa-user-management/trunk/deploy.info | ||
---|---|---|
1 |
{ |
|
2 |
"type_source": "SVN", |
|
3 |
"goal": "package -U -T 4C source:jar", |
|
4 |
"url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-user-management/trunk", |
|
5 |
"deploy_repository": "dnet4-snapshots", |
|
6 |
"version": "4", |
|
7 |
"mail": "antleb@di.uoa.gr, kiatrop@di.uoa.gr, sbaltzi@di.uoa.gr", |
|
8 |
"deploy_repository_url": "http://maven.research-infrastructures.eu/nexus/content/repositories/dnet4-snapshots", |
|
9 |
"name": "uoa-user-management" |
|
10 |
} |
|
11 |
|
modules/uoa-user-management/trunk/src/test/java/eu/dnetlib/openaire/user/AppTest.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user; |
|
2 |
|
|
3 |
import junit.framework.Test; |
|
4 |
import junit.framework.TestCase; |
|
5 |
import junit.framework.TestSuite; |
|
6 |
|
|
7 |
/** |
|
8 |
* Unit test for simple App. |
|
9 |
*/ |
|
10 |
public class AppTest |
|
11 |
extends TestCase |
|
12 |
{ |
|
13 |
/** |
|
14 |
* Create the test case |
|
15 |
* |
|
16 |
* @param testName name of the test case |
|
17 |
*/ |
|
18 |
public AppTest( String testName ) |
|
19 |
{ |
|
20 |
super( testName ); |
|
21 |
} |
|
22 |
|
|
23 |
/** |
|
24 |
* @return the suite of tests being tested |
|
25 |
*/ |
|
26 |
public static Test suite() |
|
27 |
{ |
|
28 |
return new TestSuite( AppTest.class ); |
|
29 |
} |
|
30 |
|
|
31 |
/** |
|
32 |
* Rigourous Test :-) |
|
33 |
*/ |
|
34 |
public void testApp() |
|
35 |
{ |
|
36 |
assertTrue( true ); |
|
37 |
} |
|
38 |
} |
modules/uoa-user-management/trunk/src/main/java/eu/dnetlib/openaire/user/LDAPUser.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user; |
|
2 |
|
|
3 |
/** |
|
4 |
* Created by sofia on 29/11/2016. |
|
5 |
*/ |
|
6 |
public class LDAPUser { |
|
7 |
|
|
8 |
private String cn; |
|
9 |
private String displayName; |
|
10 |
private String email; |
|
11 |
|
|
12 |
public LDAPUser(){ |
|
13 |
} |
|
14 |
|
|
15 |
public LDAPUser(String cn, String displayName, String email) |
|
16 |
{ |
|
17 |
|
|
18 |
this.cn = cn; |
|
19 |
this.displayName = displayName; |
|
20 |
this.email = email; |
|
21 |
} |
|
22 |
|
|
23 |
public String getCn() { |
|
24 |
return cn; |
|
25 |
} |
|
26 |
|
|
27 |
public void setCn(String cn) { |
|
28 |
this.cn = cn; |
|
29 |
} |
|
30 |
|
|
31 |
public String getDisplayName() { |
|
32 |
return displayName; |
|
33 |
} |
|
34 |
|
|
35 |
public void setDisplayName(String displayName) { |
|
36 |
this.displayName = displayName; |
|
37 |
} |
|
38 |
|
|
39 |
public String getEmail() { |
|
40 |
return email; |
|
41 |
} |
|
42 |
|
|
43 |
public void setEmail(String email) { |
|
44 |
this.email = email; |
|
45 |
} |
|
46 |
|
|
47 |
} |
modules/uoa-user-management/trunk/src/main/java/eu/dnetlib/openaire/user/ldap/UserActionsLDAP.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.ldap; |
|
2 |
|
|
3 |
import eu.dnetlib.openaire.user.IUserActions; |
|
4 |
import eu.dnetlib.openaire.user.user.UserProfileIS; |
|
5 |
import org.apache.log4j.Logger; |
|
6 |
|
|
7 |
import java.util.UUID; |
|
8 |
|
|
9 |
import com.unboundid.ldap.sdk.Attribute; |
|
10 |
import com.unboundid.ldap.sdk.DN; |
|
11 |
import com.unboundid.ldap.sdk.Entry; |
|
12 |
import com.unboundid.ldap.sdk.Filter; |
|
13 |
import com.unboundid.ldap.sdk.LDAPConnection; |
|
14 |
import com.unboundid.ldap.sdk.Modification; |
|
15 |
import com.unboundid.ldap.sdk.ModificationType; |
|
16 |
import com.unboundid.ldap.sdk.SearchRequest; |
|
17 |
import com.unboundid.ldap.sdk.SearchResult; |
|
18 |
import com.unboundid.ldap.sdk.SearchResultEntry; |
|
19 |
import com.unboundid.ldap.sdk.SearchScope; |
|
20 |
|
|
21 |
|
|
22 |
/** |
|
23 |
* Created by sofia on 31/10/2016. |
|
24 |
*/ |
|
25 |
public class UserActionsLDAP implements IUserActions { |
|
26 |
|
|
27 |
transient Logger logger = Logger.getLogger(UserActionsLDAP.class); |
|
28 |
|
|
29 |
private int ldapPort = 0; |
|
30 |
private String ldapAddress; |
|
31 |
private String ldapUsername; |
|
32 |
private String ldapPassword; |
|
33 |
private String ldapUsersDN; |
|
34 |
|
|
35 |
@Override |
|
36 |
public boolean activateUser(String activationId) throws Exception { |
|
37 |
LDAPConnection connection = null; |
|
38 |
try { |
|
39 |
logger.debug("activating user with activationId " + activationId); |
|
40 |
connection = new LDAPConnection(ldapAddress, ldapPort, ldapUsername, ldapPassword); |
|
41 |
Filter filter = Filter.createEqualityFilter("employeeNumber", activationId); |
|
42 |
SearchRequest searchRequest = new SearchRequest(ldapUsersDN, SearchScope.SUB, filter, "uid"); |
|
43 |
SearchResult searchResult = connection.search(searchRequest); |
|
44 |
String dn = null; |
|
45 |
|
|
46 |
if ( searchResult.getSearchEntries().size() > 0 ) { |
|
47 |
|
|
48 |
for (SearchResultEntry entry : searchResult.getSearchEntries()) { |
|
49 |
dn = "uid=" + entry.getAttributeValue("uid") + "," + ldapUsersDN; |
|
50 |
} |
|
51 |
|
|
52 |
Modification mod1 = new Modification(ModificationType.REPLACE, "JoomlaBlockUser", "0"); |
|
53 |
Modification mod2 = new Modification(ModificationType.REPLACE, "employeeNumber"); |
|
54 |
connection.modify(dn, mod1, mod2); |
|
55 |
return true; |
|
56 |
} else { |
|
57 |
return false; |
|
58 |
} |
|
59 |
} catch (Exception e) { |
|
60 |
logger.error("", e); |
|
61 |
throw e; |
|
62 |
} finally { |
|
63 |
if (connection != null) |
|
64 |
connection.close(); |
|
65 |
} |
|
66 |
} |
|
67 |
|
|
68 |
@Override |
|
69 |
public String addUser(String email, String password) throws Exception { |
|
70 |
throw new UnsupportedOperationException(); |
|
71 |
} |
|
72 |
|
|
73 |
@Override |
|
74 |
public String addUser(String username, String email, String password, String firstName, String lastName) throws Exception { |
|
75 |
logger.debug("adding user " + username + " " + email + " to ldap"); |
|
76 |
Attribute cn = new Attribute("cn", username); |
|
77 |
Attribute displayName = new Attribute("displayName", firstName + " " + lastName); |
|
78 |
Attribute mail = new Attribute("mail", email); |
|
79 |
Attribute givenName = new Attribute("givenName", firstName); |
|
80 |
Attribute joomlaBlockUser = new Attribute("JoomlaBlockUser", "1"); |
|
81 |
Attribute joomlaGroup = new Attribute("JoomlaGroup", "Registered"); |
|
82 |
Attribute objectClass = new Attribute("objectClass", "top", "inetOrgPerson", "JoomlaUser"); |
|
83 |
Attribute userPassword = new Attribute("userPassword", Joomla15PasswordHash.create(password)); |
|
84 |
Attribute sn = new Attribute("sn", lastName); |
|
85 |
Attribute uid = new Attribute("uid", username); |
|
86 |
// Attribute joomlaUserParams = new Attribute("JoomlaUserParams", ""); |
|
87 |
String activationId = UUID.randomUUID().toString(); |
|
88 |
Attribute x500UniqueIdentifier = new Attribute("employeeNumber", activationId); |
|
89 |
LDAPConnection connection = null; |
|
90 |
try { |
|
91 |
DN dn = new DN("uid=" + username + "," + ldapUsersDN); |
|
92 |
System.out.println("cn: " + cn + " displayName: " + displayName + " mail: " + mail + " givenName: " + givenName + " joomlaBlockUser: " + joomlaBlockUser + " joomlaGroup: " + joomlaGroup + " objectClass: " + objectClass + " userPassword: " + userPassword + " sn: " + sn + " uid: " + uid + " x500UniqueIdentifier: " + x500UniqueIdentifier); |
|
93 |
Entry entry = new Entry(dn.toNormalizedString(), cn, displayName, mail, givenName, joomlaBlockUser, joomlaGroup, objectClass, userPassword, sn, uid/* |
|
94 |
* , |
|
95 |
* joomlaUserParams |
|
96 |
*/, x500UniqueIdentifier); |
|
97 |
connection = new LDAPConnection(ldapAddress, ldapPort, ldapUsername, ldapPassword); |
|
98 |
connection.add(entry); |
|
99 |
|
|
100 |
return activationId; |
|
101 |
} catch (Exception e) { |
|
102 |
logger.error("", e); |
|
103 |
throw e; |
|
104 |
} finally { |
|
105 |
if (connection != null) |
|
106 |
connection.close(); |
|
107 |
} |
|
108 |
} |
|
109 |
|
|
110 |
@Override |
|
111 |
public boolean correctCreds(String email, String password) throws Exception { |
|
112 |
LDAPConnection connection = null; |
|
113 |
try { |
|
114 |
logger.debug("checking if user " + email + " entered a correct password when logging in"); |
|
115 |
connection = new LDAPConnection(ldapAddress, ldapPort, ldapUsername, ldapPassword); |
|
116 |
Filter filter = Filter.createEqualityFilter("mail", email); |
|
117 |
SearchRequest searchRequest = new SearchRequest(ldapUsersDN, SearchScope.SUB, filter, "userPassword"); |
|
118 |
SearchResult searchResult = connection.search(searchRequest); |
|
119 |
for (SearchResultEntry entry : searchResult.getSearchEntries()) { |
|
120 |
if (Joomla15PasswordHash.check(password, entry.getAttributeValue("userPassword"))) |
|
121 |
return true; |
|
122 |
} |
|
123 |
return false; |
|
124 |
} catch (Exception e) { |
|
125 |
logger.error("", e); |
|
126 |
throw e; |
|
127 |
} finally { |
|
128 |
if (connection != null) |
|
129 |
connection.close(); |
|
130 |
} |
|
131 |
} |
|
132 |
|
|
133 |
@Override |
|
134 |
public void editUser(String email, String fname, String lname, String inst) throws Exception { |
|
135 |
LDAPConnection connection = null; |
|
136 |
try { |
|
137 |
logger.debug("editing user " + email); |
|
138 |
connection = new LDAPConnection(ldapAddress, ldapPort, ldapUsername, ldapPassword); |
|
139 |
Filter filter = Filter.createEqualityFilter("mail", email); |
|
140 |
SearchRequest searchRequest = new SearchRequest(ldapUsersDN, SearchScope.SUB, filter, "uid"); |
|
141 |
SearchResult searchResult = connection.search(searchRequest); |
|
142 |
String dn = null; |
|
143 |
for (SearchResultEntry entry : searchResult.getSearchEntries()) { |
|
144 |
dn = "uid=" + entry.getAttributeValue("uid") + "," + ldapUsersDN; |
|
145 |
} |
|
146 |
Modification mod1 = new Modification(ModificationType.REPLACE, "displayName", fname + " " + lname); |
|
147 |
Modification mod2 = new Modification(ModificationType.REPLACE, "givenName", fname); |
|
148 |
Modification mod3 = new Modification(ModificationType.REPLACE, "sn", lname); |
|
149 |
Modification mod4 = new Modification(ModificationType.REPLACE, "o", inst); |
|
150 |
connection.modify(dn, mod1, mod2, mod3, mod4); |
|
151 |
} catch (Exception e) { |
|
152 |
logger.error("", e); |
|
153 |
throw e; |
|
154 |
} finally { |
|
155 |
if (connection != null) |
|
156 |
connection.close(); |
|
157 |
} |
|
158 |
} |
|
159 |
|
|
160 |
@Override |
|
161 |
public eu.dnetlib.openaire.user.user.UserProfile getUser(String userIdentifier) throws Exception { |
|
162 |
LDAPConnection connection = null; |
|
163 |
try { |
|
164 |
logger.debug("getting user " + userIdentifier + " from ldap"); |
|
165 |
connection = new LDAPConnection(ldapAddress, ldapPort, ldapUsername, ldapPassword); |
|
166 |
Filter filter = Filter.createEqualityFilter("mail", userIdentifier); |
|
167 |
SearchRequest searchRequest = new SearchRequest(ldapUsersDN, SearchScope.SUB, filter, "mail", "givenName", "sn", "o", "uid"); |
|
168 |
SearchResult searchResult = connection.search(searchRequest); |
|
169 |
UserProfileIS profile = new UserProfileIS(); |
|
170 |
for (SearchResultEntry entry : searchResult.getSearchEntries()) { |
|
171 |
profile.setEmail(entry.getAttributeValue("mail")); |
|
172 |
profile.setFname(entry.getAttributeValue("givenName")); |
|
173 |
profile.setLname(entry.getAttributeValue("sn")); |
|
174 |
profile.setInstitution(entry.getAttributeValue("o")); |
|
175 |
profile.setUsername(entry.getAttributeValue("uid")); |
|
176 |
} |
|
177 |
return profile; |
|
178 |
} catch (Exception e) { |
|
179 |
logger.error("", e); |
|
180 |
throw e; |
|
181 |
} finally { |
|
182 |
if (connection != null) |
|
183 |
connection.close(); |
|
184 |
} |
|
185 |
} |
|
186 |
|
|
187 |
@Override |
|
188 |
public boolean isAdmin(String email) throws Exception { |
|
189 |
LDAPConnection connection = null; |
|
190 |
try { |
|
191 |
logger.debug("checking if user " + email + " is an administrator"); |
|
192 |
connection = new LDAPConnection(ldapAddress, ldapPort, ldapUsername, ldapPassword); |
|
193 |
Filter filter = Filter.createEqualityFilter("mail", email); |
|
194 |
SearchRequest searchRequest = new SearchRequest(ldapUsersDN, SearchScope.SUB, filter, "JoomlaGroup"); |
|
195 |
SearchResult searchResult = connection.search(searchRequest); |
|
196 |
|
|
197 |
for (SearchResultEntry entry : searchResult.getSearchEntries()) { |
|
198 |
for (String role : entry.getAttributeValues("JoomlaGroup")) |
|
199 |
if (role.equals("validatorAdmin")) |
|
200 |
return true; |
|
201 |
} |
|
202 |
logger.debug(email + " is not administrator"); |
|
203 |
return false; |
|
204 |
} catch (Exception e) { |
|
205 |
logger.error("", e); |
|
206 |
throw e; |
|
207 |
} finally { |
|
208 |
if (connection != null) |
|
209 |
connection.close(); |
|
210 |
} |
|
211 |
} |
|
212 |
|
|
213 |
@Override |
|
214 |
public boolean isUserActivated(String email) throws Exception { |
|
215 |
LDAPConnection connection = null; |
|
216 |
try { |
|
217 |
logger.debug("checking if user " + email + " is activated"); |
|
218 |
connection = new LDAPConnection(ldapAddress, ldapPort, ldapUsername, ldapPassword); |
|
219 |
Filter filter = Filter.createEqualityFilter("mail", email); |
|
220 |
SearchRequest searchRequest = new SearchRequest(ldapUsersDN, SearchScope.SUB, filter, "JoomlaBlockUser"); |
|
221 |
SearchResult searchResult = connection.search(searchRequest); |
|
222 |
for (SearchResultEntry entry : searchResult.getSearchEntries()) { |
|
223 |
int val = entry.getAttributeValueAsInteger("JoomlaBlockUser"); |
|
224 |
if (val == 0) |
|
225 |
return true; |
|
226 |
else |
|
227 |
return false; |
|
228 |
} |
|
229 |
} catch (Exception e) { |
|
230 |
logger.error("", e); |
|
231 |
throw e; |
|
232 |
} finally { |
|
233 |
if (connection != null) |
|
234 |
connection.close(); |
|
235 |
} |
|
236 |
return false; |
|
237 |
} |
|
238 |
|
|
239 |
@Override |
|
240 |
public String prepareResetPassword(String email) throws Exception { |
|
241 |
LDAPConnection connection = null; |
|
242 |
try { |
|
243 |
logger.debug("preparing reset password for user " + email); |
|
244 |
connection = new LDAPConnection(ldapAddress, ldapPort, ldapUsername, ldapPassword); |
|
245 |
Filter filter = Filter.createEqualityFilter("mail", email); |
|
246 |
SearchRequest searchRequest = new SearchRequest(ldapUsersDN, SearchScope.SUB, filter, "uid"); |
|
247 |
SearchResult searchResult = connection.search(searchRequest); |
|
248 |
String dn = null; |
|
249 |
for (SearchResultEntry entry : searchResult.getSearchEntries()) { |
|
250 |
dn = "uid=" + entry.getAttributeValue("uid") + "," + ldapUsersDN; |
|
251 |
} |
|
252 |
String uuid = UUID.randomUUID().toString(); |
|
253 |
Modification mod = new Modification(ModificationType.REPLACE, "employeeNumber", uuid); |
|
254 |
connection.modify(dn, mod); |
|
255 |
return uuid; |
|
256 |
} catch (Exception e) { |
|
257 |
logger.error("", e); |
|
258 |
throw e; |
|
259 |
} finally { |
|
260 |
if (connection != null) |
|
261 |
connection.close(); |
|
262 |
} |
|
263 |
} |
|
264 |
|
|
265 |
@Override |
|
266 |
public void resetPassword(String uuid, String password) throws Exception { |
|
267 |
LDAPConnection connection = null; |
|
268 |
try { |
|
269 |
connection = new LDAPConnection(ldapAddress, ldapPort, ldapUsername, ldapPassword); |
|
270 |
Filter filter = Filter.createEqualityFilter("employeeNumber", uuid); |
|
271 |
SearchRequest searchRequest = new SearchRequest(ldapUsersDN, SearchScope.SUB, filter, "uid"); |
|
272 |
SearchResult searchResult = connection.search(searchRequest); |
|
273 |
String dn = null; |
|
274 |
for (SearchResultEntry entry : searchResult.getSearchEntries()) { |
|
275 |
dn = "uid=" + entry.getAttributeValue("uid") + "," + ldapUsersDN; |
|
276 |
} |
|
277 |
Modification mod1 = new Modification(ModificationType.REPLACE, "userPassword", Joomla15PasswordHash.create(password)); |
|
278 |
Modification mod2 = new Modification(ModificationType.REPLACE, "employeeNumber"); |
|
279 |
connection.modify(dn, mod1, mod2); |
|
280 |
} catch (Exception e) { |
|
281 |
logger.error("", e); |
|
282 |
throw e; |
|
283 |
} finally { |
|
284 |
if (connection != null) |
|
285 |
connection.close(); |
|
286 |
} |
|
287 |
} |
|
288 |
|
|
289 |
@Override |
|
290 |
public boolean userExists(String email) throws Exception { |
|
291 |
LDAPConnection connection = null; |
|
292 |
try { |
|
293 |
logger.debug("checking if user " + email + " exists in ldap"); |
|
294 |
connection = new LDAPConnection(ldapAddress, ldapPort, ldapUsername, ldapPassword); |
|
295 |
Filter filter = Filter.createEqualityFilter("mail", email); |
|
296 |
SearchRequest searchRequest = new SearchRequest(ldapUsersDN, SearchScope.SUB, filter, "mail"); |
|
297 |
|
|
298 |
SearchResult searchResult = connection.search(searchRequest); |
|
299 |
if (!searchResult.getSearchEntries().isEmpty()) |
|
300 |
return true; |
|
301 |
|
|
302 |
return false; |
|
303 |
} catch (Exception e) { |
|
304 |
logger.error("", e); |
|
305 |
throw e; |
|
306 |
} finally { |
|
307 |
if (connection != null) |
|
308 |
connection.close(); |
|
309 |
} |
|
310 |
} |
|
311 |
|
|
312 |
@Override |
|
313 |
public boolean usernameExists(String username) throws Exception { |
|
314 |
LDAPConnection connection = null; |
|
315 |
try { |
|
316 |
logger.debug("checking if user " + username + " exists in ldap"); |
|
317 |
connection = new LDAPConnection(ldapAddress, ldapPort, ldapUsername, ldapPassword); |
|
318 |
Filter filter = Filter.createEqualityFilter("uid", username); |
|
319 |
SearchRequest searchRequest = new SearchRequest(ldapUsersDN, SearchScope.SUB, filter, "uid"); |
|
320 |
SearchResult searchResult = connection.search(searchRequest); |
|
321 |
|
|
322 |
if (!searchResult.getSearchEntries().isEmpty()) { |
|
323 |
return true; |
|
324 |
} |
|
325 |
|
|
326 |
return false; |
|
327 |
} catch (Exception e) { |
|
328 |
logger.error("", e); |
|
329 |
throw e; |
|
330 |
} finally { |
|
331 |
if (connection != null) |
|
332 |
connection.close(); |
|
333 |
} |
|
334 |
} |
|
335 |
|
|
336 |
@Override |
|
337 |
public String getEmailFromUsername(String username) throws Exception { |
|
338 |
LDAPConnection connection = null; |
|
339 |
try { |
|
340 |
logger.debug("getting email for user " + username); |
|
341 |
connection = new LDAPConnection(ldapAddress, ldapPort, ldapUsername, ldapPassword); |
|
342 |
Filter filter = Filter.createEqualityFilter("uid", username); |
|
343 |
SearchRequest searchRequest = new SearchRequest(ldapUsersDN, SearchScope.SUB, filter, "mail"); |
|
344 |
SearchResult searchResult = connection.search(searchRequest); |
|
345 |
for (SearchResultEntry entry : searchResult.getSearchEntries()) { |
|
346 |
return entry.getAttributeValue("mail"); |
|
347 |
} |
|
348 |
return null; |
|
349 |
} catch (Exception e) { |
|
350 |
logger.error("", e); |
|
351 |
throw e; |
|
352 |
} finally { |
|
353 |
if (connection != null) |
|
354 |
connection.close(); |
|
355 |
} |
|
356 |
} |
|
357 |
|
|
358 |
public void setLdapPort(int ldapPort) { |
|
359 |
this.ldapPort = ldapPort; |
|
360 |
} |
|
361 |
|
|
362 |
public void setLdapAddress(String ldapAddress) { |
|
363 |
this.ldapAddress = ldapAddress; |
|
364 |
} |
|
365 |
|
|
366 |
public void setLdapUsername(String ldapUsername) { |
|
367 |
this.ldapUsername = ldapUsername; |
|
368 |
} |
|
369 |
|
|
370 |
public void setLdapPassword(String ldapPassword) { |
|
371 |
this.ldapPassword = ldapPassword; |
|
372 |
} |
|
373 |
|
|
374 |
public void setLdapUsersDN(String ldapUsersDN) { |
|
375 |
this.ldapUsersDN = ldapUsersDN; |
|
376 |
} |
|
377 |
|
|
378 |
|
|
379 |
public static void main(String[] args) { |
|
380 |
|
|
381 |
UserActionsLDAP ldap = new UserActionsLDAP(); |
|
382 |
|
|
383 |
String ldapAddress = "esperos.di.uoa.gr"; |
|
384 |
String ldapUsername = "cn=admin,dc=openaire,dc=eu"; |
|
385 |
String ldapPassword = "serenata"; |
|
386 |
String ldapUsersDN = "ou=users,dc=openaire,dc=eu"; |
|
387 |
int ldapPort = 389; |
|
388 |
|
|
389 |
ldap.setLdapAddress(ldapAddress); |
|
390 |
ldap.setLdapUsername(ldapUsername); |
|
391 |
ldap.setLdapPassword(ldapPassword); |
|
392 |
ldap.setLdapUsersDN(ldapUsersDN); |
|
393 |
ldap.setLdapPort(ldapPort); |
|
394 |
|
|
395 |
|
|
396 |
|
|
397 |
// CHECK: usernameExists(uid) |
|
398 |
// TERMINAL: ldapsearch -x -LLL -h esperos.di.uoa.gr -b dc=openaire,dc=eu dn |
|
399 |
|
|
400 |
// try { |
|
401 |
// if (ldap.usernameExists("ngasparis")) |
|
402 |
// System.out.println("username exists"); |
|
403 |
// else |
|
404 |
// System.out.println("username doesn't exist"); |
|
405 |
// |
|
406 |
// } catch (Exception e) { |
|
407 |
// e.printStackTrace(); |
|
408 |
// } |
|
409 |
|
|
410 |
|
|
411 |
// CHECK: userExists(mail) |
|
412 |
// TERMINAL: ldapsearch -x -LLL -h esperos.di.uoa.gr -b dc=openaire,dc=eu mail |
|
413 |
|
|
414 |
// try { |
|
415 |
// if (ldap.userExists("n.gasparis@di.uoa.gr")) |
|
416 |
// System.out.println("user exists"); |
|
417 |
// else |
|
418 |
// System.out.println("user doesn't exist"); |
|
419 |
// } catch (Exception e) { |
|
420 |
// e.printStackTrace(); |
|
421 |
// } |
|
422 |
|
|
423 |
|
|
424 |
// CHECK: isUserActivated(mail) |
|
425 |
// TODO not sure! |
|
426 |
// TERMINAL: ldapsearch -x -LLL -h esperos.di.uoa.gr -b dc=openaire,dc=eu mail |
|
427 |
|
|
428 |
// try { |
|
429 |
// if (ldap.isUserActivated("n.gasparis@di.uoa.gr")) |
|
430 |
// System.out.println("user is activated"); |
|
431 |
// else |
|
432 |
// System.out.println("user isn't activated"); |
|
433 |
// } catch (Exception e) { |
|
434 |
// e.printStackTrace(); |
|
435 |
// } |
|
436 |
|
|
437 |
|
|
438 |
// CHECK: getUser(mail) |
|
439 |
// TERMINAL(mail): ldapsearch -x -LLL -h esperos.di.uoa.gr -b dc=openaire,dc=eu mail |
|
440 |
// TERMINAL(firstname): ldapsearch -x -LLL -h esperos.di.uoa.gr -b dc=openaire,dc=eu givenName |
|
441 |
// TERMINAL(lastname): ldapsearch -x -LLL -h esperos.di.uoa.gr -b dc=openaire,dc=eu sn |
|
442 |
// TERMINAL(institution): ldapsearch -x -LLL -h esperos.di.uoa.gr -b dc=openaire,dc=eu o |
|
443 |
// TERMINAL(username): ldapsearch -x -LLL -h esperos.di.uoa.gr -b dc=openaire,dc=eu uid |
|
444 |
|
|
445 |
// try { |
|
446 |
// gr.uoa.di.validator.api.user.UserProfileIS userProfile = (gr.uoa.di.validator.api.user.UserProfileIS) ldap.getUser("n.gasparis@di.uoa.gr"); |
|
447 |
// System.out.println("User with Firstname: " + userProfile.getFname() + |
|
448 |
// "\n Lastname: " + userProfile.getLname() + |
|
449 |
// "\n Username: " + userProfile.getUsername() + |
|
450 |
// "\n Email: " + userProfile.getEmail() + |
|
451 |
// "\n Institution: " + userProfile.getInstitution()); |
|
452 |
// |
|
453 |
// } catch (Exception e) { |
|
454 |
// e.printStackTrace(); |
|
455 |
// } |
|
456 |
|
|
457 |
// CHECK: getEmailFromUsername(username) |
|
458 |
// ldapsearch -x -LLL -h esperos.di.uoa.gr -b dc=openaire,dc=eu mail |
|
459 |
|
|
460 |
// try { |
|
461 |
// if (ldap.usernameExists("user is adminngasparis")) { |
|
462 |
// String email = ldap.getEmailFromUsername("ngasparis"); |
|
463 |
// System.out.println("username exists with email: " + email); |
|
464 |
// } |
|
465 |
// else |
|
466 |
// System.out.println("username doesn't exist"); |
|
467 |
// |
|
468 |
// } catch (Exception e) { |
|
469 |
// e.printStackTrace(); |
|
470 |
// } |
|
471 |
|
|
472 |
// CHECK: isAdmin(mail) |
|
473 |
// TODO not sure |
|
474 |
|
|
475 |
// try { |
|
476 |
// if (ldap.isAdmin("n.gasparis@di.uoa.gr")) |
|
477 |
// System.out.println("user is admin"); |
|
478 |
// else |
|
479 |
// System.out.println("user isn't admin"); |
|
480 |
// } catch (Exception e) { |
|
481 |
// e.printStackTrace(); |
|
482 |
// } |
|
483 |
|
|
484 |
// CHECK: addUser(username, email, password, firstname, lastname) |
|
485 |
// TERMINAL (check only): ldapsearch -x -LLL -h esperos.di.uoa.gr -b dc=openaire,dc=eu dn |
|
486 |
// |
|
487 |
// try { |
|
488 |
// String activationId = ldap.addUser("sbaltzi", "sbaltzi@di.uoa.gr", "12345678", "sofia", "baltzi"); |
|
489 |
// System.out.println("Added user with activation id:" + activationId); |
|
490 |
// gr.uoa.di.validator.api.user.UserProfileIS userProfile = (gr.uoa.di.validator.api.user.UserProfileIS) ldap.getUser("sbaltzi@di.uoa.gr"); |
|
491 |
// System.out.println("User with Firstname: " + userProfile.getFname() + |
|
492 |
// "\n Lastname: " + userProfile.getLname() + |
|
493 |
// "\n Username: " + userProfile.getUsername() + |
|
494 |
// "\n Email: " + userProfile.getEmail() + |
|
495 |
// "\n Institution: " + userProfile.getInstitution()); |
|
496 |
// |
|
497 |
// } catch (Exception e) { |
|
498 |
// e.printStackTrace(); |
|
499 |
// } |
|
500 |
|
|
501 |
// CHECK: correctCreds(mail, password) |
|
502 |
|
|
503 |
// try { |
|
504 |
// if (ldap.correctCreds("sbaltzi@di.uoa.gr", "12345678")) |
|
505 |
// System.out.println("correct credentials"); |
|
506 |
// else |
|
507 |
// System.out.println("wrong credentials"); |
|
508 |
// } catch (Exception e) { |
|
509 |
// e.printStackTrace(); |
|
510 |
// } |
|
511 |
|
|
512 |
|
|
513 |
// CHECK: editUser() |
|
514 |
// TERMINAL(mail): ldapsearch -x -LLL -h esperos.di.uoa.gr -b dc=openaire,dc=eu mail |
|
515 |
// TERMINAL(firstname): ldapsearch -x -LLL -h esperos.di.uoa.gr -b dc=openaire,dc=eu givenName |
|
516 |
// TERMINAL(lastname): ldapsearch -x -LLL -h esperos.di.uoa.gr -b dc=openaire,dc=eu sn |
|
517 |
// TERMINAL(institution): ldapsearch -x -LLL -h esperos.di.uoa.gr -b dc=openaire,dc=eu o |
|
518 |
// TERMINAL(username): ldapsearch -x -LLL -h esperos.di.uoa.gr -b dc=openaire,dc=eu uid |
|
519 |
|
|
520 |
// try { |
|
521 |
// ldap.editUser("sbaltzi@di.uoa.gr", "sofie", "baltzi", "di"); |
|
522 |
// gr.uoa.di.validator.api.user.UserProfileIS userProfile = (gr.uoa.di.validator.api.user.UserProfileIS) ldap.getUser("sbaltzi@di.uoa.gr"); |
|
523 |
// System.out.println("User with Firstname: " + userProfile.getFname() + |
|
524 |
// "\n Lastname: " + userProfile.getLname() + |
|
525 |
// "\n Username: " + userProfile.getUsername() + |
|
526 |
// "\n Email: " + userProfile.getEmail() + |
|
527 |
// "\n Institution: " + userProfile.getInstitution()); |
|
528 |
// |
|
529 |
// } catch (Exception e) { |
|
530 |
// e.printStackTrace(); |
|
531 |
// } |
|
532 |
|
|
533 |
// CHECK: prepareResetPassword(mail) |
|
534 |
// CHECK: resetPassword(mail, newpassword) |
|
535 |
|
|
536 |
// try { |
|
537 |
// String uuid = ldap.prepareResetPassword("sbaltzi@di.uoa.gr"); |
|
538 |
// System.out.println("uuid is " + uuid); |
|
539 |
// ldap.resetPassword(uuid, "12345678"); |
|
540 |
// if (ldap.correctCreds("sbaltzi@di.uoa.gr", "12345678")) |
|
541 |
// System.out.println("correct credentials"); |
|
542 |
// else |
|
543 |
// System.out.println("wrong credentials"); |
|
544 |
// } catch (Exception e) { |
|
545 |
// e.printStackTrace(); |
|
546 |
// } |
|
547 |
|
|
548 |
} |
|
549 |
} |
modules/uoa-user-management/trunk/src/main/java/eu/dnetlib/openaire/user/ldap/MUserActionsLDAP2DB.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.ldap; |
|
2 |
|
|
3 |
import eu.dnetlib.openaire.user.dao.SQLMigrationUserDAO; |
|
4 |
|
|
5 |
/** |
|
6 |
* Created by sofia on 29/11/2016. |
|
7 |
*/ |
|
8 |
public class MUserActionsLDAP2DB { |
|
9 |
|
|
10 |
|
|
11 |
} |
modules/uoa-user-management/trunk/src/main/java/eu/dnetlib/openaire/user/ldap/MUserActionsLDAP.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.ldap; |
|
2 |
|
|
3 |
import com.unboundid.ldap.sdk.*; |
|
4 |
import eu.dnetlib.openaire.user.LDAPUser; |
|
5 |
import eu.dnetlib.openaire.user.MigrationUser; |
|
6 |
import eu.dnetlib.openaire.user.Role; |
|
7 |
import eu.dnetlib.openaire.user.dao.RoleDAO; |
|
8 |
import eu.dnetlib.openaire.user.dao.SQLMigrationUserDAO; |
|
9 |
import eu.dnetlib.openaire.user.store.LDAPConnector; |
|
10 |
import org.apache.log4j.Logger; |
|
11 |
import org.springframework.beans.factory.annotation.Autowired; |
|
12 |
|
|
13 |
import java.sql.SQLException; |
|
14 |
|
|
15 |
/** |
|
16 |
* Created by sofia on 7/11/2016. |
|
17 |
*/ |
|
18 |
public class MUserActionsLDAP { |
|
19 |
|
|
20 |
@Autowired |
|
21 |
private LDAPConnector ldapConnector; |
|
22 |
|
|
23 |
private static final Logger logger = Logger.getLogger(MUserActionsLDAP.class); |
|
24 |
|
|
25 |
public boolean authenticate(String cn, String password) throws LDAPException { |
|
26 |
LDAPConnection connection = ldapConnector.getConnection(); |
|
27 |
String usersDN = ldapConnector.getUsersDN(); |
|
28 |
|
|
29 |
try { |
|
30 |
logger.debug("checking if user " + cn + " entered a correct password when logging in"); |
|
31 |
|
|
32 |
Filter filter = Filter.createEqualityFilter("cn", cn); |
|
33 |
|
|
34 |
SearchRequest searchRequest = new SearchRequest(usersDN, SearchScope.SUB, filter, "userPassword"); |
|
35 |
SearchResult searchResult = connection.search(searchRequest); |
|
36 |
|
|
37 |
for (SearchResultEntry entry : searchResult.getSearchEntries()) { |
|
38 |
if (Joomla15PasswordHash.check(password, entry.getAttributeValue("userPassword"))) |
|
39 |
return true; |
|
40 |
} |
|
41 |
|
|
42 |
return false; |
|
43 |
} |
|
44 |
finally { |
|
45 |
if (connection != null) |
|
46 |
connection.close(); |
|
47 |
} |
|
48 |
} |
|
49 |
|
|
50 |
public boolean usernameExists(String username) throws LDAPException |
|
51 |
{ |
|
52 |
|
|
53 |
logger.debug("checking if username " + username + " exists"); |
|
54 |
LDAPConnection connection = ldapConnector.getConnection(); |
|
55 |
String usersDN = ldapConnector.getUsersDN(); |
|
56 |
|
|
57 |
try { |
|
58 |
logger.debug("checking if username " + username + " exists"); |
|
59 |
|
|
60 |
Filter filter = Filter.createEqualityFilter("cn", username); |
|
61 |
|
|
62 |
SearchRequest searchRequest = new SearchRequest(usersDN, SearchScope.SUB, filter, "cn"); |
|
63 |
SearchResult searchResult = connection.search(searchRequest); |
|
64 |
|
|
65 |
if (!searchResult.getSearchEntries().isEmpty()) { |
|
66 |
logger.debug("User exists."); |
|
67 |
return true; |
|
68 |
} |
|
69 |
logger.debug("User does not exist."); |
|
70 |
return false; |
|
71 |
} |
|
72 |
finally { |
|
73 |
if (connection != null) |
|
74 |
connection.close(); |
|
75 |
} |
|
76 |
} |
|
77 |
|
|
78 |
public boolean authenticateUser(String email, String password) throws LDAPException { |
|
79 |
LDAPConnection connection = ldapConnector.getConnection(); |
|
80 |
String usersDN = ldapConnector.getUsersDN(); |
|
81 |
|
|
82 |
try { |
|
83 |
logger.debug("checking if user " + email + " entered a correct password when logging in"); |
|
84 |
Filter filter = Filter.createEqualityFilter("mail", email); |
|
85 |
|
|
86 |
SearchRequest searchRequest = new SearchRequest(usersDN, SearchScope.SUB, filter, "userPassword"); |
|
87 |
SearchResult searchResult = connection.search(searchRequest); |
|
88 |
|
|
89 |
for (SearchResultEntry entry : searchResult.getSearchEntries()) { |
|
90 |
if (Joomla15PasswordHash.check(password, entry.getAttributeValue("userPassword"))) |
|
91 |
logger.debug("User exists."); |
|
92 |
return true; |
|
93 |
} |
|
94 |
|
|
95 |
logger.debug("User does not exist."); |
|
96 |
return false; |
|
97 |
} |
|
98 |
finally { |
|
99 |
if (connection != null) |
|
100 |
connection.close(); |
|
101 |
} |
|
102 |
} |
|
103 |
|
|
104 |
public String getRole(String email, String password) throws LDAPException, SQLException { |
|
105 |
|
|
106 |
boolean authenticated = authenticateUser(email, password); |
|
107 |
|
|
108 |
if (authenticated) |
|
109 |
{ |
|
110 |
SQLMigrationUserDAO muDAO = new SQLMigrationUserDAO(); |
|
111 |
MigrationUser mUser = new MigrationUser(); |
|
112 |
mUser = muDAO.fetchByEmail(email); |
|
113 |
RoleDAO roleDAO = new RoleDAO(); |
|
114 |
Role role = roleDAO.fetchById(mUser.getRoleId()); |
|
115 |
return role.getRole(); |
|
116 |
} |
|
117 |
return null; |
|
118 |
} |
|
119 |
|
|
120 |
public LDAPUser getUser(String username) throws LDAPException { |
|
121 |
|
|
122 |
LDAPConnection connection = ldapConnector.getConnection(); |
|
123 |
String usersDN = ldapConnector.getUsersDN(); |
|
124 |
|
|
125 |
try { |
|
126 |
|
|
127 |
logger.debug("getting user " + username + " from ldap"); |
|
128 |
Filter filter = Filter.createEqualityFilter("cn",username); |
|
129 |
SearchRequest searchRequest = |
|
130 |
new SearchRequest(usersDN, SearchScope.SUB, filter, "mail", "displayName", "cn"); |
|
131 |
|
|
132 |
SearchResult searchResult = connection.search(searchRequest); |
|
133 |
LDAPUser user = new LDAPUser(); |
|
134 |
|
|
135 |
for (SearchResultEntry entry : searchResult.getSearchEntries()) { |
|
136 |
user.setCn(entry.getAttributeValue("cn")); |
|
137 |
user.setEmail(entry.getAttributeValue("mail")); |
|
138 |
user.setDisplayName(entry.getAttributeValue("displayName")); |
|
139 |
} |
|
140 |
logger.debug("Cn = " + user.getCn() + " mail = " + user.getEmail() + " displayName = " + user.getDisplayName()); |
|
141 |
return user; |
|
142 |
|
|
143 |
} finally { |
|
144 |
if (connection != null) |
|
145 |
connection.close(); |
|
146 |
} |
|
147 |
} |
|
148 |
|
|
149 |
} |
modules/uoa-user-management/trunk/src/main/java/eu/dnetlib/openaire/user/ldap/Joomla15PasswordHash.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.ldap; |
|
2 |
|
|
3 |
import com.unboundid.util.Base64; |
|
4 |
|
|
5 |
import java.security.GeneralSecurityException; |
|
6 |
import java.security.MessageDigest; |
|
7 |
import java.util.Random; |
|
8 |
|
|
9 |
/** |
|
10 |
* Created by sofia on 31/10/2016. |
|
11 |
*/ |
|
12 |
public class Joomla15PasswordHash { |
|
13 |
|
|
14 |
public static boolean check(String passwd,String dbEntry) { |
|
15 |
String hashed = "{MD5}"+ Base64.encode(pack(Joomla15PasswordHash.md5(passwd))); |
|
16 |
if(dbEntry.equals(hashed)) |
|
17 |
return true; |
|
18 |
else |
|
19 |
return false; |
|
20 |
} |
|
21 |
|
|
22 |
static Random _rnd; |
|
23 |
|
|
24 |
public static String create(String passwd) { |
|
25 |
return "{MD5}"+Base64.encode(pack(Joomla15PasswordHash.md5(passwd))); |
|
26 |
} |
|
27 |
|
|
28 |
/** Takes the MD5 hash of a sequence of ASCII or LATIN1 characters, |
|
29 |
* and returns it as a 32-character lowercase hex string. |
|
30 |
* |
|
31 |
* Equivalent to MySQL's MD5() function |
|
32 |
* and to perl's Digest::MD5::md5_hex(), |
|
33 |
* and to PHP's md5(). |
|
34 |
* |
|
35 |
* Does no error-checking of the input, but only uses the low 8 bits |
|
36 |
* from each input character. |
|
37 |
*/ |
|
38 |
public static String md5(String data) { |
|
39 |
byte[] bdata = new byte[data.length()]; int i; byte[] hash; |
|
40 |
|
|
41 |
for (i=0;i<data.length();i++) bdata[i]=(byte)(data.charAt(i)&0xff ); |
|
42 |
|
|
43 |
try { |
|
44 |
MessageDigest md5er = MessageDigest.getInstance("MD5"); |
|
45 |
hash = md5er.digest(bdata); |
|
46 |
} catch (GeneralSecurityException e) { throw new RuntimeException(e); } |
|
47 |
|
|
48 |
StringBuffer r = new StringBuffer(32); |
|
49 |
for (i=0;i<hash.length;i++) { |
|
50 |
String x = Integer.toHexString(hash[i]&0xff); |
|
51 |
if (x.length()<2) r.append("0"); |
|
52 |
r.append(x); |
|
53 |
} |
|
54 |
return r.toString(); |
|
55 |
} |
|
56 |
|
|
57 |
public static byte[] pack(String md5) { |
|
58 |
byte[] bytes = new byte[16]; |
|
59 |
int j = 0; |
|
60 |
for(int i=0; i < 31; i+=2) { |
|
61 |
bytes[j] = (byte) Integer.parseInt(md5.charAt(i)+""+md5.charAt(i+1),16); |
|
62 |
j++; |
|
63 |
} |
|
64 |
return bytes; |
|
65 |
} |
|
66 |
} |
modules/uoa-user-management/trunk/src/main/java/eu/dnetlib/openaire/user/Role.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user; |
|
2 |
|
|
3 |
import java.io.Serializable; |
|
4 |
|
|
5 |
/** |
|
6 |
* Created by sofia on 8/11/2016. |
|
7 |
*/ |
|
8 |
public class Role implements Serializable |
|
9 |
{ |
|
10 |
private int id; |
|
11 |
private String role; |
|
12 |
|
|
13 |
public Role() { |
|
14 |
|
|
15 |
} |
|
16 |
|
|
17 |
public Role(int id) { |
|
18 |
this.id = id; |
|
19 |
} |
|
20 |
|
|
21 |
public Role(String role) { |
|
22 |
this.role = role; |
|
23 |
} |
|
24 |
|
|
25 |
public Role(int id, String role){ |
|
26 |
|
|
27 |
this.id = id; |
|
28 |
this.role = role; |
|
29 |
} |
|
30 |
|
|
31 |
public int getId(){ |
|
32 |
return id; |
|
33 |
} |
|
34 |
|
|
35 |
public void setId(int id){ |
|
36 |
this.id = id; |
|
37 |
} |
|
38 |
|
|
39 |
public String getRole() { |
|
40 |
return role; |
|
41 |
} |
|
42 |
|
|
43 |
public void setRole(String role) { |
|
44 |
this.role = role; |
|
45 |
} |
|
46 |
|
|
47 |
} |
modules/uoa-user-management/trunk/src/main/java/eu/dnetlib/openaire/user/dao/SQLUserDAO.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.dao; |
|
2 |
|
|
3 |
import eu.dnetlib.openaire.user.User; |
|
4 |
import eu.dnetlib.openaire.user.store.Statement; |
|
5 |
|
|
6 |
import java.sql.ResultSet; |
|
7 |
import java.sql.SQLException; |
|
8 |
import java.util.List; |
|
9 |
|
|
10 |
|
|
11 |
/** |
|
12 |
* Created by sofia on 24/6/2016. |
|
13 |
*/ |
|
14 |
public class SQLUserDAO implements UserDAO { |
|
15 |
@Override |
|
16 |
public User fetchById(String id) { |
|
17 |
return null; |
|
18 |
} |
|
19 |
|
|
20 |
@Override |
|
21 |
public User fetchByEmail(String mail) { |
|
22 |
return null; |
|
23 |
} |
|
24 |
|
|
25 |
@Override |
|
26 |
public User fetchByUsername(String username) { |
|
27 |
return null; |
|
28 |
} |
|
29 |
|
|
30 |
@Override |
|
31 |
public User fetchByName(String name) { |
|
32 |
return null; |
|
33 |
} |
|
34 |
|
|
35 |
@Override |
|
36 |
public User fetchBySurname(String surname) { |
|
37 |
return null; |
|
38 |
} |
|
39 |
|
|
40 |
@Override |
|
41 |
public List<User> fetchAll() { |
|
42 |
return null; |
|
43 |
} |
|
44 |
|
|
45 |
@Override |
|
46 |
public int countAll() { |
|
47 |
return 0; |
|
48 |
} |
|
49 |
|
|
50 |
@Override |
|
51 |
public void insert(User u) { |
|
52 |
|
|
53 |
} |
|
54 |
|
|
55 |
@Override |
|
56 |
public void delete(User u) { |
|
57 |
|
|
58 |
} |
|
59 |
|
|
60 |
@Override |
|
61 |
public User update(User u) { |
|
62 |
return null; |
|
63 |
} |
|
64 |
|
|
65 |
@Override |
|
66 |
public User fromResultSet(ResultSet set) throws SQLException { |
|
67 |
|
|
68 |
User user = new User(set.getString("username")); |
|
69 |
|
|
70 |
user.setPassword(set.getString("password")); |
|
71 |
user.setEmail(set.getString("email")); |
|
72 |
user.setName(set.getString("name")); |
|
73 |
user.setSurname(set.getString("surname")); |
|
74 |
|
|
75 |
return user; |
|
76 |
} |
|
77 |
|
|
78 |
@Override |
|
79 |
public int executeUpdate(String sql) { |
|
80 |
return 0; |
|
81 |
} |
|
82 |
|
|
83 |
@Override |
|
84 |
public int executeUpdate(String sql, Statement.Initializer init) { |
|
85 |
return 0; |
|
86 |
} |
|
87 |
|
|
88 |
@Override |
|
89 |
public User executeQuery(String sql) { |
|
90 |
return null; |
|
91 |
} |
|
92 |
|
|
93 |
@Override |
|
94 |
public User executeQuery(String sql, Statement.Initializer init) { |
|
95 |
return null; |
|
96 |
} |
|
97 |
|
|
98 |
@Override |
|
99 |
public long executeCount(String sql) { |
|
100 |
return 0; |
|
101 |
} |
|
102 |
|
|
103 |
@Override |
|
104 |
public long executeCount(String sql, Statement.Initializer init) { |
|
105 |
return 0; |
|
106 |
} |
|
107 |
} |
modules/uoa-user-management/trunk/src/main/java/eu/dnetlib/openaire/user/dao/UserDAO.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.dao; |
|
2 |
|
|
3 |
import eu.dnetlib.openaire.user.User; |
|
4 |
import eu.dnetlib.openaire.user.store.Statement; |
|
5 |
|
|
6 |
import java.sql.ResultSet; |
|
7 |
import java.sql.SQLException; |
|
8 |
import java.util.List; |
|
9 |
|
|
10 |
|
|
11 |
/** |
|
12 |
* Created by sofia on 24/6/2016. |
|
13 |
*/ |
|
14 |
public interface UserDAO { |
|
15 |
|
|
16 |
List<User> fetchAll(); |
|
17 |
|
|
18 |
User fetchById(String id); |
|
19 |
User fetchByEmail(String mail); |
|
20 |
User fetchByUsername(String username); |
|
21 |
User fetchByName(String name); |
|
22 |
User fetchBySurname(String surname); |
|
23 |
|
|
24 |
int countAll(); |
|
25 |
|
|
26 |
void insert(User u); |
|
27 |
void delete (User u); |
|
28 |
User update(User u); |
|
29 |
|
|
30 |
User fromResultSet(ResultSet set) throws SQLException; |
|
31 |
|
|
32 |
int executeUpdate(String sql); |
|
33 |
int executeUpdate(String sql, Statement.Initializer init); |
|
34 |
|
|
35 |
User executeQuery(String sql); |
|
36 |
User executeQuery(String sql, Statement.Initializer init); |
|
37 |
|
|
38 |
long executeCount(String sql); |
|
39 |
long executeCount(String sql, Statement.Initializer init); |
|
40 |
|
|
41 |
|
|
42 |
} |
modules/uoa-user-management/trunk/src/main/java/eu/dnetlib/openaire/user/dao/LDAPUserDAO.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.dao; |
|
2 |
|
|
3 |
import eu.dnetlib.openaire.user.User; |
|
4 |
import eu.dnetlib.openaire.user.store.Statement; |
|
5 |
|
|
6 |
import java.sql.ResultSet; |
|
7 |
import java.sql.SQLException; |
|
8 |
import java.util.List; |
|
9 |
|
|
10 |
/** |
|
11 |
* Created by sofia on 4/8/2016. |
|
12 |
*/ |
|
13 |
public class LDAPUserDAO implements UserDAO { |
|
14 |
@Override |
|
15 |
public List<User> fetchAll() { |
|
16 |
return null; |
|
17 |
} |
|
18 |
|
|
19 |
@Override |
|
20 |
public User fetchById(String id) { |
|
21 |
return null; |
|
22 |
} |
|
23 |
|
|
24 |
@Override |
|
25 |
public User fetchByEmail(String mail) { |
|
26 |
return null; |
|
27 |
} |
|
28 |
|
|
29 |
@Override |
|
30 |
public User fetchByUsername(String username) { |
|
31 |
return null; |
|
32 |
} |
|
33 |
|
|
34 |
@Override |
|
35 |
public User fetchByName(String name) { |
|
36 |
return null; |
|
37 |
} |
|
38 |
|
|
39 |
@Override |
|
40 |
public User fetchBySurname(String surname) { |
|
41 |
return null; |
|
42 |
} |
|
43 |
|
|
44 |
@Override |
|
45 |
public int countAll() { |
|
46 |
return 0; |
|
47 |
} |
|
48 |
|
|
49 |
@Override |
|
50 |
public void insert(User u) { |
|
51 |
|
|
52 |
} |
|
53 |
|
|
54 |
@Override |
|
55 |
public void delete(User u) { |
|
56 |
|
|
57 |
} |
|
58 |
|
|
59 |
@Override |
|
60 |
public User update(User u) { |
|
61 |
return null; |
|
62 |
} |
|
63 |
|
|
64 |
@Override |
|
65 |
public User fromResultSet(ResultSet set) throws SQLException { |
|
66 |
return null; |
|
67 |
} |
|
68 |
|
|
69 |
@Override |
|
70 |
public int executeUpdate(String sql) { |
|
71 |
return 0; |
|
72 |
} |
|
73 |
|
|
74 |
@Override |
|
75 |
public int executeUpdate(String sql, Statement.Initializer init) { |
|
76 |
return 0; |
|
77 |
} |
|
78 |
|
|
79 |
@Override |
|
80 |
public User executeQuery(String sql) { |
|
81 |
return null; |
|
82 |
} |
|
83 |
|
|
84 |
@Override |
|
85 |
public User executeQuery(String sql, Statement.Initializer init) { |
|
86 |
return null; |
|
87 |
} |
|
88 |
|
|
89 |
@Override |
|
90 |
public long executeCount(String sql) { |
|
91 |
return 0; |
|
92 |
} |
|
93 |
|
|
94 |
@Override |
|
95 |
public long executeCount(String sql, Statement.Initializer init) { |
|
96 |
return 0; |
|
97 |
} |
|
98 |
} |
modules/uoa-user-management/trunk/src/main/java/eu/dnetlib/openaire/user/dao/RoleDAO.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.dao; |
|
2 |
|
|
3 |
import eu.dnetlib.openaire.user.Role; |
|
4 |
import eu.dnetlib.openaire.user.store.DataSourceConnector; |
|
5 |
import eu.dnetlib.openaire.user.store.Statement; |
|
6 |
import org.springframework.beans.factory.annotation.Autowired; |
|
7 |
|
|
8 |
import javax.sql.DataSource; |
|
9 |
|
|
10 |
import java.sql.Connection; |
|
11 |
import java.sql.PreparedStatement; |
|
12 |
import java.sql.ResultSet; |
|
13 |
import java.sql.SQLException; |
|
14 |
import java.util.ArrayList; |
|
15 |
import java.util.List; |
|
16 |
|
|
17 |
import static eu.dnetlib.openaire.user.queries.RoleQueries.*; |
|
18 |
|
|
19 |
/** |
|
20 |
* Created by sofia on 8/11/2016. |
|
21 |
*/ |
|
22 |
public class RoleDAO { |
|
23 |
|
|
24 |
private final DataSource ds; |
|
25 |
|
|
26 |
@Autowired |
|
27 |
private DataSourceConnector dataSourceConnector; |
|
28 |
|
|
29 |
public RoleDAO() { |
|
30 |
this.ds = dataSourceConnector.getDatasource(); |
|
31 |
} |
|
32 |
|
|
33 |
// FETCH |
|
34 |
|
|
35 |
public List<Role> fetchAll() |
|
36 |
throws SQLException |
|
37 |
{ |
|
38 |
return (List<Role>) executeQuery(FETCH_ALL); |
|
39 |
} |
|
40 |
|
|
41 |
public Role fetchById(final int id) |
|
42 |
throws SQLException |
|
43 |
{ |
|
44 |
List<Role> roles = executeQuery(FETCH_BY_ID, new Statement.Initializer() { |
|
45 |
@Override |
|
46 |
public void prepare(PreparedStatement stmt) throws SQLException { |
|
47 |
stmt.setInt(1, id); |
|
48 |
} |
|
49 |
}); |
|
50 |
|
|
51 |
if (roles.isEmpty()) |
|
52 |
return null; |
|
53 |
|
|
54 |
return roles.get(0); |
|
55 |
} |
|
56 |
|
|
57 |
public Role fetchByRole(final String role) |
|
58 |
throws SQLException |
|
59 |
{ |
|
60 |
List<Role> roles = executeQuery(FETCH_BY_ROLE, new Statement.Initializer() { |
|
61 |
@Override |
|
62 |
public void prepare(PreparedStatement stmt) throws SQLException { |
|
63 |
stmt.setString(1, role); |
|
64 |
} |
|
65 |
}); |
|
66 |
|
|
67 |
if (roles.isEmpty()) |
|
68 |
return null; |
|
69 |
|
|
70 |
return roles.get(0); |
|
71 |
} |
|
72 |
|
|
73 |
public long countAll() throws SQLException |
|
74 |
{ |
|
75 |
return executeCount(COUNT_ALL); |
|
76 |
} |
|
77 |
|
|
78 |
|
|
79 |
public boolean insert(final Role role) |
|
80 |
throws SQLException |
|
81 |
{ |
|
82 |
return executeUpdate(INSERT, new Statement.Initializer() { |
|
83 |
@Override |
|
84 |
public void prepare(PreparedStatement stmt) |
|
85 |
throws SQLException { |
|
86 |
stmt.setString(1, role.getRole()); |
|
87 |
} |
|
88 |
}) > 0; |
|
89 |
|
|
90 |
} |
|
91 |
|
|
92 |
public boolean delete(final String role) |
|
93 |
throws SQLException |
|
94 |
{ |
|
95 |
return executeUpdate(DELETE, new Statement.Initializer() { |
|
96 |
@Override |
|
97 |
public void prepare(PreparedStatement stmt) |
|
98 |
throws SQLException { |
|
99 |
stmt.setString(1, role); |
|
100 |
} |
|
101 |
}) > 0; |
|
102 |
} |
|
103 |
|
|
104 |
public boolean update(final Role role) |
|
105 |
throws SQLException |
|
106 |
{ |
|
107 |
return executeUpdate(UPDATE, new Statement.Initializer() { |
|
108 |
@Override |
|
109 |
public void prepare(PreparedStatement stmt) |
|
110 |
throws SQLException { |
|
111 |
stmt.setString(1, role.getRole()); |
|
112 |
} |
|
113 |
}) > 0; |
|
114 |
} |
|
115 |
|
|
116 |
protected Role fromResultSet(ResultSet set) |
|
117 |
throws SQLException |
|
118 |
{ |
|
119 |
Role role = new Role(set.getString("role")); |
|
120 |
|
|
121 |
role.setId(set.getInt("id")); |
|
122 |
|
|
123 |
return role; |
|
124 |
} |
|
125 |
|
|
126 |
protected int executeUpdate(String sql, Statement.Initializer init) |
|
127 |
throws SQLException |
|
128 |
{ |
|
129 |
Connection connection = ds.getConnection(); |
|
130 |
|
|
131 |
try { |
|
132 |
PreparedStatement stmt = connection.prepareStatement(sql); |
|
133 |
init.prepare(stmt); |
|
134 |
return stmt.executeUpdate(); |
|
135 |
} finally { |
|
136 |
connection.close(); |
|
137 |
} |
|
138 |
} |
|
139 |
|
|
140 |
protected List<Role> executeQuery(String sql, Statement.Initializer init) |
|
141 |
throws SQLException |
|
142 |
{ |
|
143 |
Connection connection = ds.getConnection(); |
|
144 |
|
|
145 |
try { |
|
146 |
PreparedStatement stmt = connection.prepareStatement(sql); |
|
147 |
init.prepare(stmt); |
|
148 |
|
|
149 |
ResultSet set = stmt.executeQuery(); |
|
150 |
|
|
151 |
try { |
|
152 |
List<Role> results = new ArrayList<>(); |
|
153 |
|
|
154 |
while (set.next()) |
|
155 |
results.add(fromResultSet(set)); |
|
156 |
|
|
157 |
return results; |
|
158 |
} finally { |
|
159 |
set.close(); |
|
160 |
} |
|
161 |
} finally { |
|
162 |
connection.close(); |
|
163 |
} |
|
164 |
} |
|
165 |
|
|
166 |
protected int executeUpdate(String sql) |
|
167 |
throws SQLException |
|
168 |
{ |
|
169 |
return executeUpdate(sql, Statement.emptyInitializer()); |
|
170 |
} |
|
171 |
|
|
172 |
protected List<Role> executeQuery(String sql) |
|
173 |
throws SQLException |
|
174 |
{ |
|
175 |
return executeQuery(sql, Statement.emptyInitializer()); |
|
176 |
} |
|
177 |
|
|
178 |
public long executeCount(String sql) |
|
179 |
throws SQLException |
|
180 |
{ |
|
181 |
return executeCount(sql, Statement.emptyInitializer()); |
|
182 |
} |
|
183 |
|
|
184 |
public long executeCount(String sql, Statement.Initializer init) |
|
185 |
throws SQLException |
|
186 |
{ |
|
187 |
Connection connection = ds.getConnection(); |
|
188 |
|
|
189 |
try { |
|
190 |
PreparedStatement stmt = connection.prepareStatement(sql); |
|
191 |
init.prepare(stmt); |
|
192 |
|
|
193 |
ResultSet set = stmt.executeQuery(); |
|
194 |
|
|
195 |
try { |
|
196 |
if (set.next()) |
|
197 |
return set.getLong(1); |
|
198 |
throw new IllegalArgumentException(stmt.toString()); |
|
199 |
} finally { |
|
200 |
set.close(); |
|
201 |
} |
|
202 |
} finally { |
|
203 |
connection.close(); |
|
204 |
} |
|
205 |
} |
|
206 |
} |
modules/uoa-user-management/trunk/src/main/java/eu/dnetlib/openaire/user/dao/SQLMigrationUserDAO.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.dao; |
|
2 |
|
|
3 |
import eu.dnetlib.openaire.user.MigrationUser; |
|
4 |
import eu.dnetlib.openaire.user.store.DataSourceConnector; |
|
5 |
import eu.dnetlib.openaire.user.store.Statement; |
|
6 |
import org.springframework.beans.factory.annotation.Autowired; |
|
7 |
import org.springframework.stereotype.Component; |
|
8 |
|
|
9 |
import java.sql.Connection; |
|
10 |
import java.sql.PreparedStatement; |
|
11 |
import java.sql.ResultSet; |
|
12 |
import java.sql.SQLException; |
|
13 |
import java.util.ArrayList; |
|
14 |
import java.util.List; |
|
15 |
|
|
16 |
import static eu.dnetlib.openaire.user.queries.SQLMigrationUserQueries.*; |
|
17 |
|
|
18 |
|
|
19 |
/** |
|
20 |
* Created by sofia on 1/11/2016. |
|
21 |
*/ |
|
22 |
@Component |
|
23 |
public class SQLMigrationUserDAO { |
|
24 |
|
|
25 |
@Autowired(required = true) |
|
26 |
private DataSourceConnector dataSourceConnector; |
|
27 |
|
|
28 |
// FETCH |
|
29 |
public List<MigrationUser> fetchAll() |
|
30 |
throws SQLException |
|
31 |
{ |
|
32 |
return (List<MigrationUser>) executeQuery(FETCH_ALL); |
|
33 |
} |
|
34 |
|
|
35 |
public MigrationUser fetchById(final int id) |
|
36 |
throws SQLException { |
|
37 |
List<MigrationUser> users = executeQuery(FETCH_BY_ID, new Statement.Initializer() { |
|
38 |
@Override |
|
39 |
public void prepare(PreparedStatement stmt) throws SQLException { |
|
40 |
stmt.setInt(1, id); |
|
41 |
} |
|
42 |
}); |
|
43 |
|
|
44 |
if (users.isEmpty()) |
|
45 |
return null; |
|
46 |
|
|
47 |
return users.get(0); |
|
48 |
} |
|
49 |
|
|
50 |
public MigrationUser fetchByUsername(final String username) |
|
51 |
throws SQLException |
|
52 |
{ |
|
53 |
List<MigrationUser> users = executeQuery(FETCH_BY_USERNAME, new Statement.Initializer() { |
|
54 |
@Override |
|
55 |
public void prepare(PreparedStatement stmt) throws SQLException { |
|
56 |
stmt.setString(1, username); |
|
57 |
} |
|
58 |
}); |
|
59 |
|
|
60 |
if (users.isEmpty()) |
|
61 |
return null; |
|
62 |
|
Also available in: Unified diff
For the java8 dnet45 migration