Completed
Pull Request — newinternal (#285)
by Simon
07:17 queued 04:17
created

SecurityManager::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Security;
10
11
use Waca\DataObjects\User;
12
use Waca\Exceptions\AccessDeniedException;
13
use Waca\IdentificationVerifier;
14
15
final class SecurityManager
16
{
17
    /** @var IdentificationVerifier */
18
    private $identificationVerifier;
19
    /** @var SecurityConfigurationFactory */
20
    private $securityConfigurationFactory;
21
22
    /**
23
     * SecurityManager constructor.
24
     *
25
     * @param IdentificationVerifier $identificationVerifier
26
     * @param bool                   $forceIdentification
27
     */
28
    public function __construct(IdentificationVerifier $identificationVerifier, $forceIdentification)
29
    {
30
        $this->identificationVerifier = $identificationVerifier;
31
32
        $this->securityConfigurationFactory = new SecurityConfigurationFactory($forceIdentification);
33
    }
34
35
    public function configure()
36
    {
37
        return $this->securityConfigurationFactory;
38
    }
39
40
    /**
41
     * @param $value
42
     * @param $filter
43
     *
44
     * @return bool
45
     * @throws AccessDeniedException
46
     * @category Security-Critical
47
     */
48
    private function test($value, $filter)
49
    {
50
        if (!$filter) {
51
            return false;
52
        }
53
54
        if ($value == SecurityConfiguration::DENY) {
55
            // FILE_NOT_FOUND...?
56
            throw new AccessDeniedException();
57
        }
58
59
        return $value === SecurityConfiguration::ALLOW;
60
    }
61
62
    /**
63
     * Tests if a user is allowed to perform an action.
64
     *
65
     * This method should form a hard, deterministic security barrier, and only return true if it is absolutely sure
66
     * that a user should have access to something.
67
     *
68
     * @param SecurityConfiguration $config
69
     * @param User                  $user
70
     *
71
     * @return bool
72
     *
73
     * @category Security-Critical
74
     */
75
    public function allows(SecurityConfiguration $config, User $user)
76
    {
77
        if ($config->requiresIdentifiedUser() && !$user->isCommunityUser() && !$user->isIdentified($this->identificationVerifier)) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
78
            return false;
79
        }
80
81
        try {
82
            $allowed = $this->test($config->getAdmin(), $user->isAdmin())
83
                || $this->test($config->getUser(), $user->isUser())
84
                || $this->test($config->getCommunity(), $user->isCommunityUser())
85
                || $this->test($config->getSuspended(), $user->isSuspended())
86
                || $this->test($config->getDeclined(), $user->isDeclined())
87
                || $this->test($config->getNew(), $user->isNewUser())
88
                || $this->test($config->getCheckuser(), $user->isCheckuser());
89
90
            return $allowed;
91
        }
92
        catch (AccessDeniedException $ex) {
93
            // something is set to deny.
94
            return false;
95
        }
96
    }
97
}