Passed
Pull Request — master (#2138)
by Janko
39:06 queued 27:26
created

AccessCheck::checkUserAccess()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 8

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 15
c 2
b 0
f 0
nc 6
nop 2
dl 0
loc 31
ccs 15
cts 15
cp 1
crap 8
rs 8.4444
1
<?php
2
3
namespace Stu\Module\Control;
4
5
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use request;
7
use Stu\Lib\AccountNotVerifiedException;
8
use Stu\Module\Config\StuConfigInterface;
9
use Stu\Module\PlayerSetting\Lib\UserEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Stu\Orm\Repository\SessionStringRepositoryInterface;
11
12
class AccessCheck implements AccessCheckInterface
13
{
14 17
    public function __construct(
15
        private readonly SessionStringRepositoryInterface $sessionStringRepository,
16
        private readonly StuConfigInterface $stuConfig
17 17
    ) {}
18
19 15
    #[Override]
20
    public function checkUserAccess(
21
        ControllerInterface $controller,
22
        GameControllerInterface $game
23
    ): bool {
24
25 15
        if ($controller instanceof NoAccessCheckControllerInterface) {
26 1
            return true;
27
        }
28
29 14
        $hasUser = $game->hasUser();
30 14
        if ($hasUser && $game->getUser()->getState() === UserEnum::USER_STATE_ACCOUNT_VERIFICATION) {
31 1
            throw new AccountNotVerifiedException();
32
        }
33
34 13
        if (!$this->isSessionValid($controller, $hasUser, $game)) {
35 3
            return false;
36
        }
37
38 10
        if (!$controller instanceof AccessCheckControllerInterface) {
39 4
            return true;
40
        }
41
42 6
        $feature = $controller->getFeatureIdentifier();
43 6
        if ($hasUser && $this->isFeatureGranted($game->getUser()->getId(), $feature, $game)) {
44 2
            return true;
45
        }
46
47 4
        $game->addInformation('[b][color=#ff2626]Aktion nicht möglich, Spieler ist nicht berechtigt![/color][/b]');
48
49 4
        return false;
50
    }
51
52 13
    private function isSessionValid(
53
        ControllerInterface $controller,
54
        bool $hasUser,
55
        GameControllerInterface $game
56
    ): bool {
57
58 13
        if (!$controller instanceof ActionControllerInterface) {
59 7
            return true;
60
        }
61
62 6
        if (!$controller->performSessionCheck()) {
63 2
            return true;
64
        }
65
66 4
        $sessionString = request::indString('sstr');
67 4
        if (!$sessionString) {
0 ignored issues
show
introduced by
The condition $sessionString is always false.
Loading history...
68 2
            return false;
69
        }
70
71 2
        if (!$hasUser) {
72 1
            return false;
73
        }
74
75 1
        return $this->sessionStringRepository->isValid(
76 1
            $sessionString,
77 1
            $game->getUser()->getId()
78 1
        );
79
    }
80
81 7
    #[Override]
82
    public function isFeatureGranted(int $userId, AccessGrantedFeatureEnum $feature, GameControllerInterface $game): bool
83
    {
84 7
        if ($game->isAdmin()) {
85 1
            return true;
86
        }
87
88 6
        $grantedFeatures = $this->stuConfig->getGameSettings()->getGrantedFeatures();
89 6
        foreach ($grantedFeatures as $entry) {
90
            if (
91 5
                $entry['feature'] === $feature->name
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Stu\Module\Control\AccessGrantedFeatureEnum.
Loading history...
92 5
                && in_array($userId, $entry['userIds'])
93
            ) {
94 1
                return true;
95
            }
96
        }
97
98 5
        return false;
99
    }
100
}
101