ManagerCtrl.java 5.51 KB
package com.ui.ctrl;


import com.ui.User.MD5Util;
import com.ui.User.UserAuthLocal;
import com.ui.contants.HttpUriContants;
import com.ui.http.HttpRestClient;
import com.ui.model.BaseResponse;
import com.ui.model.req.AuthModule;
import com.ui.model.req.PageRequest;
import com.ui.model.req.User;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("manage")
public class ManagerCtrl {

    Logger log = LoggerFactory.getLogger(ManagerCtrl.class);

    @Autowired
    HttpRestClient httpRestClient;

    @Autowired
    UserAuthLocal userAuthLocal;

    @RequestMapping("/toUser")
    public ModelAndView toAddUser() {
        return new ModelAndView("manager/user");
    }

    @RequestMapping("/toModule")
    public ModelAndView toAddModule(Model model) {
        BaseResponse baseResponse = httpRestClient.defaultPost(HttpUriContants.GET_All_MODULEGROUP, null, BaseResponse.class);
        if (baseResponse != null) {
            model.addAttribute("modulegroups", baseResponse.getData());
        }
        return new ModelAndView("manager/module");
    }

    @RequestMapping("/saveUser")
    @ResponseBody
    public BaseResponse saveUser(@RequestBody User user, HttpSession session) {
        BaseResponse baseResponse = null;
        if (StringUtils.isNotBlank(user.getModules()) && StringUtils.endsWith(user.getModules(), ",")) {
            user.setModules(user.getModules().substring(0, user.getModules().length() - 1));
        }
        if (StringUtils.isNotBlank(user.getModuleGroups()) && StringUtils.endsWith(user.getModuleGroups(), ",")) {
            user.setModuleGroups(user.getModuleGroups().substring(0, user.getModuleGroups().length() - 1));
        }
        if (user.getId() < 1) {//add
            if (userAuthLocal.getUserByname(user.getName()) != null) {
                return new BaseResponse(201, "用户已经存在");
            }
            user.setPwd(RandomStringUtils.random(8, true, true));
            baseResponse = httpRestClient.defaultPost(HttpUriContants.USER_INSERT, user, BaseResponse.class);
        } else {
            User u = (User) session.getAttribute("user");
            String username = u.getName();
            if ("admin".equals(user.getName()) && !"admin".equals(username)) {
                return new BaseResponse(201, "此用户无法修改");
            }
            baseResponse = httpRestClient.defaultPost(HttpUriContants.USER_UPDATE, user, BaseResponse.class);

        }
        userAuthLocal.flushUser(user.getName());
        return baseResponse;
    }

    @RequestMapping("/getUsers")
    @ResponseBody
    public BaseResponse getUsers(PageRequest req, String keys) {
        BaseResponse response = httpRestClient.defaultPost(HttpUriContants.GET_USERS + "?keys="+keys, req, BaseResponse.class);
        return response;
    }
    
    @RequestMapping("/getAllUser")
    @ResponseBody
    public BaseResponse getAllUser(PageRequest req) {
        BaseResponse response = httpRestClient.defaultPost(HttpUriContants.GET_USERS_ALL, req, BaseResponse.class);
        return response;
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public BaseResponse deleteUser(String name) {
        if ("admin".equals(name)) {
            return new BaseResponse(201, "此用户无法删除");
        }
        Map map = new HashMap<>();
        map.put("name", name);
        BaseResponse response = httpRestClient.defaultGet(HttpUriContants.USER_DELETE_NAME, BaseResponse.class, map);
        userAuthLocal.flushUser(name);
        return response;
    }

    @RequestMapping("/saveAuthModule")
    @ResponseBody
    public BaseResponse saveAuthModule(@RequestBody AuthModule authModule) {
        BaseResponse baseResponse = null;
        if (authModule.getId() < 1) {//add
            if (userAuthLocal.getAuthModuleByname(authModule.getModuleName()) != null) {
                return new BaseResponse(201, "模块已经存在");
            }
            baseResponse = httpRestClient.defaultPost(HttpUriContants.MODULE_INSERT, authModule, BaseResponse.class);
        } else {
            baseResponse = httpRestClient.defaultPost(HttpUriContants.MODULE_UPDATE, authModule, BaseResponse.class);

        }
        userAuthLocal.flushModule(authModule.getModuleName());
        return baseResponse;
    }

    @RequestMapping("/getAuthModules")
    @ResponseBody
    public BaseResponse getAuthModules(PageRequest req) {
        BaseResponse response = httpRestClient.defaultPost(HttpUriContants.GET_MODULES, req, BaseResponse.class);
        return response;
    }

    @RequestMapping("/deleteAuthModule")
    @ResponseBody
    public BaseResponse deleteAuthModule(String name) {
        if ("manage".equals(name)) {
            return new BaseResponse(201, "此模块无法delete");
        }
        Map map = new HashMap<>();
        map.put("name", name);
        BaseResponse response = httpRestClient.defaultGet(HttpUriContants.MODULE_DELETE_NAME, BaseResponse.class, map);
        userAuthLocal.flushModule(name);
        return response;
    }

}