package org.ccpit.base.security;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.ccpit.base.role.Role;
import org.ccpit.base.user.UserService;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2015/9/7.
 */
public class SecurityAllRoleTag extends CmsTagSupport {
    private String roles;

    @Override
    public boolean validate() {
        if (roles==null||roles.isEmpty())
            return true;

        String[] roles = getRoles().split(SPLIT);
        List<Role> tagRoles = new ArrayList<Role>();
        for (String roleId:roles){
            Role r =new Role();
            if (StringUtils.isNumeric(roleId))
                r.setId(Long.parseLong(roleId));
            tagRoles.add(r);
        }
        Set<Role> userRoles = ac.getBean(UserService.class).getUserRolesIncludeGroup(getUser());
        boolean can = true;
        for (Role r:tagRoles){
            if (!userRoles.contains(r)){
                can = false;
                break;
            }
        }
        return can;
    }

    public String getRoles() {
        return roles;
    }

    public void setRoles(String roles) {
        this.roles = roles;
    }
}
