Passed
Push — search ( bdb480...5e7f6e )
by Simon
17:14 queued 07:17
created

DomainAccessManager::switchDomain()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 13
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 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
}