01: package auth;
02:
03: import auth.Authenticator;
04: import util.Logger;
05:
06: import java.sql.SQLException;
07: import java.util.Arrays;
08:
09: import velosurf.context.DBReference;
10: import velosurf.context.RowIterator;
11: import velosurf.sql.PooledPreparedStatement;
12: import velosurf.sql.Database;
13: import velosurf.tools.VelosurfTool;
14: import org.apache.velocity.tools.view.context.ViewContext;
15:
16: public class DBAuthenticator extends Authenticator {
17:
18: private static Database sDb = null;
19:
20: public void init(Object initData) {
21: super .init(initData);
22:
23: // init only if there was no error in super class
24: if (initData instanceof ViewContext) {
25: if (sDb == null) {
26: sDb = VelosurfTool
27: .getDefaultConnection(((ViewContext) initData)
28: .getServletContext());
29: }
30: }
31: }
32:
33: protected String getPassword(String login) {
34: try {
35: PooledPreparedStatement stmt = sDb
36: .prepare("select password from user where login = ?");
37: RowIterator ri = stmt.query(Arrays
38: .asList(new String[] { login }));
39: while (ri.hasNext()) {
40: return "" + ri.get("password");
41: }
42: } catch (SQLException sqle) {
43: Logger.log(sqle);
44: }
45: return null;
46: }
47:
48: protected Object getUser(String login) {
49: try {
50: DBReference dbr = new DBReference(sDb);
51: dbr.put("login", login);
52: return sDb.getAttribute("user_by_login").fetch(dbr);
53: } catch (SQLException sqle) {
54: Logger.log(sqle);
55: }
56: return null;
57: }
58:
59: }
|