jasny /
auth
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Jasny\Auth\User; |
||
| 6 | |||
| 7 | use Jasny\Auth\ContextInterface as Context; |
||
| 8 | use Jasny\Auth\UserInterface; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * A simple user class which can be used be used instead of creating a custom user class. |
||
| 12 | */ |
||
| 13 | final class BasicUser implements UserInterface |
||
| 14 | { |
||
| 15 | /** @var string|int */ |
||
| 16 | public $id; |
||
| 17 | |||
| 18 | protected string $hashedPassword = ''; |
||
| 19 | |||
| 20 | /** @var string|int */ |
||
| 21 | public $role; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @inheritDoc |
||
| 25 | */ |
||
| 26 | 1 | public function getAuthId(): string |
|
| 27 | { |
||
| 28 | 1 | return (string)$this->id; |
|
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @inheritDoc |
||
| 33 | */ |
||
| 34 | 1 | public function verifyPassword(string $password): bool |
|
| 35 | { |
||
| 36 | 1 | return password_verify($password, $this->hashedPassword); |
|
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @inheritDoc |
||
| 41 | */ |
||
| 42 | 1 | public function getAuthChecksum(): string |
|
| 43 | { |
||
| 44 | 1 | return hash('sha256', $this->id . $this->hashedPassword); |
|
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @inheritDoc |
||
| 49 | */ |
||
| 50 | 1 | public function getAuthRole(?Context $context = null) |
|
| 51 | { |
||
| 52 | 1 | return $this->role; |
|
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @inheritDoc |
||
| 57 | */ |
||
| 58 | 1 | public function requiresMfa(): bool |
|
| 59 | { |
||
| 60 | 1 | return false; |
|
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Factory method; create object from data loaded from DB. |
||
| 65 | * |
||
| 66 | * @param array $data |
||
| 67 | * @return static |
||
| 68 | */ |
||
| 69 | 2 | public static function fromData(array $data): self |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 70 | { |
||
| 71 | 2 | $user = new self(); |
|
| 72 | |||
| 73 | 2 | foreach ($data as $key => $value) { |
|
| 74 | 2 | $user->{$key} = $value; |
|
| 75 | } |
||
| 76 | |||
| 77 | 2 | return $user; |
|
|
0 ignored issues
–
show
|
|||
| 78 | } |
||
| 79 | } |
||
| 80 |