package com.inspect.common.security.aspect;

import com.inspect.base.core.exception.InnerAuthException;
import com.inspect.base.core.utils.ServletUtils;
import com.inspect.base.core.utils.StringUtils;
import com.inspect.common.security.annotation.InnerAuth;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class InnerAuthAspect implements Ordered {
    @Around("@annotation(innerAuth)")
    public Object innerAround(ProceedingJoinPoint point, InnerAuth innerAuth) throws Throwable {
        String source = ServletUtils.getRequest().getHeader("from-source");
        if (!StringUtils.equals("inner", source)) {
            throw new InnerAuthException("没有内部访问权限，不允许访问");
        } else {
            String userid = ServletUtils.getRequest().getHeader("user_id");
            String username = ServletUtils.getRequest().getHeader("username");
            if (!innerAuth.isUser() || !StringUtils.isEmpty(userid) && !StringUtils.isEmpty(username)) {
                return point.proceed();
            } else {
                throw new InnerAuthException("没有设置用户信息，不允许访问 ");
            }
        }
    }

    public int getOrder() {
        return -2147483647;
    }
}
