AbstractSchemaGenerator.php 6.74 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
<?php
/**
 *
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Webapi\Model;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\Webapi\Authorization;
use Magento\Framework\Webapi\CustomAttribute\ServiceTypeListInterface;
use Magento\Webapi\Model\Cache\Type\Webapi;

/**
 * Abstract API schema generator.
 */
abstract class AbstractSchemaGenerator
{
    /**
     * @var Webapi
     */
    protected $cache;

    /**
     * @var \Magento\Framework\Reflection\TypeProcessor
     */
    protected $typeProcessor;

    /**
     * @var \Magento\Framework\Webapi\CustomAttribute\ServiceTypeListInterface
     */
    protected $serviceTypeList;

    /**
     * @var ServiceMetadata
     */
    protected $serviceMetadata;

    /**
     * @var Authorization
     */
    protected $authorization;

    /**
     * Instance of serializer.
     *
     * @var Json
     */
    private $serializer;

    /**
     * Initialize dependencies.
     *
     * @param Webapi $cache
     * @param \Magento\Framework\Reflection\TypeProcessor $typeProcessor
     * @param \Magento\Framework\Webapi\CustomAttribute\ServiceTypeListInterface $serviceTypeList
     * @param \Magento\Webapi\Model\ServiceMetadata $serviceMetadata
     * @param Authorization $authorization
     * @param Json|null $serializer
     */
    public function __construct(
        Webapi $cache,
        \Magento\Framework\Reflection\TypeProcessor $typeProcessor,
        ServiceTypeListInterface $serviceTypeList,
        ServiceMetadata $serviceMetadata,
        Authorization $authorization,
        Json $serializer = null
    ) {
        $this->cache = $cache;
        $this->typeProcessor = $typeProcessor;
        $this->serviceTypeList = $serviceTypeList;
        $this->serviceMetadata = $serviceMetadata;
        $this->authorization = $authorization;
        $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
    }

    /**
     * Retrieve a list of all services.
     *
     * @return string[]
     */
    public function getListOfServices()
    {
        return array_keys($this->serviceMetadata->getServicesConfig());
    }

    /**
     * Generate schema based on requested services (uses cache)
     *
     * @param array $requestedServices
     * @param string $requestScheme
     * @param string $requestHost
     * @param string $endPointUrl
     * @return string
     */
    public function generate($requestedServices, $requestScheme, $requestHost, $endPointUrl)
    {
        /** Sort requested services by names to prevent caching of the same schema file more than once. */
        ksort($requestedServices);
        $prefix = get_class($this) . $this->serializer->serialize($requestedServices);
        $cacheId = $this->cache->generateCacheIdUsingContext($prefix);
        $cachedSchemaContent = $this->cache->load($cacheId);
        if ($cachedSchemaContent !== false) {
            return $cachedSchemaContent;
        }
        $allowedServicesMetadata = $this->getAllowedServicesMetadata($requestedServices);
        $this->collectCallInfo($allowedServicesMetadata);
        $schemaContent = $this->generateSchema($allowedServicesMetadata, $requestScheme, $requestHost, $endPointUrl);
        $this->cache->save($schemaContent, $cacheId, [Webapi::CACHE_TAG]);

        return $schemaContent;
    }

    /**
     * Generate schema based on requested services' metadata.
     *
     * @param array $requestedServiceMetadata
     * @param string $requestScheme
     * @param string $requestHost
     * @param string $requestUri
     * @return string
     */
    abstract protected function generateSchema($requestedServiceMetadata, $requestScheme, $requestHost, $requestUri);

    /**
     * Get service metadata
     *
     * @param string $serviceName
     * @return array
     */
    abstract protected function getServiceMetadata($serviceName);

    /**
     * Get name of complexType for message element.
     *
     * @param string $messageName
     * @return string
     */
    public function getElementComplexTypeName($messageName)
    {
        return ucfirst($messageName);
    }

    /**
     * Collect data about complex types call info.
     *
     * Walks through all requested services and checks all methods 'in' and 'out' parameters.
     *
     * @param array $requestedServiceMetadata
     * @return void
     */
    protected function collectCallInfo($requestedServiceMetadata)
    {
        foreach ($requestedServiceMetadata as $serviceName => $serviceData) {
            foreach ($serviceData['methods'] as $methodName => $methodData) {
                $this->typeProcessor->processInterfaceCallInfo($methodData['interface'], $serviceName, $methodName);
            }
        }
    }

    /**
     * Retrieve information only about those services/methods which are visible to current user.
     *
     * @param string[] $requestedServices
     * @return array
     */
    protected function getAllowedServicesMetadata($requestedServices)
    {
        $allowedServicesMetadata = [];
        foreach ($requestedServices as $serviceName) {
            $serviceMetadata = $this->getServiceMetadata($serviceName);
            foreach ($serviceMetadata[ServiceMetadata::KEY_SERVICE_METHODS] as $methodName => $methodData) {
                if (!$this->authorization->isAllowed($methodData[ServiceMetadata::KEY_ACL_RESOURCES])) {
                    unset($serviceMetadata[ServiceMetadata::KEY_SERVICE_METHODS][$methodName]);
                }
            }
            if (!empty($serviceMetadata[ServiceMetadata::KEY_SERVICE_METHODS])) {
                $this->removeRestrictedRoutes($serviceMetadata);
                $allowedServicesMetadata[$serviceName] = $serviceMetadata;
            }
        }
        return $allowedServicesMetadata;
    }

    /**
     * Remove routes which should not be visible to current user.
     *
     * @param array &$serviceMetadata
     * @return void
     */
    protected function removeRestrictedRoutes(&$serviceMetadata)
    {
        $allowedMethodNames = array_keys($serviceMetadata[ServiceMetadata::KEY_SERVICE_METHODS]);
        /** Remove routes which reference methods not visible to current user */
        if (isset($serviceMetadata[ServiceMetadata::KEY_ROUTES])) {
            foreach ($serviceMetadata[ServiceMetadata::KEY_ROUTES] as $path => &$routeGroup) {
                foreach ($routeGroup as $httpMethod => &$route) {
                    if (!in_array($route[ServiceMetadata::KEY_ROUTE_METHOD], $allowedMethodNames)) {
                        unset($routeGroup[$httpMethod]);
                    }
                }
                if (empty($routeGroup)) {
                    unset($serviceMetadata[ServiceMetadata::KEY_ROUTES][$path]);
                }
            }
        }
    }
}