View Javadoc

1   /**
2    * This file Copyright (c) 2003-2013 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.lang.StringUtils;
44  
45  
46  /**
47   * @author Vinzenz Wyser
48   * @version 2.0
49   */
50  public class DialogPassword extends DialogBox {
51  
52      private boolean useBcrypt;
53  
54      /**
55       * @see info.magnolia.cms.gui.dialog.DialogControl#drawHtml(Writer)
56       */
57      @Override
58      public void drawHtml(Writer out) throws IOException {
59          Password control = new Password(this.getName(), this.getValue());
60          if (this.getConfigValue("saveInfo").equals("false")) {
61              control.setSaveInfo(false);
62          }
63          control.setCssClass(CssConstants.CSSCLASS_EDIT);
64          control.setCssStyles("width", this.getConfigValue("width", "100%"));
65          if (useBcrypt) {
66              control.setEncoding(ControlImpl.ENCRYPTION_HASH_BCRYPT);
67          } else {
68              control.setEncoding(ControlImpl.ENCRYPTION_NO_ENCODING_BASE64);
69          }
70          if (this.getConfigValue("onchange", null) != null) {
71              control.setEvent("onchange", this.getConfigValue("onchange"));
72          }
73          this.drawHtmlPre(out);
74          out.write(control.getHtml());
75          if (this.getConfigValue("verification", "true").equals("true")) {
76              Password control2 = new Password(this.getName() + "_verification", StringUtils.EMPTY);
77              control2.setSaveInfo(false);
78              control2.setCssClass(CssConstants.CSSCLASS_EDIT);
79              control2.setCssStyles("width", this.getConfigValue("width", "100%"));
80              control2.setEvent("onchange", "mgnlDialogPasswordVerify('" + this.getName() + "')");
81              out.write("<div class=\"" + CssConstants.CSSCLASS_DESCRIPTION + "\">" + getMessage("dialog.password.verify") + "</div>");
82              out.write(control2.getHtml());
83          }
84          this.drawHtmlPost(out);
85      }
86  
87      @Override
88      public boolean validate() {
89          if (super.getConfigValue("verification", "true").equals("true")) {
90              String newP = super.getRequest().getParameter(getName());
91              String verP = super.getRequest().getParameter(getName() + "_verification");
92              // we do not set passwords back and forth, we just send empty string of the same length of the original password (in case of base 64 or no encryption). So in this case trimmed newP has to be 0 as well as verP
93              // in all other cases they have to match exactly.
94              if (!(newP.trim().length() == 0 && verP.length() == 0) && !newP.equals(verP)) {
95                  setValidationMessage(getMessage("dialog.password.failed.js"));
96                  return false;
97              }
98          }
99          return super.validate();
100     }
101 
102     public boolean isBcrypt() {
103         return useBcrypt;
104     }
105 
106     public void setBcrypt(boolean useBcrypt) {
107         this.useBcrypt = useBcrypt;
108     }
109 
110 }