Passed
Push — multiproject/local-access ( 5353e5...767924 )
by Simon
03:31
created

DomainAccessManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\DomainSwitchNotAllowedException;
15
use Waca\Helpers\PreferenceManager;
16
use Waca\WebRequest;
17
18
class DomainAccessManager implements IDomainAccessManager
19
{
20
    private IUserAccessLoader $userAccessLoader;
21
22
    public function __construct(IUserAccessLoader $userAccessLoader)
23
    {
24
        $this->userAccessLoader = $userAccessLoader;
25
    }
26
27
    /**
28
     * Returns the domains the user is a member of.
29
     *
30
     * Note - this *does not* determine the access rights that a user has in any
31
     * specific domain. Permissions checks still need to be performed.
32
     *
33
     * @param User $user
34
     *
35
     * @return Domain[]
36
     */
37
    public function getAllowedDomains(User $user): array
38
    {
39
        if ($user->isCommunityUser()) {
40
            return [];
41
        }
42
43
        return $this->userAccessLoader->loadDomainsForUser($user);
44
    }
45
46
    public function switchDomain(User $user, Domain $newDomain): void
47
    {
48
        $mapToId = function(DataObject $object) {
49
            return $object->getId();
50
        };
51
52
        $allowed = in_array($newDomain->getId(), array_map($mapToId, $this->getAllowedDomains($user)));
53
54
        if ($allowed) {
55
            WebRequest::setActiveDomain($newDomain);
56
        }
57
        else {
58
            throw new DomainSwitchNotAllowedException();
59
        }
60
    }
61
62
    public function switchToDefaultDomain(User $user): void
63
    {
64
        $domains = $this->getAllowedDomains($user);
65
        $preferenceManager = new PreferenceManager($user->getDatabase(), $user->getId(), null);
66
        $defaultDomainPreference = $preferenceManager->getPreference(PreferenceManager::PREF_DEFAULT_DOMAIN);
67
68
        $chosenDomain = null;
69
        foreach ($domains as $d) {
70
            if ($d->getId() == $defaultDomainPreference) {
71
                $chosenDomain = $d;
72
                break;
73
            }
74
        }
75
76
        if ($chosenDomain !== null) {
77
            WebRequest::setActiveDomain($chosenDomain);
78
            return;
79
        }
80
81
        if (count($domains) > 0) {
82
            WebRequest::setActiveDomain($domains[0]);
83
        }
84
    }
85
}