Completed
Push — master ( fd5325...d7e193 )
by Schlaefer
05:54 queued 03:00
created

ActionAuthorizationComponent::isAuthorized()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 11
nop 2
dl 0
loc 21
rs 8.6506
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace App\Controller\Component;
14
15
use Cake\Controller\Component;
16
use Saito\App\Registry;
17
use Saito\User\CurrentUser\CurrentUserInterface;
18
19
class ActionAuthorizationComponent extends Component
20
{
21
22
    /**
23
     * Check if user is authorized to use the controller-action
24
     *
25
     * @param CurrentUserInterface $user current-user
26
     * @param string $action current controller action
27
     * @return bool true is authorized, false otherwise
28
     */
29
    public function isAuthorized(CurrentUserInterface $user, $action)
30
    {
31
        $Controller = $this->_registry->getController();
32
        if (isset($Controller->actionAuthConfig)
33
            && isset($Controller->actionAuthConfig[$action])) {
34
            $requiredRole = $Controller->actionAuthConfig[$action];
35
36
            return Registry::get('Permission')
37
                ->check($user->getRole(), $requiredRole);
38
        }
39
40
        $prefix = $this->request->getParam('prefix');
0 ignored issues
show
Deprecated Code introduced by
The property Cake\Controller\Component::$request has been deprecated with message: 3.4.0 Storing references to the request is deprecated. Use Component::getController() or callback $event->getSubject() to access the controller & request instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
41
        $plugin = $this->request->getParam('plugin');
0 ignored issues
show
Deprecated Code introduced by
The property Cake\Controller\Component::$request has been deprecated with message: 3.4.0 Storing references to the request is deprecated. Use Component::getController() or callback $event->getSubject() to access the controller & request instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
42
        $isAdminRoute = ($prefix && strtolower($prefix) === 'admin')
43
            || ($plugin && strtolower($plugin) === 'admin');
44
        if ($isAdminRoute) {
45
            return $user->permission('saito.core.admin.backend');
46
        }
47
48
        return true;
49
    }
50
}
51