Failed Conditions
Pull Request — master (#682)
by
unknown
09:41
created

lib/Db/CredentialMapper.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

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 CredentialMapper extends QBMapper {
35
	const TABLE_NAME = 'passman_credentials';
36
	private Utils $utils;
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
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
	 * Obtains the credentials by vault id (not guid)
46
	 *
47
	 * @param string $vault_id
48
	 * @param string $user_id
49
	 * @return Credential[]
50
	 */
51
	public function getCredentialsByVaultId(string $vault_id, string $user_id) {
52
		$qb = $this->db->getQueryBuilder();
53
		$qb->select('*')
54
			->from(self::TABLE_NAME)
55
			->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR)))
56
			->andWhere($qb->expr()->eq('vault_id', $qb->createNamedParameter($vault_id, IQueryBuilder::PARAM_STR)));
57
58
		/** @var Credential[] $credentials */
59
		$credentials = $this->findEntities($qb);
60
		return $credentials;
61
	}
62
63
	/**
64
	 * Get a random credential from a vault
65
	 *
66
	 * @param string $vault_id
67
	 * @param string $user_id
68
	 * @return Credential[]
69
	 */
70
	public function getRandomCredentialByVaultId(string $vault_id, string $user_id) {
71
		$qb = $this->db->getQueryBuilder();
72
		$qb->select('*')
73
			->from(self::TABLE_NAME)
74
			->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR)))
75
			->andWhere($qb->expr()->eq('vault_id', $qb->createNamedParameter($vault_id, IQueryBuilder::PARAM_STR)))
76
			->andWhere($qb->expr()->isNull('shared_key'))
77
			->setMaxResults(20);
78
79
		$entities = $this->findEntities($qb);
80
		$count = count($entities) - 1;
81
82
		/** @var Credential[] $entity */
83
		$entity = array_splice($entities, rand(0, $count), 1);
84
		return $entity;
85
	}
86
87
	/**
88
	 * Get expired credentials
89
	 *
90
	 * @param int $timestamp
91
	 * @return Credential[]
92
	 */
93
	public function getExpiredCredentials(int $timestamp) {
94
		$qb = $this->db->getQueryBuilder();
95
		$qb->select('*')
96
			->from(self::TABLE_NAME)
97
			->where($qb->expr()->gt('expire_time',  $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
98
			->andWhere($qb->expr()->lt('expire_time', $qb->createNamedParameter($timestamp, IQueryBuilder::PARAM_INT)));
99
100
		/** @var Credential[] $credentials */
101
		$credentials = $this->findEntities($qb);
102
		return $credentials;
103
	}
104
105
	/**
106
	 * Get an credential by id.
107
	 * Optional user id
108
	 *
109
	 * @param int $credential_id
110
	 * @param string|null $user_id
111
	 * @return Credential
112
	 * @throws DoesNotExistException
113
	 * @throws MultipleObjectsReturnedException
114
	 */
115
	public function getCredentialById(int $credential_id, string $user_id = null) {
116
		$qb = $this->db->getQueryBuilder();
117
		$qb->select('*')
118
			->from(self::TABLE_NAME)
119
			->where($qb->expr()->eq('id', $qb->createNamedParameter($credential_id, IQueryBuilder::PARAM_INT)));
120
121
		if ($user_id !== null) {
122
			$qb->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR)));
123
		}
124
125
		/** @var Credential $credential */
126
		$credential = $this->findEntity($qb);
127
		return $credential;
128
	}
129
130
	/**
131
	 * Get credential label by id
132
	 *
133
	 * @param int $credential_id
134
	 * @return Credential
135
	 * @throws DoesNotExistException
136
	 * @throws MultipleObjectsReturnedException
137
	 */
138
	public function getCredentialLabelById(int $credential_id) {
139
		$qb = $this->db->getQueryBuilder();
140
		$qb->select(['id', 'label'])
141
			->from(self::TABLE_NAME)
142
			->where($qb->expr()->eq('id', $qb->createNamedParameter($credential_id, IQueryBuilder::PARAM_INT)));
143
144
		/** @var Credential $credential */
145
		$credential = $this->findEntity($qb);
146
		return $credential;
147
	}
148
149
	/**
150
	 * Save credential to the database.
151
	 *
152
	 * @param $raw_credential
153
	 * @return Credential
154
	 */
