From 2cf5183f14c08bb36bb2c1aa3d90c1896e6aac6c Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Tue, 7 Apr 2020 14:08:53 -0600 Subject: [PATCH] Allow lookup by serial number --- lib/Machine.lib.php | 25 +++++++++++++++++++++++++ public/index.php | 8 ++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/Machine.lib.php b/lib/Machine.lib.php index 716fc8f..fded1b6 100644 --- a/lib/Machine.lib.php +++ b/lib/Machine.lib.php @@ -35,6 +35,31 @@ class Machine { return $database->has('machines', ['machineid' => $id]); } + /** + * Check if a given serial number can identify exactly one machine. + * @global $database $database + * @param type $serial + * @return bool + */ + public static function serialExists(string $serial): bool { + global $database; + return $database->has('machines', ['serial' => $serial]) && $database->count('machines', ['serial' => $serial]) == 1; + } + + /** + * Convert a serial into a machine ID. + * @global $database $database + * @param string $serial + * @return string|bool machine ID if found, otherwise false. + */ + public static function getIDFromSerial(string $serial) { + global $database; + if (Machine::serialExists($serial)) { + return $database->get('machines', 'machineid', ['serial' => $serial]) . ""; + } + return false; + } + public function save() { global $database; if ($this->exists) { diff --git a/public/index.php b/public/index.php index 2eaccb1..ca185d8 100644 --- a/public/index.php +++ b/public/index.php @@ -23,7 +23,7 @@ require_once __DIR__ . "/../lib/required_public.php";

@@ -33,7 +33,7 @@ require_once __DIR__ . "/../lib/required_public.php";

No machine with ID could be found.