Browse Source

Fix adding users to a blank database

rpm
J. King 3 years ago
parent
commit
3eab5aad5d
  1. 2
      lib/Database.php
  2. 7
      tests/cases/Database/SeriesUser.php

2
lib/Database.php

@ -271,7 +271,7 @@ class Database {
}
$hash = (strlen($password) > 0) ? password_hash($password, \PASSWORD_DEFAULT) : "";
// NOTE: This roundabout construction (with 'select' rather than 'values') is required by MySQL, because MySQL is riddled with pitfalls and exceptions
$this->db->prepare("INSERT INTO arsse_users(id,password,num) select ?, ?, ((select max(num) from arsse_users) + 1)", "str", "str")->runArray([$user,$hash]);
$this->db->prepare("INSERT INTO arsse_users(id,password,num) select ?, ?, (coalesce((select max(num) from arsse_users), 0) + 1)", "str", "str")->runArray([$user,$hash]);
return true;
}

7
tests/cases/Database/SeriesUser.php

@ -205,4 +205,11 @@ trait SeriesUser {
$this->assertException("alreadyExists", "User", "ExceptionConflict");
Arsse::$db->userRename("john.doe@example.com", "jane.doe@example.com");
}
public function testAddFirstUser(): void {
// first truncate the users table
static::$drv->exec("DELETE FROM arsse_users");
// add a user; if the max of the num column is not properly coalesced, this will result in a constraint violation
$this->assertTrue(Arsse::$db->userAdd("john.doe@example.com", ""));
}
}

Loading…
Cancel
Save