|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BrainExe\Core\Authentication; |
|
4
|
|
|
|
|
5
|
|
|
use BrainExe\Annotations\Annotations\Inject; |
|
6
|
|
|
use BrainExe\Annotations\Annotations\Service; |
|
7
|
|
|
use BrainExe\Core\Authentication\Exception\UserNotFoundException; |
|
8
|
|
|
use Predis\Client; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @api |
|
12
|
|
|
* @Service("Core.Authentication.LoadUser", public=false) |
|
13
|
|
|
*/ |
|
14
|
|
|
class LoadUser |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Client |
|
18
|
|
|
*/ |
|
19
|
|
|
private $redis; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param Client $redis |
|
23
|
|
|
* @Inject("@redis") |
|
24
|
|
|
*/ |
|
25
|
5 |
|
public function __construct(Client $redis) |
|
26
|
|
|
{ |
|
27
|
5 |
|
$this->redis = $redis; |
|
28
|
5 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $username |
|
32
|
|
|
* @return UserVO |
|
33
|
|
|
* @throws UserNotFoundException |
|
34
|
|
|
*/ |
|
35
|
2 |
|
public function loadUserByUsername(string $username) : UserVO |
|
36
|
|
|
{ |
|
37
|
2 |
|
$userId = $this->redis->hget(UserProvider::REDIS_USER_NAMES, mb_strtolower($username)); |
|
38
|
|
|
|
|
39
|
2 |
|
if (empty($userId)) { |
|
40
|
1 |
|
throw new UserNotFoundException(sprintf('Username "%s" does not exist.', $username)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
return $this->loadUserById($userId); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param int $userId |
|
48
|
|
|
* @return UserVO |
|
49
|
|
|
* @throws UserNotFoundException |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function loadUserById(int $userId) : UserVO |
|
52
|
|
|
{ |
|
53
|
2 |
|
$redisUser = $this->redis->hgetall($this->getKey($userId)); |
|
54
|
|
|
|
|
55
|
2 |
|
if (empty($redisUser)) { |
|
56
|
1 |
|
throw new UserNotFoundException(sprintf('User "%d" does not exist.', $userId)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
return $this->buildUserVO($userId, $redisUser); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param int $userId |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
2 |
|
private function getKey(int $userId) : string |
|
67
|
|
|
{ |
|
68
|
2 |
|
return sprintf(UserProvider::REDIS_USER, $userId); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param int $userId |
|
73
|
|
|
* @param $redisUser |
|
74
|
|
|
* @return UserVO |
|
75
|
|
|
*/ |
|
76
|
1 |
|
private function buildUserVO(int $userId, array $redisUser) : UserVO |
|
77
|
|
|
{ |
|
78
|
1 |
|
$user = new UserVO(); |
|
79
|
1 |
|
$user->id = $userId; |
|
80
|
1 |
|
$user->username = $redisUser['username']; |
|
81
|
1 |
|
$user->email = $redisUser['email'] ?? ''; |
|
82
|
1 |
|
$user->password_hash = $redisUser['password']; |
|
83
|
1 |
|
$user->one_time_secret = $redisUser['one_time_secret'] ?? null; |
|
84
|
1 |
|
$user->roles = array_filter(explode(',', $redisUser['roles'])); |
|
85
|
1 |
|
$user->avatar = $redisUser['avatar'] ?? UserVO::DEFAULT_AVATAR; |
|
86
|
|
|
|
|
87
|
1 |
|
return $user; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|