Browse Source

added Group Variables

- updated Config/Aauth
- added Migration
- added GroupVariableModel & GroupVariableModelTest
v3-dev
REJack 6 years ago
parent
commit
0356236f7a
No known key found for this signature in database
GPG Key ID: 4A44B48700429F46
  1. 34
      app/Config/Aauth.php
  2. 83
      app/Database/Migrations/20181031064311_create_group_variables.php
  3. 379
      app/Models/Aauth/GroupVariableModel.php
  4. 142
      tests/Aauth/Database/GroupVariableModelTest.php

34
app/Config/Aauth.php

@ -327,6 +327,11 @@ class Aauth extends BaseConfig
| The table which contains join of subgroups and groups | The table which contains join of subgroups and groups
| (default: 'aauth_group_to_group') | (default: 'aauth_group_to_group')
| |
| 'dbTableGroupVariables'
|
| The table which contains group variables
| (default: 'aauth_group_variables')
|
| 'dbTablePerms' | 'dbTablePerms'
| |
| The table which contains permissions | 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. | If this is enabled, it simply set a flag when rows are deleted.
| (default: false) | (default: false)
*/ */
public $dbProfile = 'default'; public $dbProfile = 'default';
public $dbTableUsers = 'aauth_users'; public $dbTableUsers = 'aauth_users';
public $dbTableUserVariables = 'aauth_user_variables'; public $dbTableUserVariables = 'aauth_user_variables';
public $dbTableLoginAttempts = 'aauth_login_attempts'; public $dbTableLoginAttempts = 'aauth_login_attempts';
public $dbTableLoginTokens = 'aauth_login_tokens'; public $dbTableLoginTokens = 'aauth_login_tokens';
public $dbTableGroups = 'aauth_groups'; public $dbTableGroups = 'aauth_groups';
public $dbTableGroupToUser = 'aauth_group_to_user'; public $dbTableGroupToUser = 'aauth_group_to_user';
public $dbTableGroupToGroup = 'aauth_group_to_group'; public $dbTableGroupToGroup = 'aauth_group_to_group';
public $dbTablePerms = 'aauth_perms'; public $dbTableGroupVariables = 'aauth_group_variables';
public $dbTablePermToUser = 'aauth_perm_to_user'; public $dbTablePerms = 'aauth_perms';
public $dbTablePermToGroup = 'aauth_perm_to_group'; public $dbTablePermToUser = 'aauth_perm_to_user';
public $dbSoftDeleteUsers = true; public $dbTablePermToGroup = 'aauth_perm_to_group';
public $dbSoftDeleteGroups = true; public $dbSoftDeleteUsers = true;
public $dbSoftDeletePerms = true; public $dbSoftDeleteGroups = true;
public $dbSoftDeletePerms = true;
} }

83
app/Database/Migrations/20181031064311_create_group_variables.php

@ -0,0 +1,83 @@
<?php
/**
* CodeIgniter-Aauth
*
* Aauth is a User Authorization Library for CodeIgniter 4.x, which aims to make
* easy some essential jobs such as login, permissions and access operations.
* Despite ease of use, it has also very advanced features like grouping,
* access management, public access etc..
*
* @package CodeIgniter-Aauth
* @author Emre Akay
* @author Raphael "REJack" Jackstadt
* @copyright 2014-2019 Emre Akay
* @license https://opensource.org/licenses/MIT MIT License
* @link https://github.com/emreakay/CodeIgniter-Aauth
*/
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
use Config\Aauth as AauthConfig;
/**
* Create group variables table
*
* @package CodeIgniter-Aauth
*
* @codeCoverageIgnore
*/
class Migration_create_group_variables extends Migration
{
/**
* Create Table
*
* @return void
*/
public function up()
{
$config = new AauthConfig();
$this->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);
}
}

379
app/Models/Aauth/GroupVariableModel.php

@ -0,0 +1,379 @@
<?php
/**
* CodeIgniter-Aauth
*
* Aauth is a User Authorization Library for CodeIgniter 4.x, which aims to make
* easy some essential jobs such as login, permissions and access operations.
* Despite ease of use, it has also very advanced features like grouping,
* access management, public access etc..
*
* @package CodeIgniter-Aauth
* @author Emre Akay
* @author Raphael "REJack" Jackstadt
* @copyright 2014-2019 Emre Akay
* @license https://opensource.org/licenses/MIT MIT License
* @link https://github.com/emreakay/CodeIgniter-Aauth
*/
namespace App\Models\Aauth;
use Config\Aauth as AauthConfig;
use Config\Database;
use CodeIgniter\Database\BaseBuilder;
use CodeIgniter\Database\BaseConnection;
use CodeIgniter\Database\ConnectionInterface;
/**
* Group Variable Model.
*
* @package CodeIgniter-Aauth
*
* @since 3.0.0
*/
class GroupVariableModel
{
/**
* Database Connection
*
* @var ConnectionInterface
*/
protected $db;
/**
* Query Builder object
*
* @var BaseBuilder
*/
protected $builder;
/**
* Name of database table
*
* @var string
*/
protected $table;
/**
* The Database connection group that
* should be instantiated.
*
* @var string
*/
protected $DBGroup;
/**
* The format that the results should be returned as.
* Will be overridden if the as* methods are used.
*
* @var string
*/
protected $returnType = 'array';
/**
* Used by asArray and asObject to provide
* temporary overrides of model default.
*
* @var string
*/
protected $tempReturnType;
/**
* Aauth Config object
*
* @var BaseConfig
*/
protected $config;
/**
* Constructor
*
* @param ConnectionInterface $db Database object
*/
public function __construct(ConnectionInterface &$db = null)
{
$this->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;
}
}

142
tests/Aauth/Database/GroupVariableModelTest.php

@ -0,0 +1,142 @@
<?php namespace Tests\Aauth\Database;
use Config\Aauth as AauthConfig;
use CodeIgniter\Test\CIDatabaseTestCase;
use App\Models\Aauth\GroupVariableModel;
class GroupVariableModelTest extends CIDatabaseTestCase
{
protected $refresh = true;
protected $basePath = TESTPATH . '../application' . 'Database/Migrations';
protected $namespace = 'App';
public function setUp()
{
parent::setUp();
$this->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());
}
}
Loading…
Cancel
Save