Passed
Push — master ( e2eb31...3c0b9f )
by Arnold
08:55
created

AuthStorage::fetchUserByUsername()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
use Jasny\Auth;
6
use Jasny\Auth\User\BasicUser;
7
8
/**
9
 * Authentication storage class.
10
 *
11
 * Normally users would be fetched from the database.
12
 * @see https://www.jasny.net/auth/setup/storage.html
13
 */
14
class AuthStorage implements Auth\StorageInterface
15
{
16
    /**
17
     * A list of users.
18
     * DO NOT COPY THIS; Use a database to store and fetch the users instead.
19
     */
20
    public const USERS = [
21
        [
22
            'id' => 1,
23
            'username' => 'jackie',
24
            'hashedPassword' => '$2y$10$lVUeiphXLAm4pz6l7lF9i.6IelAqRxV4gCBu8GBGhCpaRb6o0qzUO', // jackie123
25
            'role' => 1, // user
26
        ],
27
        [
28
            'id' => 2,
29
            'username' => 'john',
30
            'hashedPassword' => '$2y$10$RU85KDMhbh8pDhpvzL6C5.kD3qWpzXARZBzJ5oJ2mFoW7Ren.apC2', // john123
31
            'role' => 10, // admin
32
        ],
33
    ];
34
35
    /**
36
     * Fetch a user by ID.
37
     */
38
    public function fetchUserById(string $id): ?Auth\UserInterface
39
    {
40
        foreach (self::USERS as $user) {
41
            if ((string)$user['id'] === $id) {
42
                return BasicUser::fromData($user);
43
            }
44
        }
45
46
        return null;
47
    }
48
49
    /**
50
     * Fetch a user by username
51
     */
52
    public function fetchUserByUsername(string $username): ?Auth\UserInterface
53
    {
54
        foreach (self::USERS as $user) {
55
            if ($user['username'] === $username) {
56
                return BasicUser::fromData($user);
57
            }
58
        }
59
60
        return null;
61
    }
62
63
    /**
64
     * Fetch the context by ID.
65
     */
66
    public function fetchContext(string $id) : ?Auth\ContextInterface
67
    {
68
        // Return null if this application doesn't work with teams or organizations for auth.
69
        return null;
70
    }
71
72
    /**
73
     * Get the default context of the user.
74
     */
75
    public function getContextForUser(Auth\UserInterface $user) : ?Auth\ContextInterface
76
    {
77
        return null;
78
    }
79
}