提交 d83e4bad 编写于 作者: C Calvin

#85 showcase中补充用户的角色管理(checkBox提交)的演示,比旧版有改进。

上级 13a8efec
......@@ -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;
......
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<Role, Long> {
}
......@@ -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<User> getAllUser() {
public List<User> getAllUsers() {
if (applicationStatistics != null) {
applicationStatistics.incrListUserTimes();
......@@ -142,11 +146,28 @@ public class AccountService {
}
}
//--------------------//
// Role Management //
//--------------------//
public List<Role> getAllRoles() {
return (List<Role>) 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;
......
......@@ -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<User> users = accountService.getAllUser();
List<User> 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<Long> 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");
}
}
......@@ -12,7 +12,7 @@
</bean>
<!-- 項目自定义的Realm -->
<bean id="shiroDbRealm" class="org.springside.examples.showcase.service.ShiroDbRealm" depends-on="userDao">
<bean id="shiroDbRealm" class="org.springside.examples.showcase.service.ShiroDbRealm" depends-on="userDao,roleDao">
<property name="accountService" ref="accountService"/>
</bean>
......
......@@ -56,6 +56,12 @@
<input type="password" id="plainPassword" name="plainPassword" class="input-large" placeholder="...Leave it blank if no change"/>
</div>
</div>
<div class="control-group">
<label for="groupList" class="control-label">角色:</label>
<div class="controls">
<form:bscheckboxes path="roleList" items="${allRoles}" itemLabel="name" itemValue="id" />
</div>
</div>
<div class="control-group">
<label for="status" class="control-label">状态:</label>
<div class="controls">
......
......@@ -15,7 +15,7 @@
<body>
<h1>帐号管理</h1>
<c:if test="${not empty message}">
<div id="message" class="success">${message}</div>
<div id="message" class="alert alert-success"><button data-dismiss="alert" class="close">×</button>${message}</div>
</c:if>
<table class="table table-striped table-bordered table-condensed">
......
......@@ -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"));
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册