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