Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2014 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package com.gamewin.weixin.web.account; import java.util.List; import java.util.Map; import javax.servlet.ServletRequest; import javax.validation.Valid; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authz.annotation.Logical; import org.apache.shiro.authz.annotation.RequiresRoles; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; 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.RequestMethod; 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.modules.web.Servlets; import com.gamewin.weixin.entity.User; import com.gamewin.weixin.model.UserDto; import com.gamewin.weixin.service.account.AccountService; import com.gamewin.weixin.service.account.ShiroDbRealm.ShiroUser; import com.github.pagehelper.PageInfo; import com.google.common.collect.Maps; /** * ??Controller. * * @author calvin */ @Controller @RequestMapping(value = "/admin/user") public class UserAdminController { private static final String PAGE_SIZE = "10"; private static Map<String, String> sortTypes = Maps.newLinkedHashMap(); static { sortTypes.put("auto", ""); sortTypes.put("title", ""); } private static Map<String, String> allStatus = Maps.newHashMap(); static { allStatus.put("enabled", ""); allStatus.put("disabled", ""); allStatus.put("Audit", ""); } @Autowired private AccountService accountService; @RequestMapping(value = "findUserTree") @ResponseBody public String findUserTree(@RequestParam("id") Long id) { //UserTree return accountService.getUserTree2(id); } @RequiresRoles(value = { "admin", "TwoAdmin", "ThreeAdmin" }, logical = Logical.OR) @RequestMapping(value = "auditUserlist", method = RequestMethod.GET) public String auditUserlist(@RequestParam(value = "page", defaultValue = "1") int pageNumber, @RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize, @RequestParam(value = "sortType", defaultValue = "auto") String sortType, Model model, ServletRequest request) { Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_"); ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal(); Page<User> users = null; if ("admin".equals(user.getRoles())) { users = accountService.getUserByAuditUserAdminlist(user.id, searchParams, pageNumber, pageSize, sortType); } else { users = accountService.getUserByAuditUserlist(user.id, searchParams, pageNumber, pageSize, sortType); } model.addAttribute("users", users); model.addAttribute("sortType", sortType); model.addAttribute("sortTypes", sortTypes); model.addAttribute("allStatus", allStatus); // ?????URL model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_")); return "audit/auditUserList"; } @RequiresRoles(value = { "admin", "TwoAdmin", "ThreeAdmin" }, logical = Logical.OR) @RequestMapping(method = RequestMethod.GET) public String list(@RequestParam(value = "page", defaultValue = "1") int pageNumber, @RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize, @RequestParam(value = "sortType", defaultValue = "auto") String sortType, Model model, ServletRequest request) { Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_"); ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal(); String usertype = user.getRoles(); List<User> users = null; if ("admin".equals(usertype)) { users = accountService.getUserAllUserlist(searchParams, pageNumber, pageSize, sortType); } else if ("TwoAdmin".equals(usertype) || "ThreeAdmin".equals(usertype)) { users = accountService.getUserByUpUserlist(user.id, searchParams, pageNumber, pageSize, sortType); } PageInfo<User> page = new PageInfo<User>(users); model.addAttribute("page", page); model.addAttribute("usersx", users); model.addAttribute("sortType", sortType); model.addAttribute("sortTypes", sortTypes); model.addAttribute("allStatus", allStatus); // ?????URL model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_")); return "account/adminUserList"; } @RequiresRoles("admin") @RequestMapping(value = "update/{id}", method = RequestMethod.GET) public String updateForm(@PathVariable("id") Long id, Model model) { model.addAttribute("user", accountService.getUser(id)); List<UserDto> userdto = accountService.getUserByUpAdminUserlist(); model.addAttribute("userdto", userdto); return "account/adminUserForm"; } @RequiresRoles("admin") @RequestMapping(value = "update", method = RequestMethod.POST) public String update(@Valid @ModelAttribute("user") User user, RedirectAttributes redirectAttributes, ServletRequest request) { String upuserId = request.getParameter("upuserId"); if (!StringUtils.isEmpty(upuserId)) { User upuser = accountService.getUser(Long.parseLong(upuserId)); if (upuser != null && ("TwoAdmin".equals(upuser.getRoles()) || "ThreeAdmin".equals(upuser.getRoles()))) { user.setUpuser(upuser); } } accountService.updateUser(user); redirectAttributes.addFlashAttribute("message", "" + user.getLoginName() + "?"); return "redirect:/admin/user"; } @RequiresRoles("admin") @RequestMapping(value = "upTwoAdmin/{id}", method = RequestMethod.GET) public String update(@PathVariable("id") Long id, Model model, RedirectAttributes redirectAttributes) { User user = accountService.getUser(id); if (user != null && "ThreeAdmin".equals(user.getRoles())) { user.setRoles("TwoAdmin"); accountService.updateUser(user); } redirectAttributes.addFlashAttribute("message", "" + user.getLoginName() + "?"); return "redirect:/admin/user"; } @RequiresRoles(value = { "admin", "TwoAdmin", "ThreeAdmin" }, logical = Logical.OR) @RequestMapping(value = "disabled/{id}") public String disabled(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) { User user = accountService.getUser(id); user.setStatus("disabled"); accountService.updateUser(user); redirectAttributes.addFlashAttribute("message", "" + user.getLoginName() + "?"); return "redirect:/admin/user"; } @RequiresRoles(value = { "admin", "TwoAdmin", "ThreeAdmin" }, logical = Logical.OR) @RequestMapping(value = "auditPass/{id}") public String auditPass(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) { User user = accountService.getUser(id); ShiroUser nowuser = (ShiroUser) SecurityUtils.getSubject().getPrincipal(); if ("admin".equals(nowuser.getRoles()) || user.getUpuser().getLoginName().equals(nowuser.getLoginName())) { user.setStatus("enabled"); accountService.updateUser(user); redirectAttributes.addFlashAttribute("message", "" + user.getLoginName() + "?"); return "redirect:/admin/user/auditUserlist"; } else { redirectAttributes.addFlashAttribute("message", "??!"); return "redirect:/admin/user/auditUserlist"; } } @RequiresRoles(value = { "admin", "TwoAdmin", "ThreeAdmin" }, logical = Logical.OR) @RequestMapping(value = "delete/{id}") public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) { User user = accountService.getUser(id); accountService.deleteUser(user); redirectAttributes.addFlashAttribute("message", "" + user.getLoginName() + "?"); return "redirect:/admin/user"; } /** * RequestMapping?Model, Struts2 Preparable,?formid?User,?Form?? * update()formidupdate. */ @ModelAttribute public void getUser(@RequestParam(value = "id", defaultValue = "-1") Long id, Model model) { if (id != -1) { model.addAttribute("user", accountService.getUser(id)); } } }