|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Nextcloud - passman |
|
4
|
|
|
* |
|
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
6
|
|
|
* later. See the COPYING file. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Sander Brand <[email protected]> |
|
9
|
|
|
* @copyright Sander Brand 2016 |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace OCA\Passman\Db; |
|
12
|
|
|
|
|
13
|
|
|
use OCA\Passman\Utility\Utils; |
|
14
|
|
|
use OCP\IDBConnection; |
|
15
|
|
|
use OCP\AppFramework\Db\Mapper; |
|
16
|
|
|
|
|
17
|
|
|
class CredentialMapper extends Mapper { |
|
18
|
|
|
private $utils; |
|
19
|
1 |
|
public function __construct(IDBConnection $db, Utils $utils) { |
|
20
|
1 |
|
parent::__construct($db, 'passman_credentials'); |
|
21
|
1 |
|
$this->utils = $utils; |
|
22
|
1 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found |
|
27
|
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result |
|
28
|
|
|
*/ |
|
29
|
1 |
|
public function getCredentialsByVaultId($vault_id, $user_id) { |
|
30
|
|
|
$sql = 'SELECT * FROM `*PREFIX*passman_credentials` ' . |
|
31
|
1 |
|
'WHERE `user_id` = ? and vault_id = ?'; |
|
32
|
1 |
|
return $this->findEntities($sql, [$user_id, $vault_id]); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param $vault_id |
|
37
|
|
|
* @param $user_id |
|
38
|
|
|
* @return Credential[] |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function getRandomCredentialByVaultId($vault_id, $user_id) { |
|
41
|
|
|
$sql = 'SELECT * FROM `*PREFIX*passman_credentials` ' . |
|
42
|
1 |
|
'WHERE `user_id` = ? and vault_id = ? AND shared_key is NULL LIMIT 20'; |
|
43
|
1 |
|
$entities = $this->findEntities($sql, [$user_id, $vault_id]); |
|
44
|
1 |
|
$count = count($entities)-1; |
|
45
|
1 |
|
$entities = array_splice($entities, rand(0, $count), 1); |
|
46
|
1 |
|
return $entities; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param $timestamp |
|
51
|
|
|
* @return Credential[] |
|
52
|
|
|
*/ |
|
53
|
2 |
|
public function getExpiredCredentials($timestamp){ |
|
54
|
|
|
$sql = 'SELECT * FROM `*PREFIX*passman_credentials` ' . |
|
55
|
2 |
|
'WHERE `expire_time` > 0 AND `expire_time` < ?'; |
|
56
|
2 |
|
return $this->findEntities($sql, [$timestamp]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param $credential_id |
|
61
|
|
|
* @param null $user_id |
|
62
|
|
|
* @return Credential |
|
63
|
|
|
*/ |
|
64
|
1 |
View Code Duplication |
public function getCredentialById($credential_id, $user_id = null){ |
|
65
|
|
|
$sql = 'SELECT * FROM `*PREFIX*passman_credentials` ' . |
|
66
|
1 |
|
'WHERE `id` = ?'; |
|
67
|
|
|
// If we want to check the owner, add it to the query |
|
68
|
1 |
|
$params = [$credential_id]; |
|
69
|
1 |
|
if ($user_id !== null){ |
|
70
|
1 |
|
$sql .= ' and `user_id` = ? '; |
|
71
|
1 |
|
array_push($params, $user_id); |
|
72
|
|
|
} |
|
73
|
1 |
|
return $this->findEntity($sql,$params); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param $credential_id |
|
78
|
|
|
* @return Credential |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function getCredentialLabelById($credential_id){ |
|
81
|
|
|
$sql = 'SELECT id, label FROM `*PREFIX*passman_credentials` ' . |
|
82
|
1 |
|
'WHERE `id` = ? '; |
|
83
|
1 |
|
return $this->findEntity($sql,[$credential_id]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param $raw_credential |
|
88
|
|
|
* @return Credential |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public function create($raw_credential){ |
|
91
|
1 |
|
$credential = new Credential(); |
|
92
|
|
|
|
|
93
|
1 |
|
$credential->setGuid($this->utils->GUID()); |
|
94
|
1 |
|
$credential->setVaultId($raw_credential['vault_id']); |
|
95
|
1 |
|
$credential->setUserId($raw_credential['user_id']); |
|
96
|
1 |
|
$credential->setLabel($raw_credential['label']); |
|
97
|
1 |
|
$credential->setDescription($raw_credential['description']); |
|
98
|
1 |
|
$credential->setCreated($this->utils->getTime()); |
|
99
|
1 |
|
$credential->setChanged($this->utils->getTime()); |
|
100
|
1 |
|
$credential->setTags($raw_credential['tags']); |
|
101
|
1 |
|
$credential->setEmail($raw_credential['email']); |
|
102
|
1 |
|
$credential->setUsername($raw_credential['username']); |
|
103
|
1 |
|
$credential->setPassword($raw_credential['password']); |
|
104
|
1 |
|
$credential->setUrl($raw_credential['url']); |
|
105
|
1 |
|
$credential->setFavicon($raw_credential['favicon']); |
|
106
|
1 |
|
$credential->setRenewInterval($raw_credential['renew_interval']); |
|
107
|
1 |
|
$credential->setExpireTime($raw_credential['expire_time']); |
|
108
|
1 |
|
$credential->setDeleteTime($raw_credential['delete_time']); |
|
109
|
1 |
|
$credential->setFiles($raw_credential['files']); |
|
110
|
1 |
|
$credential->setCustomFields($raw_credential['custom_fields']); |
|
111
|
1 |
|
$credential->setOtp($raw_credential['otp']); |
|
112
|
1 |
|
$credential->setHidden($raw_credential['hidden']); |
|
113
|
1 |
|
$credential->setSharedKey($raw_credential['shared_key']); |
|
114
|
1 |
|
return parent::insert($credential); |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param $raw_credential array An array containing all the credential fields |
|
119
|
|
|
* @return Credential The updated credential |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public function updateCredential($raw_credential){ |
|
122
|
1 |
|
$original = $this->getCredentialByGUID($raw_credential['guid']); |
|
123
|
1 |
|
$credential = new Credential(); |
|
124
|
1 |
|
$credential->setId($original->getId()); |
|
125
|
1 |
|
$credential->setGuid($original->getGuid()); |
|
126
|
1 |
|
$credential->setVaultId($original->getVaultId()); |
|
127
|
1 |
|
$credential->setUserId($original->getUserId()); |
|
128
|
1 |
|
$credential->setLabel($raw_credential['label']); |
|
129
|
1 |
|
$credential->setDescription($raw_credential['description']); |
|
130
|
1 |
|
$credential->setCreated($original->getCreated()); |
|
131
|
1 |
|
$credential->setChanged($this->utils->getTime()); |
|
132
|
1 |
|
$credential->setTags($raw_credential['tags']); |
|
133
|
1 |
|
$credential->setEmail($raw_credential['email']); |
|
134
|
1 |
|
$credential->setUsername($raw_credential['username']); |
|
135
|
1 |
|
$credential->setPassword($raw_credential['password']); |
|
136
|
1 |
|
$credential->setUrl($raw_credential['url']); |
|
137
|
1 |
|
$credential->setFavicon($raw_credential['favicon']); |
|
138
|
1 |
|
$credential->setRenewInterval($raw_credential['renew_interval']); |
|
139
|
1 |
|
$credential->setExpireTime($raw_credential['expire_time']); |
|
140
|
1 |
|
$credential->setFiles($raw_credential['files']); |
|
141
|
1 |
|
$credential->setCustomFields($raw_credential['custom_fields']); |
|
142
|
1 |
|
$credential->setOtp($raw_credential['otp']); |
|
143
|
1 |
|
$credential->setHidden($raw_credential['hidden']); |
|
144
|
1 |
|
$credential->setDeleteTime($raw_credential['delete_time']); |
|
145
|
1 |
|
$credential->setSharedKey($raw_credential['shared_key']); |
|
146
|
1 |
|
return parent::update($credential); |
|
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
1 |
|
public function deleteCredential(Credential $credential){ |
|
150
|
1 |
|
return $this->delete($credential); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
1 |
|
public function upd(Credential $credential){ |
|
154
|
1 |
|
$this->update($credential); |
|
155
|
1 |
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Finds a credential by the given guid |
|
159
|
|
|
* @param $credential_guid |
|
160
|
|
|
* @return Credential |
|
161
|
|
|
*/ |
|
162
|
1 |
View Code Duplication |
public function getCredentialByGUID($credential_guid, $user_id = null){ |
|
163
|
1 |
|
$q = 'SELECT * FROM `*PREFIX*passman_credentials` WHERE guid = ? '; |
|
164
|
1 |
|
$params = [$credential_guid]; |
|
165
|
1 |
|
if ($user_id !== null){ |
|
166
|
1 |
|
$q .= ' and `user_id` = ? '; |
|
167
|
1 |
|
array_push($params, $user_id); |
|
168
|
|
|
} |
|
169
|
1 |
|
return $this->findEntity($q, $params); |
|
170
|
|
|
} |
|
171
|
|
|
} |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.