Completed
Push — master ( 946999...5f7661 )
by Marcos
04:12
created

CredentialMapper::getExpiredCredentials()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
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 1
	public function getCredentialsByVaultId($vault_id, $user_id) {
47
		$sql = 'SELECT * FROM `*PREFIX*passman_credentials` ' .
48 1
			'WHERE `user_id` = ? and vault_id = ?';
49 1
		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 1
	public function getRandomCredentialByVaultId($vault_id, $user_id) {
60
		$sql = 'SELECT * FROM `*PREFIX*passman_credentials` ' .
61 1
			'WHERE `user_id` = ? and vault_id = ? AND shared_key is NULL LIMIT 20';
62 1
		$entities = $this->findEntities($sql, [$user_id, $vault_id]);
63 1
		$count = count($entities) - 1;
64 1
		$entities = array_splice($entities, rand(0, $count), 1);
65 1
		return $entities;
66
	}
67
68
	/**
69
	 * Get expired credentials
70
	 *
71
	 * @param $timestamp
72
	 * @return Credential[]
73
	 */
74 1
	public function getExpiredCredentials($timestamp) {
75
		$sql = 'SELECT * FROM `*PREFIX*passman_credentials` ' .
76 1
			'WHERE `expire_time` > 0 AND `expire_time` < ?';
77 1
		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 1 View Code Duplication
	public function getCredentialById($credential_id, $user_id = null) {
89
		$sql = 'SELECT * FROM `*PREFIX*passman_credentials` ' .
90 1
			'WHERE `id` = ?';
91
		// If we want to check the owner, add it to the query
92 1
		$params = [$credential_id];
93 1
		if ($user_id !== null) {
94 1
			$sql .= ' and `user_id` = ? ';
95 1
			array_push($params, $user_id);
96 1
		}
97 1
		return $this->findEntity($sql, $params);
98
	}
99
100
	/**
101
	 * Get credential label by id
102
	 *
103
	 * @param $credential_id
104
	 * @return Credential
105
	 */
106 1
	public function getCredentialLabelById($credential_id) {
107
		$sql = 'SELECT id, label FROM `*PREFIX*passman_credentials` ' .
108 1
			'WHERE `id` = ? ';
109 1
		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 1
	public function create($raw_credential) {
119 1
		$credential = new Credential();
120
121 1
		$credential->setGuid($this->utils->GUID());
122 1
		$credential->setVaultId($raw_credential['vault_id']);
123 1
		$credential->setUserId($raw_credential['user_id']);
124 1
		$credential->setLabel($raw_credential['label']);
125 1
		$credential->setDescription($raw_credential['description']);
126 1
		$credential->setCreated($this->utils->getTime());
127 1
		$credential->setChanged($this->utils->getTime());
128 1
		$credential->setTags($raw_credential['tags']);
129 1
		$credential->setEmail($raw_credential['email']);
130 1
		$credential->setUsername($raw_credential['username']);
131 1
		$credential->setPassword($raw_credential['password']);
132 1
		$credential->setUrl($raw_credential['url']);
133 1
		$credential->setFavicon($raw_credential['favicon']);
134 1
		$credential->setRenewInterval($raw_credential['renew_interval']);
135 1
		$credential->setExpireTime($raw_credential['expire_time']);
136 1
		$credential->setDeleteTime($raw_credential['delete_time']);
137 1
		$credential->setFiles($raw_credential['files']);
138 1
		$credential->setCustomFields($raw_credential['custom_fields']);
139 1
		$credential->setOtp($raw_credential['otp']);
140 1
		$credential->setHidden($raw_credential['hidden']);
141 1
		if (isset($raw_credential['shared_key'])) {
142
			$credential->setSharedKey($raw_credential['shared_key']);
143
		}
144 1
		return parent::insert($credential);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (insert() instead of create()). Are you sure this is correct? If so, you might want to change this to $this->insert().

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:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
145
	}
146
147
	/**
148
	 * Update a credential
149
	 *
150
	 * @param $raw_credential array An array containing all the credential fields
151
	 * @return Credential The updated credential
152
	 */
153 1
	public function updateCredential($raw_credential) {
154 1
		$original = $this->getCredentialByGUID($raw_credential['guid']);
155 1
		$credential = new Credential();
156 1
		$credential->setId($original->getId());
157 1
		$credential->setGuid($original->getGuid());
158 1
		$credential->setVaultId($original->getVaultId());
159 1
		$credential->setUserId($original->getUserId());
160 1
		$credential->setLabel($raw_credential['label']);
161 1
		$credential->setDescription($raw_credential['description']);
162 1
		$credential->setCreated($original->getCreated());
163 1
		$credential->setChanged($this->utils->getTime());
164 1
		$credential->setTags($raw_credential['tags']);
165 1
		$credential->setEmail($raw_credential['email']);
166 1
		$credential->setUsername($raw_credential['username']);
167 1
		$credential->setPassword($raw_credential['password']);
168 1
		$credential->setUrl($raw_credential['url']);
169 1
		$credential->setFavicon($raw_credential['favicon']);
170 1
		$credential->setRenewInterval($raw_credential['renew_interval']);
171 1
		$credential->setExpireTime($raw_credential['expire_time']);
172 1
		$credential->setFiles($raw_credential['files']);
173 1
		$credential->setCustomFields($raw_credential['custom_fields']);
174 1
		$credential->setOtp($raw_credential['otp']);
175 1
		$credential->setHidden($raw_credential['hidden']);
176 1
		$credential->setDeleteTime($raw_credential['delete_time']);
177 1
		if (isset($raw_credential['shared_key'])) {
178 1
			$credential->setSharedKey($raw_credential['shared_key']);
179 1
		}
180 1
		return parent::update($credential);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (update() instead of updateCredential()). Are you sure this is correct? If so, you might want to change this to $this->update().

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:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
181
	}
182
183 1
	public function deleteCredential(Credential $credential) {
184 1
		return $this->delete($credential);
185
	}
186
187 1
	public function upd(Credential $credential) {
188 1
		$this->update($credential);
189 1
	}
190
191
	/**
192
	 * Finds a credential by the given guid
193
	 *
194
	 * @param $credential_guid
195
	 * @return Credential
196
	 */
197 1 View Code Duplication
	public function getCredentialByGUID($credential_guid, $user_id = null) {
198 1
		$q = 'SELECT * FROM `*PREFIX*passman_credentials` WHERE guid = ? ';
199 1
		$params = [$credential_guid];
200 1
		if ($user_id !== null) {
201 1
			$q .= ' and `user_id` = ? ';
202 1
			array_push($params, $user_id);
203 1
		}
204 1
		return $this->findEntity($q, $params);
205
	}
206
}