diff --git a/app/Libraries/Aauth.php b/app/Libraries/Aauth.php index 42c09b2..62119b3 100644 --- a/app/Libraries/Aauth.php +++ b/app/Libraries/Aauth.php @@ -753,7 +753,9 @@ class Aauth */ public function deleteUser(int $userId) { - $userModel = new UserModel(); + $userModel = new UserModel(); + $groupToUserModel = new GroupToUserModel(); + $permToUserModel = new PermToUserModel(); if (! $userModel->existsById($userId)) { @@ -762,7 +764,24 @@ class Aauth return false; } - return $userModel->delete($userId); + $userModel->transStart(); + $groupToUserModel->deleteAllByUserId($userId); + $permToUserModel->deleteAllByUserId($userId); + $userModel->delete($userId); + $userModel->transComplete(); + + if ($userModel->transStatus() === false) + { + $userModel->transRollback(); + + return false; + } + else + { + $userModel->transCommit(); + + return true; + } } /** diff --git a/tests/Aauth/Database/UserSessionModelTest.php b/tests/Aauth/Database/UserSessionModelTest.php new file mode 100644 index 0000000..ac0a828 --- /dev/null +++ b/tests/Aauth/Database/UserSessionModelTest.php @@ -0,0 +1,54 @@ +model = new UserSessionModel($this->db); + $this->config = new AauthConfig(); + } + + //-------------------------------------------------------------------- + + public function testDelete() + { + $id = md5(time()); + $this->hasInDatabase($this->config->dbTableUserSessions, [ + 'id' => $id, + 'ip_address' => '127.0.0.1', + 'timestamp' => time(), + 'data' => '', + ]); + $this->seeNumRecords(1, $this->config->dbTableUserSessions, []); + $this->model->delete($id); + $this->seeNumRecords(0, $this->config->dbTableUserSessions, []); + } + + public function testDBInsert() + { + $this->seeNumRecords(0, $this->config->dbTableUserSessions, []); + $id = md5(time()); + $this->model->insert([ + 'id' => $id, + 'ip_address' => '127.0.0.1', + 'timestamp' => time(), + 'data' => '', + ]); + $this->seeNumRecords(1, $this->config->dbTableUserSessions, []); + $this->assertEquals(1, $this->model->affectedRows()); + $this->assertEquals(1, $this->model->countAll()); + } + +} diff --git a/tests/Aauth/Libraries/Aauth/GroupTest.php b/tests/Aauth/Libraries/Aauth/GroupTest.php index ae1bf27..ee7f473 100644 --- a/tests/Aauth/Libraries/Aauth/GroupTest.php +++ b/tests/Aauth/Libraries/Aauth/GroupTest.php @@ -220,6 +220,13 @@ class GroupTest extends CIDatabaseTestCase ]); } + public function testGetUserGroups() + { + $this->assertCount(2, $this->library->getUserGroups(1)); + $this->assertCount(1, $this->library->getUserGroups(2)); + $this->assertFalse($this->library->getUserGroups(99)); + } + public function testRemoveMemberFromAll() { $this->assertTrue($this->library->removeMemberFromAll(1)); diff --git a/tests/Aauth/Libraries/Aauth/PermTest.php b/tests/Aauth/Libraries/Aauth/PermTest.php index bdbf595..be876b6 100644 --- a/tests/Aauth/Libraries/Aauth/PermTest.php +++ b/tests/Aauth/Libraries/Aauth/PermTest.php @@ -176,6 +176,37 @@ class PermTest extends CIDatabaseTestCase $this->assertEquals('testPerm1', $permsOrderBy['perms'][1]['name']); } + public function testGetUserPerms() + { + $this->assertCount(0, $this->library->getUserPerms(1, 1)); + + $this->hasInDatabase($this->config->dbTablePerms, [ + 'id' => 1, + 'name' => 'testPerm1', + 'definition' => 'Test Perm 1', + ]); + $this->hasInDatabase($this->config->dbTablePerms, [ + 'id' => 2, + 'name' => 'testPerm2', + 'definition' => 'Test Perm 2', + ]); + $this->hasInDatabase($this->config->dbTablePermToUser, [ + 'perm_id' => 1, + 'user_id' => 1, + 'state' => 1, + ]); + $this->hasInDatabase($this->config->dbTablePermToUser, [ + 'perm_id' => 2, + 'user_id' => 1, + 'state' => 0, + ]); + + $this->assertCount(1, $this->library->getUserPerms(1, 1)); + $this->assertCount(1, $this->library->getUserPerms(1, 0)); + $this->assertCount(2, $this->library->getUserPerms(1)); + $this->assertFalse($this->library->getUserPerms(99, 1)); + } + /** * @runInSeparateProcess * @preserveGlobalState disabled @@ -387,6 +418,37 @@ class PermTest extends CIDatabaseTestCase $this->assertEquals(lang('Aauth.notFoundGroup'), $this->library->getErrorsArray()[0]); } + public function testGetGroupPerms() + { + $this->assertCount(0, $this->library->getGroupPerms(1, 1)); + + $this->hasInDatabase($this->config->dbTablePerms, [ + 'id' => 1, + 'name' => 'testPerm1', + 'definition' => 'Test Perm 1', + ]); + $this->hasInDatabase($this->config->dbTablePerms, [ + 'id' => 2, + 'name' => 'testPerm2', + 'definition' => 'Test Perm 2', + ]); + $this->hasInDatabase($this->config->dbTablePermToGroup, [ + 'perm_id' => 1, + 'group_id' => 1, + 'state' => 1, + ]); + $this->hasInDatabase($this->config->dbTablePermToGroup, [ + 'perm_id' => 2, + 'group_id' => 1, + 'state' => 0, + ]); + + $this->assertCount(1, $this->library->getGroupPerms(1, 1)); + $this->assertCount(1, $this->library->getGroupPerms(1, 0)); + $this->assertCount(2, $this->library->getGroupPerms(1)); + $this->assertFalse($this->library->getGroupPerms(99, 1)); + } + public function testListGroupPerms() { $this->hasInDatabase($this->config->dbTablePerms, [