package com.zhiqiyun.open.router.config;

import com.dliyun.oap.framework.security.ServiceAccessController;
import com.zhiqiyun.open.core.models.app.AppKeyInfo;
import com.zhiqiyun.open.core.service.AppKeyInfoService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class ServicePermissionController implements ServiceAccessController {

    @Autowired
    private AppKeyInfoService appKeyInfoService;

    public boolean isAppGranted(String appKey, String method, String version) {
        AppKeyInfo appKeyInfo = this.appKeyInfoService.findByAppKey(appKey);
        if (appKeyInfo == null) {
            return false;
        }
        List<String> permissions = appKeyInfo.getPermissions();
        return permissions != null && permissions.contains(String.format("%s:%s", method, version));
    }
}
