diff --git a/examples/showcase/src/main/java/org/springside/examples/showcase/entity/Role.java b/examples/showcase/src/main/java/org/springside/examples/showcase/entity/Role.java index 96005735fb833349d92e3e6fed04856a4db61ec8..de80bf0334370364fa6a2f86fb9a42f154c945fa 100644 --- a/examples/showcase/src/main/java/org/springside/examples/showcase/entity/Role.java +++ b/examples/showcase/src/main/java/org/springside/examples/showcase/entity/Role.java @@ -27,6 +27,13 @@ public class Role extends IdEntity { private String permissions; + public Role() { + } + + public Role(Long id) { + this.id = id; + } + @Column(nullable = false, unique = true) public String getName() { return name; diff --git a/examples/showcase/src/main/java/org/springside/examples/showcase/repository/jpa/RoleDao.java b/examples/showcase/src/main/java/org/springside/examples/showcase/repository/jpa/RoleDao.java new file mode 100644 index 0000000000000000000000000000000000000000..83905f6d4f92dd0234668a2fbf12009c514232d8 --- /dev/null +++ b/examples/showcase/src/main/java/org/springside/examples/showcase/repository/jpa/RoleDao.java @@ -0,0 +1,8 @@ +package org.springside.examples.showcase.repository.jpa; + +import org.springframework.data.repository.PagingAndSortingRepository; +import org.springside.examples.showcase.entity.Role; + +public interface RoleDao extends PagingAndSortingRepository { + +} diff --git a/examples/showcase/src/main/java/org/springside/examples/showcase/service/AccountService.java b/examples/showcase/src/main/java/org/springside/examples/showcase/service/AccountService.java index 4532e61602fc3ae193a662e389e7f49efcbafb7a..7708e750937af871a3ed24d46ed125c0c33e8812 100644 --- a/examples/showcase/src/main/java/org/springside/examples/showcase/service/AccountService.java +++ b/examples/showcase/src/main/java/org/springside/examples/showcase/service/AccountService.java @@ -11,7 +11,9 @@ import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import org.springside.examples.showcase.demos.jms.simple.NotifyMessageProducer; import org.springside.examples.showcase.demos.jmx.ApplicationStatistics; +import org.springside.examples.showcase.entity.Role; import org.springside.examples.showcase.entity.User; +import org.springside.examples.showcase.repository.jpa.RoleDao; import org.springside.examples.showcase.repository.jpa.UserDao; import org.springside.modules.persistence.Hibernates; import org.springside.modules.security.utils.Digests; @@ -34,6 +36,8 @@ public class AccountService { private UserDao userDao; + private RoleDao roleDao; + private NotifyMessageProducer notifyProducer; //JMS消息发送 private ApplicationStatistics applicationStatistics; @@ -77,7 +81,7 @@ public class AccountService { user.setPassword(Encodes.encodeHex(hashPassword)); } - public List getAllUser() { + public List getAllUsers() { if (applicationStatistics != null) { applicationStatistics.incrListUserTimes(); @@ -142,11 +146,28 @@ public class AccountService { } } + //--------------------// + // Role Management // + //--------------------// + + public List getAllRoles() { + return (List) roleDao.findAll(); + } + + //-----------------// + // Setter methods // + //-----------------// + @Autowired public void setUserDao(UserDao userDao) { this.userDao = userDao; } + @Autowired + public void setRoleDao(RoleDao roleDao) { + this.roleDao = roleDao; + } + @Autowired(required = false) public void setNotifyProducer(NotifyMessageProducer notifyProducer) { this.notifyProducer = notifyProducer; diff --git a/examples/showcase/src/main/java/org/springside/examples/showcase/web/UserController.java b/examples/showcase/src/main/java/org/springside/examples/showcase/web/UserController.java index 1b6d52b6d4a5fc122c7409271c26b71acafd1afd..079e14b318473cb5d8997530354bb3773e90d07b 100644 --- a/examples/showcase/src/main/java/org/springside/examples/showcase/web/UserController.java +++ b/examples/showcase/src/main/java/org/springside/examples/showcase/web/UserController.java @@ -11,12 +11,15 @@ import org.apache.shiro.authz.annotation.RequiresRoles; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.web.bind.WebDataBinder; +import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.mvc.support.RedirectAttributes; +import org.springside.examples.showcase.entity.Role; import org.springside.examples.showcase.entity.User; import org.springside.examples.showcase.service.AccountService; @@ -40,7 +43,7 @@ public class UserController { @RequiresRoles(value = { "Admin", "User" }, logical = Logical.OR) @RequestMapping(value = "") public String list(Model model) { - List users = accountService.getAllUser(); + List users = accountService.getAllUsers(); model.addAttribute("users", users); model.addAttribute("allStatus", allStatus); return "account/userList"; @@ -51,13 +54,24 @@ public class UserController { public String updateForm(@PathVariable("id") Long id, Model model) { model.addAttribute("user", accountService.getUser(id)); model.addAttribute("allStatus", allStatus); + model.addAttribute("allRoles", accountService.getAllRoles()); return "account/userForm"; } @RequiresPermissions("user:edit") @RequestMapping(value = "save/{userId}") - public String update(@Valid @ModelAttribute("user") User user, RedirectAttributes redirectAttributes) { + public String update(@Valid @ModelAttribute("user") User user, + @RequestParam(value = "roleList") List checkedRoleList, RedirectAttributes redirectAttributes) { + + //bind roleList + user.getRoleList().clear(); + for (Long roleId : checkedRoleList) { + Role role = new Role(roleId); + user.getRoleList().add(role); + } + accountService.saveUser(user); + redirectAttributes.addFlashAttribute("message", "保存用户成功"); return "redirect:/account/user"; } @@ -86,4 +100,12 @@ public class UserController { } return null; } + + /** + * 不自动绑定对象中的roleList属性,另行处理 + */ + @InitBinder + protected void initBinder(WebDataBinder binder) { + binder.setDisallowedFields("roleList"); + } } diff --git a/examples/showcase/src/main/resources/security/applicationContext-shiro.xml b/examples/showcase/src/main/resources/security/applicationContext-shiro.xml index 43f0c98c3a623ea286bf7f2094d2b48f6831ef30..daf26d9a5109dfafeeebf36ca0c5bed5f1ad38a7 100644 --- a/examples/showcase/src/main/resources/security/applicationContext-shiro.xml +++ b/examples/showcase/src/main/resources/security/applicationContext-shiro.xml @@ -12,7 +12,7 @@ - + diff --git a/examples/showcase/src/main/webapp/WEB-INF/views/account/userForm.jsp b/examples/showcase/src/main/webapp/WEB-INF/views/account/userForm.jsp index cad8e239adcfae637589917e2ca879bb9bc02546..395511d8da195bb1e43ba456f996139c4a719447 100644 --- a/examples/showcase/src/main/webapp/WEB-INF/views/account/userForm.jsp +++ b/examples/showcase/src/main/webapp/WEB-INF/views/account/userForm.jsp @@ -56,6 +56,12 @@ +
+ +
+ +
+
diff --git a/examples/showcase/src/main/webapp/WEB-INF/views/account/userList.jsp b/examples/showcase/src/main/webapp/WEB-INF/views/account/userList.jsp index e961cde4f5069855bae863fc474169d889e59204..5d07dd82998c89e54387aa0da8ae378af016763c 100644 --- a/examples/showcase/src/main/webapp/WEB-INF/views/account/userList.jsp +++ b/examples/showcase/src/main/webapp/WEB-INF/views/account/userList.jsp @@ -15,7 +15,7 @@

帐号管理

-
${message}
+
${message}
diff --git a/examples/showcase/src/test/functional/org/springside/examples/showcase/functional/account/UserManagerFT.java b/examples/showcase/src/test/functional/org/springside/examples/showcase/functional/account/UserManagerFT.java index 9cefc2bac95b636a4b6df133360afb87af8ca61a..3edc5ba1f7c9e3b6ab86c00b945b7c7f59cf9555 100644 --- a/examples/showcase/src/test/functional/org/springside/examples/showcase/functional/account/UserManagerFT.java +++ b/examples/showcase/src/test/functional/org/springside/examples/showcase/functional/account/UserManagerFT.java @@ -35,17 +35,17 @@ public class UserManagerFT extends BaseSeleniumTestCase { //点击提交按钮 s.type(By.name("name"), "user_foo"); - s.getSelect(By.name("status")).selectByValue("disabled"); + s.check(By.id("status2")); s.click(By.id("submit_btn")); //重新进入用户修改页面, 检查最后修改者 s.click(By.id("editLink-user")); assertEquals("user_foo", s.getValue(By.name("name"))); - assertEquals("disabled", s.getSelect(By.name("status")).getFirstSelectedOption().getText()); + assertTrue(s.isChecked(By.id("status2"))); //恢复原有值 s.type(By.name("name"), "user"); - s.getSelect(By.name("status")).selectByValue("enabled"); + s.check(By.id("status1")); s.click(By.id("submit_btn")); }