nextcloud /
passman
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Nextcloud - passman |
||
| 4 | * |
||
| 5 | * @copyright Copyright (c) 2016, Sander Brand ([email protected]) |
||
| 6 | * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected]) |
||
| 7 | * @license GNU AGPL version 3 or any later version |
||
| 8 | * |
||
| 9 | * This program is free software: you can redistribute it and/or modify |
||
| 10 | * it under the terms of the GNU Affero General Public License as |
||
| 11 | * published by the Free Software Foundation, either version 3 of the |
||
| 12 | * License, or (at your option) any later version. |
||
| 13 | * |
||
| 14 | * This program is distributed in the hope that it will be useful, |
||
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 17 | * GNU Affero General Public License for more details. |
||
| 18 | * |
||
| 19 | * You should have received a copy of the GNU Affero General Public License |
||
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
| 21 | * |
||
| 22 | */ |
||
| 23 | |||
| 24 | namespace OCA\Passman\Db; |
||
| 25 | |||
| 26 | use OCA\Passman\Utility\Utils; |
||
| 27 | use OCP\AppFramework\Db\DoesNotExistException; |
||
| 28 | use OCP\AppFramework\Db\Entity; |
||
| 29 | use OCP\AppFramework\Db\MultipleObjectsReturnedException; |
||
| 30 | use OCP\AppFramework\Db\QBMapper; |
||
| 31 | use OCP\DB\QueryBuilder\IQueryBuilder; |
||
| 32 | use OCP\IDBConnection; |
||
| 33 | |||
| 34 | class VaultMapper extends QBMapper { |
||
| 35 | const TABLE_NAME = 'passman_vaults'; |
||
| 36 | private Utils $utils; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 37 | |||
| 38 | public function __construct(IDBConnection $db, Utils $utils) { |
||
| 39 | parent::__construct($db, self::TABLE_NAME); |
||
| 40 | $this->utils = $utils; |
||
| 41 | } |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * @param int $vault_id |
||
| 46 | * @param string $user_id |
||
| 47 | * @return Entity[] |
||
| 48 | */ |
||
| 49 | public function find(int $vault_id, string $user_id) { |
||
| 50 | $qb = $this->db->getQueryBuilder(); |
||
| 51 | $qb->select('*') |
||
| 52 | ->from(self::TABLE_NAME) |
||
| 53 | ->where($qb->expr()->eq('id', $qb->createNamedParameter($vault_id, IQueryBuilder::PARAM_INT))) |
||
| 54 | ->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR))); |
||
| 55 | |||
| 56 | return $this->findEntities($qb); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param string $vault_guid |
||
| 61 | * @param string $user_id |
||
| 62 | * @return Entity |
||
| 63 | * @throws DoesNotExistException |
||
| 64 | * @throws MultipleObjectsReturnedException |
||
| 65 | */ |
||
| 66 | public function findByGuid(string $vault_guid, string $user_id) { |
||
| 67 | $qb = $this->db->getQueryBuilder(); |
||
| 68 | $qb->select('*') |
||
| 69 | ->from(self::TABLE_NAME) |
||
| 70 | ->where($qb->expr()->eq('guid', $qb->createNamedParameter($vault_guid, IQueryBuilder::PARAM_STR))) |
||
| 71 | ->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR))); |
||
| 72 | |||
| 73 | return $this->findEntity($qb); |
||
| 74 | } |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * @param string $user_id |
||
| 79 | * @return Entity[] |
||
| 80 | */ |
||
| 81 | public function findVaultsFromUser(string $user_id) { |
||
| 82 | $qb = $this->db->getQueryBuilder(); |
||
| 83 | $qb->select('*') |
||
| 84 | ->from(self::TABLE_NAME) |
||
| 85 | ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR))); |
||
| 86 | |||
| 87 | return $this->findEntities($qb); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Creates a vault |
||
| 92 | * |
||
| 93 | * @param string $vault_name |
||
| 94 | * @param string $user_id |
||
| 95 | * @return Vault|Entity |
||
| 96 | */ |
||
| 97 | public function create(string $vault_name, string $user_id) { |
||
| 98 | $vault = new Vault(); |
||
| 99 | $vault->setName($vault_name); |
||
| 100 | $vault->setUserId($user_id); |
||
| 101 | $vault->setGuid($this->utils->GUID()); |
||
| 102 | $vault->setCreated($this->utils->getTime()); |
||
| 103 | $vault->setLastAccess(0); |
||
| 104 | return parent::insert($vault); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Update last access time of a vault |
||
| 109 | * |
||
| 110 | * @param int $vault_id |
||
| 111 | * @param string $user_id |
||
| 112 | * @return Vault|Entity |
||
| 113 | */ |
||
| 114 | public function setLastAccess(int $vault_id, string $user_id) { |
||
| 115 | $vault = new Vault(); |
||
| 116 | $vault->setId($vault_id); |
||
| 117 | $vault->setUserId($user_id); |
||
| 118 | $vault->setLastAccess(Utils::getTime()); |
||
| 119 | return $this->update($vault); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Update vault |
||
| 124 | * |
||
| 125 | * @param Vault $vault |
||
| 126 | * @return Vault|Entity |
||
| 127 | */ |
||
| 128 | public function updateVault(Vault $vault) { |
||
| 129 | return $this->update($vault); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Update the sharing key's |
||
| 134 | * |
||
| 135 | * @param int $vault_id |
||
| 136 | * @param string $privateKey |
||
| 137 | * @param string $publicKey |
||
| 138 | * @return Vault|Entity |
||
| 139 | */ |
||
| 140 | public function updateSharingKeys(int $vault_id, string $privateKey, string $publicKey) { |
||
| 141 | $vault = new Vault(); |
||
| 142 | $vault->setId($vault_id); |
||
| 143 | $vault->setPrivateSharingKey($privateKey); |
||
| 144 | $vault->setPublicSharingKey($publicKey); |
||
| 145 | $vault->setSharingKeysGenerated($this->utils->getTime()); |
||
| 146 | return $this->update($vault); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Delete a vault |
||
| 151 | * |
||
| 152 | * @param Vault $vault |
||
| 153 | */ |
||
| 154 | public function deleteVault(Vault $vault) { |
||
| 155 | $this->delete($vault); |
||
| 156 | } |
||
| 157 | } |
||
| 158 |