Passed
Push — master ( ca52c8...cf0bfa )
by Jan
12:46
created

EnforceTFARedirectHelper::checkifRolesAreRisky()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 15
rs 9.6111
cc 5
nc 8
nop 2
1
<?php
2
/*
3
 * Copyright (C)  2020-2022  Jan Böhmer
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace App\Services\UserSystem;
20
21
use App\Entity\User;
22
use LogicException;
23
24
class EnforceTFARedirectHelper
25
{
26
    /** @var bool */
27
    private $enforce_tfa;
28
29
    /** @var string[] */
30
    private $risky_roles;
31
32
    public function __construct(bool $enforce_tfa, array $risky_roles)
33
    {
34
        foreach ($risky_roles as $role) {
35
            if (!is_string($role)) {
36
                throw new LogicException('All Roles must be an string!');
37
            }
38
        }
39
40
        $this->enforce_tfa = $enforce_tfa;
41
        $this->risky_roles = $risky_roles;
42
    }
43
44
    /**
45
     * Check if the enforcement of TFA is enabled.
46
     * @return bool
47
     */
48
    public function isTFAEnforcementEnabled(): bool
49
    {
50
        return $this->enforce_tfa;
51
    }
52
53
    /**
54
     * Check if the given user has roles that are considered risky.
55
     * @param  User  $user
56
     * @return bool
57
     */
58
    public function checkIfUserHasRiskyRoles(User $user): bool
59
    {
60
        return $this->checkifRolesAreRisky($user->getRoles());
61
    }
62
63
    /**
64
     * Check if the given user needs a redirect to settings, because TFA enforcement is enabled and the user has risky roles.
65
     * @param  User  $user
66
     * @return bool
67
     */
68
    public function doesUserNeedRedirectForTFAEnforcement(User $user): bool
69
    {
70
        return $this->isTFAEnforcementEnabled()
71
            && !$user->isTFAEnabled()
72
            && $this->checkIfUserHasRiskyRoles($user);
73
    }
74
75
    /**
76
     * Check if one of the given roles is considered risky
77
     * @param  array  $roles
78
     * @return void
79
     */
80
    public function checkifRolesAreRisky(array $roles, ?array $risky_roles = null): bool
81
    {
82
        if ($risky_roles === null) {
83
            $risky_roles = $this->risky_roles;
84
        }
85
86
        foreach ($roles as $role) {
87
            foreach ($risky_roles as $risky_role) {
88
                if (preg_match('/'. $risky_role . '/', $role)) {
89
                    return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type void.
Loading history...
90
                }
91
            }
92
        }
93
94
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type void.
Loading history...
95
    }
96
}