Completed
Push — master ( dbecc0...75f541 )
by Marcos
02:07 queued 02:01
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
 * 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);
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...
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);
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...
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
}