Completed
Push — dev/product_visibility ( 1ee472 )
by Kiyotaka
06:23
created

IpAddrListener::onKernelRequest()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 1
dl 0
loc 24
rs 8.9137
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\EventListener;
15
16
use Eccube\Common\EccubeConfig;
17
use Eccube\Request\Context;
18
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
20
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
21
22
class IpAddrListener implements EventSubscriberInterface
23
{
24
    /**
25
     * @var EccubeConfig
26
     */
27
    protected $eccubeConfig;
28
29
    /**
30
     * @var Context
31
     */
32
    protected $requestContext;
33
34
    public function __construct(EccubeConfig $eccubeConfig, Context $requestContext)
35
    {
36
        $this->eccubeConfig = $eccubeConfig;
37
        $this->requestContext = $requestContext;
38
    }
39
40
    public function onKernelRequest(GetResponseEvent $event)
41
    {
42
        if (!$event->isMasterRequest()) {
43
            return;
44
        }
45
46
        if (!$this->requestContext->isAdmin()) {
47
            return;
48
        }
49
50
        // IPアドレス許可リストを確認
51
        $allowHosts = $this->eccubeConfig['eccube_admin_allow_hosts'];
52
53
        if (!empty($allowHosts) && array_search($event->getRequest()->getClientIp(), $allowHosts) === false) {
54
            throw new AccessDeniedHttpException();
55
        }
56
57
        // IPアドレス拒否リストを確認
58
        $denyHosts = $this->eccubeConfig['eccube_admin_deny_hosts'];
59
60
        if (array_search($event->getRequest()->getClientIp(), $denyHosts) !== false) {
61
            throw new AccessDeniedHttpException();
62
        }
63
    }
64
65
    public static function getSubscribedEvents()
66
    {
67
        return [
68
            'kernel.request' => ['onKernelRequest', 512],
69
        ];
70
    }
71
}
72