package com.mossle.auth.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import com.mossle.api.tenant.TenantHolder;

import com.mossle.auth.component.RoleDefChecker;
import com.mossle.auth.persistence.domain.Perm;
import com.mossle.auth.persistence.domain.PermType;
import com.mossle.auth.persistence.domain.Role;
import com.mossle.auth.persistence.domain.RoleDef;
import com.mossle.auth.persistence.manager.PermManager;
import com.mossle.auth.persistence.manager.PermTypeManager;
import com.mossle.auth.persistence.manager.RoleDefManager;
import com.mossle.auth.persistence.manager.RoleManager;
import com.mossle.auth.support.CheckRoleException;

import com.mossle.core.spring.MessageHelper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("auth")
public class RolePermController {
    private static Logger logger = LoggerFactory
            .getLogger(RolePermController.class);
    private PermManager permManager;
    private RoleDefManager roleDefManager;
    private PermTypeManager permTypeManager;
    private MessageHelper messageHelper;
    private RoleDefChecker roleDefChecker;
    private RoleManager roleManager;
    private TenantHolder tenantHolder;

    @RequestMapping("role-perm-save")
    public String save(
            @RequestParam("id") Long id,
            Model model,
            @RequestParam(value = "selectedItem", required = false) List<Long> selectedItem,
            RedirectAttributes redirectAttributes) {
        if (selectedItem == null) {
            selectedItem = Collections.emptyList();
        }

        try {
            Role role = roleManager.get(id);
            RoleDef roleDef = role.getRoleDef();
            roleDefChecker.check(roleDef);
            roleDef.getPerms().clear();

            for (Long permId : selectedItem) {
                Perm perm = permManager.get(permId);
                roleDef.getPerms().add(perm);
            }

            roleDefManager.save(roleDef);
            messageHelper.addFlashMessage(redirectAttributes,
                    "core.success.save", "保存成功");
        } catch (CheckRoleException ex) {
            logger.warn(ex.getMessage(), ex);
            messageHelper.addFlashMessage(redirectAttributes, ex.getMessage());

            return input(id, model);
        }

        return "redirect:/auth/role-perm-input.do?id=" + id;
    }

    @RequestMapping("role-perm-input")
    public String input(@RequestParam("id") Long id, Model model) {
        Role role = roleManager.get(id);
        RoleDef roleDef = role.getRoleDef();
        List<Long> selectedItem = new ArrayList<Long>();

        for (Perm perm : roleDef.getPerms()) {
            selectedItem.add(perm.getId());
        }

        String hql = "from PermType where type=0 and tenantId=?";
        List<PermType> permTypes = permTypeManager.find(hql,
                tenantHolder.getTenantId());
        model.addAttribute("permTypes", permTypes);
        model.addAttribute("selectedItem", selectedItem);
        model.addAttribute("id", id);

        return "auth/role-perm-input";
    }

    // ~ ======================================================================
    @Resource
    public void setPermManager(PermManager permManager) {
        this.permManager = permManager;
    }

    @Resource
    public void setRoleDefManager(RoleDefManager roleDefManager) {
        this.roleDefManager = roleDefManager;
    }

    @Resource
    public void setRoleDefChecker(RoleDefChecker roleDefChecker) {
        this.roleDefChecker = roleDefChecker;
    }

    @Resource
    public void setPermTypeManager(PermTypeManager permTypeManager) {
        this.permTypeManager = permTypeManager;
    }

    @Resource
    public void setMessageHelper(MessageHelper messageHelper) {
        this.messageHelper = messageHelper;
    }

    @Resource
    public void setRoleManager(RoleManager roleManager) {
        this.roleManager = roleManager;
    }

    @Resource
    public void setTenantHolder(TenantHolder tenantHolder) {
        this.tenantHolder = tenantHolder;
    }
}

@@ -180,24 +180,68 @@
 antHolder;%0A%0A
+import com.mossle.auth.component.AuthCache;%0A
 import com.m
@@ -273,24 +273,24 @@
 DefChecker;%0A
-
 import com.m
@@ -476,16 +476,70 @@
 oleDef;%0A
+import com.mossle.auth.persistence.domain.UserStatus;%0A
 import c
@@ -1655,16 +1655,49 @@
 tHolder;
+%0A    private AuthCache authCache;
 %0A%0A    @R
@@ -2569,16 +2569,246 @@
 %22%E4%BF%9D%E5%AD%98%E6%88%90%E5%8A%9F%22);
+%0A%0A            for (Role roleInstance : roleDef.getRoles()) %7B%0A                for (UserStatus userStatus : roleInstance.getUserStatuses()) %7B%0A                    authCache.evictUserStatus(userStatus);%0A                %7D%0A            %7D
 %0A       
@@ -4730,24 +4730,24 @@
 ntHolder) %7B%0A
-
         this
@@ -4781,11 +4781,120 @@
 r;%0A    %7D
+%0A%0A    @Resource%0A    public void setAuthCache(AuthCache authCache) %7B%0A        this.authCache = authCache;%0A    %7D
 %0A%7D%0A

解决角色修改权限后，用户权限未刷新的问题