package org.ccpit.base.security;

import org.apache.commons.lang.StringUtils;
import org.ccpit.base.user.User;
import org.ccpit.base.usergroup.UserGroup;
import org.ccpit.base.usergroup.UserGroupService;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * Created by Administrator on 2015/9/7.
 */
public class SecurityAllGroupTag 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 = true;
        User user = getUser();
        for (String gs:g){
            if (StringUtils.isNumeric(gs)){
                UserGroup userGroup = userGroupService.get(Long.parseLong(gs));
                if (!userGroup.getUsers().contains(user)){
                    can = false;
                    break;
                }

            }
        }
        return can;
    }

    public String getGroups() {
        return groups;
    }

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