package ccpit.base.security;

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

/**
 * 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;
    }
}
