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\IDBConnection; |
28
|
|
|
use OCP\AppFramework\Db\Mapper; |
29
|
|
|
|
30
|
|
|
class CredentialMapper extends Mapper { |
31
|
|
|
private $utils; |
32
|
|
|
|
33
|
|
|
public function __construct(IDBConnection $db, Utils $utils) { |
34
|
|
|
parent::__construct($db, 'passman_credentials'); |
35
|
|
|
$this->utils = $utils; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Obtains the credentials by vault id (not guid) |
41
|
|
|
* |
42
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found |
43
|
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result |
44
|
|
|
* @return Credential[] |
45
|
|
|
*/ |
46
|
|
|
public function getCredentialsByVaultId($vault_id, $user_id) { |
47
|
|
|
$sql = 'SELECT * FROM `*PREFIX*passman_credentials` ' . |
48
|
|
|
'WHERE `user_id` = ? and vault_id = ?'; |
49
|
|
|
return $this->findEntities($sql, [$user_id, $vault_id]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get a random credentail from a vault |
54
|
|
|
* |
55
|
|
|
* @param $vault_id |
56
|
|
|
* @param $user_id |
57
|
|
|
* @return Credential |
58
|
|
|
*/ |
59
|
|
|
public function getRandomCredentialByVaultId($vault_id, $user_id) { |
60
|
|
|
$sql = 'SELECT * FROM `*PREFIX*passman_credentials` ' . |
61
|
|
|
'WHERE `user_id` = ? and vault_id = ? AND shared_key is NULL LIMIT 20'; |
62
|
|
|
$entities = $this->findEntities($sql, [$user_id, $vault_id]); |
63
|
|
|
$count = count($entities) - 1; |
64
|
|
|
$entities = array_splice($entities, rand(0, $count), 1); |
65
|
|
|
return $entities; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get expired credentials |
70
|
|
|
* |
71
|
|
|
* @param $timestamp |
72
|
|
|
* @return Credential[] |
73
|
|
|
*/ |
74
|
|
|
public function getExpiredCredentials($timestamp) { |
75
|
|
|
$sql = 'SELECT * FROM `*PREFIX*passman_credentials` ' . |
76
|
|
|
'WHERE `expire_time` > 0 AND `expire_time` < ?'; |
77
|
|
|
return $this->findEntities($sql, [$timestamp]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get an credential by id. |
82
|
|
|
* Optional user id |
83
|
|
|
* |
84
|
|
|
* @param $credential_id |
85
|
|
|
* @param null $user_id |
86
|
|
|
* @return Credential |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public function getCredentialById($credential_id, $user_id = null) { |
|
|
|
|
89
|
|
|
$sql = 'SELECT * FROM `*PREFIX*passman_credentials` ' . |
90
|
|
|
'WHERE `id` = ?'; |
91
|
|
|
// If we want to check the owner, add it to the query |
92
|
|
|
$params = [$credential_id]; |
93
|
|
|
if ($user_id !== null) { |
94
|
|
|
$sql .= ' and `user_id` = ? '; |
95
|
|
|
array_push($params, $user_id); |
96
|
|
|
} |
97
|
|
|
return $this->findEntity($sql, $params); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get credential label by id |
102
|
|
|
* |
103
|
|
|
* @param $credential_id |
104
|
|
|
* @return Credential |
105
|
|
|
*/ |
106
|
|
|
public function getCredentialLabelById($credential_id) { |
107
|
|
|
$sql = 'SELECT id, label FROM `*PREFIX*passman_credentials` ' . |
108
|
|
|
'WHERE `id` = ? '; |
109
|
|
|
return $this->findEntity($sql, [$credential_id]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Save credential to the database. |
114
|
|
|
* |
115
|
|
|
* @param $raw_credential |
116
|
|
|
* @return Credential |
117
|
|
|
*/ |
118
|
|
|
public function create($raw_credential) { |
119
|
|
|
$credential = new Credential(); |
120
|
|
|
|
121
|
|
|
$credential->setGuid($this->utils->GUID()); |
122
|
|
|
$credential->setVaultId($raw_credential['vault_id']); |
123
|
|
|
$credential->setUserId($raw_credential['user_id']); |
124
|
|
|
$credential->setLabel($raw_credential['label']); |
125
|
|
|
$credential->setDescription($raw_credential['description']); |
126
|
|
|
$credential->setCreated($this->utils->getTime()); |
127
|
|
|
$credential->setChanged($this->utils->getTime()); |
128
|
|
|
$credential->setTags($raw_credential['tags']); |
129
|
|
|
$credential->setEmail($raw_credential['email']); |
130
|
|
|
$credential->setUsername($raw_credential['username']); |
131
|
|
|
$credential->setPassword($raw_credential['password']); |
132
|
|
|
$credential->setUrl($raw_credential['url']); |
133
|
|
|
$credential->setIcon($raw_credential['favicon']); |
134
|
|
|
$credential->setRenewInterval($raw_credential['renew_interval']); |
135
|
|
|
$credential->setExpireTime($raw_credential['expire_time']); |
136
|
|
|
$credential->setDeleteTime($raw_credential['delete_time']); |
137
|
|
|
$credential->setFiles($raw_credential['files']); |
138
|
|
|
$credential->setCustomFields($raw_credential['custom_fields']); |
139
|
|
|
$credential->setOtp($raw_credential['otp']); |
140
|
|
|
$credential->setHidden($raw_credential['hidden']); |
141
|
|
|
if (isset($raw_credential['shared_key'])) { |
142
|
|
|
$credential->setSharedKey($raw_credential['shared_key']); |
143
|
|
|
} |
144
|
|
|
return parent::insert($credential); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Update a credential |
149
|
|
|
* |
150
|
|
|
* @param $raw_credential array An array containing all the credential fields |
151
|
|
|
* @param $useRawUser bool |
152
|
|
|
* @return Credential The updated credential |
153
|
|
|
*/ |
154
|
|
|
public function updateCredential($raw_credential, $useRawUser) { |
155
|
|
|
$original = $this->getCredentialByGUID($raw_credential['guid']); |
156
|
|
|
$uid = ($useRawUser) ? $raw_credential['user_id'] : $original->getUserId(); |
157
|
|
|
|
158
|
|
|
$credential = new Credential(); |
159
|
|
|
$credential->setId($original->getId()); |
160
|
|
|
$credential->setGuid($original->getGuid()); |
161
|
|
|
$credential->setVaultId($original->getVaultId()); |
162
|
|
|
$credential->setUserId($uid); |
163
|
|
|
$credential->setLabel($raw_credential['label']); |
164
|
|
|
$credential->setDescription($raw_credential['description']); |
165
|
|
|
$credential->setCreated($original->getCreated()); |
166
|
|
|
$credential->setChanged($this->utils->getTime()); |
167
|
|
|
$credential->setTags($raw_credential['tags']); |
168
|
|
|
$credential->setEmail($raw_credential['email']); |
169
|
|
|
$credential->setUsername($raw_credential['username']); |
170
|
|
|
$credential->setPassword($raw_credential['password']); |
171
|
|
|
$credential->setUrl($raw_credential['url']); |
172
|
|
|
$credential->setIcon($raw_credential['icon']); |
173
|
|
|
$credential->setRenewInterval($raw_credential['renew_interval']); |
174
|
|
|
$credential->setExpireTime($raw_credential['expire_time']); |
175
|
|
|
$credential->setFiles($raw_credential['files']); |
176
|
|
|
$credential->setCustomFields($raw_credential['custom_fields']); |
177
|
|
|
$credential->setOtp($raw_credential['otp']); |
178
|
|
|
$credential->setHidden($raw_credential['hidden']); |
179
|
|
|
$credential->setDeleteTime($raw_credential['delete_time']); |
180
|
|
|
|
181
|
|
|
if (isset($raw_credential['shared_key'])) { |
182
|
|
|
$credential->setSharedKey($raw_credential['shared_key']); |
183
|
|
|
} |
184
|
|
|
return parent::update($credential); |
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function deleteCredential(Credential $credential) { |
188
|
|
|
return $this->delete($credential); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function upd(Credential $credential) { |
192
|
|
|
$this->update($credential); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Finds a credential by the given guid |
197
|
|
|
* |
198
|
|
|
* @param $credential_guid |
199
|
|
|
* @return Credential |
200
|
|
|
*/ |
201
|
|
View Code Duplication |
public function getCredentialByGUID($credential_guid, $user_id = null) { |
|
|
|
|
202
|
|
|
$q = 'SELECT * FROM `*PREFIX*passman_credentials` WHERE guid = ? '; |
203
|
|
|
$params = [$credential_guid]; |
204
|
|
|
if ($user_id !== null) { |
205
|
|
|
$q .= ' and `user_id` = ? '; |
206
|
|
|
array_push($params, $user_id); |
207
|
|
|
} |
208
|
|
|
return $this->findEntity($q, $params); |
209
|
|
|
} |
210
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.