From c3786bd3650432d75799138e461f92bda969eb2d Mon Sep 17 00:00:00 2001 From: REJack Date: Mon, 31 Dec 2018 17:21:08 +0000 Subject: [PATCH] added GroupVar functions & tests --- app/Libraries/Aauth.php | 125 +++++++++++++++++- .../Libraries/Aauth/GroupVariablesTest.php | 107 +++++++++++++++ 2 files changed, 231 insertions(+), 1 deletion(-) create mode 100644 tests/Aauth/Libraries/Aauth/GroupVariablesTest.php diff --git a/app/Libraries/Aauth.php b/app/Libraries/Aauth.php index 7f91bbd..7847a81 100644 --- a/app/Libraries/Aauth.php +++ b/app/Libraries/Aauth.php @@ -27,6 +27,7 @@ use \App\Models\Aauth\PermToUserModel; use \App\Models\Aauth\LoginAttemptModel; use \App\Models\Aauth\LoginTokenModel; use \App\Models\Aauth\UserVariableModel; +use \App\Models\Aauth\GroupVariableModel; /** * Aauth Library @@ -1146,7 +1147,7 @@ class Aauth $userModel = new UserModel(); - if (! @$userModel->existsById($userId)) + if (! $userModel->existsById($userId)) { return false; } @@ -1736,6 +1737,128 @@ class Aauth ]; } + /** + * Set Group Variable as key value + * + * if variable not set before, it will be set + * if set, overwrites the value + * + * @param string $key + * @param string $value + * @param integer $groupId Group id + * + * @return boolean + */ + public function setGroupVar(string $key, string $value, int $groupId) + { + $groupModel = new GroupModel(); + + if (! $groupModel->existsById($groupId)) + { + return false; + } + + $groupVariableModel = new GroupVariableModel(); + + return $groupVariableModel->save($groupId, $key, $value); + } + + /** + * Unset Group Variable as key value + * + * @param string $key + * @param integer $groupId Group id + * + * @return boolean + */ + public function unsetGroupVar(string $key, int $groupId) + { + $groupModel = new GroupModel(); + + if (! $groupModel->existsById($groupId)) + { + return false; + } + + $groupVariableModel = new GroupVariableModel(); + + return $groupVariableModel->delete($groupId, $key); + } + + /** + * Get Group Variable by key + * + * @param string $key Variable Key + * @param integer $groupId Group id + * + * @return boolean|string false if var is not set, the value of var if set + */ + public function getGroupVar(string $key, int $groupId) + { + $groupModel = new GroupModel(); + + if (! $groupModel->existsById($groupId)) + { + return false; + } + + $groupVariableModel = new GroupVariableModel(); + + if (! $variable = $groupVariableModel->find($groupId, $key)) + { + return false; + } + + return $variable; + } + + /** + * Get Group Variables by group id + * + * Return array with all group keys & variables + * + * @param integer $groupId Group id + * + * @return array + */ + public function getGroupVars(int $groupId = null) + { + $groupModel = new GroupModel(); + + if (! $groupModel->existsById($groupId)) + { + return false; + } + + $groupVariableModel = new GroupVariableModel(); + + return $groupVariableModel->findAll($groupId); + } + + /** + * List Group Variable Keys by GroupId + * + * Return array of variable keys or false + * + * @param integer $groupId Group id + * + * @return boolean|array + */ + public function listGroupVarKeys(int $groupId = null) + { + $groupModel = new GroupModel(); + + if (! $groupModel->existsById($groupId)) + { + return false; + } + + $groupVariableModel = new GroupVariableModel(); + $groupVariableModel->select('data_key as key'); + + return $groupVariableModel->findAll($groupId); + } + //-------------------------------------------------------------------- // Perm Functions //-------------------------------------------------------------------- diff --git a/tests/Aauth/Libraries/Aauth/GroupVariablesTest.php b/tests/Aauth/Libraries/Aauth/GroupVariablesTest.php new file mode 100644 index 0000000..fe7dde2 --- /dev/null +++ b/tests/Aauth/Libraries/Aauth/GroupVariablesTest.php @@ -0,0 +1,107 @@ +library = new Aauth(null, true); + $this->config = new AauthConfig(); + } + + //-------------------------------------------------------------------- + + public function testSetGroupVar() + { + $this->assertTrue($this->library->setGroupVar('test_var', 'test', 1)); + $this->seeInDatabase($this->config->dbTableGroupVariables, [ + 'group_id' => 1, + 'data_key' => 'test_var', + 'data_value' => 'test', + ]); + + $this->assertFalse($this->library->setGroupVar('test_var', 'test', 99)); + } + + public function testUnsetGroupVar() + { + $this->hasInDatabase($this->config->dbTableGroupVariables, [ + 'group_id' => 1, + 'data_key' => 'test_var', + 'data_value' => 'test', + ]); + + $this->assertTrue($this->library->unsetGroupVar('test_var', 1)); + + $this->assertFalse($this->library->unsetGroupVar('test_var', 99)); + } + + public function testGetGroupVar() + { + $this->hasInDatabase($this->config->dbTableGroupVariables, [ + 'group_id' => 1, + 'data_key' => 'test_var', + 'data_value' => 'test', + ]); + + $this->assertEquals('test', $this->library->getGroupVar('test_var', 1)); + + $this->assertFalse($this->library->getGroupVar('test_var_99', 1)); + + $this->assertFalse($this->library->getGroupVar('test_var', 99)); + } + + public function testGetGroupVars() + { + $this->hasInDatabase($this->config->dbTableGroupVariables, [ + 'group_id' => 1, + 'data_key' => 'test_var', + 'data_value' => 'test', + ]); + $this->hasInDatabase($this->config->dbTableGroupVariables, [ + 'group_id' => 1, + 'data_key' => 'test_var2', + 'data_value' => 'test2', + ]); + + $this->assertCount(2, $this->library->getGroupVars(1)); + + $this->assertFalse($this->library->getGroupVars(99)); + } + + public function testListGroupVarKeys() + { + $this->hasInDatabase($this->config->dbTableGroupVariables, [ + 'group_id' => 1, + 'data_key' => 'test_var', + 'data_value' => 'test', + ]); + $this->hasInDatabase($this->config->dbTableGroupVariables, [ + 'group_id' => 1, + 'data_key' => 'test_var2', + 'data_value' => 'test2', + ]); + + $this->assertCount(2, $this->library->listGroupVarKeys(1)); + $this->assertEquals([['key' => 'test_var'], ['key' => 'test_var2']], $this->library->listGroupVarKeys(1)); + + $this->assertFalse($this->library->listGroupVarKeys(99)); + } +}