Passed
Push — master ( 84cb95...42814d )
by Dante
02:12
created

ApiController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeFilter() 0 9 2
B allowed() 0 39 8
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2020 ChannelWeb Srl, Chialab Srl
7
 *
8
 * This file is part of BEdita: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
14
 */
15
namespace App\Controller;
16
17
use BEdita\WebTools\Controller\ApiProxyTrait;
18
use Cake\Core\Configure;
19
use Cake\Event\EventInterface;
20
use Cake\Http\Exception\UnauthorizedException;
21
use Cake\Http\Response;
22
use Cake\Utility\Hash;
23
24
/**
25
 * ApiController class.
26
 *
27
 * It proxies requests to BEdita API.
28
 * The response will be the raw json response of the API itself.
29
 * Links of API host is masked with manager host.
30
 */
31
class ApiController extends AppController
32
{
33
    use ApiProxyTrait;
34
35
    /**
36
     * @inheritDoc
37
     */
38
    public function beforeFilter(EventInterface $event): ?Response
39
    {
40
        parent::beforeFilter($event);
41
        if (!$this->allowed()) {
42
            throw new UnauthorizedException(__('You are not authorized to access this resource'));
43
        }
44
        $this->Security->setConfig('unlockedActions', ['post', 'patch', 'delete']);
45
46
        return null;
47
    }
48
49
    /**
50
     * Check if the request is allowed.
51
     *
52
     * @return bool
53
     */
54
    protected function allowed(): bool
55
    {
56
        // block requests from browser address bar
57
        $sameOrigin = (string)Hash::get((array)$this->request->getHeader('Sec-Fetch-Site'), 0) === 'same-origin';
58
        $noReferer = empty((array)$this->request->getHeader('Referer'));
59
        $isNavigate = in_array('navigate', (array)$this->request->getHeader('Sec-Fetch-Mode'));
60
        if (!$sameOrigin || $noReferer || $isNavigate) {
61
            return false;
62
        }
63
        /** @var \Authentication\Identity|null $user */
64
        $user = $this->Authentication->getIdentity();
65
        $roles = empty($user) ? [] : (array)$user->get('roles');
66
        if (empty($roles)) {
67
            return false;
68
        }
69
        if (in_array('admin', $roles)) {
70
            return true;
71
        }
72
        $method = $this->request->getMethod();
73
        $action = $this->request->getParam('pass')[0] ?? null;
74
        $blockedMethods = (array)Configure::read('ApiProxy.blocked', [
75
            'objects' => ['GET', 'POST', 'PATCH', 'DELETE'],
76
            'users' => ['GET', 'POST', 'PATCH', 'DELETE'],
77
        ]);
78
        $blocked = in_array($method, $blockedMethods[$action] ?? []);
79
        $modules = $this->viewBuilder()->getVar('modules');
80
        $modules = array_values($modules);
81
        $modules = (array)Hash::combine($modules, '{n}.name', '{n}.hints.allow');
82
        $modules = array_merge(
83
            $modules,
84
            [
85
                'history' => ['GET'],
86
                'model' => ['GET'],
87
            ],
88
        );
89
        $allowedMethods = (array)Hash::get($modules, $action, []);
90
        $allowed = in_array($method, $allowedMethods);
91
92
        return $allowed && !$blocked;
93
    }
94
}
95