com.xyxy.platform.examples.showcase.web.UserController.java Source code

Java tutorial

Introduction

Here is the source code for com.xyxy.platform.examples.showcase.web.UserController.java

Source

/*******************************************************************************
 * Copyright (c) 2005, 2014
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.xyxy.platform.examples.showcase.web;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.validation.Valid;

import com.xyxy.platform.examples.showcase.entity.User;
import com.xyxy.platform.examples.showcase.service.AccountService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.xyxy.platform.examples.showcase.entity.Role;
import com.xyxy.platform.modules.core.web.Servlets;

import com.google.common.collect.Maps;

@Controller
@RequestMapping(value = "/account/user")
public class UserController {

    private static Map<String, String> allStatus = Maps.newHashMap();

    static {
        allStatus.put("enabled", "");
        allStatus.put("disabled", "");
    }

    @Autowired
    private AccountService accountService;

    // ReuireRolesOr?And.
    @RequiresRoles(value = { "Admin", "User" }, logical = Logical.OR)
    @RequestMapping(value = "")
    public String list(Model model, ServletRequest request) {

        Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");

        List<User> users = accountService.searchUser(searchParams);
        model.addAttribute("users", users);
        model.addAttribute("allStatus", allStatus);
        return "account/userList";
    }

    @RequiresRoles("Admin")
    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String updateForm(@PathVariable("id") Long id, Model model) {
        model.addAttribute("user", accountService.getUser(id));
        model.addAttribute("allStatus", allStatus);
        model.addAttribute("allRoles", accountService.getAllRole());
        return "account/userForm";
    }

    /**
     * ?checkBox roleList.
     */
    @RequiresPermissions("user:edit")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    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";
    }

    @RequestMapping(value = "checkLoginName")
    @ResponseBody
    public String checkLoginName(@RequestParam("oldLoginName") String oldLoginName,
            @RequestParam("loginName") String loginName) {
        if (loginName.equals(oldLoginName)) {
            return "true";
        } else if (accountService.findUserByLoginName(loginName) == null) {
            return "true";
        }

        return "false";
    }

    /**
     * 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));
        }
    }

    /**
     * ?roleList??
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.setDisallowedFields("roleList");
    }
}