diff --git a/app/Config/Aauth.php b/app/Config/Aauth.php index 391ea6d..b486080 100644 --- a/app/Config/Aauth.php +++ b/app/Config/Aauth.php @@ -327,6 +327,11 @@ class Aauth extends BaseConfig | The table which contains join of subgroups and groups | (default: 'aauth_group_to_group') | + | 'dbTableGroupVariables' + | + | The table which contains group variables + | (default: 'aauth_group_variables') + | | 'dbTablePerms' | | The table which contains permissions @@ -360,18 +365,19 @@ class Aauth extends BaseConfig | If this is enabled, it simply set a flag when rows are deleted. | (default: false) */ - public $dbProfile = 'default'; - public $dbTableUsers = 'aauth_users'; - public $dbTableUserVariables = 'aauth_user_variables'; - public $dbTableLoginAttempts = 'aauth_login_attempts'; - public $dbTableLoginTokens = 'aauth_login_tokens'; - public $dbTableGroups = 'aauth_groups'; - public $dbTableGroupToUser = 'aauth_group_to_user'; - public $dbTableGroupToGroup = 'aauth_group_to_group'; - public $dbTablePerms = 'aauth_perms'; - public $dbTablePermToUser = 'aauth_perm_to_user'; - public $dbTablePermToGroup = 'aauth_perm_to_group'; - public $dbSoftDeleteUsers = true; - public $dbSoftDeleteGroups = true; - public $dbSoftDeletePerms = true; + public $dbProfile = 'default'; + public $dbTableUsers = 'aauth_users'; + public $dbTableUserVariables = 'aauth_user_variables'; + public $dbTableLoginAttempts = 'aauth_login_attempts'; + public $dbTableLoginTokens = 'aauth_login_tokens'; + public $dbTableGroups = 'aauth_groups'; + public $dbTableGroupToUser = 'aauth_group_to_user'; + public $dbTableGroupToGroup = 'aauth_group_to_group'; + public $dbTableGroupVariables = 'aauth_group_variables'; + public $dbTablePerms = 'aauth_perms'; + public $dbTablePermToUser = 'aauth_perm_to_user'; + public $dbTablePermToGroup = 'aauth_perm_to_group'; + public $dbSoftDeleteUsers = true; + public $dbSoftDeleteGroups = true; + public $dbSoftDeletePerms = true; } diff --git a/app/Database/Migrations/20181031064311_create_group_variables.php b/app/Database/Migrations/20181031064311_create_group_variables.php new file mode 100644 index 0000000..5ddc4d9 --- /dev/null +++ b/app/Database/Migrations/20181031064311_create_group_variables.php @@ -0,0 +1,83 @@ +forge->addField([ + 'id' => [ + 'type' => 'INT', + 'constraint' => 11, + 'unsigned' => true, + 'auto_increment' => true, + ], + 'group_id' => [ + 'type' => 'INT', + 'constraint' => 11, + 'unsigned' => true, + ], + 'data_key' => [ + 'type' => 'VARCHAR', + 'constraint' => 100, + ], + 'data_value' => [ + 'type' => 'TEXT', + ], + 'created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP', + 'updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP', + 'system' => [ + 'type' => 'TINYINT', + 'constraint' => 1, + 'default' => 0, + ], + ]); + $this->forge->addKey('id', true); + $this->forge->createTable($config->dbTableGroupVariables, true); + } + + //-------------------------------------------------------------------- + + /** + * Drops Table + * + * @return void + */ + public function down() + { + $config = new AauthConfig(); + $this->forge->dropTable($config->dbTableGroupVariables, true); + } +} diff --git a/app/Models/Aauth/GroupVariableModel.php b/app/Models/Aauth/GroupVariableModel.php new file mode 100644 index 0000000..9362ef3 --- /dev/null +++ b/app/Models/Aauth/GroupVariableModel.php @@ -0,0 +1,379 @@ +config = new AauthConfig(); + $this->DBGroup = $this->config->dbProfile; + $this->table = $this->config->dbTableGroupVariables; + $this->tempReturnType = $this->returnType; + + if ($db instanceof ConnectionInterface) + { + $this->db = & $db; + } + else + { + $this->db = Database::connect($this->DBGroup); + } + } + + /** + * Find group variable + * + * Find Group Variable by groupId, dataKey & optional system + * + * @param integer $groupId Group id + * @param string $dataKey Key of variable + * @param boolean $system Whether system variable + * + * @return string|boolean + */ + public function find(int $groupId, string $dataKey, bool $system = null) + { + $builder = $this->builder(); + $builder->select('data_value'); + $builder->where('group_id', $groupId); + $builder->where('data_key', $dataKey); + + $builder->where('system', ($system ? 1 : 0)); + + if ($row = $builder->get()->getFirstRow($this->tempReturnType)) + { + return $row['data_value']; + } + + return false; + } + + /** + * Find all group variables + * + * @param integer $groupId Group id + * @param boolean $system Whether system variable + * + * @return object + */ + public function findAll(int $groupId, bool $system = null) + { + $builder = $this->builder(); + $builder->where('group_id', $groupId); + $builder->where('system', ($system ? 1 : 0)); + + $this->tempReturnType = $this->returnType; + + return $builder->get()->getResult($this->tempReturnType); + } + + /** + * Update/Insert Group Variable + * + * @param integer $groupId Group id + * @param string $dataKey Key of variable + * @param string $dataValue Value of variable + * @param boolean $system Whether system variable + * + * @return BaseBuilder + */ + public function save(int $groupId, string $dataKey, string $dataValue, bool $system = null) + { + $builder = $this->builder(); + $builder->where('group_id', $groupId); + $builder->where('data_key', $dataKey); + $builder->where('system', ($system ? 1 : 0)); + + if ($builder->countAllResults()) + { + $response = $this->update($groupId, $dataKey, $dataValue, $system); + } + else + { + $response = $this->insert($groupId, $dataKey, $dataValue, $system); + } + + return $response; + } + + /** + * Inserts Group Variable + * + * @param integer $groupId Group id + * @param string $dataKey Key of variable + * @param string $dataValue Value of variable + * @param boolean $system Whether system variable + * + * @return boolean + */ + public function insert(int $groupId, string $dataKey, string $dataValue, bool $system = null) + { + $builder = $this->builder(); + + $data['group_id'] = $groupId; + $data['data_key'] = $dataKey; + $data['data_value'] = $dataValue; + $data['system'] = ($system ? 1 : 0); + $data['created_at'] = date('Y-m-d H:i:s'); + $data['updated_at'] = date('Y-m-d H:i:s'); + + $builder->insert($data); + + return true; + } + + /** + * Update Group Variable + * + * @param integer $groupId Group id + * @param string $dataKey Key of variable + * @param string $dataValue Value of variable + * @param boolean $system Whether system variable + * + * @return BaseBuilder + */ + public function update(int $groupId, string $dataKey, string $dataValue, bool $system = null) + { + $builder = $this->builder(); + $builder->where('group_id', $groupId); + $builder->where('data_key', $dataKey); + $builder->where('system', ($system ? 1 : 0)); + + $data['data_value'] = $dataValue; + $data['updated_at'] = date('Y-m-d H:i:s'); + + return $builder->set($data)->update(); + } + + /** + * Delete Group Variable + * + * @param integer $groupId Group id + * @param string $dataKey Key of variable + * @param boolean $system Whether system variable + * + * @return boolean + */ + public function delete(int $groupId, string $dataKey, bool $system = null) + { + $builder = $this->builder(); + $builder->where('group_id', $groupId); + $builder->where('data_key', $dataKey); + $builder->where('system', ($system ? 1 : 0)); + $builder->delete(); + + return true; + } + + /** + * Delete all Group Variables by Group ID + * + * @param integer $groupId Group id + * + * @return boolean + */ + public function deleteAllByGroupId(int $groupId) + { + $builder = $this->builder(); + $builder->where('group_id', $groupId); + $builder->delete(); + + return true; + } + + //-------------------------------------------------------------------- + // Utility + //-------------------------------------------------------------------- + + /** + * Sets the return type of the results to be as an associative array. + * + * @return Model + */ + public function asArray() + { + $this->tempReturnType = $this->returnType = 'array'; + + return $this; + } + + /** + * Sets the return type to be of the specified type of object. + * Defaults to a simple object, but can be any class that has + * class vars with the same name as the table columns, or at least + * allows them to be created. + * + * @param string $class Class + * + * @return Model + */ + public function asObject(string $class = 'object') + { + $this->tempReturnType = $this->returnType = $class; + + return $this; + } + + /** + * Returns the first row of the result set. Will take any previous + * Query Builder calls into account when determing the result set. + * + * @return array|object|null + */ + public function first() + { + $builder = $this->builder(); + + $row = $builder->limit(1, 0)->get(); + $row = $row->getFirstRow($this->tempReturnType); + + $this->tempReturnType = $this->returnType; + + return $row; + } + + /** + * Provides a shared instance of the Query Builder. + * + * @param string $table Table name + * + * @return BaseBuilder + */ + protected function builder(string $table = null) + { + if ($this->builder instanceof BaseBuilder) + { + return $this->builder; + } + + $table = empty($table) ? $this->table : $table; + + $this->builder = $this->db->table($table); + + return $this->builder; + } + + /** + * Provides direct access to method in the builder (if available) + * and the database connection. + * + * @param string $name Name + * @param array $params Params + * + * @return Model|null + */ + public function __call(string $name, array $params) + { + $result = null; + + if (method_exists($this->db, $name)) + { + $result = $this->db->$name(...$params); + } + elseif (method_exists($builder = $this->builder(), $name)) + { + $result = $builder->$name(...$params); + } + + // Don't return the builder object unless specifically requested + //, since that will interrupt the usability flow + // and break intermingling of model and builder methods. + if ($name !== 'builder' && empty($result)) + { + return $result; + } + if ($name !== 'builder' && ! $result instanceof BaseBuilder) + { + return $result; + } + + return $this; + } +} diff --git a/tests/Aauth/Database/GroupVariableModelTest.php b/tests/Aauth/Database/GroupVariableModelTest.php new file mode 100644 index 0000000..bf294ec --- /dev/null +++ b/tests/Aauth/Database/GroupVariableModelTest.php @@ -0,0 +1,142 @@ +model = new GroupVariableModel($this->db); + $this->config = new AauthConfig(); + } + + //-------------------------------------------------------------------- + + public function testFind() + { + $groupVariable = $this->model->find(99, 'test'); + $this->assertFalse($groupVariable); + + $this->hasInDatabase($this->config->dbTableGroupVariables, [ + 'group_id' => 99, + 'data_key' => 'test', + 'data_value' => 'TRUE', + ]); + $groupVariable = $this->model->find(99, 'test'); + $this->assertEquals('TRUE', $groupVariable); + } + + public function testFindAll() + { + $this->hasInDatabase($this->config->dbTableGroupVariables, [ + 'group_id' => 99, + 'data_key' => 'test', + 'data_value' => 'TRUE', + ]); + $groupVariables = $this->model->findAll(99); + $this->assertCount(1, $groupVariables); + } + + public function testSave() + { + $this->model->save(99, 'test', 'TRUE'); + $this->seeInDatabase($this->config->dbTableGroupVariables, [ + 'group_id' => 99, + 'data_key' => 'test', + 'data_value' => 'TRUE', + ]); + + $this->model->save(99, 'test', 'TRUE2'); + $this->seeInDatabase($this->config->dbTableGroupVariables, [ + 'group_id' => 99, + 'data_key' => 'test', + 'data_value' => 'TRUE2', + ]); + } + + public function testDelete() + { + $this->hasInDatabase($this->config->dbTableGroupVariables, [ + 'group_id' => 99, + 'data_key' => 'test', + 'data_value' => 'TRUE', + ]); + $criteria = [ + 'group_id' => 99, + ]; + $this->seeNumRecords(1, $this->config->dbTableGroupVariables, $criteria); + $this->model->delete(99, 'test'); + $this->seeNumRecords(0, $this->config->dbTableGroupVariables, $criteria); + } + + public function testDeleteAllByGroupId() + { + $this->hasInDatabase($this->config->dbTableGroupVariables, [ + 'group_id' => 99, + 'data_key' => 'test', + 'data_value' => 'TRUE', + ]); + $criteria = [ + 'group_id' => 99, + ]; + $this->seeNumRecords(1, $this->config->dbTableGroupVariables, $criteria); + $this->model->deleteAllByGroupId(99); + $this->seeNumRecords(0, $this->config->dbTableGroupVariables, $criteria); + } + + public function testAsArrayFirst() + { + $this->hasInDatabase($this->config->dbTableGroupVariables, [ + 'group_id' => 99, + 'data_key' => 'test', + 'data_value' => 'TRUE', + ]); + $groupVariable = $this->model->asArray()->where(['data_key' => 'test', 'data_value' => 'TRUE'])->first(); + $this->assertInternalType('array', $groupVariable); + } + + public function testAsObjectFirst() + { + $this->hasInDatabase($this->config->dbTableGroupVariables, [ + 'group_id' => 99, + 'data_key' => 'test', + 'data_value' => 'TRUE', + ]); + $groupVariable = $this->model->asObject()->where(['data_key' => 'test', 'data_value' => 'TRUE'])->first(); + $this->assertInternalType('object', $groupVariable); + } + + public function testConfigDBGroup() + { + $this->model = new GroupVariableModel(); + $this->hasInDatabase($this->config->dbTableGroupVariables, [ + 'group_id' => 99, + 'data_key' => 'test', + 'data_value' => 'TRUE', + ]); + $groupVariable = $this->model->asObject()->where(['data_key' => 'test', 'data_value' => 'TRUE'])->first(); + $this->assertInternalType('object', $groupVariable); + } + + public function testDBCallEmpty() + { + $this->assertEquals(0, $this->model->insertID()); + } + + public function testDBCall() + { + $this->model->save(99, 'test', 'TRUE'); + $this->assertEquals(1, $this->model->insertID()); + } + +}