/** * @param UserVO|null $user * @return string */ protected function getUsername($user) { if ($user instanceof UserVO && !$user instanceof AnonymusUserVO) { /** @var UserVO $user */ return $user->getUsername(); } else { return '-anonymous-'; } }
/** * @param UserVO $user * @throws UserException */ protected function checkInput(UserVO $user) { if (mb_strlen($user->username) <= 1) { throw new UserException('Username must not be empty'); } if (mb_strlen($user->password) <= 1) { throw new UserException('Password must not be empty'); } try { $this->userProvider->loadUserByUsername($user->getUsername()); throw new UserException(sprintf('User %s already exists', $user->getUsername())); } catch (UserNotFoundException $e) { // all fine } }
/** * @param UserVO $user * @return int $userId */ public function register(UserVO $user) : int { $redis = $this->getRedis()->pipeline(); $userArray = ['username' => $user->getUsername(), 'password' => $this->generateHash($user->password), 'roles' => implode(',', $user->roles), 'one_time_secret' => $user->one_time_secret, 'avatar' => $user->avatar]; $newUserId = $this->generateUniqueId('userid'); $redis->hset(self::REDIS_USER_NAMES, mb_strtolower($user->getUsername()), $newUserId); $redis->hmset($this->getKey($newUserId), $userArray); $redis->execute(); $user->id = $newUserId; return $newUserId; }