1
|
package eu.dnetlib.goldoa.service.dao;
|
2
|
|
3
|
import eu.dnetlib.goldoa.domain.AccountAction;
|
4
|
import eu.dnetlib.goldoa.domain.UserRole;
|
5
|
import eu.dnetlib.goldoa.domain.Role;
|
6
|
import eu.dnetlib.goldoa.domain.User;
|
7
|
|
8
|
import org.apache.commons.codec.digest.DigestUtils;
|
9
|
import org.hibernate.Criteria;
|
10
|
import org.hibernate.Hibernate;
|
11
|
import org.hibernate.criterion.Restrictions;
|
12
|
import org.hibernate.type.StringType;
|
13
|
import org.springframework.stereotype.Repository;
|
14
|
|
15
|
import java.sql.ResultSet;
|
16
|
import java.sql.SQLException;
|
17
|
import java.sql.Timestamp;
|
18
|
import java.util.ArrayList;
|
19
|
import java.util.List;
|
20
|
|
21
|
/**
|
22
|
* Created by antleb on 3/13/15.
|
23
|
*/
|
24
|
@Repository
|
25
|
public class UserDAO extends AbstractDao<String, User> {
|
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
|
return users.get(0);
|
50
|
}
|
51
|
|
52
|
public void saveToken(final String email, final String token, final int tokenTTL) {
|
53
|
AccountAction acc_action = new AccountAction();
|
54
|
acc_action.setUser(getUserByEmail(email));
|
55
|
acc_action.setType("activation");
|
56
|
acc_action.setToken(token);
|
57
|
acc_action.setDate(new Timestamp(System.currentTimeMillis()));
|
58
|
acc_action.setExpires(new Timestamp(System.currentTimeMillis() + 1000L * 60L * 60L * 24L * tokenTTL));
|
59
|
getSession().persist(acc_action);
|
60
|
}
|
61
|
|
62
|
public boolean verifyLogin(String email, String password) {
|
63
|
List users = createEntityCriteria().add(Restrictions.eq("email", email)).list();
|
64
|
// .add(Restrictions.eq("password", DigestUtils.md5Hex(password))).list();
|
65
|
|
66
|
if (users.size() != 0)
|
67
|
return true;
|
68
|
return false;
|
69
|
}
|
70
|
|
71
|
@SuppressWarnings("unchecked")
|
72
|
public boolean verifyToken(final String email, final String token) {
|
73
|
Criteria cr = getSession().createCriteria(AccountAction.class);
|
74
|
List<AccountAction> ac = cr.add(Restrictions.eq("email", email))
|
75
|
.add(Restrictions.eq("token", token)).list();
|
76
|
if (ac.size() > 0)
|
77
|
return true;
|
78
|
return false;
|
79
|
}
|
80
|
|
81
|
public void activateUser(final String email) {
|
82
|
User user = (User) createEntityCriteria().add(Restrictions.eq("email", email)).list().get(0);
|
83
|
user.setActive(true);
|
84
|
getSession().update(user);
|
85
|
}
|
86
|
|
87
|
public void deleteToken(final String email, final String token) {
|
88
|
Criteria cr = getSession().createCriteria(AccountAction.class);
|
89
|
AccountAction ac = (AccountAction) cr.add(Restrictions.eq("email", email))
|
90
|
.add(Restrictions.eq("token", token))
|
91
|
.add(Restrictions.eq("type", "activation")).list().get(0);
|
92
|
getSession().delete(ac);
|
93
|
}
|
94
|
|
95
|
@SuppressWarnings("unchecked")
|
96
|
public boolean updatePassword(final String newPassword, final String email) {
|
97
|
List<User> rs = createEntityCriteria().add(Restrictions.eq("email", email)).list();
|
98
|
if (rs.get(0) != null) {
|
99
|
User user = rs.get(0);
|
100
|
user.setPassword(DigestUtils.md5Hex(newPassword));
|
101
|
getSession().update(user);
|
102
|
return true;
|
103
|
}
|
104
|
return false;
|
105
|
|
106
|
}
|
107
|
|
108
|
@SuppressWarnings("unchecked")
|
109
|
public List<Role> getRoles() {
|
110
|
Criteria cr = getSession().createCriteria(Role.class);
|
111
|
return cr.list();
|
112
|
}
|
113
|
|
114
|
public void saveUser(final User user) {
|
115
|
for (UserRole ur : user.getRoles()) {
|
116
|
Role r = ur.getPk().getRole();
|
117
|
getSession().persist(r);
|
118
|
}
|
119
|
persist(user);
|
120
|
}
|
121
|
|
122
|
//TODO MOVE TO PROJECT DAO
|
123
|
/*public void removeProjectCoordinators(final String email) {
|
124
|
Criteria cr = getSession().createCriteria(ProjectCoordinator.class);
|
125
|
ProjectCoordinator pc = (ProjectCoordinator) cr.add(Restrictions.eq("email",email)).list().get(0);
|
126
|
getSession().delete(pc);
|
127
|
}*/
|
128
|
|
129
|
/*public void saveProjectCoordinators(final String email, final List<ProjectCoordinator> projectCoordinators) {
|
130
|
User user = (User) createEntityCriteria().add(Restrictions.eq("email", email)).list().get(0);
|
131
|
user.setProjectCoordinators(projectCoordinators);
|
132
|
persist(user);
|
133
|
}*/
|
134
|
|
135
|
|
136
|
|
137
|
/*public void saveAffiliations(final User user, final List<Affiliation> affiliations) {
|
138
|
user.setAffiliations(affiliations);
|
139
|
persist(user);
|
140
|
}*/
|
141
|
|
142
|
|
143
|
@SuppressWarnings("unchecked")
|
144
|
public List<User> getUsersByRole(String role_id) {
|
145
|
|
146
|
Criteria cr = getSession().createCriteria(Role.class);
|
147
|
Role role = (Role) cr.add(Restrictions.eq("id", role_id)).list().get(0);
|
148
|
cr = getSession().createCriteria(UserRole.class);
|
149
|
|
150
|
List<UserRole> rs = cr.add(Restrictions.eq("approved", true))
|
151
|
.add(Restrictions.eq("pk.role", role)).list();
|
152
|
|
153
|
|
154
|
List<User> users = new ArrayList<>();
|
155
|
for (UserRole ur : rs)
|
156
|
users.add(ur.getPk().getUser());
|
157
|
return users;
|
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
|
getSession().update(user);
|
178
|
}
|
179
|
|
180
|
public void acceptUserRole(String email, String roleId) {
|
181
|
changeRoleApprovedStatus(email, roleId, true);
|
182
|
}
|
183
|
|
184
|
public void rejectUserRole(String email, String roleId) {
|
185
|
changeRoleApprovedStatus(email, roleId, false);
|
186
|
}
|
187
|
|
188
|
public void changeRoleApprovedStatus(String email, String roleId, boolean status) {
|
189
|
Criteria cr = getSession().createCriteria(UserRole.class);
|
190
|
User user = getUserByEmail(email);
|
191
|
Role role = getRole(roleId);
|
192
|
UserRole ur = (UserRole) cr.add(Restrictions.eq("pk.user", user))
|
193
|
.add(Restrictions.eq("pk.role", role)).list().get(0);
|
194
|
|
195
|
ur.setApproved(status);
|
196
|
getSession().update(ur);
|
197
|
}
|
198
|
|
199
|
@SuppressWarnings("unchecked")
|
200
|
public List<User> getUsers() {
|
201
|
return createEntityCriteria().list();
|
202
|
}
|
203
|
}
|