001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.datatypes;
011:
012: import java.util.*;
013: import org.mmbase.bridge.*;
014: import org.mmbase.datatypes.processors.Processor;
015: import org.mmbase.util.LocalizedString;
016: import org.mmbase.util.logging.*;
017:
018: /**
019: * A confirmed password datatype must have the same value as another field of the node (and makes
020: * only sense as a field of a node).
021: *
022: * @author Michiel Meeuwissen
023: * @version $Id: ConfirmPasswordDataType.java,v 1.14 2007/06/21 07:32:31 pierre Exp $
024: * @since MMBase-1.8
025: */
026: public class ConfirmPasswordDataType extends StringDataType {
027: private static final Logger log = Logging
028: .getLoggerInstance(ConfirmPasswordDataType.class);
029:
030: private static final long serialVersionUID = 1L; // increase this if object serialization changes (which we shouldn't do!)
031:
032: protected PasswordRestriction passwordRestriction = new PasswordRestriction(
033: "password");
034:
035: /**
036: * Constructor for string data type.
037: * @param name the name of the data type
038: */
039: public ConfirmPasswordDataType(String name) {
040: super (name);
041: }
042:
043: protected void inheritRestrictions(BasicDataType origin) {
044: super .inheritRestrictions(origin);
045: if (origin instanceof ConfirmPasswordDataType) {
046: ConfirmPasswordDataType dataType = (ConfirmPasswordDataType) origin;
047: passwordRestriction.inherit(dataType.passwordRestriction);
048: }
049: }
050:
051: protected void cloneRestrictions(BasicDataType origin) {
052: super .cloneRestrictions(origin);
053: if (origin instanceof ConfirmPasswordDataType) {
054: ConfirmPasswordDataType dataType = (ConfirmPasswordDataType) origin;
055: passwordRestriction = new PasswordRestriction(
056: dataType.passwordRestriction);
057: }
058: }
059:
060: public int getEnforceStrength() {
061: return Math.max(super .getEnforceStrength(), passwordRestriction
062: .getEnforceStrength());
063: }
064:
065: protected Collection<LocalizedString> validateCastValue(
066: Collection<LocalizedString> errors, Object castValue,
067: Object value, Node node, Field field) {
068: errors = super .validateCastValue(errors, castValue, value,
069: node, field);
070: errors = passwordRestriction.validate(errors, castValue, node,
071: field);
072: return errors;
073: }
074:
075: /**
076: * The field property is the name of the other password field that this fields 'confirms'. It default to 'password'.
077: * In datatype XML it can be set with the generic <property name="field" value="..." />
078: */
079: public void setField(String field) {
080: passwordRestriction.setValue(field);
081: }
082:
083: /**
084: * Returns the name of the field which is 'confirmed' by this datatype.
085: */
086: public String getField() {
087: edit();
088: return passwordRestriction.getField();
089: }
090:
091: protected StringBuilder toStringBuilder() {
092: StringBuilder buf = super .toStringBuilder();
093: buf.append(" confirm(").append(passwordRestriction.getValue())
094: .append(")");
095: return buf;
096: }
097:
098: protected class PasswordRestriction extends AbstractRestriction {
099: PasswordRestriction(PasswordRestriction source) {
100: super (source);
101: }
102:
103: PasswordRestriction(String field) {
104: super ("confirmpassword", field);
105: }
106:
107: protected final String getField() {
108: return (String) value;
109: }
110:
111: protected boolean simpleValid(final Object v, final Node node,
112: final Field field) {
113: if (node != null && field != null && v != null) {
114: if (!node.isChanged(getField()))
115: return true;
116:
117: Field passwordField = node.getNodeManager().getField(
118: getField());
119: Processor setProcessor = passwordField.getDataType()
120: .getProcessor(PROCESS_SET);
121: Object processedValue = setProcessor.process(node,
122: field, v);
123: String passwordValue = (String) node
124: .getObjectValue(getField());
125: if (log.isDebugEnabled()) {
126: log.debug("Password checking "
127: + (node.isNew() ? "new" : "existing")
128: + " node. Password field " + passwordField
129: + " set-processor " + setProcessor);
130: log.debug("Offered value '" + v + "' --> '"
131: + processedValue);
132: log.debug("Comparing '" + passwordValue
133: + "' with '" + processedValue + "'(" + v
134: + ")");
135: }
136: return passwordValue.equals(v)
137: || passwordValue.equals(processedValue);
138: } else {
139: return true;
140: }
141: }
142: }
143:
144: }
|