1
|
package eu.dnetlib.validator.admin.api;
|
2
|
|
3
|
import java.math.BigInteger;
|
4
|
import java.security.MessageDigest;
|
5
|
import java.util.UUID;
|
6
|
|
7
|
import org.apache.log4j.Logger;
|
8
|
import org.springframework.transaction.annotation.Propagation;
|
9
|
import org.springframework.transaction.annotation.Transactional;
|
10
|
|
11
|
import eu.dnetlib.domain.functionality.UserProfile;
|
12
|
import eu.dnetlib.validator.commons.dao.users.UserStored;
|
13
|
import eu.dnetlib.validator.commons.dao.users.UsersDAO;
|
14
|
|
15
|
@Transactional(propagation = Propagation.REQUIRED)
|
16
|
public class UserApiImplLocal implements UserApi {
|
17
|
|
18
|
private static Logger logger = Logger.getLogger(UserApiImplLocal.class);
|
19
|
private UsersDAO usersDao;
|
20
|
|
21
|
@Override
|
22
|
@Transactional()
|
23
|
public boolean activateUser(String activationId) throws Exception {
|
24
|
UserStored newUser = new UserStored();
|
25
|
newUser.setActivationId(activationId);
|
26
|
usersDao.save(newUser);
|
27
|
return true;
|
28
|
}
|
29
|
|
30
|
@Override
|
31
|
@Transactional(propagation = Propagation.REQUIRED)
|
32
|
public String addUser(String email, String password) throws Exception {
|
33
|
String hashword;
|
34
|
MessageDigest md5 = MessageDigest.getInstance("MD5");
|
35
|
md5.update(password.getBytes());
|
36
|
BigInteger hash = new BigInteger(1, md5.digest());
|
37
|
hashword = hash.toString(16);
|
38
|
String activationId = UUID.randomUUID().toString();
|
39
|
UserStored newUser = new UserStored();
|
40
|
newUser.setEmail(email);
|
41
|
newUser.setPassword(hashword);
|
42
|
newUser.setActivationId(activationId);
|
43
|
usersDao.save(newUser);
|
44
|
return activationId;
|
45
|
}
|
46
|
|
47
|
@Override
|
48
|
public boolean correctCreds(String email, String password) throws Exception {
|
49
|
String hashword;
|
50
|
MessageDigest md5 = MessageDigest.getInstance("MD5");
|
51
|
md5.update(password.getBytes());
|
52
|
BigInteger hash = new BigInteger(1, md5.digest());
|
53
|
hashword = hash.toString(16);
|
54
|
UserStored newUser = new UserStored();
|
55
|
newUser.setEmail(email);
|
56
|
newUser.setPassword(hashword);
|
57
|
|
58
|
if (usersDao.checkCorrectCreds(newUser)) {
|
59
|
logger.debug("Password verified");
|
60
|
|
61
|
return true;
|
62
|
} else {
|
63
|
logger.debug("no user found with email: " + email + "and pass: " + password + " (" + hashword + ")");
|
64
|
|
65
|
return false;
|
66
|
}
|
67
|
}
|
68
|
|
69
|
@Override
|
70
|
public boolean isAdmin(String email) throws Exception {
|
71
|
return usersDao.isAdmin(email);
|
72
|
}
|
73
|
|
74
|
@Override
|
75
|
public boolean isUserActivated(String email) throws Exception {
|
76
|
return usersDao.isActivated(email);
|
77
|
}
|
78
|
|
79
|
@Override
|
80
|
@Transactional(propagation = Propagation.REQUIRED)
|
81
|
public String prepareResetPassword(String email) throws Exception {
|
82
|
String uuid = UUID.randomUUID().toString();
|
83
|
usersDao.prepareResetPassword(uuid,email);
|
84
|
return uuid;
|
85
|
}
|
86
|
|
87
|
@Override
|
88
|
@Transactional(propagation = Propagation.REQUIRED)
|
89
|
public void resetPassword(String uuid, String password) throws Exception {
|
90
|
String hashword;
|
91
|
MessageDigest md5 = MessageDigest.getInstance("MD5");
|
92
|
md5.update(password.getBytes());
|
93
|
BigInteger hash = new BigInteger(1, md5.digest());
|
94
|
hashword = hash.toString(16);
|
95
|
usersDao.ResetPassword(uuid, hashword);
|
96
|
}
|
97
|
|
98
|
@Override
|
99
|
public boolean userExists(String email) throws Exception {
|
100
|
return usersDao.userExists(email);
|
101
|
}
|
102
|
|
103
|
@Override
|
104
|
public void editUser(String email, String fname, String lname, String inst) throws Exception {
|
105
|
// TODO Auto-generated method stub
|
106
|
|
107
|
}
|
108
|
|
109
|
@Override
|
110
|
public String addUser(String username, String email, String password, String firstName, String lastName) {
|
111
|
// TODO Auto-generated method stub
|
112
|
return null;
|
113
|
}
|
114
|
|
115
|
@Override
|
116
|
public boolean usernameExists(String username) throws Exception {
|
117
|
// TODO Auto-generated method stub
|
118
|
return false;
|
119
|
}
|
120
|
|
121
|
@Override
|
122
|
public String getEmailFromUsername(String username) throws Exception {
|
123
|
// TODO Auto-generated method stub
|
124
|
return null;
|
125
|
}
|
126
|
|
127
|
@Override
|
128
|
public UserProfile getUser(String userIdentifier) throws Exception {
|
129
|
// TODO Auto-generated method stub
|
130
|
return null;
|
131
|
}
|
132
|
|
133
|
public UsersDAO getUsersDao() {
|
134
|
return usersDao;
|
135
|
}
|
136
|
|
137
|
public void setUsersDao(UsersDAO usersDao) {
|
138
|
this.usersDao = usersDao;
|
139
|
}
|
140
|
|
141
|
|
142
|
}
|