View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.gui.dialog;
35  
36  import info.magnolia.cms.gui.control.ControlImpl;
37  import info.magnolia.cms.gui.control.Password;
38  import info.magnolia.cms.gui.misc.CssConstants;
39  
40  import java.io.IOException;
41  import java.io.Writer;
42  
43  import org.apache.commons.codec.binary.Base64;
44  import org.apache.commons.lang.StringUtils;
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  
48  
49  /**
50   * @author Vinzenz Wyser
51   * @version 2.0
52   */
53  public class DialogPassword extends DialogBox {
54      
55      private static final Logger log = LoggerFactory.getLogger(DialogPassword.class);
56  
57      /**
58       * @see info.magnolia.cms.gui.dialog.DialogControl#drawHtml(Writer)
59       */
60      public void drawHtml(Writer out) throws IOException {
61          Password control = new Password(this.getName(), this.getValue());
62          if (this.getConfigValue("saveInfo").equals("false")) { //$NON-NLS-1$ //$NON-NLS-2$
63              control.setSaveInfo(false);
64          }
65          control.setCssClass(CssConstants.CSSCLASS_EDIT);
66          control.setCssStyles("width", this.getConfigValue("width", "100%")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
67          control.setEncoding(ControlImpl.ENCODING_BASE64);
68          if (this.getConfigValue("onchange", null) != null) { //$NON-NLS-1$
69              control.setEvent("onchange", this.getConfigValue("onchange")); //$NON-NLS-1$ //$NON-NLS-2$
70          }
71          this.drawHtmlPre(out);
72          out.write(control.getHtml());
73          if (this.getConfigValue("verification", "true").equals("true")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
74              Password control2 = new Password(this.getName() + "_verification", StringUtils.EMPTY); //$NON-NLS-1$
75  //            Password control2=new Password(this.getName()+"_verification",control.getValue());
76  //            control2.setEncoding(control.getEncoding());
77              control2.setSaveInfo(false);
78              control2.setCssClass(CssConstants.CSSCLASS_EDIT);
79              control2.setCssStyles("width", //$NON-NLS-1$
80                  this.getConfigValue("width", "100%")); //$NON-NLS-1$ //$NON-NLS-2$
81              control2.setEvent("onchange", //$NON-NLS-1$
82                  "mgnlDialogPasswordVerify('" + this.getName() + "')"); //$NON-NLS-1$ //$NON-NLS-2$
83              // todo: verification on submit; think about
84              out.write("<div class=\"" //$NON-NLS-1$
85                  + CssConstants.CSSCLASS_DESCRIPTION
86                  + "\">" //$NON-NLS-1$
87                  + getMessage("dialog.password.verify") //$NON-NLS-1$
88                  + "</div>"); //$NON-NLS-1$
89              out.write(control2.getHtml());
90          }
91          this.drawHtmlPost(out);
92      }
93  
94      public boolean validate() {
95          if (super.getConfigValue("verification", "true").equals("true")) {
96              String newP = super.getRequest().getParameter(getName());
97              String verP = super.getRequest().getParameter(getName() + "_verification");
98              String oriP = this.getStorageNode().getNodeData(getName()).getString();
99              if (Base64.isArrayByteBase64(oriP.getBytes())) {
100                 oriP = new String(Base64.decodeBase64(oriP.getBytes()));
101             }
102             // we do not set passwords back and forth, we just send empty string of the same length of the original password. So in this case trimmed newP has to be 0 as well as verP
103             // in all other cases they have to match exactly.
104             if (!(newP.trim().length() == 0 && verP.length() == 0) && !newP.equals(verP)) {
105                 setValidationMessage(getMessage("dialog.password.failed.js"));
106                 return false;
107             }
108         }
109         return super.validate();
110     }
111     
112     
113 }