V agendě uživatelů aplikována validace formuláře.

Closes #98
multitenant
František Přibyl 11 years ago
parent 4a55467e1e
commit a834b91091

@ -55,7 +55,11 @@ public class UserServiceImpl extends AbstractService<User> implements UserServic
@Override
@Transactional
public void saveWithPwd(User user, String password) {
this.setPassword(user, password);
if ((password != null) && !password.isEmpty())
{
this.setPassword(user, password);
}
this.update(user);
}

@ -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
@ -22,77 +24,91 @@ public class UserForm extends FormViewModel<User> {
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();
}
protected void doSave()
{
userService.saveWithPwd(getDataBean(), this.password);
}
@Override
protected void doAdd() {
if (!password.isEmpty()) {
userService.setPassword(getDataBean(), password);
userService.add(getDataBean());
}
protected void doAdd()
{
userService.setPassword(getDataBean(), password);
userService.add(getDataBean());
}
@Override
public boolean isCanSave() {
return password.equals(retPasswd) && isLoginFree() && getDataBean().getUsername() != null && !getDataBean().getUsername().isEmpty();
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"));
}
}
}
}

@ -1,3 +1,11 @@
BuildingsFormCodeConstr = Zadejte k\u00F3d budovy...
MaterialFormCodeConstr = Zadejte k\u00F3d materi\u00E1lu...
BuildingsFormCodeConstr=Zadejte k\u00F3d budovy...
MaterialFormCodeConstr=Zadejte k\u00F3d materi\u00E1lu...
MUnitsFormCodeConstr=Zadejte k\u00f3d m\u011brn\u00e9 jednotky...
Lad = \u00E1
Led = \u00E9
Leh = \u011B
Lid = \u00ED
Lod = \u00F3
Lyd = \u00FD
Lzh = \u017E

@ -0,0 +1,5 @@
NullPointerErr=Chyba ukazatele...
DataTypeErr=Chybný datový typ...
UserPasswordIsEmpty=Uživatelské heslo musí být zadané...
UserPasswordsAreNotSame=Znovu zadané heslo není stejné...

@ -21,6 +21,7 @@
<system-config>
<label-location>/WEB-INF/locales/zk-label.properties</label-location>
<label-location>/WEB-INF/locales/columns.properties</label-location>
<label-location>/WEB-INF/locales/validators.properties</label-location>
</system-config>
<language-config>

@ -1,35 +1,97 @@
<?page title="${labels.UsersFormTitle}" contentType="text/html;charset=UTF-8"?>
<zk>
<window id="editWin" border="normal" closable="true" width="450px" apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('info.bukova.isspst.ui.users.UserForm')">
<caption zclass="form-caption" label="${labels.UsersFormTitle}" />
<grid width="440px">
<columns>
<column hflex="min"></column>
<column></column>
<column></column>
</columns>
<rows>
<row><label value="${labels.UsersFormLogin}"/><textbox value="@bind(vm.dataBean.username)" instant="true" disabled="@load(vm.edit)" onChange="@command('checkLogin')"/><label value="Login je obsazený" sclass="error" visible="@load(not vm.loginFree)"/></row>
<row><label value="${labels.UsersFormFirstName}"/><textbox value="@bind(vm.dataBean.firstName)"/></row>
<row><label value="${labels.UsersFormSureName}"/><textbox value="@bind(vm.dataBean.lastName)"/></row>
<row><label value="${labels.UsersFormPersonalID}"/><textbox value="@bind(vm.dataBean.personalNumber)" width="90px"/></row>
<row><label value="${labels.UsersFormEmail}"/><textbox value="@bind(vm.dataBean.email)"/></row>
<row><label value=""/><checkbox label="${labels.UsersFormSendNotify}" checked="@bind(vm.dataBean.notify)"/></row>
<row><label value="${labels.UsersFormPassword}"/><textbox value="@bind(vm.password)" type="password" instant="true"/></row>
<row><label value="${labels.UsersFormRepeatPassword}"/><textbox value="@bind(vm.retPasswd)" type="password" instant="true"/><label value="Hesla nesouhlasí" sclass="error" visible="@load(not vm.pwMatches)"/></row>
<row><label value=""/><checkbox label="${labels.UsersFormActive}" checked="@bind(vm.dataBean.enabled)" disabled="@load(vm.dataBean.username eq 'admin')" /></row>
</rows>
</grid>
<groupbox mold="3d">
<caption label="Role"/>
<vbox children="@load(vm.userRoles.roleChecks)">
<template name="children">
<checkbox label="@load(each.role.description)" checked="@bind(each.checked)" disabled="@load(vm.dataBean.username eq 'admin')" />
</template>
</vbox>
</groupbox>
<include src="/app/formButtons.zul" />
</window>
<window
id="editWin"
border="normal"
closable="true"
width="450px"
apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('info.bukova.isspst.ui.users.UserForm')"
validationMessages="@id('vmsg')">
<caption
zclass="form-caption"
label="${labels.UsersFormTitle}" />
<grid
width="440px"
form="@id('fx') @load(vm.dataBean) @save(vm.dataBean, before='save') @validator(vm.userFormValidator)">
<columns>
<column hflex="min"></column>
<column></column>
<column></column>
</columns>
<rows>
<row>
<label value="${labels.UsersFormLogin}" />
<textbox
value="@bind(vm.dataBean.username)"
instant="true"
disabled="@load(vm.edit)"
onChange="@command('checkLogin')" />
<label
value="Login je obsazený"
sclass="error"
visible="@load(not vm.loginFree)" />
</row>
<row>
<label value="${labels.UsersFormFirstName}" />
<textbox value="@bind(vm.dataBean.firstName)" />
</row>
<row>
<label value="${labels.UsersFormSureName}" />
<textbox value="@bind(vm.dataBean.lastName)" />
</row>
<row>
<label value="${labels.UsersFormPersonalID}" />
<textbox
value="@bind(vm.dataBean.personalNumber)"
width="90px" />
</row>
<row>
<label value="${labels.UsersFormEmail}" />
<textbox value="@bind(vm.dataBean.email)" />
</row>
<row>
<label value="" />
<checkbox
label="${labels.UsersFormSendNotify}"
checked="@bind(vm.dataBean.notify)" />
</row>
<row>
<label value="${labels.UsersFormPassword}" />
<textbox
id="idUserPasswordOriginal"
value="@save(vm.password, before='save')"
type="password"
instant="true" />
</row>
<row>
<label value="${labels.UsersFormRepeatPassword}" />
<textbox
id="idUserPasswordDuplicate"
value="@save(vm.retPasswd, before='save')"
type="password"
instant="true" />
</row>
<row>
<label value="" />
<checkbox
label="${labels.UsersFormActive}"
checked="@bind(vm.dataBean.enabled)"
disabled="@load(vm.dataBean.username eq 'admin')" />
</row>
</rows>
</grid>
<groupbox mold="3d">
<caption label="Role" />
<vbox children="@load(vm.userRoles.roleChecks)">
<template name="children">
<checkbox
label="@load(each.role.description)"
checked="@bind(each.checked)"
disabled="@load(vm.dataBean.username eq 'admin')" />
</template>
</vbox>
</groupbox>
<include src="/app/formButtons.zul" />
</window>
</zk>
Loading…
Cancel
Save