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