Cleaned up User and UserSession queries.

This commit is contained in:
flash 2023-07-28 15:07:30 +00:00
parent 20b309563e
commit 5c8ffa09fc
3 changed files with 30 additions and 34 deletions

View file

@ -17,8 +17,6 @@ use Misuzu\Database\Database;
final class DB {
private static $instance;
public const PREFIX = 'msz_';
public const ATTRS = [
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,

View file

@ -70,18 +70,17 @@ class User implements HasRankInterface {
private $totp = null;
public const TABLE = 'users';
private const QUERY_SELECT = 'SELECT %1$s FROM `' . DB::PREFIX . self::TABLE . '` AS '. self::TABLE;
private const SELECT = '%1$s.`user_id`, %1$s.`username`, %1$s.`password`, %1$s.`email`, %1$s.`user_super`, %1$s.`user_title`'
. ', %1$s.`user_country`, %1$s.`user_colour`, %1$s.`display_role`, %1$s.`user_totp_key`'
. ', %1$s.`user_about_content`, %1$s.`user_about_parser`'
. ', %1$s.`user_signature_content`, %1$s.`user_signature_parser`'
. ', %1$s.`user_birthdate`, %1$s.`user_background_settings`'
. ', INET6_NTOA(%1$s.`register_ip`) AS `register_ip`'
. ', INET6_NTOA(%1$s.`last_ip`) AS `last_ip`'
. ', UNIX_TIMESTAMP(%1$s.`user_created`) AS `user_created`'
. ', UNIX_TIMESTAMP(%1$s.`user_active`) AS `user_active`'
. ', UNIX_TIMESTAMP(%1$s.`user_deleted`) AS `user_deleted`';
private const QUERY_SELECT = 'SELECT %1$s FROM `msz_users`';
private const SELECT = '`user_id`, `username`, `password`, `email`, `user_super`, `user_title`'
. ', `user_country`, `user_colour`, `display_role`, `user_totp_key`'
. ', `user_about_content`, `user_about_parser`'
. ', `user_signature_content`, `user_signature_parser`'
. ', `user_birthdate`, `user_background_settings`'
. ', INET6_NTOA(`register_ip`) AS `register_ip`'
. ', INET6_NTOA(`last_ip`) AS `last_ip`'
. ', UNIX_TIMESTAMP(`user_created`) AS `user_created`'
. ', UNIX_TIMESTAMP(`user_active`) AS `user_active`'
. ', UNIX_TIMESTAMP(`user_deleted`) AS `user_deleted`';
public function getId(): int {
return $this->user_id < 1 ? -1 : $this->user_id;
@ -312,7 +311,7 @@ class User implements HasRankInterface {
$this->last_ip = $lastRemoteAddress;
DB::prepare(
'UPDATE `' . DB::PREFIX . self::TABLE . '`'
'UPDATE `msz_users`'
. ' SET `user_active` = FROM_UNIXTIME(:active), `last_ip` = INET6_ATON(:address)'
. ' WHERE `user_id` = :user'
) ->bind('user', $this->user_id)
@ -464,7 +463,7 @@ class User implements HasRankInterface {
$userId = (int)DB::prepare(
'SELECT `user_id`'
. ' FROM `' . DB::PREFIX . self::TABLE . '`'
. ' FROM `msz_users`'
. ' WHERE LOWER(`username`) = LOWER(:username)'
) ->bind('username', $name)
->fetchColumn();
@ -503,7 +502,7 @@ class User implements HasRankInterface {
$userId = (int)DB::prepare(
'SELECT `user_id`'
. ' FROM `' . DB::PREFIX . self::TABLE . '`'
. ' FROM `msz_users`'
. ' WHERE LOWER(`email`) = LOWER(:email)'
) ->bind('email', $address)
->fetchColumn();
@ -560,7 +559,7 @@ class User implements HasRankInterface {
public function save(): void {
$save = DB::prepare(
'UPDATE `' . DB::PREFIX . self::TABLE . '`'
'UPDATE `msz_users`'
. ' SET `username` = :username, `email` = :email, `password` = :password'
. ', `user_super` = :is_super, `user_country` = :country, `user_colour` = :colour, `user_title` = :title'
. ', `user_totp_key` = :totp'
@ -579,7 +578,7 @@ class User implements HasRankInterface {
public function saveProfile(): void {
$save = DB::prepare(
'UPDATE `' . DB::PREFIX . self::TABLE . '`'
'UPDATE `msz_users`'
. ' SET `user_about_content` = :about_content, `user_about_parser` = :about_parser'
. ', `user_signature_content` = :signature_content, `user_signature_parser` = :signature_parser'
. ', `user_background_settings` = :background_settings, `user_birthdate` = :birthdate'
@ -602,7 +601,7 @@ class User implements HasRankInterface {
string $countryCode = 'XX'
): self {
$createUser = DB::prepare(
'INSERT INTO `' . DB::PREFIX . self::TABLE . '` (`username`, `password`, `email`, `register_ip`, `last_ip`, `user_country`, `display_role`)'
'INSERT INTO `msz_users` (`username`, `password`, `email`, `register_ip`, `last_ip`, `user_country`, `display_role`)'
. ' VALUES (:username, :password, LOWER(:email), INET6_ATON(:register_ip), INET6_ATON(:last_ip), :user_country, 1)'
) ->bind('username', $username)
->bind('email', $email)
@ -640,7 +639,7 @@ class User implements HasRankInterface {
}
private static function byQueryBase(): string {
return sprintf(self::QUERY_SELECT, sprintf(self::SELECT, self::TABLE));
return sprintf(self::QUERY_SELECT, self::SELECT);
}
public static function byId(string|int $userId): ?self {
// newer classes all treat ids as if they're strings

View file

@ -28,14 +28,13 @@ class UserSession {
private static $localSession = null;
public const TABLE = 'sessions';
private const QUERY_SELECT = 'SELECT %1$s FROM `' . DB::PREFIX . self::TABLE . '` AS '. self::TABLE;
private const SELECT = '%1$s.`session_id`, %1$s.`user_id`, %1$s.`session_key`, %1$s.`session_user_agent`, %1$s.`session_client_info`, %1$s.`session_country`, %1$s.`session_expires_bump`'
. ', INET6_NTOA(%1$s.`session_ip`) AS `session_ip`'
. ', INET6_NTOA(%1$s.`session_ip_last`) AS `session_ip_last`'
. ', UNIX_TIMESTAMP(%1$s.`session_created`) AS `session_created`'
. ', UNIX_TIMESTAMP(%1$s.`session_active`) AS `session_active`'
. ', UNIX_TIMESTAMP(%1$s.`session_expires`) AS `session_expires`';
private const QUERY_SELECT = 'SELECT %1$s FROM `msz_sessions`';
private const SELECT = '`session_id`, `user_id`, `session_key`, `session_user_agent`, `session_client_info`, `session_country`, `session_expires_bump`'
. ', INET6_NTOA(`session_ip`) AS `session_ip`'
. ', INET6_NTOA(`session_ip_last`) AS `session_ip_last`'
. ', UNIX_TIMESTAMP(`session_created`) AS `session_created`'
. ', UNIX_TIMESTAMP(`session_active`) AS `session_active`'
. ', UNIX_TIMESTAMP(`session_expires`) AS `session_expires`';
public function getId(): int {
return $this->session_id < 1 ? -1 : $this->session_id;
@ -128,13 +127,13 @@ class UserSession {
}
public function delete(): void {
DB::prepare('DELETE FROM `' . DB::PREFIX . self::TABLE . '` WHERE `session_id` = :session')
DB::prepare('DELETE FROM `msz_sessions` WHERE `session_id` = :session')
->bind('session', $this->getId())
->execute();
}
public static function purgeUser(User $user): void {
DB::prepare('DELETE FROM `' . DB::PREFIX . self::TABLE . '` WHERE `user_id` = :user')
DB::prepare('DELETE FROM `msz_sessions` WHERE `user_id` = :user')
->bind('user', $user->getId())
->execute();
}
@ -158,7 +157,7 @@ class UserSession {
public function update(): void {
DB::prepare(
'UPDATE `' . DB::PREFIX . self::TABLE . '`'
'UPDATE `msz_sessions`'
. ' SET `session_active` = FROM_UNIXTIME(:active), `session_ip_last` = INET6_ATON(:remote_addr), `session_expires` = FROM_UNIXTIME(:expires)'
. ' WHERE `session_id` = :session'
) ->bind('active', $this->session_active)
@ -180,7 +179,7 @@ class UserSession {
$token = self::generateToken();
$sessionId = DB::prepare(
'INSERT INTO `' . DB::PREFIX . self::TABLE . '`'
'INSERT INTO `msz_sessions`'
. ' (`user_id`, `session_ip`, `session_country`, `session_user_agent`, `session_client_info`, `session_key`, `session_created`, `session_expires`)'
. ' VALUES (:user, INET6_ATON(:remote_addr), :country, :user_agent, :client_info, :token, NOW(), NOW() + INTERVAL :expires SECOND)'
) ->bind('user', $user->getId())
@ -212,7 +211,7 @@ class UserSession {
}
private static function byQueryBase(): string {
return sprintf(self::QUERY_SELECT, sprintf(self::SELECT, self::TABLE));
return sprintf(self::QUERY_SELECT, self::SELECT);
}
public static function byId(int $sessionId): self {
$session = DB::prepare(self::byQueryBase() . ' WHERE `session_id` = :session_id')