diff --git a/lib/Db/EntityJSONSerializer.php b/lib/Db/EntityJSONSerializer.php new file mode 100644 index 00000000..1695e215 --- /dev/null +++ b/lib/Db/EntityJSONSerializer.php @@ -0,0 +1,20 @@ + + * @copyright Sander Brand 2016 + */ +namespace OCA\Passman\Db; +trait EntityJSONSerializer { + public function serializeFields($properties) { + $result = []; + foreach($properties as $property) { + $result[$property] = $this->$property; + } + return $result; + } +} \ No newline at end of file diff --git a/lib/Db/Vault.php b/lib/Db/Vault.php index c35b5901..d7e02636 100644 --- a/lib/Db/Vault.php +++ b/lib/Db/Vault.php @@ -28,7 +28,9 @@ use \OCP\AppFramework\Db\Entity; */ -class Vault extends Entity { +class Vault extends Entity implements \JsonSerializable{ + + use EntityJSONSerializer; protected $guid; protected $name; @@ -41,4 +43,27 @@ class Vault extends Entity { $this->addType('created', 'integer'); $this->addType('lastAccess', 'integer'); } + + public static function fromRow($row){ + $vault = new Vault(); + $vault->setId($row['id']); + $vault->setGuid($row['guid']); + $vault->setName($row['name']); + $vault->setCreated($row['created']); + $vault->setlastAccess($row['last_access']); + return $vault; + } + + /** + * Turns entitie attributes into an array + */ + public function jsonSerialize() { + return [ + 'id' => $this->getId(), + 'guid' => $this->getGuid(), + 'name' => $this->getName(), + 'created' => $this->getCreated(), + 'last_access' => $this->getlastAccess(), + ]; + } } \ No newline at end of file diff --git a/lib/Db/VaultMapper.php b/lib/Db/VaultMapper.php index 27d31253..ffbb6675 100644 --- a/lib/Db/VaultMapper.php +++ b/lib/Db/VaultMapper.php @@ -11,10 +11,9 @@ namespace OCA\Passman\Db; use OCA\Passman\Utility\Utils; - use OCP\IDBConnection; use OCP\AppFramework\Db\Mapper; -//use OCA\Passman\Db\Vault; + class VaultMapper extends Mapper { private $utils; public function __construct(IDBConnection $db, Utils $utils) { @@ -34,7 +33,7 @@ class VaultMapper extends Mapper { } public function findVaultsFromUser($userId){ - $sql = 'SELECT * FROM `*PREFIX*passman_vaults` ' . + $sql = 'SELECT id, name, created, guid, last_access FROM `*PREFIX*passman_vaults` ' . 'WHERE `user_id` = ? '; $params = [$userId]; return $this->findEntities($sql, $params);