@@ -55,8 +55,12 @@ public class UserServiceImpl extends AbstractService<User> implements UserServic
|
||||
@Override
|
||||
@Transactional
|
||||
public void saveWithPwd(User user, String password) {
|
||||
this.setPassword(user, password);
|
||||
this.update(user);
|
||||
if ((password != null) && !password.isEmpty())
|
||||
{
|
||||
this.setPassword(user, password);
|
||||
}
|
||||
|
||||
this.update(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
package info.bukova.isspst.ui.users;
|
||||
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.zkoss.bind.annotation.Command;
|
||||
import org.zkoss.bind.annotation.Init;
|
||||
import org.zkoss.bind.annotation.NotifyChange;
|
||||
import org.zkoss.zk.ui.select.annotation.WireVariable;
|
||||
|
||||
import info.bukova.isspst.data.User;
|
||||
import info.bukova.isspst.services.users.RoleService;
|
||||
import info.bukova.isspst.services.users.UserService;
|
||||
import info.bukova.isspst.ui.FormViewModel;
|
||||
import info.bukova.isspst.validators.UserFormValidator;
|
||||
|
||||
public class UserForm extends FormViewModel<User> {
|
||||
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.zkoss.bind.Validator;
|
||||
import org.zkoss.bind.annotation.Command;
|
||||
import org.zkoss.bind.annotation.Init;
|
||||
import org.zkoss.bind.annotation.NotifyChange;
|
||||
import org.zkoss.zk.ui.select.annotation.WireVariable;
|
||||
|
||||
public class UserForm extends FormViewModel<User>
|
||||
{
|
||||
@WireVariable
|
||||
private RoleService roleService;
|
||||
@WireVariable
|
||||
@@ -21,78 +23,92 @@ public class UserForm extends FormViewModel<User> {
|
||||
private String password = "";
|
||||
private UserRoles userRoles;
|
||||
private boolean loginFree;
|
||||
|
||||
|
||||
private Validator userFormValidator;
|
||||
|
||||
@Init(superclass = true)
|
||||
public void init() {
|
||||
public void init()
|
||||
{
|
||||
userRoles = new UserRoles(getDataBean(), roleService.getAll());
|
||||
loginFree = true;
|
||||
this.setUserFormValidator(new UserFormValidator(getDataBean().getCreated() == null));
|
||||
}
|
||||
|
||||
public UserRoles getUserRoles() {
|
||||
public UserRoles getUserRoles()
|
||||
{
|
||||
return userRoles;
|
||||
}
|
||||
|
||||
public String getRetPasswd() {
|
||||
|
||||
public String getRetPasswd()
|
||||
{
|
||||
return retPasswd;
|
||||
}
|
||||
|
||||
@NotifyChange({"canSave", "pwMatches"})
|
||||
public void setRetPasswd(String retPasswd) {
|
||||
public void setRetPasswd(String retPasswd)
|
||||
{
|
||||
this.retPasswd = retPasswd;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
public String getPassword()
|
||||
{
|
||||
return password;
|
||||
}
|
||||
|
||||
@NotifyChange({"canSave", "pwMatches"})
|
||||
public void setPassword(String password) {
|
||||
public void setPassword(String password)
|
||||
{
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
|
||||
@Command
|
||||
@NotifyChange({"loginFree", "canSave"})
|
||||
public void checkLogin() {
|
||||
try {
|
||||
@NotifyChange({ "loginFree", "canSave" })
|
||||
public void checkLogin()
|
||||
{
|
||||
try
|
||||
{
|
||||
userService.loadUserByUsername(getDataBean().getUsername());
|
||||
loginFree = false;
|
||||
} catch(UsernameNotFoundException e) {
|
||||
} catch (UsernameNotFoundException e)
|
||||
{
|
||||
loginFree = true;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isPwMatches() {
|
||||
return password.equals(retPasswd);
|
||||
}
|
||||
|
||||
public boolean isLoginFree() {
|
||||
|
||||
public boolean isLoginFree()
|
||||
{
|
||||
return loginFree || isEdit();
|
||||
}
|
||||
|
||||
public boolean isEdit() {
|
||||
|
||||
public boolean isEdit()
|
||||
{
|
||||
return getDataBean().getCreated() != null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void doSave() {
|
||||
if (!password.isEmpty()) {
|
||||
userService.saveWithPwd(getDataBean(), this.password);
|
||||
} else {
|
||||
super.doSave();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doAdd() {
|
||||
if (!password.isEmpty()) {
|
||||
userService.setPassword(getDataBean(), password);
|
||||
userService.add(getDataBean());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCanSave() {
|
||||
return password.equals(retPasswd) && isLoginFree() && getDataBean().getUsername() != null && !getDataBean().getUsername().isEmpty();
|
||||
protected void doSave()
|
||||
{
|
||||
userService.saveWithPwd(getDataBean(), this.password);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doAdd()
|
||||
{
|
||||
userService.setPassword(getDataBean(), password);
|
||||
userService.add(getDataBean());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCanSave()
|
||||
{
|
||||
return isLoginFree() && getDataBean().getUsername() != null && !getDataBean().getUsername().isEmpty();
|
||||
}
|
||||
|
||||
public Validator getUserFormValidator()
|
||||
{
|
||||
return userFormValidator;
|
||||
}
|
||||
|
||||
public void setUserFormValidator(Validator userFormValidator)
|
||||
{
|
||||
this.userFormValidator = userFormValidator;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package info.bukova.isspst.validators;
|
||||
|
||||
import info.bukova.isspst.StringUtils;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.zkoss.bind.ValidationContext;
|
||||
import org.zkoss.bind.validator.AbstractValidator;
|
||||
import org.zkoss.zk.ui.Component;
|
||||
import org.zkoss.zk.ui.HtmlBasedComponent;
|
||||
import org.zkoss.zk.ui.event.Event;
|
||||
import org.zkoss.zk.ui.event.EventListener;
|
||||
import org.zkoss.zul.Messagebox;
|
||||
|
||||
public abstract class BaseValidator extends AbstractValidator
|
||||
{
|
||||
HtmlBasedComponent htmlComponent;
|
||||
|
||||
protected Logger getLogger()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public void errorMsg(ValidationContext ctx, String msg, String componentIdFocused)
|
||||
{
|
||||
this.addInvalidMessage(ctx, msg);
|
||||
|
||||
Logger logger = this.getLogger();
|
||||
|
||||
if (logger != null)
|
||||
{
|
||||
logger.error(msg);
|
||||
}
|
||||
|
||||
htmlComponent = null;
|
||||
|
||||
if ((componentIdFocused != null) && (!componentIdFocused.isEmpty()))
|
||||
{
|
||||
Component component = ctx.getBindContext().getComponent().getFellowIfAny(componentIdFocused, true);
|
||||
|
||||
if ((component != null) && (component instanceof HtmlBasedComponent))
|
||||
{
|
||||
htmlComponent = (HtmlBasedComponent) component;
|
||||
}
|
||||
}
|
||||
|
||||
Messagebox.show(msg, StringUtils.localize("Error"), Messagebox.OK, Messagebox.ERROR, new EventListener<Event>()
|
||||
{
|
||||
public void onEvent(Event evt) throws InterruptedException
|
||||
{
|
||||
if (htmlComponent != null)
|
||||
{
|
||||
htmlComponent.setFocus(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void addErrorMsg(ValidationContext ctx, String msg)
|
||||
{
|
||||
this.errorMsg(ctx, msg, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate(ValidationContext ctx)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package info.bukova.isspst.validators;
|
||||
|
||||
import info.bukova.isspst.StringUtils;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.zkoss.bind.Property;
|
||||
import org.zkoss.bind.ValidationContext;
|
||||
|
||||
public class UserFormValidator extends BaseValidator
|
||||
{
|
||||
private final static Logger log = LoggerFactory.getLogger(UserFormValidator.class.getName());
|
||||
|
||||
@Override
|
||||
protected Logger getLogger()
|
||||
{
|
||||
return log;
|
||||
}
|
||||
|
||||
private boolean recordIsNew;
|
||||
|
||||
public UserFormValidator(boolean recordIsNew)
|
||||
{
|
||||
this.recordIsNew = recordIsNew;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate(ValidationContext ctx)
|
||||
{
|
||||
Property propertyOrig = ctx.getProperties("password")[0];
|
||||
String passwordOrig = (String) propertyOrig.getValue();
|
||||
|
||||
if (passwordOrig == null)
|
||||
{
|
||||
passwordOrig = "";
|
||||
}
|
||||
|
||||
if (passwordOrig.isEmpty())
|
||||
{
|
||||
if (this.recordIsNew)
|
||||
{
|
||||
this.errorMsg(ctx, StringUtils.localize("UserPasswordIsEmpty"), "idUserPasswordOriginal");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Property propertyDupl = ctx.getProperties("retPasswd")[0];
|
||||
String passwordDupl = (String) propertyDupl.getValue();
|
||||
|
||||
if (passwordDupl == null)
|
||||
{
|
||||
passwordDupl = "";
|
||||
}
|
||||
|
||||
if (!passwordOrig.equals(passwordDupl))
|
||||
{
|
||||
this.errorMsg(ctx, StringUtils.localize("UserPasswordsAreNotSame"), "idUserPasswordDuplicate");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package info.bukova.isspst.validators;
|
||||
|
||||
import info.bukova.isspst.StringUtils;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.zkoss.bind.Property;
|
||||
import org.zkoss.bind.ValidationContext;
|
||||
|
||||
public class UserPasswordDuplValidator extends BaseValidator
|
||||
{
|
||||
private final static Logger log = LoggerFactory.getLogger(UserPasswordDuplValidator.class.getName());
|
||||
|
||||
@Override
|
||||
protected Logger getLogger()
|
||||
{
|
||||
return log;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate(ValidationContext ctx)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public void aaa(ValidationContext ctx)
|
||||
{
|
||||
Object argOrig = ctx.getBindContext().getValidatorArg("orig");
|
||||
|
||||
if (argOrig == null)
|
||||
{
|
||||
this.addErrorMsg(ctx, StringUtils.localize("NullPointerErr"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(argOrig instanceof String))
|
||||
{
|
||||
this.addErrorMsg(ctx, StringUtils.localize("DataTypeErr"));
|
||||
return;
|
||||
}
|
||||
|
||||
String propertyNameOrig = (String) argOrig;
|
||||
|
||||
Property propertyOrig = ctx.getProperties(propertyNameOrig)[0];
|
||||
String passwordOrig = (String) propertyOrig.getValue();
|
||||
|
||||
if (passwordOrig == null)
|
||||
{
|
||||
passwordOrig = "";
|
||||
}
|
||||
|
||||
Property propertyDupl = ctx.getProperty();
|
||||
String passwordDupl = (String) propertyDupl.getValue();
|
||||
|
||||
if (passwordDupl == null)
|
||||
{
|
||||
passwordDupl = "";
|
||||
}
|
||||
|
||||
if (!passwordOrig.equals(passwordDupl))
|
||||
{
|
||||
this.addErrorMsg(ctx, StringUtils.localize("UserPasswordsAreNotSame"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package info.bukova.isspst.validators;
|
||||
|
||||
import info.bukova.isspst.StringUtils;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.zkoss.bind.Property;
|
||||
import org.zkoss.bind.ValidationContext;
|
||||
|
||||
public class UserPasswordOrigValidator extends BaseValidator
|
||||
{
|
||||
private final static Logger log = LoggerFactory.getLogger(UserPasswordOrigValidator.class.getName());
|
||||
|
||||
@Override
|
||||
protected Logger getLogger()
|
||||
{
|
||||
return log;
|
||||
}
|
||||
|
||||
private boolean recordIsNew;
|
||||
|
||||
public UserPasswordOrigValidator(boolean recordIsNew)
|
||||
{
|
||||
this.recordIsNew = recordIsNew;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate(ValidationContext ctx)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public void aaa(ValidationContext ctx)
|
||||
{
|
||||
Property propertyOrig = ctx.getProperty();
|
||||
String passwordOrig = (String) propertyOrig.getValue();
|
||||
|
||||
if (passwordOrig == null)
|
||||
{
|
||||
passwordOrig = "";
|
||||
}
|
||||
|
||||
if (passwordOrig.isEmpty())
|
||||
{
|
||||
if (this.recordIsNew)
|
||||
{
|
||||
this.addErrorMsg(ctx, StringUtils.localize("UserPasswordIsEmpty"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user