| 1 | <?php |
||
| 17 | class DatabaseUserProvider |
||
| 18 | { |
||
| 19 | |||
| 20 | use RedisTrait; |
||
| 21 | use IdGeneratorTrait; |
||
| 22 | use EventDispatcherTrait; |
||
| 23 | |||
| 24 | const REDIS_USER = 'user:%d'; |
||
| 25 | const REDIS_USER_NAMES = 'user_names'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var PasswordHasher |
||
| 29 | */ |
||
| 30 | private $hasher; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var LoadUser |
||
| 34 | */ |
||
| 35 | private $loadUser; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @Inject({ |
||
| 39 | "@PasswordHasher", |
||
| 40 | "@Authentication.LoadUser", |
||
| 41 | * }) |
||
| 42 | * @param PasswordHasher $passwordHasher |
||
| 43 | * @param LoadUser $loadUser |
||
| 44 | */ |
||
| 45 | 13 | public function __construct(PasswordHasher $passwordHasher, LoadUser $loadUser) |
|
| 50 | |||
| 51 | /** |
||
| 52 | * @param string $username |
||
| 53 | * @return UserVO |
||
| 54 | * @deprecated use LoadUser |
||
| 55 | * @throws UsernameNotFoundException |
||
| 56 | */ |
||
| 57 | 1 | public function loadUserByUsername($username) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * @param integer $userId |
||
| 64 | * @return UserVO |
||
| 65 | */ |
||
| 66 | 1 | public function loadUserById($userId) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @return string[] |
||
| 73 | */ |
||
| 74 | 1 | public function getAllUserNames() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @param string $password |
||
| 81 | * @return string $hash |
||
| 82 | */ |
||
| 83 | 3 | public function generateHash($password) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $password |
||
| 90 | * @param string $hash |
||
| 91 | * @return boolean |
||
| 92 | */ |
||
| 93 | 1 | public function verifyHash($password, $hash) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * @param UserVO $user |
||
| 100 | * @param string $newPassword |
||
| 101 | */ |
||
| 102 | 1 | public function changePassword(UserVO $user, $newPassword) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * @param UserVO $userVo |
||
| 112 | * @param string $property |
||
| 113 | */ |
||
| 114 | 3 | public function setUserProperty(UserVO $userVo, $property) |
|
| 115 | { |
||
| 116 | 3 | $redis = $this->getRedis(); |
|
| 117 | 3 | $value = $userVo->$property; |
|
| 118 | |||
| 119 | 3 | if (is_array($value)) { |
|
| 120 | 1 | $value = implode(',', $value); |
|
| 121 | } |
||
| 122 | 3 | $redis->HSET($this->getKey($userVo->id), $property, $value); |
|
|
1 ignored issue
–
show
|
|||
| 123 | 3 | } |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @param UserVO $user |
||
| 127 | * @return integer $user_id |
||
| 128 | */ |
||
| 129 | 1 | public function register(UserVO $user) |
|
| 130 | { |
||
| 131 | 1 | $redis = $this->getRedis()->pipeline(); |
|
| 132 | 1 | $passwordHash = $this->generateHash($user->password); |
|
| 133 | |||
| 134 | $userArray = [ |
||
| 135 | 1 | 'username' => $user->getUsername(), |
|
| 136 | 1 | 'password' => $passwordHash, |
|
| 137 | 1 | 'roles' => implode(',', $user->roles), |
|
| 138 | 1 | 'one_time_secret' => $user->one_time_secret, |
|
| 139 | 1 | 'avatar' => $user->avatar |
|
| 140 | ]; |
||
| 141 | |||
| 142 | 1 | $newUserId = $this->generateUniqueId(); |
|
| 143 | |||
| 144 | 1 | $redis->HSET(self::REDIS_USER_NAMES, strtolower($user->getUsername()), $newUserId); |
|
| 145 | 1 | $redis->HMSET($this->getKey($newUserId), $userArray); |
|
| 146 | |||
| 147 | 1 | $redis->execute(); |
|
| 148 | |||
| 149 | 1 | $user->id = $newUserId; |
|
| 150 | |||
| 151 | 1 | return $newUserId; |
|
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param integer $userId |
||
| 156 | */ |
||
| 157 | 2 | public function deleteUser($userId) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @param integer $userId |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | 5 | private function getKey($userId) |
|
| 181 | } |
||
| 182 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: