Passed
Push — master ( c3ec55...6fe0c7 )
by Simon
04:07
created

UserAccessLoader::loadDomainsForUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 24
rs 9.7998
c 1
b 0
f 0
cc 2
nc 2
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 PDO;
12
use Waca\DataObjects\Domain;
13
use Waca\DataObjects\User;
14
use Waca\DataObjects\UserRole;
15
16
final class UserAccessLoader implements IUserAccessLoader
17
{
18
    public function loadRolesForUser(User $user): array
19
    {
20
        $domain = Domain::getCurrent($user->getDatabase());
21
        $userRoles = UserRole::getForUser($user->getId(), $user->getDatabase(), $domain->getId());
22
23
        return array_map(fn(UserRole $r): string => $r->getRole(), $userRoles);
24
    }
25
26
    public function loadDomainsForUser(User $user): array
27
    {
28
        $database = $user->getDatabase();
29
30
        $statement = $database->prepare(<<<'SQL'
31
            SELECT d.* 
32
            FROM domain d
33
            INNER JOIN userdomain ud on d.id = ud.domain
34
            WHERE ud.user = :user
35
            AND d.enabled = 1
36
SQL
37
        );
38
        $statement->execute([
39
            ':user' => $user->getId()
40
        ]);
41
42
        $resultObjects = $statement->fetchAll(PDO::FETCH_CLASS, Domain::class);
43
44
        /** @var Domain $t */
45
        foreach ($resultObjects as $t) {
46
            $t->setDatabase($database);
47
        }
48
49
        return $resultObjects;
50
    }
51
}