From 85eae6c7b973dbb1928df397dc0e73bc2408eaac Mon Sep 17 00:00:00 2001 From: DariusIII Date: Thu, 21 Sep 2017 14:36:18 +0200 Subject: [PATCH] Use new column name and update User <-> UserRole relation and use it --- Changelog | 1 + nntmux/Users.php | 42 +++++++++++++------------------------- public/pages/AdminPage.php | 14 ++++++------- public/pages/BasePage.php | 4 ++-- public/pages/Page.php | 2 +- 5 files changed, 25 insertions(+), 38 deletions(-) diff --git a/Changelog b/Changelog index 5336cded6..85235a361 100755 --- a/Changelog +++ b/Changelog @@ -1,4 +1,5 @@ 2017-09-21 DariusIII + * Chg: Use new column name and update User <-> UserRole relation and use it * Chg: Update Users table, rename role to user_roles_id * Chg: Update GamesInfo model and migration * Chg: Update laravel/framework to version 5.5.9 diff --git a/nntmux/Users.php b/nntmux/Users.php index 309d20688..de53e8e5f 100755 --- a/nntmux/Users.php +++ b/nntmux/Users.php @@ -413,12 +413,11 @@ class Users /** * @param string $userName - * - * @return array|bool + * @return \Illuminate\Database\Eloquent\Model|null|static */ public function getByUsername(string $userName) { - return $this->pdo->queryOneRow(sprintf('SELECT users.*, user_roles.name as rolename, user_roles.apirequests, user_roles.downloadrequests FROM users INNER JOIN user_roles on user_roles.id = users.role WHERE username = %s', $this->pdo->escapeString($userName))); + return User::query()->where('username', $userName)->first(); } /** @@ -429,7 +428,7 @@ class Users */ public function getByEmail(string $email) { - return User::query()->where('email', '=', $email)->first(); + return User::query()->where('email', $email)->first(); } /** @@ -440,7 +439,7 @@ class Users */ public function updateUserRole(int $uid, int $role): int { - User::query()->where('id', $uid)->update(['role' => $role]); + User::query()->where('id', $uid)->update(['user_roles_id' => $role]); return self::SUCCESS; } @@ -572,7 +571,7 @@ class Users return false; } - return $user['rsstoken'] !== $rssToken ? false : $user; + return $user->rsstoken !== $rssToken ? false : $user; } /** @@ -582,25 +581,22 @@ class Users */ public function getById($id) { - $sql = sprintf('SELECT users.*, user_roles.name as rolename, user_roles.hideads, user_roles.canpreview, user_roles.apirequests, user_roles.downloadrequests, NOW() as now FROM users INNER JOIN user_roles on user_roles.id = users.role WHERE users.id = %d', $id); - - $result = $this->pdo->query($sql, true, NN_CACHE_EXPIRY_MEDIUM); + $result = User::find($id); if (empty($result)) { return false; } - return $result[0]; + return $result; } /** * @param string $rssToken - * - * @return array|bool + * @return \Illuminate\Database\Eloquent\Model|null|static */ public function getByRssToken(string $rssToken) { - return $this->pdo->queryOneRow(sprintf('SELECT users.*, user_roles.apirequests, user_roles.downloadrequests, NOW() as now FROM users INNER JOIN user_roles on user_roles.id = users.role WHERE users.rsstoken = %s', $this->pdo->escapeString($rssToken))); + return User::query()->where('rsstoken', $rssToken)->first(); } /** @@ -1494,23 +1490,13 @@ class Users */ public function roleCheck($roleID, $user): bool { - if (is_string($user) && strlen($user) > 0) { - $user = $this->pdo->escapeString($user); - $querySuffix = "username = $user"; - } elseif (is_int($user) && $user >= 0) { - $querySuffix = "id = $user"; - } else { - return false; + $result = User::query()->where('username', $user)->orWhere('id', $user)->first(['user_roles_id']); + + if ($result !== null) { + return $result['user_roles_id'] === $roleID; } - $result = $this->pdo->queryOneRow( - sprintf( - 'SELECT role FROM users WHERE %s', - $querySuffix - ) - ); - - return $result['role'] === $roleID; + return false; } /** diff --git a/public/pages/AdminPage.php b/public/pages/AdminPage.php index 719057e96..fa62a945d 100644 --- a/public/pages/AdminPage.php +++ b/public/pages/AdminPage.php @@ -19,14 +19,14 @@ class AdminPage extends BasePage // Tell Smarty which directories to use for templates $this->smarty->setTemplateDir( - [ - 'admin' => NN_THEMES.'shared/templates/admin', - 'shared' => NN_THEMES.'shared/templates', - 'default' => NN_THEMES.'Omicron/templates', - ] - ); + [ + 'admin' => NN_THEMES.'shared/templates/admin', + 'shared' => NN_THEMES.'shared/templates', + 'default' => NN_THEMES.'Omicron/templates', + ] + ); - if (! isset($this->userdata['role']) || (int) $this->userdata['role'] !== Users::ROLE_ADMIN || ! $this->users->isLoggedIn()) { + if (! isset($this->userdata['user_roles_id']) || (int) $this->userdata['user_roles_id'] !== Users::ROLE_ADMIN || ! $this->users->isLoggedIn()) { $this->show403(true); } diff --git a/public/pages/BasePage.php b/public/pages/BasePage.php index be170646e..ba263b81c 100644 --- a/public/pages/BasePage.php +++ b/public/pages/BasePage.php @@ -335,7 +335,7 @@ class BasePage { $this->userdata = $this->users->getById($this->users->currentUserId()); $this->userdata['categoryexclusions'] = $this->users->getCategoryExclusion($this->users->currentUserId()); - $this->userdata['rolecategoryexclusions'] = $this->users->getRoleCategoryExclusion($this->userdata['role']); + $this->userdata['rolecategoryexclusions'] = $this->users->getRoleCategoryExclusion($this->userdata['user_roles_id']); // Change the theme to user's selected theme if they selected one, else use the admin one. if ((int) Settings::settingValue('site.main.userselstyle') === 1) { @@ -368,7 +368,7 @@ class BasePage if ($sab->integratedBool !== false && $sab->url !== '' && $sab->apikey !== '') { $this->smarty->assign('sabapikeytype', $sab->apikeytype); } - switch ((int) $this->userdata['role']) { + switch ((int) $this->userdata['user_roles_id']) { case Users::ROLE_ADMIN: $this->smarty->assign('isadmin', 'true'); break; diff --git a/public/pages/Page.php b/public/pages/Page.php index 3af9bb714..91431d00e 100644 --- a/public/pages/Page.php +++ b/public/pages/Page.php @@ -32,7 +32,7 @@ class Page extends BasePage $role = Users::ROLE_GUEST; if ($this->userdata != null) { - $role = $this->userdata['role']; + $role = $this->userdata['user_roles_id']; } $content = new Contents(['Settings' => $this->settings]);