X-Git-Url: https://jfr.im/git/irc/unrealircd/unrealircd-webpanel.git/blobdiff_plain/6b3d3f83837e59998e08c3937578fb0e95e8aca4..33f512fa30c06fa488c42e68f9bc9f401300a5ab:/plugins/sql_auth/SQL/user.php diff --git a/plugins/sql_auth/SQL/user.php b/plugins/sql_auth/SQL/user.php index 3ed7c14..a05abb7 100644 --- a/plugins/sql_auth/SQL/user.php +++ b/plugins/sql_auth/SQL/user.php @@ -20,7 +20,7 @@ class SQLA_User * @param string $name * @param mixed $id */ - function __construct(string $name = NULL, $id = NULL) + function __construct(string $name = NULL, int $id = NULL) { $conn = sqlnew(); @@ -50,12 +50,86 @@ class SQLA_User } } - function password_verify(string $input) + /** + * Verify a user's password + * @param string $input + * @return bool + */ + function password_verify(string $input) : bool { if (password_verify($input, $this->passhash)) return true; return false; } + + /** + * Add user meta data + * @param string $key + * @param string $value + * @return bool + */ + function add_meta(string $key, string $value) + { + if (!$key || !$value) + return false; + + $meta = [ + "id" => $this->id, + "key" => $key, + "value" => $value + ]; + + $conn = sqlnew(); + + /* check if it exists first, update it if it does */ + $query = "SELECT * FROM " . SQL_PREFIX . "user_meta WHERE user_id = :id AND meta_key = :key"; + $stmt = $conn->prepare($query); + $stmt->execute(["id" => $this->id, "key" => $key]); + if ($stmt->rowCount()) // it exists, update instead of insert + { + $query = "UPDATE " . SQL_PREFIX . "user_meta SET meta_value = :value WHERE user_id = :id AND meta_key = :key"; + $stmt = $conn->prepare($query); + $stmt->execute($meta); + if ($stmt->rowCount()) + return true; + return false; + } + + else + { + $query = "INSERT INTO " . SQL_PREFIX . "user_meta (user_id, meta_key, meta_value) VALUES (:id, :key, :value)"; + $stmt = $conn->prepare($query); + $stmt->execute($meta); + if ($stmt->rowCount()) + return true; + return false; + } + } + + /** + * Delete user meta data by key + * @param string $key + * @return bool + */ + function delete_meta(string $key) + { + if (!$key ) + return false; + + $meta = [ + "id" => $this->id, + "key" => $key, + ]; + + $conn = sqlnew(); + $query = "DELETE FROM " . SQL_PREFIX . "user_meta WHERE user_id = :id AND meta_key = :key"; + $stmt = $conn->prepare($query); + $stmt->execute($meta); + if ($stmt->rowCount()) + return true; + return false; + + } } @@ -174,4 +248,5 @@ function delete_user(int $id, &$info = []) : int } $info[] = "Unknown error"; return 0; -} \ No newline at end of file +} +