From a2eb5cc7b3085ccfb34f68fe8ecf90eff5501a6c Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Tue, 28 May 2013 14:33:03 +0200 Subject: [PATCH] Register first user as admin This commit fixes #50 and is needed for the admin panel --- public/include/classes/user.class.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/public/include/classes/user.class.php b/public/include/classes/user.class.php index a8067a17..b6c03818 100644 --- a/public/include/classes/user.class.php +++ b/public/include/classes/user.class.php @@ -322,15 +322,23 @@ class User { return false; } $apikey = hash("sha256",$username.$salt); - $stmt = $this->mysqli->prepare(" - INSERT INTO $this->table (username, pass, email, pin, api_key) - VALUES (?, ?, ?, ?, ?) + if ($this->mysqli->query("SELECT id FROM $this->table LIMIT 1")->num_rows > 0) { + $stmt = $this->mysqli->prepare(" + INSERT INTO $this->table (username, pass, email, pin, api_key) + VALUES (?, ?, ?, ?, ?) "); + } else { + $stmt = $this->mysqli->prepare(" + INSERT INTO $this->table (username, pass, email, pin, api_key, admin) + VALUES (?, ?, ?, ?, ?, 1) + "); + } if ($this->checkStmt($stmt)) { $stmt->bind_param('sssss', $username, hash("sha256", $password1.$this->salt), $email1, hash("sha256", $pin.$this->salt), $apikey); if (!$stmt->execute()) { $this->setErrorMessage( 'Unable to register' ); if ($stmt->sqlstate == '23000') $this->setErrorMessage( 'Username already exists' ); + echo $this->mysqli->error; return false; } $stmt->close();