package com.tmerclub.cloud.common.security.filter.dubbo;


import com.tmerclub.cloud.api.auth.bo.UidInfoBO;
import com.tmerclub.cloud.api.platform.vo.UriPermissionVO;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.i18n.I18nMessage;
import com.tmerclub.cloud.common.security.AuthTokenContext;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.security.PmsContext;
import io.seata.core.constants.DubboConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;


/**
 * @author lanhai
 */
@Activate(group = DubboConstants.CONSUMER)
public class UserInfoConsumerFilter implements Filter {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserInfoConsumerFilter.class);

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        UidInfoBO uidInfoBO = AuthUserContext.get();
        String token = AuthTokenContext.getToken();
        List<UriPermissionVO> uriPermissionVOList = PmsContext.get();
        invocation.setObjectAttachment(Constant.DUBBO_LANG_KEY, I18nMessage.getLang());
        if (uriPermissionVOList != null) {
            invocation.setObjectAttachment(Constant.DUBBO_PREMS_KEY, uriPermissionVOList);
        }
        if (token != null) {
            invocation.setObjectAttachment(Constant.DUBBO_TOKEN_KEY, token);
        }
        if (uidInfoBO != null) {
            invocation.setObjectAttachment(Constant.DUBBO_USER_KEY, uidInfoBO);
        }
        Result invoke = invoker.invoke(invocation);
        return invoke;
    }
}
