ApiController::allowed()   B
last analyzed

Complexity

Conditions 10
Paths 21

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 28
c 2
b 0
f 0
nc 21
nop 0
dl 0
loc 39
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        $action = $action != null && strpos($action, '/') > 0 ? explode('/', $action)[0] : $action;
75
        $blockedMethods = (array)Configure::read('ApiProxy.blocked', [
76
            'objects' => ['GET', 'POST', 'PATCH', 'DELETE'],
77
            'users' => ['GET', 'POST', 'PATCH', 'DELETE'],
78
        ]);
79
        $blocked = in_array($method, $blockedMethods[$action] ?? []);
80
        $modules = $this->viewBuilder()->getVar('modules');
81
        $modules = array_values($modules);
82
        $modules = array_merge(
83
            (array)Hash::combine($modules, '{n}.name', '{n}.hints.allow'),
84
            ['history' => ['GET'], 'model' => ['GET']],
85
        );
86
        $allowedMethods = array_merge(
87
            (array)Hash::get($modules, $action, []),
88
            (array)Hash::get((array)Configure::read('ApiProxy.allowed'), $action, []),
89
        );
90
        $allowed = in_array($method, $allowedMethods);
91
92
        return $allowed && !$blocked;
93
    }
94
}
95