01: package dinamica.security;
02:
03: import java.sql.Connection;
04: import java.util.HashMap;
05: import javax.servlet.http.HttpServletRequest;
06: import javax.sql.DataSource;
07:
08: import dinamica.*;
09:
10: /**
11: * This validator can be used to detect if there are
12: * any related records in another table before trying to
13: * delete a certain record. Returns TRUE if there are no
14: * related records meaning that the record can be deleted.
15: * You will need to call this validator for every referential
16: * integrity check before deleting a record.
17: * <br><br>
18: * Rrequires the following custom attributes:<br>
19: * <ul>
20: * <li> sql: query to find any related record. You may use field makers
21: * that will be replaced by the corresponding request parameters passed
22: * as a recordset to the isValid method.
23: * </ul>
24: *
25: * (c) 2004 Martin Cordova<br>
26: * This code is released under the LGPL license<br>
27: * Dinamica Framework - http://www.martincordova.com
28: * @author Martin Cordova (dinamica@martincordova.com)
29: * */
30: public class PasswordNotUsed extends AbstractValidator {
31:
32: /* (non-Javadoc)
33: * @see dinamica.AbstractValidator#isValid(javax.servlet.http.HttpServletRequest, dinamica.Recordset, java.util.HashMap)
34: */
35: public boolean isValid(HttpServletRequest req,
36: Recordset inputParams, HashMap<String, String> attribs)
37: throws Throwable {
38:
39: boolean flag = true;
40:
41: //get security datasource name
42: String jndiName = (String) getContext().getAttribute(
43: "dinamica.security.datasource");
44: if (jndiName == null)
45: throw new Throwable(
46: "Context attribute [dinamica.security.datasource] is null, check your security filter configuration.");
47:
48: //get datasource and DB connection
49: DataSource ds = Jndi.getDataSource(jndiName);
50: Connection conn = ds.getConnection();
51: this .setConnection(conn);
52:
53: try {
54: //get db channel
55: Db db = getDb();
56:
57: //detect if sql parameter was passed to the validator
58: boolean bSql = attribs.containsKey("sql");
59:
60: if (!bSql) {
61: throw new Throwable("[" + this .getClass().getName()
62: + "] Missing attribute [sql] in validator.xml");
63: } else {
64:
65: //read config
66: String query = (String) attribs.get("sql");
67:
68: //load template and replace parameter values
69: String sql = getResource(query);
70: sql = getSQL(sql, inputParams);
71:
72: //execute sql
73: Recordset rs = db.get(sql);
74: if (rs.getRecordCount() > 0)
75: flag = false;
76:
77: }
78: } catch (Throwable e) {
79: throw e;
80: } finally {
81: if (conn != null)
82: conn.close();
83: }
84:
85: return flag;
86:
87: }
88:
89: }
|