/*
Copyright (C) 2010 maik.jablonski@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package jfix.zk;
import jfix.util.Crypts;
import jfix.util.I18N;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
public abstract class Login extends Grid {
private final static String COOKIE_LOGIN = "JZK1";
private final static String COOKIE_PASSWORD = "JZK2";
private final static byte[] SALT;
static {
if (System.getProperty("identity.fake") != null) {
SALT = "12345678".getBytes();
} else {
SALT = Crypts.createRandomPassPhrase();
}
}
Textfield loginField = new Textfield();
Passwordfield passwordField = new Passwordfield();
public abstract boolean loginPerformed(String login, String password);
public Login() {
this(I18N.get("Username"), I18N.get("Password"), true);
}
public Login(String loginLabel, String passwordLabel) {
this(loginLabel, passwordLabel, true);
}
public Login(String loginLabel, String passwordLabel,
final boolean rememberMe) {
passwordField.setType("password");
if (rememberMe) {
loginField.setText(Crypts
.decipher(ZK.getCookie(COOKIE_LOGIN), SALT));
passwordField.setText(Crypts.decipher(
ZK.getCookie(COOKIE_PASSWORD), SALT));
}
EventListener submitListener = new EventListener() {
public void onEvent(Event e) {
loginHandler(rememberMe);
}
};
Button submitButton = new Button(I18N.get("Login"), Images.SystemUsers);
submitButton.addEventListener(Events.ON_CLICK, submitListener);
addEventListener(Events.ON_OK, submitListener);
Button resetButton = new Button(I18N.get("Reset"), Images.ViewRefresh);
resetButton.addEventListener(Events.ON_CLICK, new EventListener() {
public void onEvent(Event e) {
resetHandler();
}
});
append(loginLabel, loginField);
append(passwordLabel, passwordField);
append("", submitButton, new Separator(Separator.VERTICAL), resetButton);
}
private void append(String label, Component... comp) {
Cell labelCell = new Cell(new Label(label));
labelCell.setHflex("3");
Cell compCell = new Cell(comp);
compCell.setHflex("4");
add(labelCell, compCell);
}
private void loginHandler(boolean rememberMe) {
String loginText = loginField.getText().trim();
String passwordText = passwordField.getText().trim();
if (!"".equals(loginText) && !"".equals(passwordText)) {
if (loginPerformed(loginText, passwordText)) {
if (rememberMe) {
ZK.setCookie(COOKIE_LOGIN, Crypts.cipher(loginText, SALT));
ZK.setCookie(COOKIE_PASSWORD, Crypts.cipher(passwordText,
SALT));
}
} else {
passwordField.setValue(null);
passwordField.focus();
}
}
}
private void resetHandler() {
ZK.setCookie(COOKIE_LOGIN, null);
ZK.setCookie(COOKIE_PASSWORD, null);
loginField.setValue(null);
passwordField.setValue(null);
loginField.focus();
}
}