155
	public function create($raw_credential) {
156
		$credential = new Credential();
157
158
		$credential->setGuid($this->utils->GUID());
159
		$credential->setVaultId($raw_credential['vault_id']);
160
		$credential->setUserId($raw_credential['user_id']);
161
		$credential->setLabel($raw_credential['label']);
162
		$credential->setDescription($raw_credential['description']);
163
		$credential->setCreated($this->utils->getTime());
164
		$credential->setChanged($this->utils->getTime());
165
		$credential->setTags($raw_credential['tags']);
166
		$credential->setEmail($raw_credential['email']);
167
		$credential->setUsername($raw_credential['username']);
168
		$credential->setPassword($raw_credential['password']);
169
		$credential->setUrl($raw_credential['url']);
170
		$credential->setIcon($raw_credential['icon']);
171
		$credential->setRenewInterval($raw_credential['renew_interval']);
172
		$credential->setExpireTime($raw_credential['expire_time']);
173
		$credential->setDeleteTime($raw_credential['delete_time']);
174
		$credential->setFiles($raw_credential['files']);
175
		$credential->setCustomFields($raw_credential['custom_fields']);
176
		$credential->setOtp($raw_credential['otp']);
177
		$credential->setHidden($raw_credential['hidden']);
178
		$credential->setCompromised($raw_credential['compromised']);
179
		if (isset($raw_credential['shared_key'])) {
180
			$credential->setSharedKey($raw_credential['shared_key']);
181
		}
182
		return parent::insert($credential);
183
	}
184
185
	/**
186
	 * @param $raw_credential array An array containing all the credential fields
187
	 * @param $useRawUser bool
188
	 * @return Credential|Entity The updated credential
189
	 * @throws DoesNotExistException
190
	 * @throws MultipleObjectsReturnedException
191
	 */
192
	public function updateCredential($raw_credential, bool $useRawUser) {
193
		$original = $this->getCredentialByGUID($raw_credential['guid']);
194
		$uid = ($useRawUser) ? $raw_credential['user_id'] : $original->getUserId();
195
196
		$credential = new Credential();
197
		$credential->setId($original->getId());
198
		$credential->setGuid($original->getGuid());
199
		$credential->setVaultId($original->getVaultId());
200
		$credential->setUserId($uid);
201
		$credential->setLabel($raw_credential['label']);
202
		$credential->setDescription($raw_credential['description']);
203
		$credential->setCreated($original->getCreated());
204
		$credential->setChanged($this->utils->getTime());
205
		$credential->setTags($raw_credential['tags']);
206
		$credential->setEmail($raw_credential['email']);
207
		$credential->setUsername($raw_credential['username']);
208
		$credential->setPassword($raw_credential['password']);
209
		$credential->setUrl($raw_credential['url']);
210
		$credential->setIcon($raw_credential['icon']);
211
		$credential->setRenewInterval($raw_credential['renew_interval']);
212
		$credential->setExpireTime($raw_credential['expire_time']);
213
		$credential->setFiles($raw_credential['files']);
214
		$credential->setCustomFields($raw_credential['custom_fields']);
215
		$credential->setOtp($raw_credential['otp']);
216
		$credential->setHidden($raw_credential['hidden']);
217
		$credential->setDeleteTime($raw_credential['delete_time']);
218
		$credential->setCompromised($raw_credential['compromised']);
219
220
		if (isset($raw_credential['shared_key'])) {
221
			$credential->setSharedKey($raw_credential['shared_key']);
222
		}
223
		return parent::update($credential);
224
	}
225
226
	public function deleteCredential(Credential $credential) {
227
		return $this->delete($credential);
228
	}
229
230
	public function upd(Credential $credential) {
231
		$this->update($credential);
232
	}
233
234
	/**
235
	 * Finds a credential by the given guid
236
	 *
237
	 * @param string $credential_guid
238
	 * @param string|null $user_id
239
	 * @return Credential
240
	 * @throws DoesNotExistException
241
	 * @throws MultipleObjectsReturnedException
242
	 */
243
	public function getCredentialByGUID(string $credential_guid, string $user_id = null) {
244
		$qb = $this->db->getQueryBuilder();
245
		$qb->select('*')
246
			->from(self::TABLE_NAME)
247
			->where($qb->expr()->eq('guid', $qb->createNamedParameter($credential_guid, IQueryBuilder::PARAM_STR)));
248
249
		if ($user_id !== null) {
250
			$qb->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR)));
251
		}
252
253
		/** @var Credential $credential */
254
		$credential = $this->findEntity($qb);
255
		return $credential;
256
	}
257
}
258