1
|
package eu.dnetlib.openaire.user.ldap;
|
2
|
|
3
|
import com.unboundid.ldap.sdk.*;
|
4
|
import eu.dnetlib.openaire.user.pojos.migration.LDAPUser;
|
5
|
import eu.dnetlib.openaire.user.pojos.migration.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
|
}
|