Completed
Branch master (8ca3ab)
by Sander
03:07
created

CredentialMapper::upd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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