From aca63f8e2a13f7b926066a0515040ffe585c56ea Mon Sep 17 00:00:00 2001 From: REJack Date: Fri, 28 Dec 2018 19:07:45 +0000 Subject: [PATCH] updated Libraries/Aauth & UserTest - added $this->configApp - enhanced `listUsers()` & `listUsersPaginated()` with $groupPar --- app/Libraries/Aauth.php | 56 ++++++++++++++++-------- tests/Aauth/Libraries/Aauth/UserTest.php | 8 +++- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/app/Libraries/Aauth.php b/app/Libraries/Aauth.php index 4f49a98..0543b01 100644 --- a/app/Libraries/Aauth.php +++ b/app/Libraries/Aauth.php @@ -42,6 +42,13 @@ class Aauth */ protected $config; + /** + * Variable for loading the app config array into + * + * @var \Config\App + */ + protected $configApp; + /** * Variable for loading the session service into * @@ -112,8 +119,9 @@ class Aauth $session = \Config\Services::session(); } - $this->config = $config; - $this->session = $session; + $this->configApp = new \Config\App(); + $this->config = $config; + $this->session = $session; } //-------------------------------------------------------------------- @@ -718,32 +726,38 @@ class Aauth * * Return users as an object array * - * @param integer $limit Limit of users to be returned - * @param integer $offset Offset for limited number of users - * @param boolean $includeBanneds Include banned users - * @param string $orderBy Order by MYSQL string (e.g. 'name ASC', 'email DESC') + * @param string|integer $groupPar Specify group id to list group or null for all users + * @param integer $limit Limit of users to be returned + * @param integer $offset Offset for limited number of users + * @param boolean $includeBanneds Include banned users + * @param string $orderBy Order by MYSQL string (e.g. 'name ASC', 'email DESC') * * @return array Array of users */ - public function listUsers(int $limit = 0, int $offset = 0, bool $includeBanneds = null, string $orderBy = null) + public function listUsers($groupPar = null, int $limit = 0, int $offset = 0, bool $includeBanneds = null, string $orderBy = null) { $userModel = new UserModel(); - $user = $userModel->limit($limit, $offset); + $userModel->limit($limit, $offset); $userModel->select('id, email, username, banned, created_at, updated_at, last_activity, last_ip_address, last_login'); - // eanbool $groupPar = null, + + if ($groupPar && $groupId = $this->getGroupId($groupPar)) + { + $userModel->join($this->config->dbTableGroupToUser, $this->config->dbTableGroupToUser . '.user_id = ' . $this->config->dbTableUsers . '.id'); + $userModel->where($this->config->dbTableGroupToUser . '.group_id', $groupId); + } if (is_null($includeBanneds)) { - $user->where('banned', 0); + $userModel->where('banned', 0); } if (! is_null($orderBy)) { - $user->orderBy($orderBy); + $userModel->orderBy($orderBy); } - return $user->findAll(); + return $userModel->findAll(); } /** @@ -751,19 +765,25 @@ class Aauth * * Return users as an object array * - * @param integer $limit Limit of users to be returned - * @param integer $offset Offset for limited number of users - * @param boolean $includeBanneds Include banned users - * @param string $orderBy Order by MYSQL string (e.g. 'name ASC', 'email DESC') + * @param string|integer $groupPar Specify group id to list group or null for all users + * @param integer $limit Limit of users to be returned + * @param integer $offset Offset for limited number of users + * @param boolean $includeBanneds Include banned users + * @param string $orderBy Order by MYSQL string (e.g. 'name ASC', 'email DESC') * * @return array Array of users */ - public function listUsersPaginated(int $limit = 10, bool $includeBanneds = null, string $orderBy = null) + public function listUsersPaginated($groupPar = null, int $limit = 10, bool $includeBanneds = null, string $orderBy = null) { $userModel = new UserModel(); $userModel->select('id, email, username, banned, created_at, updated_at, last_activity, last_ip_address, last_login'); - // eanbool $groupPar = null, + + if ($groupPar && $groupId = $this->getGroupId($groupPar)) + { + $userModel->join($this->config->dbTableGroupToUser, $this->config->dbTableGroupToUser . '.user_id = ' . $this->config->dbTableUsers . '.id'); + $userModel->where($this->config->dbTableGroupToUser . '.group_id', $groupId); + } if (is_null($includeBanneds)) { diff --git a/tests/Aauth/Libraries/Aauth/UserTest.php b/tests/Aauth/Libraries/Aauth/UserTest.php index 913caf9..0952108 100644 --- a/tests/Aauth/Libraries/Aauth/UserTest.php +++ b/tests/Aauth/Libraries/Aauth/UserTest.php @@ -158,9 +158,11 @@ class UserTest extends CIDatabaseTestCase $this->assertEquals('admin', $users[0]['username']); $this->assertEquals('user', $users[1]['username']); - $usersOrderBy = $this->library->listUsers(0, 0, null, 'id DESC'); + $usersOrderBy = $this->library->listUsers(null, 0, 0, null, 'id DESC'); $this->assertEquals('user', $usersOrderBy[0]['username']); $this->assertEquals('admin', $usersOrderBy[1]['username']); + + $this->assertCount(1, $this->library->listUsers($this->config->groupAdmin)); } public function testListUsersPaginated() @@ -171,9 +173,11 @@ class UserTest extends CIDatabaseTestCase $this->assertEquals('admin', $users['users'][0]['username']); $this->assertEquals('user', $users['users'][1]['username']); - $usersOrderBy = $this->library->listUsersPaginated(10, null, 'id DESC'); + $usersOrderBy = $this->library->listUsersPaginated(null, 10, null, 'id DESC'); $this->assertEquals('user', $usersOrderBy['users'][0]['username']); $this->assertEquals('admin', $usersOrderBy['users'][1]['username']); + + $this->assertCount(1, $this->library->listUsersPaginated($this->config->groupAdmin)['users']); } public function testVerifyUser()