stwalkerster /
waca
| 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
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 | } |