Dialog na změnu hesla

multitenant
Josef Rokos 11 years ago
parent 3718e4c088
commit 0926b36fa8

@ -1,14 +1,16 @@
package info.bukova.isspst.services.users;
import org.springframework.security.core.userdetails.UserDetailsService;
import info.bukova.isspst.data.User;
import info.bukova.isspst.services.Service;
import org.springframework.security.core.userdetails.UserDetailsService;
public interface UserService extends UserDetailsService, Service<User> {
public void setPassword(User user, String password);
public boolean hasRole(User user, String authority);
public void saveWithPwd(User user, String password);
public User getCurrent();
public String encodePassword(User user, String plain);
}

@ -2,6 +2,8 @@ package info.bukova.isspst.services.users;
import org.hibernate.Query;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional;
@ -35,7 +37,7 @@ public class UserServiceImpl extends AbstractService<User> implements UserServic
@Override
public void setPassword(User user, String password) {
user.setPassword(encoder.encodePassword(password, user.getUsername()));
user.setPassword(encodePassword(user, password));
}
@Override
@ -55,5 +57,21 @@ public class UserServiceImpl extends AbstractService<User> implements UserServic
this.update(user);
}
@Override
public User getCurrent() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null && auth.getPrincipal() != null) {
return (User)auth.getPrincipal();
}
return null;
}
@Override
public String encodePassword(User user, String plain) {
return encoder.encodePassword(plain, user.getUsername());
}
}

@ -1,7 +1,9 @@
package info.bukova.isspst.ui;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zul.Window;
public class NavigationVM {
@ -14,6 +16,12 @@ public class NavigationVM {
moduleUrl = Executions.getCurrent().getDesktop().getRequestPath();
}
@Command
public void passwd() {
Window window = (Window)Executions.createComponents("/app/passwd.zul", null, null);
window.doModal();
}
public String getContextPath() {
return contextPath;
}

@ -0,0 +1,83 @@
package info.bukova.isspst.ui.users;
import info.bukova.isspst.data.User;
import info.bukova.isspst.services.users.UserService;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;
public class PasswdVM {
private String oldPw;
private String newPw;
private String retPw;
private User user;
@WireVariable
private UserService userService;
@Init
public void init() {
user = userService.getCurrent();
}
@Command
public void save(@BindingParam("window") Window window) {
if (!canSave()) {
return;
}
userService.saveWithPwd(user, newPw);
Messagebox.show("Heslo bylo změněno", "Změna hesla", Messagebox.OK, Messagebox.INFORMATION);
window.detach();
}
private boolean canSave() {
if (!user.getPassword().equals(userService.encodePassword(user, oldPw))) {
Messagebox.show("Špatné staré heslo", "Chyba", Messagebox.OK, Messagebox.ERROR);
return false;
}
if (newPw == null || newPw.isEmpty()) {
Messagebox.show("Zadejte nové heslo", "Chyba", Messagebox.OK, Messagebox.ERROR);
return false;
}
if (!newPw.equals(retPw)) {
Messagebox.show("Nasouhlasí nová hesla", "Chyba", Messagebox.OK, Messagebox.ERROR);
return false;
}
return true;
}
public String getOldPw() {
return oldPw;
}
public void setOldPw(String oldPw) {
this.oldPw = oldPw;
}
public String getNewPw() {
return newPw;
}
public void setNewPw(String newPw) {
this.newPw = newPw;
}
public String getRetPw() {
return retPw;
}
public void setRetPw(String retPw) {
this.retPw = retPw;
}
}

@ -36,7 +36,7 @@
</tabpanel>
<tabpanel>
<menubar orient="vertical">
<menuitem label="Změnit heslo" width="120px"/>
<menuitem label="Změnit heslo" width="120px" onClick="@command('passwd')"/>
<menuitem label="Odhlásit" href="/j_spring_security_logout"/>
</menubar>
</tabpanel>

@ -0,0 +1,22 @@
<?page title="Změnit heslo" contentType="text/html;charset=UTF-8"?>
<zk>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<window id="passwd" title="Změnit heslo" border="normal" closable="true" width="350px"
apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('info.bukova.isspst.ui.users.PasswdVM')">
<style src="/app/form.css"/>
<grid>
<columns>
<column hflex="min"/>
<column/>
</columns>
<rows>
<row><label value="Staré heslo:"/><textbox type="password" value="@bind(vm.oldPw)"/></row>
<row><label value="Nové heslo:"/><textbox type="password" value="@bind(vm.newPw)"/></row>
<row><label value="Nové heslo znovu:"/><textbox type="password" value="@bind(vm.retPw)"/></row>
</rows>
</grid>
<button image="/img/save.png" label="Uložit" onClick="@command('save', window=passwd)" sclass="nicebutton" /><button image="~./zul/img/misc/drag-disallow.png" label="Zrušit" onClick="passwd.detach()" sclass="nicebutton"/>
</window>
</zk>
Loading…
Cancel
Save