|
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
|
|
|
} |