Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: php
php:
- "7.3"
- "7.2"
- "7.1"
- "7.0"
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"prefer-stable": true,
"require": {
"php": ">=5.6.0",
"byjg/micro-orm": "4.0.*",
"byjg/micro-orm": "4.1.*",
"byjg/cache-engine": "4.0.*",
"byjg/jwt-wrapper": "2.0.*"
},
Expand Down
20 changes: 7 additions & 13 deletions src/UsersAnyDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,23 @@ class UsersAnyDataset extends UsersBase
*/
protected $anyDataSet;

/**
* Internal Users file name
*
* @var string
*/
protected $usersFile;

/**
* AnyDataset constructor
*
* @param string $file
* @param AnyDataset $anyDataset
* @param UserDefinition $userTable
* @param UserPropertiesDefinition $propertiesTable
* @throws \ByJG\AnyDataset\Core\Exception\DatabaseException
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
* @throws \ByJG\Util\Exception\XmlUtilException
*/
public function __construct(
$file,
AnyDataset $anyDataset,
UserDefinition $userTable = null,
UserPropertiesDefinition $propertiesTable = null
) {
$this->usersFile = $file;
$this->anyDataSet = new AnyDataset($this->usersFile);
$this->anyDataSet = $anyDataset;
$this->anyDataSet->save();
$this->userTable = $userTable;
if (!$userTable->existsClosure('update', 'userid')) {
$userTable->defineClosureForUpdate('userid', function ($value, $instance) {
Expand Down Expand Up @@ -90,7 +84,7 @@ public function save(UserModel $model)
$this->anyDataSet->addField($value->getName(), $value->getValue());
}

$this->anyDataSet->save($this->usersFile);
$this->anyDataSet->save();
}

/**
Expand Down Expand Up @@ -131,7 +125,7 @@ public function removeByLoginField($login)
if ($iterator->hasNext()) {
$oldRow = $iterator->moveNext();
$this->anyDataSet->removeRow($oldRow);
$this->anyDataSet->save($this->usersFile);
$this->anyDataSet->save();
return true;
}

Expand Down
38 changes: 31 additions & 7 deletions src/UsersDBDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ByJG\Authenticate;

use ByJG\AnyDataset\Core\IteratorFilter;
use ByJG\AnyDataset\Db\DbDriverInterface;
use ByJG\AnyDataset\Db\Factory;
use ByJG\AnyDataset\Db\IteratorFilterSqlFormatter;
use ByJG\Authenticate\Definition\UserDefinition;
Expand Down Expand Up @@ -35,15 +36,16 @@ class UsersDBDataset extends UsersBase
/**
* UsersDBDataset constructor
*
* @param string $connectionString
* @param UserDefinition $userTable
* @param UserPropertiesDefinition $propertiesTable
* @param \ByJG\AnyDataset\Db\DbDriverInterface $dbDriver
* @param \ByJG\Authenticate\Definition\UserDefinition $userTable
* @param \ByJG\Authenticate\Definition\UserPropertiesDefinition $propertiesTable
*
* @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
* @throws \ByJG\MicroOrm\Exception\OrmModelInvalidException
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function __construct(
$connectionString,
DbDriverInterface $dbDriver,
UserDefinition $userTable = null,
UserPropertiesDefinition $propertiesTable = null
) {
Expand All @@ -55,7 +57,6 @@ public function __construct(
$propertiesTable = new UserPropertiesDefinition();
}

$provider = Factory::getDbRelationalInstance($connectionString);
$userMapper = new Mapper(
$userTable->model(),
$userTable->table(),
Expand All @@ -72,7 +73,7 @@ public function __construct(
$userTable->getClosureForSelect($property)
);
}
$this->userRepository = new Repository($provider, $userMapper);
$this->userRepository = new Repository($dbDriver, $userMapper);

$propertiesMapper = new Mapper(
UserPropertiesModel::class,
Expand All @@ -83,7 +84,7 @@ public function __construct(
$propertiesMapper->addFieldMap('name', $propertiesTable->getName());
$propertiesMapper->addFieldMap('value', $propertiesTable->getValue());
$propertiesMapper->addFieldMap('userid', $propertiesTable->getUserid());
$this->propertiesRepository = new Repository($provider, $propertiesMapper);
$this->propertiesRepository = new Repository($dbDriver, $propertiesMapper);

$this->userTable = $userTable;
$this->propertiesTable = $propertiesTable;
Expand Down Expand Up @@ -297,6 +298,29 @@ public function removeAllProperties($propertyName, $value = null)
return true;
}

public function getProperty($userId, $propertyName)
{
$query = Query::getInstance()
->table($this->getUserPropertiesDefinition()->table())
->where("{$this->getUserPropertiesDefinition()->getUserid()} = :id", ['id' =>$userId])
->where("{$this->getUserPropertiesDefinition()->getName()} = :name", ['name' =>$propertyName]);

$result = [];
foreach ($this->propertiesRepository->getByQuery($query) as $model) {
$result[] = $model->getValue();
}

if (count($result) === 0) {
return null;
}

if (count($result) === 1) {
return $result[0];
}

return $result;
}

/**
* Return all property's fields from this user
*
Expand Down
7 changes: 4 additions & 3 deletions src/UsersMoodleDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
define('AUTH_PASSWORD_NOT_CACHED', 'not cached'); // String used in password field when password is not stored.

use ByJG\AnyDataset\Db\DbDriverInterface;
use ByJG\Authenticate\Definition\UserDefinition;
use ByJG\Authenticate\Definition\UserPropertiesDefinition;
use ByJG\Authenticate\Exception\NotImplementedException;
Expand All @@ -25,15 +26,15 @@ class UsersMoodleDataset extends UsersDBDataset
/**
* DBDataset constructor
*
* @param string $connectionString
* @param DbDriverInterface $dbDriver
* @param string $siteSalt
* @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
* @throws \ByJG\MicroOrm\Exception\OrmModelInvalidException
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function __construct($connectionString, $siteSalt = "")
public function __construct(DbDriverInterface $dbDriver, $siteSalt = "")
{
parent::__construct($connectionString);
parent::__construct($dbDriver);

$this->siteSalt = $siteSalt;
}
Expand Down
4 changes: 3 additions & 1 deletion tests/UsersAnyDataset2ByUsernameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ByJG\Authenticate;

use ByJG\AnyDataset\Core\AnyDataset;
use ByJG\Authenticate\Definition\UserDefinition;
use ByJG\Authenticate\Definition\UserPropertiesDefinition;
use ByJG\Authenticate\Model\UserModel;
Expand Down Expand Up @@ -38,8 +39,9 @@ public function __setUp($loginField)
'theiruserid'
);

$anydataset = new AnyDataset('php://memory');
$this->object = new UsersAnyDataset(
'php://memory',
$anydataset,
$this->userDefinition,
$this->propertyDefinition
);
Expand Down
8 changes: 7 additions & 1 deletion tests/UsersAnyDatasetByUsernameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ByJG\Authenticate;

use ByJG\AnyDataset\Core\AnyDataset;
use ByJG\Authenticate\Definition\UserDefinition;
use ByJG\Authenticate\Definition\UserPropertiesDefinition;
use ByJG\Authenticate\Model\UserModel;
Expand Down Expand Up @@ -36,8 +37,9 @@ public function __setUp($loginField)
$this->userDefinition = new UserDefinition('users', UserModel::class, $loginField);
$this->propertyDefinition = new UserPropertiesDefinition();

$anydataSet = new AnyDataset('php://memory');
$this->object = new UsersAnyDataset(
'php://memory',
$anydataSet,
$this->userDefinition,
$this->propertyDefinition
);
Expand Down Expand Up @@ -83,6 +85,10 @@ public function testAddUserError()

public function testAddProperty()
{
// Check state
$user = $this->object->getById($this->prefix . '2');
$this->assertEmpty($user->get('city'));

// Add one property
$this->object->addProperty($this->prefix . '2', 'city', 'Rio de Janeiro');
$user = $this->object->getById($this->prefix . '2');
Expand Down
10 changes: 6 additions & 4 deletions tests/UsersDBDataset2ByUserNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

class UsersDBDataset2ByUserNameTest extends UsersDBDatasetByUsernameTest
{
protected $db;

public function __setUp($loginField)
{
$this->prefix = "";

$db = Factory::getDbRelationalInstance(self::CONNECTION_STRING);
$db->execute('create table mytable (
$this->db = Factory::getDbRelationalInstance(self::CONNECTION_STRING);
$this->db->execute('create table mytable (
myuserid integer primary key autoincrement,
myname varchar(45),
myemail varchar(200),
Expand All @@ -26,7 +28,7 @@ public function __setUp($loginField)
myadmin char(1));'
);

$db->execute('create table theirproperty (
$this->db->execute('create table theirproperty (
theirid integer primary key autoincrement,
theiruserid integer,
theirname varchar(45),
Expand All @@ -51,7 +53,7 @@ public function __setUp($loginField)
$this->propertyDefinition = new UserPropertiesDefinition('theirproperty', 'theirid', 'theirname', 'theirvalue', 'theiruserid');

$this->object = new UsersDBDataset(
self::CONNECTION_STRING,
$this->db,
$this->userDefinition,
$this->propertyDefinition
);
Expand Down
12 changes: 7 additions & 5 deletions tests/UsersDBDatasetByUsernameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ class UsersDBDatasetByUsernameTest extends UsersAnyDatasetByUsernameTest

const CONNECTION_STRING='sqlite:///tmp/teste.db';

protected $db;

public function __setUp($loginField)
{
$this->prefix = "";

$db = Factory::getDbRelationalInstance(self::CONNECTION_STRING);
$db->execute('create table users (
$this->db = Factory::getDbRelationalInstance(self::CONNECTION_STRING);
$this->db->execute('create table users (
userid integer primary key autoincrement,
name varchar(45),
email varchar(200),
Expand All @@ -30,7 +32,7 @@ public function __setUp($loginField)
admin char(1));'
);

$db->execute('create table users_property (
$this->db->execute('create table users_property (
id integer primary key autoincrement,
userid integer,
name varchar(45),
Expand All @@ -40,7 +42,7 @@ public function __setUp($loginField)
$this->userDefinition = new UserDefinition('users', UserModel::class, $loginField);
$this->propertyDefinition = new UserPropertiesDefinition();
$this->object = new UsersDBDataset(
self::CONNECTION_STRING,
$this->db,
$this->userDefinition,
$this->propertyDefinition
);
Expand Down Expand Up @@ -126,7 +128,7 @@ public function testWithUpdateValue()

// Test it!
$newObject = new UsersDBDataset(
self::CONNECTION_STRING,
$this->db,
$this->userDefinition,
$this->propertyDefinition
);
Expand Down
12 changes: 7 additions & 5 deletions tests/UsersDBDatasetDefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public function setOtherfield($otherfield)

class UsersDBDatasetDefinitionTest extends UsersDBDatasetByUsernameTest
{
protected $db;

/**
* @param $loginField
* @throws \ByJG\AnyDataset\Exception\NotFoundException
Expand All @@ -43,8 +45,8 @@ public function __setUp($loginField)
{
$this->prefix = "";

$db = Factory::getDbRelationalInstance(self::CONNECTION_STRING);
$db->execute('create table mytable (
$this->db = Factory::getDbRelationalInstance(self::CONNECTION_STRING);
$this->db->execute('create table mytable (
myuserid integer primary key autoincrement,
myname varchar(45),
myemail varchar(200),
Expand All @@ -55,7 +57,7 @@ public function __setUp($loginField)
myadmin char(1));'
);

$db->execute('create table theirproperty (
$this->db->execute('create table theirproperty (
theirid integer primary key autoincrement,
theiruserid integer,
theirname varchar(45),
Expand All @@ -81,7 +83,7 @@ public function __setUp($loginField)
$this->propertyDefinition = new UserPropertiesDefinition('theirproperty', 'theirid', 'theirname', 'theirvalue', 'theiruserid');

$this->object = new UsersDBDataset(
self::CONNECTION_STRING,
$this->db,
$this->userDefinition,
$this->propertyDefinition
);
Expand Down Expand Up @@ -180,7 +182,7 @@ public function testWithUpdateValue()

// Test it!
$newObject = new UsersDBDataset(
self::CONNECTION_STRING,
$this->db,
$this->userDefinition,
$this->propertyDefinition
);
Expand Down