1
|
package eu.dnetlib.openaire.user.ldap;
|
2
|
|
3
|
import com.unboundid.ldap.sdk.*;
|
4
|
import eu.dnetlib.openaire.user.MigrationUser;
|
5
|
import eu.dnetlib.openaire.user.Role;
|
6
|
import eu.dnetlib.openaire.user.dao.RoleDAO;
|
7
|
import eu.dnetlib.openaire.user.dao.SQLMigrationUserDAO;
|
8
|
import eu.dnetlib.openaire.user.store.LDAPConnector;
|
9
|
import java.sql.SQLException;
|
10
|
|
11
|
/**
|
12
|
* Created by sofia on 7/11/2016.
|
13
|
*/
|
14
|
public class MUserActionsLDAP
|
15
|
{
|
16
|
public static boolean authenticateUser(String email, String password) throws LDAPException
|
17
|
{
|
18
|
LDAPConnection connection = LDAPConnector.v().getConnection();
|
19
|
String usersDN = LDAPConnector.v().getUsersDN();
|
20
|
|
21
|
try {
|
22
|
System.out.println("checking if user " + email + " entered a correct password when logging in");
|
23
|
|
24
|
Filter filter = Filter.createEqualityFilter("mail", email);
|
25
|
|
26
|
SearchRequest searchRequest = new SearchRequest(usersDN, SearchScope.SUB, filter, "userPassword");
|
27
|
SearchResult searchResult = connection.search(searchRequest);
|
28
|
|
29
|
for (SearchResultEntry entry : searchResult.getSearchEntries()) {
|
30
|
if (Joomla15PasswordHash.check(password, entry.getAttributeValue("userPassword")))
|
31
|
return true;
|
32
|
}
|
33
|
|
34
|
return false;
|
35
|
}
|
36
|
finally {
|
37
|
if (connection != null)
|
38
|
connection.close();
|
39
|
}
|
40
|
}
|
41
|
|
42
|
public static String getRole(String email, String password) throws LDAPException, SQLException {
|
43
|
boolean authenticated = authenticateUser(email, password);
|
44
|
|
45
|
if (authenticated)
|
46
|
{
|
47
|
SQLMigrationUserDAO muDAO = new SQLMigrationUserDAO();
|
48
|
MigrationUser mUser = new MigrationUser();
|
49
|
mUser = muDAO.fetchByEmail(email);
|
50
|
RoleDAO roleDAO = new RoleDAO();
|
51
|
Role role = roleDAO.fetchById(mUser.getRoleId());
|
52
|
return role.getRole();
|
53
|
}
|
54
|
return null;
|
55
|
}
|
56
|
}
|