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.User;
import org.ccpit.base.user.UserService;
import org.ccpit.base.usergroup.UserGroup;
import org.ccpit.base.usergroup.UserGroupDao;
import org.ccpit.base.usergroup.UserGroupService;

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 SecurityAnyGroupTag extends CmsTagSupport {
    private String groups;

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

        String[] g = getGroups().split(SPLIT);
        UserGroupService userGroupService = ac.getBean(UserGroupService.class);
        boolean can = false;
        User user = getUser();
        for (String gs:g){
            if (StringUtils.isNumeric(gs)){
                UserGroup userGroup = userGroupService.get(Long.parseLong(gs));
                if (userGroup.getUsers().contains(user)){
                    can = true;
                    break;
                }

            }
        }
        return can;
    }

    public String getGroups() {
        return groups;
    }

    public void setGroups(String groups) {
        this.groups = groups;
    }
}
