Skip to content

Commit cbac3ba

Browse files
committed
Fix regression with user id set to '0'
1 parent 65691d8 commit cbac3ba

File tree

6 files changed

+24
-26
lines changed

6 files changed

+24
-26
lines changed

src/libraries/Comment.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ public function getParentUrl() {
211211
}
212212

213213
public function getUser() {
214-
return User::findUserById($this->user_id);
214+
if (is_null($this->user_id)) return null;
215+
return new User($this->user_id);
215216
}
216217

217218
public function getUserId() {

src/libraries/Document.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ public function getURI() {
282282
}
283283

284284
public function getUser() {
285-
return User::findUserById($this->user_id);
285+
if (is_null($this->user_id)) return null;
286+
return new User($this->user_id);
286287
}
287288

288289
public function getUserId() {
@@ -309,7 +310,7 @@ protected static function normalize(StdClass &$data) {
309310
$data->edited_datetime = $data->edited_datetime;
310311

311312
if (!is_null($data->user_id))
312-
$data->user_id = $data->user_id;
313+
$data->user_id = (int) $data->user_id;
313314

314315
return true;
315316
}

src/libraries/NewsPost.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ public function getURI() {
315315
}
316316

317317
public function getUser() {
318-
return User::findUserById($this->user_id);
318+
if (is_null($this->user_id)) return null;
319+
return new User($this->user_id);
319320
}
320321

321322
public function getUserId() {

src/libraries/Packet.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,8 @@ public function getUsedBy() {
531531
}
532532

533533
public function getUser() {
534-
return User::findUserById($this->user_id);
534+
if (is_null($this->user_id)) return null;
535+
return new User($this->user_id);
535536
}
536537

537538
public function getUserId() {
@@ -590,7 +591,7 @@ protected static function normalize( StdClass &$data ) {
590591
$data->edited_datetime = $data->edited_datetime;
591592

592593
if (!is_null($data->user_id))
593-
$data->user_id = $data->user_id;
594+
$data->user_id = (int) $data->user_id;
594595

595596
return true;
596597
}

src/libraries/Server.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ public function getUpdatedDateTime() {
204204
}
205205

206206
public function getUser() {
207-
return User::findUserById($this->user_id);
207+
if (is_null($this->user_id)) return null;
208+
return new User($this->user_id);
208209
}
209210

210211
public function getUserId() {

src/libraries/User.php

+12-19
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function allocate()
122122
$this->setOptions(self::DEFAULT_OPTION);
123123
$this->setTimezone(self::DEFAULT_TZ);
124124

125-
if (empty($id)) return;
125+
if (is_null($id)) return;
126126

127127
if (!isset(Common::$database))
128128
{
@@ -368,20 +368,6 @@ public static function generateVerifierToken(string $username, string $email)
368368
return hash('sha256', $digest);
369369
}
370370

371-
public static function findUserById(?int $user_id)
372-
{
373-
if (is_null($user_id)) return null;
374-
375-
try
376-
{
377-
return new User($user_id);
378-
}
379-
catch (UserNotFoundException $e)
380-
{
381-
return null;
382-
}
383-
}
384-
385371
public static function &getAllUsers($order = null, $limit = null, $index = null)
386372
{
387373
if (!(is_numeric($limit) || is_numeric($index))) {
@@ -479,7 +465,7 @@ public function getId()
479465

480466
public function getName()
481467
{
482-
return (is_null($this->display_name) ? $this->username : $this->display_name);
468+
return $this->display_name ?? $this->username;
483469
}
484470

485471
public function getOption(int $option)
@@ -511,9 +497,16 @@ public function getPasswordSalt()
511497

512498
public function getURI()
513499
{
514-
return Common::relativeUrlToAbsolute(
515-
'/user/' . $this->getId() . '/' . Common::sanitizeForUrl($this->getName(), true)
516-
);
500+
$id = $this->getId();
501+
502+
if (is_null($id))
503+
{
504+
throw new UnexpectedValueException('user id is null');
505+
}
506+
507+
return Common::relativeUrlToAbsolute(sprintf(
508+
'/user/%s/%s', $id, Common::sanitizeForUrl($this->getName(), true)
509+
));
517510
}
518511

519512
public static function getUserCount()

0 commit comments

Comments
 (0)