1
|
package eu.dnetlib.goldoa.service.dao;
|
2
|
|
3
|
import eu.dnetlib.goldoa.domain.*;
|
4
|
import org.apache.commons.codec.digest.DigestUtils;
|
5
|
import org.apache.commons.logging.Log;
|
6
|
import org.apache.commons.logging.LogFactory;
|
7
|
import org.hibernate.Criteria;
|
8
|
import org.hibernate.criterion.Order;
|
9
|
import org.hibernate.criterion.Restrictions;
|
10
|
import org.hibernate.transform.Transformers;
|
11
|
import org.springframework.beans.factory.annotation.Autowired;
|
12
|
import org.springframework.stereotype.Repository;
|
13
|
|
14
|
import java.math.BigInteger;
|
15
|
import java.sql.Timestamp;
|
16
|
import java.util.List;
|
17
|
import java.util.UUID;
|
18
|
|
19
|
/**
|
20
|
* Created by antleb on 3/13/15.
|
21
|
*/
|
22
|
@Repository
|
23
|
public class UserDAO extends AbstractDao<String, User> {
|
24
|
private Log LOGGER = LogFactory.getLog(UserDAO.class);
|
25
|
|
26
|
/**
|
27
|
* Returns the person with the given id. Objects of different type are placeholders containing only their id.
|
28
|
* Objects representing relations are fully initialized, with placeholders for other main entities.
|
29
|
*
|
30
|
* @param userId
|
31
|
* @return
|
32
|
*/
|
33
|
public User getUserById(String userId) {
|
34
|
return (User) createEntityCriteria().add(Restrictions.eq("id", userId)).list().get(0);
|
35
|
}
|
36
|
|
37
|
/**
|
38
|
* Returns the person with the given email. Objects of different type are placeholders containing only their id.
|
39
|
* Objects representing relations are fully initialized, with placeholders for other main entities.
|
40
|
*
|
41
|
* @param email
|
42
|
* @return
|
43
|
*/
|
44
|
@SuppressWarnings("unchecked")
|
45
|
public User getUserByEmail(String email) {
|
46
|
List<User> users = createEntityCriteria().add(Restrictions.eq("email", email)).list();
|
47
|
if (users.size() == 0)
|
48
|
return null;
|
49
|
User u = users.get(0);
|
50
|
return u;
|
51
|
}
|
52
|
|
53
|
@SuppressWarnings("unchecked")
|
54
|
public List<Role> getUserRoles(String email) {
|
55
|
Criteria cr = getSession().createCriteria(UserRole.class);
|
56
|
return cr.add(Restrictions.eq("pk.user",email))
|
57
|
.setResultTransformer( Transformers.aliasToBean(Role.class)).list();
|
58
|
}
|
59
|
|
60
|
public void saveToken(final String email, final String token, final int tokenTTL) {
|
61
|
AccountAction acc_action = new AccountAction();
|
62
|
acc_action.setUser(getUserByEmail(email));
|
63
|
acc_action.setType("activation");
|
64
|
acc_action.setToken(token);
|
65
|
acc_action.setDate(new Timestamp(System.currentTimeMillis()));
|
66
|
acc_action.setExpires(new Timestamp(System.currentTimeMillis() + 1000L * 60L * 60L * 24L * tokenTTL));
|
67
|
BigInteger id = (BigInteger) getSession().createSQLQuery("select nextval('account_action_seq') as id").list().get(0);
|
68
|
acc_action.setId(id);
|
69
|
getSession().persist(acc_action);
|
70
|
}
|
71
|
|
72
|
@SuppressWarnings("unchecked")
|
73
|
public boolean verifyLogin(String email, String password) {
|
74
|
|
75
|
List<User> users = createEntityCriteria().add(Restrictions.eq("email", email))
|
76
|
.add(Restrictions.eq("password", DigestUtils.md5Hex(password))).list();
|
77
|
|
78
|
if (users.size() != 0)
|
79
|
return true;
|
80
|
return false;
|
81
|
}
|
82
|
|
83
|
@SuppressWarnings("unchecked")
|
84
|
public boolean verifyToken(final String email, final String token) {
|
85
|
Criteria cr = getSession().createCriteria(AccountAction.class);
|
86
|
List<AccountAction> ac = cr.createAlias("user","u").add(Restrictions.eq("u.email", email))
|
87
|
.add(Restrictions.eq("token", token)).list();
|
88
|
if (ac.size() > 0)
|
89
|
return true;
|
90
|
return false;
|
91
|
}
|
92
|
|
93
|
public void activateUser(final String email) {
|
94
|
User user = (User) createEntityCriteria().add(Restrictions.eq("email", email)).list().get(0);
|
95
|
user.setActive(true);
|
96
|
getSession().update(user);
|
97
|
}
|
98
|
|
99
|
public void deleteToken(final String email, final String token) {
|
100
|
Criteria cr = getSession().createCriteria(AccountAction.class);
|
101
|
AccountAction ac = (AccountAction) cr.createAlias("user","u").add(Restrictions.eq("u.email", email))
|
102
|
.add(Restrictions.eq("token", token))
|
103
|
.add(Restrictions.eq("type", "activation")).list().get(0);
|
104
|
getSession().delete(ac);
|
105
|
}
|
106
|
|
107
|
@SuppressWarnings("unchecked")
|
108
|
public boolean updatePassword(final String newPassword, final String email) {
|
109
|
List<User> rs = createEntityCriteria().add(Restrictions.eq("email", email)).list();
|
110
|
if (rs.get(0) != null) {
|
111
|
User user = rs.get(0);
|
112
|
user.setPassword(DigestUtils.md5Hex(newPassword));
|
113
|
User user_merged = (User) getSession().merge(user);
|
114
|
return true;
|
115
|
}
|
116
|
return false;
|
117
|
|
118
|
}
|
119
|
|
120
|
@SuppressWarnings("unchecked")
|
121
|
public List<Role> getRoles() {
|
122
|
return (List<Role>)getSession().createCriteria(Role.class).list();
|
123
|
}
|
124
|
|
125
|
@SuppressWarnings("unchecked")
|
126
|
public void saveUser(final User user) {
|
127
|
User u = getUserByEmail(user.getEmail());
|
128
|
if (u != null) {
|
129
|
if(u.getPublisher() != null && u.getPublisher() == user.getPublisher())
|
130
|
user.setPublisher(u.getPublisher());
|
131
|
if(user.getPassword() == null)
|
132
|
user.setPassword(u.getPassword());
|
133
|
if (user.getPassword() != null && !user.getPassword().equals(u.getPassword()))
|
134
|
user.setPassword(DigestUtils.md5Hex(user.getPassword()));
|
135
|
if(u.isActive())
|
136
|
user.setActive(true);
|
137
|
User user_merged = (User) getSession().merge(user);
|
138
|
}else
|
139
|
persist(user);
|
140
|
}
|
141
|
|
142
|
@SuppressWarnings("unchecked")
|
143
|
public List<User> getUsersByRole(String role_id) {
|
144
|
return createEntityCriteria()
|
145
|
.createAlias("roles" , "r")
|
146
|
.add(Restrictions.eq("r.pk.role.id",role_id))
|
147
|
.list();
|
148
|
}
|
149
|
|
150
|
public List<User> getModerators() {
|
151
|
return getUsersByRole("moderator");
|
152
|
}
|
153
|
|
154
|
public List<User> getAccountingOfficers() {
|
155
|
return getUsersByRole("accounting");
|
156
|
}
|
157
|
|
158
|
public Role getRole(final String roleId) {
|
159
|
Criteria cr = getSession().createCriteria(Role.class);
|
160
|
cr.add(Restrictions.eq("id", roleId));
|
161
|
return (Role)cr.list().get(0);
|
162
|
}
|
163
|
|
164
|
public void deactivateUser(String email) {
|
165
|
User user = (User) createEntityCriteria().add(Restrictions.eq("email", email)).list().get(0);
|
166
|
user.setActive(false);
|
167
|
User user_merged = (User) getSession().merge(user);
|
168
|
}
|
169
|
|
170
|
public void acceptUserRole(String email, String roleId) {
|
171
|
User user = getUserByEmail(email);
|
172
|
for (UserRole ur : user.getRoles()) {
|
173
|
if (ur.getPk().getRole().getId().equals(roleId)) {
|
174
|
ur.setApproved(true);
|
175
|
getSession().update(user);
|
176
|
}
|
177
|
}
|
178
|
}
|
179
|
|
180
|
public void rejectUserRole(String email, String roleId) {
|
181
|
User user = getUserByEmail(email);
|
182
|
UserRole ur = null;
|
183
|
for (UserRole u : user.getRoles()) {
|
184
|
if (u.getPk().getRole().getId().equals(roleId))
|
185
|
ur = u;
|
186
|
}
|
187
|
user.getRoles().remove(ur);
|
188
|
getSession().update(user);
|
189
|
getSession().delete(ur);
|
190
|
}
|
191
|
|
192
|
|
193
|
@SuppressWarnings("unchecked")
|
194
|
public List<User> getUsers() {
|
195
|
return createEntityCriteria()
|
196
|
.addOrder(Order.asc("email"))
|
197
|
// .setCacheable(true)
|
198
|
// .setCacheRegion("user")
|
199
|
.list();
|
200
|
}
|
201
|
|
202
|
public User createInActiveResearcher(String firstname, String lastname, String email, Organization organization) {
|
203
|
User user = new User();
|
204
|
user.setFirstname(firstname);
|
205
|
user.setLastname(lastname);
|
206
|
user.setEmail(email);
|
207
|
user.setPassword(DigestUtils.md5Hex(UUID.randomUUID().toString()));
|
208
|
|
209
|
UserRole personRole = new UserRole();
|
210
|
UserRolePK urPk = new UserRolePK();
|
211
|
urPk.setUser(user);
|
212
|
urPk.setRole(new Role("researcher","Researcher"));
|
213
|
personRole.setPk(urPk);
|
214
|
|
215
|
Affiliation affiliation = new Affiliation();
|
216
|
affiliation.setOrganization(organization);
|
217
|
affiliation.getUsers().add(user);
|
218
|
user.getAffiliations().add(affiliation);
|
219
|
|
220
|
saveUser(user);
|
221
|
return user;
|
222
|
}
|
223
|
}
|