2016-09-11 18:33:09 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Nextcloud - passman
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later. See the COPYING file.
|
|
|
|
*
|
|
|
|
* @author Sander Brand <brantje@gmail.com>
|
|
|
|
* @copyright Sander Brand 2016
|
|
|
|
*/
|
|
|
|
namespace OCA\Passman\Db;
|
|
|
|
|
|
|
|
use OCA\Passman\Utility\Utils;
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\AppFramework\Db\Mapper;
|
2016-09-11 19:07:39 +08:00
|
|
|
|
2016-09-11 18:33:09 +08:00
|
|
|
class VaultMapper extends Mapper {
|
|
|
|
private $utils;
|
|
|
|
public function __construct(IDBConnection $db, Utils $utils) {
|
|
|
|
parent::__construct($db, 'passman_vaults');
|
|
|
|
$this->utils = $utils;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
|
|
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
|
2016-10-07 18:58:41 +08:00
|
|
|
* @return Vault
|
2016-09-11 18:33:09 +08:00
|
|
|
*/
|
2016-09-29 02:26:59 +08:00
|
|
|
public function find($vault_id, $user_id) {
|
2016-09-11 18:33:09 +08:00
|
|
|
$sql = 'SELECT * FROM `*PREFIX*passman_vaults` ' .
|
2016-09-29 02:26:59 +08:00
|
|
|
'WHERE `id`= ? and `user_id` = ?';
|
|
|
|
return $this->findEntities($sql, [$vault_id, $user_id]);
|
2016-09-11 18:33:09 +08:00
|
|
|
}
|
2016-10-07 18:58:41 +08:00
|
|
|
/**
|
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
|
|
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
|
|
|
|
* @return Vault
|
|
|
|
*/
|
|
|
|
public function findByGuid($vault_guid, $user_id) {
|
|
|
|
$sql = 'SELECT * FROM `*PREFIX*passman_vaults` ' .
|
|
|
|
'WHERE `guid`= ? and `user_id` = ?';
|
|
|
|
return $this->findEntity($sql, [$vault_guid, $user_id]);
|
|
|
|
}
|
2016-09-11 18:33:09 +08:00
|
|
|
|
2016-09-29 02:26:59 +08:00
|
|
|
|
2016-10-07 18:58:41 +08:00
|
|
|
/**
|
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
|
|
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
|
2016-10-08 05:57:42 +08:00
|
|
|
* @return Vault[]
|
2016-10-07 18:58:41 +08:00
|
|
|
*/
|
2016-09-11 18:33:09 +08:00
|
|
|
public function findVaultsFromUser($userId){
|
2016-09-26 03:40:18 +08:00
|
|
|
$sql = 'SELECT * FROM `*PREFIX*passman_vaults` ' .
|
2016-09-11 18:33:09 +08:00
|
|
|
'WHERE `user_id` = ? ';
|
|
|
|
$params = [$userId];
|
|
|
|
return $this->findEntities($sql, $params);
|
|
|
|
}
|
|
|
|
|
2016-10-11 07:41:32 +08:00
|
|
|
/**
|
|
|
|
* Creates a vault
|
|
|
|
* @param $vault_name
|
|
|
|
* @param $userId
|
|
|
|
* @return Vault
|
|
|
|
*/
|
2016-09-11 18:33:09 +08:00
|
|
|
public function create($vault_name, $userId){
|
|
|
|
$vault = new Vault();
|
|
|
|
$vault->setName($vault_name);
|
|
|
|
$vault->setUserId($userId);
|
|
|
|
$vault->setGuid($this->utils->GUID());
|
|
|
|
$vault->setCreated($this->utils->getTime());
|
2016-09-29 02:26:59 +08:00
|
|
|
$vault->setLastAccess(0);
|
2016-09-11 18:33:09 +08:00
|
|
|
return parent::insert($vault);
|
|
|
|
}
|
|
|
|
|
2016-09-29 02:26:59 +08:00
|
|
|
public function setLastAccess($vault_id, $user_id){
|
2016-09-13 00:25:16 +08:00
|
|
|
$vault = new Vault();
|
|
|
|
$vault->setId($vault_id);
|
2016-09-29 02:26:59 +08:00
|
|
|
$vault->setUserId($user_id);
|
2016-10-11 07:41:32 +08:00
|
|
|
$vault->setLastAccess(Utils::getTime());
|
2016-09-13 00:25:16 +08:00
|
|
|
$this->update($vault);
|
|
|
|
}
|
2016-09-26 00:34:33 +08:00
|
|
|
|
2016-09-29 04:05:09 +08:00
|
|
|
public function updateVault(Vault $vault){
|
|
|
|
$this->update($vault);
|
|
|
|
}
|
|
|
|
|
2016-09-26 00:34:33 +08:00
|
|
|
public function updateSharingKeys($vault_id, $privateKey, $publicKey){
|
|
|
|
$vault = new Vault();
|
|
|
|
$vault->setId($vault_id);
|
|
|
|
$vault->setPrivateSharingKey($privateKey);
|
|
|
|
$vault->setPublicSharingKey($publicKey);
|
|
|
|
$vault->setSharingKeysGenerated($this->utils->getTime());
|
|
|
|
$this->update($vault);
|
|
|
|
}
|
2016-09-11 18:33:09 +08:00
|
|
|
}
|