From 0926b36fa8496c5242524e3752790f2862bf4742 Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Wed, 14 May 2014 20:35:57 +0200 Subject: [PATCH 1/3] =?UTF-8?q?Dialog=20na=20zm=C4=9Bnu=20hesla?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../isspst/services/users/UserService.java | 6 +- .../services/users/UserServiceImpl.java | 20 ++++- .../info/bukova/isspst/ui/NavigationVM.java | 8 ++ .../info/bukova/isspst/ui/users/PasswdVM.java | 83 +++++++++++++++++++ src/main/webapp/app/navigation.zul | 2 +- src/main/webapp/app/passwd.zul | 22 +++++ 6 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 src/main/java/info/bukova/isspst/ui/users/PasswdVM.java create mode 100644 src/main/webapp/app/passwd.zul diff --git a/src/main/java/info/bukova/isspst/services/users/UserService.java b/src/main/java/info/bukova/isspst/services/users/UserService.java index 466ea95e..429b472b 100644 --- a/src/main/java/info/bukova/isspst/services/users/UserService.java +++ b/src/main/java/info/bukova/isspst/services/users/UserService.java @@ -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 { 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); } diff --git a/src/main/java/info/bukova/isspst/services/users/UserServiceImpl.java b/src/main/java/info/bukova/isspst/services/users/UserServiceImpl.java index f2f1531c..ae4ee230 100644 --- a/src/main/java/info/bukova/isspst/services/users/UserServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/users/UserServiceImpl.java @@ -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 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 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()); + } + } diff --git a/src/main/java/info/bukova/isspst/ui/NavigationVM.java b/src/main/java/info/bukova/isspst/ui/NavigationVM.java index 3a4d927e..3c64dc6b 100644 --- a/src/main/java/info/bukova/isspst/ui/NavigationVM.java +++ b/src/main/java/info/bukova/isspst/ui/NavigationVM.java @@ -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; } diff --git a/src/main/java/info/bukova/isspst/ui/users/PasswdVM.java b/src/main/java/info/bukova/isspst/ui/users/PasswdVM.java new file mode 100644 index 00000000..47286063 --- /dev/null +++ b/src/main/java/info/bukova/isspst/ui/users/PasswdVM.java @@ -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; + } + + + +} diff --git a/src/main/webapp/app/navigation.zul b/src/main/webapp/app/navigation.zul index e6e11425..214dab4c 100644 --- a/src/main/webapp/app/navigation.zul +++ b/src/main/webapp/app/navigation.zul @@ -36,7 +36,7 @@ - + diff --git a/src/main/webapp/app/passwd.zul b/src/main/webapp/app/passwd.zul new file mode 100644 index 00000000..fbed95d7 --- /dev/null +++ b/src/main/webapp/app/passwd.zul @@ -0,0 +1,22 @@ + + + + + +