Passed
Pull Request — bugfix (#733)
by Simon
08:02 queued 05:20
created

DomainAccessManager   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 16
c 2
b 0
f 0
dl 0
loc 57
ccs 0
cts 29
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getAllowedDomains() 0 7 2
A switchDomain() 0 13 2
A switchToDefaultDomain() 0 5 2
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\DataObject;
12
use Waca\DataObjects\Domain;
13
use Waca\DataObjects\User;
14
use Waca\Exceptions\AccessDeniedException;
15
use Waca\WebRequest;
16
17
class DomainAccessManager
18
{
19
    /**
20
     * @var SecurityManager
21
     */
22
    private $securityManager;
23
24
    public function __construct(SecurityManager $securityManager)
25
    {
26
        $this->securityManager = $securityManager;
27
    }
28
29
    /**
30
     * @param User $user
31
     *
32
     * @return Domain[]
33
     */
34
    public function getAllowedDomains(User $user): array
35
    {
36
        if ($user->isCommunityUser()) {
37
            return [];
38
        }
39
40
        return Domain::getDomainByUser($user->getDatabase(), $user, true);
41
    }
42
43
    public function switchDomain(User $user, Domain $newDomain): void
44
    {
45
        $mapToId = function(DataObject $object) {
46
            return $object->getId();
47
        };
48
49
        $allowed = in_array($newDomain->getId(), array_map($mapToId, self::getAllowedDomains($user)));
0 ignored issues
show
Bug Best Practice introduced by
The method Waca\Security\DomainAcce...er::getAllowedDomains() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        $allowed = in_array($newDomain->getId(), array_map($mapToId, self::/** @scrutinizer ignore-call */ getAllowedDomains($user)));
Loading history...
50
51
        if ($allowed) {
52
            WebRequest::setActiveDomain($newDomain);
53
        }
54
        else {
55
            throw new AccessDeniedException($this->securityManager, $this);
56
        }
57
    }
58
59
    /**
60
     * Not a very smart way of doing this - just set the user's current domain to the first one in the list.
61
     *
62
     * We may wish to allow the user to configure a default domain, but I don't expect this to be needed by many people,
63
     * so for now they can suffer until someone complains.
64
     *
65
     * @param User $user
66
     *
67
     * @return void
68
     */
69
    public function switchToDefaultDomain(User $user): void
70
    {
71
        $domains = $this->getAllowedDomains($user);
72
        if (count($domains) > 0) {
73
            WebRequest::setActiveDomain($domains[0]);
74
        }
75
    }
76
}