svn-gvsig-desktop / trunk / libraries / libAuthenticationService / src / org / gvsig / authentication / Authentication.java @ 4319
History | View | Annotate | Download (9.41 KB)
1 |
package org.gvsig.authentication; |
---|---|
2 |
|
3 |
import java.sql.ResultSet; |
4 |
import java.sql.SQLException; |
5 |
import java.sql.Statement; |
6 |
import java.util.Hashtable; |
7 |
import java.util.Vector; |
8 |
|
9 |
import org.gvsig.authentication.db.DatabaseHandler; |
10 |
import org.gvsig.authentication.db.QueryBuilder; |
11 |
import org.gvsig.authentication.exception.DBConnectionException; |
12 |
import org.gvsig.authentication.exception.InvalidUserException; |
13 |
import org.gvsig.authentication.utils.LogManager; |
14 |
import org.gvsig.authentication.utils.Logger; |
15 |
|
16 |
|
17 |
/**
|
18 |
* Provides the functionality to process the authentication and the authorization in the DB.
|
19 |
* @author laura
|
20 |
*/
|
21 |
public class Authentication { |
22 |
|
23 |
private DatabaseHandler dbHandler;
|
24 |
private Logger m_log = LogManager.getLogManager().getLogger(Authentication.class.getName()); |
25 |
|
26 |
public Authentication() {
|
27 |
dbHandler = new DatabaseHandler();
|
28 |
} |
29 |
|
30 |
/**
|
31 |
* Tries to log in the db with the info sent by the user.
|
32 |
* @param userName
|
33 |
* @param password
|
34 |
* @return
|
35 |
*/
|
36 |
public int login(String userName, String password) |
37 |
{ |
38 |
Statement stm = null; |
39 |
ResultSet rs = null; |
40 |
String query = null; |
41 |
int userId = -1; |
42 |
|
43 |
try {
|
44 |
|
45 |
dbHandler.initConnection("default");
|
46 |
query = QueryBuilder.getLoginQuery(userName, password); |
47 |
stm = dbHandler.getConnection().createStatement(); |
48 |
rs = stm.executeQuery(query); |
49 |
if (rs.first()){
|
50 |
userId = rs.getInt(QueryBuilder.USER_ID); |
51 |
} |
52 |
return userId;
|
53 |
|
54 |
} catch (SQLException e) { |
55 |
m_log.error("DatabaseHandler.login()." ,e);
|
56 |
e.printStackTrace(); |
57 |
return userId;
|
58 |
} catch (DBConnectionException e) {
|
59 |
m_log.error("DatabaseHandler.login()." ,e);
|
60 |
e.printStackTrace(); |
61 |
return userId;
|
62 |
}finally{
|
63 |
if (dbHandler.getConnection() != null){ |
64 |
try{
|
65 |
stm.close(); |
66 |
rs.close(); |
67 |
dbHandler.releaseConnection("default");
|
68 |
}catch(Exception fin){ |
69 |
m_log.error("",fin);
|
70 |
fin.printStackTrace(); |
71 |
return userId;
|
72 |
} |
73 |
} |
74 |
} |
75 |
} |
76 |
|
77 |
private Vector getRoles(String userId) |
78 |
{ |
79 |
Statement stm = null; |
80 |
ResultSet rs = null; |
81 |
String query = null; |
82 |
int roleId = -1; |
83 |
Vector roles = new Vector(); |
84 |
query = QueryBuilder.getUserRolesIds(userId); |
85 |
|
86 |
try {
|
87 |
|
88 |
stm = dbHandler.getConnection().createStatement(); |
89 |
rs = stm.executeQuery(query); |
90 |
// rs contains the roles ids to which this user belongs
|
91 |
// at this moment we are going to simplify this to one role per user
|
92 |
if (rs.first())
|
93 |
{ |
94 |
do{
|
95 |
roleId = rs.getInt(QueryBuilder.USER_ROLES_ROLES_ID_COL); |
96 |
if(roleId > -1) |
97 |
{ |
98 |
roles.add(""+roleId);
|
99 |
} |
100 |
}while(rs.next());
|
101 |
} |
102 |
|
103 |
} catch (SQLException e) { |
104 |
// TODO Auto-generated catch block
|
105 |
e.printStackTrace(); |
106 |
}finally{
|
107 |
if (dbHandler.getConnection() != null){ |
108 |
try{
|
109 |
rs.close(); |
110 |
stm.close(); |
111 |
}catch(Exception fin){ |
112 |
m_log.error("",fin);
|
113 |
fin.printStackTrace(); |
114 |
return null; |
115 |
} |
116 |
} |
117 |
} |
118 |
return roles;
|
119 |
} |
120 |
|
121 |
/**
|
122 |
* gets the gvSIG config file
|
123 |
* @param userName
|
124 |
* @param password
|
125 |
* @return a String with the gvSIG confog file
|
126 |
* @throws InvalidUserException
|
127 |
*/
|
128 |
public Hashtable getGvSigConfig(String userName, String password) |
129 |
throws InvalidUserException
|
130 |
{ |
131 |
int userId = login(userName, password);
|
132 |
if ( userId > -1){ |
133 |
return getGvSigConfig(userId);
|
134 |
} |
135 |
else{
|
136 |
throw new InvalidUserException("Authentication ERROR: Invalid user trying to get gvSIG file"); |
137 |
} |
138 |
} |
139 |
|
140 |
/**
|
141 |
* Gets the gvSig configuration file associated to the user role.
|
142 |
* First checks if the user/password is correct.
|
143 |
* @param userName
|
144 |
* @param password
|
145 |
* @return
|
146 |
*/
|
147 |
private Hashtable getGvSigConfig(int userId) |
148 |
{ |
149 |
Statement stm = null; |
150 |
ResultSet rs = null; |
151 |
String query = null; |
152 |
Vector roles = new Vector(); |
153 |
Hashtable ht = new Hashtable(); |
154 |
try {
|
155 |
|
156 |
dbHandler.initConnection("default");
|
157 |
roles = getRoles(String.valueOf(userId));
|
158 |
if (roles.size() > 0 )//if (roleId > -1) |
159 |
{ |
160 |
query = QueryBuilder.getGvSigConfigDataQuery(roles); |
161 |
stm = dbHandler.getConnection().createStatement(); |
162 |
rs = stm.executeQuery(query); |
163 |
if (rs.first())
|
164 |
{ |
165 |
do{
|
166 |
ht.put(rs.getString(QueryBuilder.GRANTS_NAME_COL),rs.getString(QueryBuilder.GRANTS_DATA_COL)); |
167 |
}while (rs.next());
|
168 |
} |
169 |
} |
170 |
return ht;
|
171 |
|
172 |
} catch (SQLException e) { |
173 |
m_log.error("DatabaseHandler.getGvSigConfig()." ,e);
|
174 |
e.printStackTrace(); |
175 |
return null; |
176 |
} catch (DBConnectionException e) {
|
177 |
m_log.error("DatabaseHandler.getGvSigConfig()." ,e);
|
178 |
e.printStackTrace(); |
179 |
return null; |
180 |
}finally{
|
181 |
if (dbHandler.getConnection() != null){ |
182 |
try{
|
183 |
rs.close(); |
184 |
stm.close(); |
185 |
dbHandler.releaseConnection("default");
|
186 |
}catch(Exception fin){ |
187 |
m_log.error("",fin);
|
188 |
fin.printStackTrace(); |
189 |
return null; |
190 |
} |
191 |
} |
192 |
} |
193 |
} |
194 |
|
195 |
/**
|
196 |
* Returns mapserver map file for this user
|
197 |
* @param userName
|
198 |
* @param password
|
199 |
* @return
|
200 |
* @throws InvalidUserException
|
201 |
*/
|
202 |
public String getMapFile(String userName, String password) |
203 |
throws InvalidUserException
|
204 |
{ |
205 |
int userId = login(userName, password);
|
206 |
if (userId > -1){ |
207 |
return getMapFile(String.valueOf(userId)); |
208 |
} |
209 |
else{
|
210 |
throw new InvalidUserException("Authentication ERROR: Invalid user trying to get gvSIG file"); |
211 |
} |
212 |
} |
213 |
/**
|
214 |
* Gets the mapserver map file associated to the user role.
|
215 |
* First checks if the user/password is correct.
|
216 |
* @param userName
|
217 |
* @param password
|
218 |
* @return
|
219 |
*/
|
220 |
private String getMapFile(String userId) |
221 |
{ |
222 |
Statement stm = null; |
223 |
ResultSet rs = null; |
224 |
String query = null; |
225 |
Vector roles = new Vector(); |
226 |
try {
|
227 |
|
228 |
dbHandler.initConnection("default");
|
229 |
roles = getRoles(userId); |
230 |
|
231 |
if (roles.size() > 0 )//if (roleId > -1) |
232 |
{ |
233 |
query = QueryBuilder.getMapConfigDataQuery(roles); |
234 |
stm = dbHandler.getConnection().createStatement(); |
235 |
rs = stm.executeQuery(query); |
236 |
// esta ordenado por prioridad descendente, el ultimo es el de prioridad mas alta
|
237 |
if (rs.last())
|
238 |
{ |
239 |
return rs.getString(QueryBuilder.GRANTS_DATA_COL);
|
240 |
} |
241 |
} |
242 |
return ""; |
243 |
|
244 |
} catch (SQLException e) { |
245 |
m_log.error("DatabaseHandler.getGvSigConfig()." ,e);
|
246 |
e.printStackTrace(); |
247 |
return ""; |
248 |
} catch (DBConnectionException e) {
|
249 |
m_log.error("DatabaseHandler.getGvSigConfig()." ,e);
|
250 |
e.printStackTrace(); |
251 |
return ""; |
252 |
}finally{
|
253 |
if (dbHandler.getConnection() != null){ |
254 |
try{
|
255 |
dbHandler.releaseConnection("default");
|
256 |
rs.close(); |
257 |
stm.close(); |
258 |
}catch(Exception fin){ |
259 |
m_log.error("",fin);
|
260 |
fin.printStackTrace(); |
261 |
return ""; |
262 |
} |
263 |
} |
264 |
} |
265 |
} |
266 |
/**
|
267 |
* Gets all the configuration associated to this user/password
|
268 |
* returns the gvSIG config file and the mapserver map file
|
269 |
* First checks if the user/password is correct.
|
270 |
* @param userName
|
271 |
* @param password
|
272 |
* @return
|
273 |
*/
|
274 |
public Hashtable getConfig(String userName, String password) |
275 |
throws InvalidUserException
|
276 |
{ |
277 |
Hashtable ht = new Hashtable(); |
278 |
Hashtable configTable = new Hashtable(); |
279 |
String query;
|
280 |
int userId;
|
281 |
Statement stm = null; |
282 |
ResultSet rs = null; |
283 |
String mapserverFile = ""; |
284 |
String wms = ""; |
285 |
Vector roles = new Vector(); |
286 |
try {
|
287 |
|
288 |
userId = login(userName, password); |
289 |
|
290 |
if(userId > -1){ |
291 |
|
292 |
dbHandler.initConnection("default");
|
293 |
roles = getRoles(String.valueOf(userId));
|
294 |
if(roles.size() > 0 ) //if (roleId > -1) |
295 |
{ |
296 |
query = QueryBuilder.getMapConfigDataQuery(roles); |
297 |
stm = dbHandler.getConnection().createStatement(); |
298 |
rs = stm.executeQuery(query); |
299 |
//el ultimo es del role de este usuario con mayor prioridad
|
300 |
if (rs.last())
|
301 |
{ |
302 |
mapserverFile = rs.getString(QueryBuilder.GRANTS_DATA_COL); |
303 |
} |
304 |
rs.close(); |
305 |
rs = null;
|
306 |
|
307 |
query = QueryBuilder.getWMSQuery(roles); |
308 |
stm = dbHandler.getConnection().createStatement(); |
309 |
rs = stm.executeQuery(query); |
310 |
//el ultimo es del role de este usuario con mayor prioridad
|
311 |
if (rs.last())
|
312 |
{ |
313 |
wms = rs.getString(QueryBuilder.GRANTS_DATA_COL); |
314 |
} |
315 |
rs.close(); |
316 |
rs = null;
|
317 |
|
318 |
query = QueryBuilder.getGvSigConfigDataQuery(roles); |
319 |
m_log.debug("getGvSigConfigDataQuery(roles): " + query);
|
320 |
rs = stm.executeQuery(query); |
321 |
if (rs.first())
|
322 |
{ |
323 |
do{
|
324 |
configTable.put(rs.getString(QueryBuilder.GRANTS_NAME_COL),rs.getString(QueryBuilder.GRANTS_DATA_COL)); |
325 |
}while (rs.next());
|
326 |
} |
327 |
} |
328 |
ht.put("gvsig_config", configTable);
|
329 |
ht.put("mapserver_file", mapserverFile);
|
330 |
ht.put("wms", wms);
|
331 |
} |
332 |
else{
|
333 |
throw new InvalidUserException ("Authentication Error: Invalid user."); |
334 |
} |
335 |
return ht;
|
336 |
|
337 |
} catch (SQLException e) { |
338 |
m_log.error("Authentication.getConfig()." ,e);
|
339 |
e.printStackTrace(); |
340 |
return null; |
341 |
} catch (DBConnectionException e) {
|
342 |
m_log.error("Authentication.getConfig()." ,e);
|
343 |
e.printStackTrace(); |
344 |
return null; |
345 |
} catch (Exception e) { |
346 |
m_log.error("Authentication.getConfig()." ,e);
|
347 |
e.printStackTrace(); |
348 |
return null; |
349 |
}finally{
|
350 |
if (dbHandler.getConnection() != null){ |
351 |
try{
|
352 |
dbHandler.releaseConnection("default");
|
353 |
rs.close(); |
354 |
stm.close(); |
355 |
}catch(Exception fin){ |
356 |
m_log.error("",fin);
|
357 |
fin.printStackTrace(); |
358 |
return null; |
359 |
} |
360 |
} |
361 |
} |
362 |
|
363 |
} |
364 |
} |