Dialog na změnu hesla

This commit is contained in:
2014-05-14 20:35:57 +02:00
parent 3718e4c088
commit 0926b36fa8
6 changed files with 137 additions and 4 deletions
@@ -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;
}
}