package com.ym.mec.common.security;

import java.util.Collection;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Component("pcs")
public class PermissionCheckService {

	public boolean hasPermissions(String... permissions) {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (authentication == null) {
			return false;
		}

		Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();

		for (String perm : permissions) {
			for (GrantedAuthority authority : authorities) {
				if (StringUtils.equalsIgnoreCase(perm, authority.getAuthority())) {
					return true;
				}
			}
		}

		return false;
	}

	public boolean hasRoles(String... roles) {

		return hasPermissions(roles);
	}

}
