package org.ccpit.base.security;

import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.ccpit.base.role.Role;
import org.ccpit.base.utils.UrlRolesMapper;
import org.ccpit.base.utils.UserRolesUtil;
import org.springframework.stereotype.Component;

/**
 * 自定义标签 判断用户权限
 * Created by Administrator on 2015/9/7.
 */
@Component
public class SecurityUriTag extends CmsTagSupport{
    /** Uri to check */
    private String uri;

    @Override
    public boolean validate() {
        Set<Role> roles = ac.getBean(UrlRolesMapper.class).getRoles(getUri());
        if (roles==null || roles.isEmpty()){
        	return false;
        }

        UserRolesUtil userRolesUtil = ac.getBean(UserRolesUtil.class);
        Set<Role> roleSet = userRolesUtil.getAllRoles((HttpServletRequest) pageContext.getRequest());
        if (roleSet.isEmpty()){
        	return false;
        }
        return CollectionUtils.containsAny(roles,roleSet);
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }
}
