package com.manage.controller;

import com.manage.vo.Power_UserVo;
import com.manage.vo.User_Dept_Menu;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.util.List;

/**
 * @ProjectName:
 * @Description:
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2019/8/14 11:15
 * @UpdateUser: 曾文和
 * @UpdateDate: 2019/8/14 11:15
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class HasAnyPermission extends TagSupport {
    private String permissions;

    public String getPermissions() {
        return permissions;
    }

    public void setPermissions(String permissions) {
        this.permissions = permissions;
    }

    @Override
    public int doStartTag() throws JspException {
        HttpSession session = pageContext.getSession();
        String[] arrPermissions = permissions.split(",");
        //用户是否分配了权限　
        Power_UserVo user = (Power_UserVo)session.getAttribute("CURRENT_USER");
        //系统管理员全部放过
        if(user.getRoleId() == 0){
            return TagSupport.EVAL_BODY_INCLUDE;
        }
        List<User_Dept_Menu> hasPermissions = user.getMenuList();
        if(null != hasPermissions && !hasPermissions.isEmpty()){
            for (User_Dept_Menu pm : hasPermissions){
                if(StringUtils.isNoneBlank(pm.getMenuUrl())){
                    for (String psermission : arrPermissions){
                        //用户分配的权限中，是否包含该权限
                        if (pm.getMenuUrl().equals(psermission)){
                            return TagSupport.EVAL_BODY_INCLUDE;
                        }
                    }
                }
            }
        }
        return TagSupport.SKIP_BODY;
    }
}
