1
|
package gr.uoa.di.validator.dao.users;
|
2
|
|
3
|
import gr.uoa.di.validator.dao.AbstractDAO;
|
4
|
|
5
|
import java.sql.Connection;
|
6
|
import java.sql.PreparedStatement;
|
7
|
import java.sql.ResultSet;
|
8
|
import java.sql.SQLException;
|
9
|
|
10
|
public class UserStoredDAOimpl extends AbstractDAO<UserStored> implements UserStoredDAO {
|
11
|
|
12
|
|
13
|
@Override
|
14
|
protected PreparedStatement getUpdateStatement(UserStored t, Connection con)
|
15
|
throws SQLException {
|
16
|
String query="UPDATE users SET activation_id=? WHERE activation_id=?";
|
17
|
PreparedStatement stmt = con.prepareStatement(query);
|
18
|
stmt.setString(1, null);
|
19
|
stmt.setString(2, t.getActivationId());
|
20
|
return stmt;
|
21
|
}
|
22
|
|
23
|
|
24
|
@Override
|
25
|
protected PreparedStatement getInsertStatement(UserStored t, Connection con)
|
26
|
throws SQLException {
|
27
|
String query="INSERT INTO users(email,password,activation_id) VALUES(?,?,?)";
|
28
|
PreparedStatement stmt = con.prepareStatement(query);
|
29
|
stmt.setString(1, t.getEmail());
|
30
|
stmt.setString(2, t.getPassword());
|
31
|
stmt.setString(3, t.getActivationId());
|
32
|
return stmt;
|
33
|
}
|
34
|
|
35
|
@Override
|
36
|
protected PreparedStatement getDeleteStatement(String id, Connection con)
|
37
|
throws SQLException {
|
38
|
// TODO Auto-generated method stub
|
39
|
return null;
|
40
|
}
|
41
|
|
42
|
@Override
|
43
|
protected int getLastId() throws SQLException {
|
44
|
// TODO Auto-generated method stub
|
45
|
return 0;
|
46
|
}
|
47
|
|
48
|
@Override
|
49
|
public UserStored get(String id) {
|
50
|
// TODO Auto-generated method stub
|
51
|
return null;
|
52
|
}
|
53
|
|
54
|
|
55
|
@Override
|
56
|
public boolean checkCorrectCreds(UserStored newUser) {
|
57
|
ResultSet rs = null;
|
58
|
Connection con = null;
|
59
|
PreparedStatement stmt = null;
|
60
|
logger.debug("Accessing DB to check correct credentials");
|
61
|
try {
|
62
|
con = getConnection();
|
63
|
String query="SELECT * FROM users WHERE email=? AND password=?";
|
64
|
stmt = con.prepareStatement(query);
|
65
|
stmt.setString(1,newUser.getEmail());
|
66
|
stmt.setString(2,newUser.getPassword());
|
67
|
rs = stmt.executeQuery();
|
68
|
if (rs.next()){
|
69
|
return true;
|
70
|
}
|
71
|
} catch (SQLException e) {
|
72
|
logger.error("Error while accessing DB to check correct credentials: "+e);
|
73
|
} finally {
|
74
|
if (stmt != null) {
|
75
|
try {
|
76
|
stmt.close();
|
77
|
} catch (SQLException e) {
|
78
|
logger.error("Error while accessing DB to check correct credentials: "+e);
|
79
|
}
|
80
|
}
|
81
|
}
|
82
|
return false;
|
83
|
}
|
84
|
|
85
|
|
86
|
@Override
|
87
|
public boolean isAdmin(String email) {
|
88
|
ResultSet rs = null;
|
89
|
Connection con = null;
|
90
|
PreparedStatement stmt = null;
|
91
|
logger.debug("Accessing DB to check if user is admin");
|
92
|
try {
|
93
|
con = getConnection();
|
94
|
String query="SELECT * FROM admins WHERE username=? AND level=?";
|
95
|
stmt = con.prepareStatement(query);
|
96
|
stmt.setString(1,email);
|
97
|
stmt.setString(2,"secondary");
|
98
|
rs = stmt.executeQuery();
|
99
|
if (rs.next()){
|
100
|
return true;
|
101
|
}
|
102
|
} catch (SQLException e) {
|
103
|
logger.error("Error while accessing DB to check if user is admin: "+e);
|
104
|
} finally {
|
105
|
if (stmt != null) {
|
106
|
try {
|
107
|
stmt.close();
|
108
|
} catch (SQLException e) {
|
109
|
logger.error("Error while accessing DB to check if user is admin: "+e);
|
110
|
}
|
111
|
}
|
112
|
}
|
113
|
return false;
|
114
|
}
|
115
|
|
116
|
@Override
|
117
|
public boolean isRepoAdmin(String email) {
|
118
|
ResultSet rs = null;
|
119
|
Connection con = null;
|
120
|
PreparedStatement stmt = null;
|
121
|
logger.debug("Accessing DB to check if user is repoAdmin");
|
122
|
try {
|
123
|
con = getConnection();
|
124
|
String query="SELECT * FROM admins WHERE username=? AND level=?";
|
125
|
stmt = con.prepareStatement(query);
|
126
|
stmt.setString(1,email);
|
127
|
stmt.setString(2,"secondary");
|
128
|
rs = stmt.executeQuery();
|
129
|
if (rs.next()){
|
130
|
return true;
|
131
|
}
|
132
|
} catch (SQLException e) {
|
133
|
logger.error("Error while accessing DB to check if user is repoAdmin: "+e);
|
134
|
} finally {
|
135
|
if (stmt != null) {
|
136
|
try {
|
137
|
stmt.close();
|
138
|
} catch (SQLException e) {
|
139
|
logger.error("Error while accessing DB to check if user is repoAdmin: "+e);
|
140
|
}
|
141
|
}
|
142
|
}
|
143
|
return false;
|
144
|
}
|
145
|
|
146
|
@Override
|
147
|
public boolean isActivated(String email) {
|
148
|
ResultSet rs = null;
|
149
|
Connection con = null;
|
150
|
PreparedStatement stmt = null;
|
151
|
logger.debug("Accessing DB to check if user is activated");
|
152
|
try {
|
153
|
con = getConnection();
|
154
|
String query="SELECT * FROM users WHERE email=? AND activation_id is null";
|
155
|
stmt = con.prepareStatement(query);
|
156
|
stmt.setString(1,email);
|
157
|
// stmt.setString(2,"NULL");
|
158
|
rs = stmt.executeQuery();
|
159
|
if (rs.next()){
|
160
|
return true;
|
161
|
}
|
162
|
} catch (SQLException e) {
|
163
|
logger.error("Error while accessing DB to check if user is activated "+e);
|
164
|
} finally {
|
165
|
if (stmt != null) {
|
166
|
try {
|
167
|
stmt.close();
|
168
|
} catch (SQLException e) {
|
169
|
logger.error("Error while accessing DB to check if user is activated "+e);
|
170
|
}
|
171
|
}
|
172
|
}
|
173
|
return false;
|
174
|
}
|
175
|
|
176
|
|
177
|
@Override
|
178
|
public boolean userExists(String email) {
|
179
|
ResultSet rs = null;
|
180
|
Connection con = null;
|
181
|
PreparedStatement stmt = null;
|
182
|
logger.debug("Accessing DB to check if user "+email+" exists");
|
183
|
try {
|
184
|
con = getConnection();
|
185
|
// con.setAutoCommit(false);
|
186
|
// con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
|
187
|
String query="SELECT * FROM users WHERE email=?";
|
188
|
stmt = con.prepareStatement(query);
|
189
|
stmt.setString(1,email);
|
190
|
rs = stmt.executeQuery();
|
191
|
if (rs.next()){
|
192
|
logger.debug("user exists");
|
193
|
return true;
|
194
|
}
|
195
|
} catch (SQLException e) {
|
196
|
logger.error("Error while accessing DB to check if user exists: "+e);
|
197
|
} finally {
|
198
|
if (stmt != null) {
|
199
|
try {
|
200
|
stmt.close();
|
201
|
} catch (SQLException e) {
|
202
|
logger.error("Error while accessing DB to check if user exists: "+e);
|
203
|
}
|
204
|
}
|
205
|
}
|
206
|
return false;
|
207
|
}
|
208
|
|
209
|
|
210
|
@Override
|
211
|
public void prepareResetPassword(String uuid, String email) {
|
212
|
Connection con = null;
|
213
|
PreparedStatement stmt = null;
|
214
|
logger.debug("Accessing DB to prepare reset password");
|
215
|
try {
|
216
|
con = getConnection();
|
217
|
String query="UPDATE users SET activation_id=? WHERE email=?";;
|
218
|
stmt = con.prepareStatement(query);
|
219
|
stmt.setString(1,uuid);
|
220
|
stmt.setString(2,email);
|
221
|
stmt.executeUpdate();
|
222
|
} catch (SQLException e) {
|
223
|
logger.error("Error while accessing DB to prepare reset password: "+e);
|
224
|
} finally {
|
225
|
if (stmt != null) {
|
226
|
try {
|
227
|
stmt.close();
|
228
|
} catch (SQLException e) {
|
229
|
logger.error("Error while accessing DB to prepare reset password: "+e);
|
230
|
}
|
231
|
}
|
232
|
}
|
233
|
}
|
234
|
|
235
|
|
236
|
@Override
|
237
|
public void ResetPassword(String uuid, String password) {
|
238
|
Connection con = null;
|
239
|
PreparedStatement stmt = null;
|
240
|
logger.debug("Accessing DB to reset password");
|
241
|
try {
|
242
|
con = getConnection();
|
243
|
String query="UPDATE users SET password=? WHERE activation_id=?";;
|
244
|
stmt = con.prepareStatement(query);
|
245
|
stmt.setString(1,password);
|
246
|
stmt.setString(2,uuid);
|
247
|
stmt.executeUpdate();
|
248
|
} catch (SQLException e) {
|
249
|
logger.error("Error while accessing DB to reset password: "+e);
|
250
|
} finally {
|
251
|
if (stmt != null) {
|
252
|
try {
|
253
|
stmt.close();
|
254
|
} catch (SQLException e) {
|
255
|
logger.error("Error while accessing DB to reset password: "+e);
|
256
|
}
|
257
|
}
|
258
|
}
|
259
|
}
|
260
|
|
261
|
}
|