Failed Conditions
Push — master ( e8410d...a45585 )
by Marcos
09:36 queued 11s
created

lib/Service/CredentialService.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Service;
25
26
use OCA\Passman\Db\Credential;
27
use OCA\Passman\Db\CredentialMapper;
28
use OCA\Passman\Db\SharingACL;
29
use OCA\Passman\Db\SharingACLMapper;
30
use OCP\AppFramework\Db\DoesNotExistException;
31
use OCP\AppFramework\Db\Entity;
32
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
33
use OCP\IConfig;
34
35
36
class CredentialService {
37
38
	private CredentialMapper $credentialMapper;
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
39
	private SharingACLMapper $sharingACL;
40
	private ShareService $shareService;
41
	private EncryptService $encryptService;
42
	private $server_key;
43
44
	public function __construct(CredentialMapper $credentialMapper, SharingACLMapper $sharingACL, ShareService $shareService, EncryptService $encryptService, IConfig $config) {
45
		$this->credentialMapper = $credentialMapper;
46
		$this->sharingACL = $sharingACL;
47
		$this->shareService = $shareService;
48
		$this->encryptService = $encryptService;
49
		$this->server_key = $config->getSystemValue('passwordsalt', '');
50
	}
51
52
	/**
53
	 * Create a new credential
54
	 *
55
	 * @param array $credential
56
	 * @return Credential
57
	 * @throws \Exception
58
	 */
59
	public function createCredential(array $credential) {
60
		$credential = $this->encryptService->encryptCredential($credential);
61
		return $this->credentialMapper->create($credential);
62
	}
63
64
	/**
65
	 * Update credential
66
	 *
67
	 * @param array $credential
68
	 * @param false $useRawUser
69
	 * @return Credential|Entity
70
	 * @throws DoesNotExistException
71
	 * @throws MultipleObjectsReturnedException
72
	 */
73
	public function updateCredential(array $credential, $useRawUser = false) {
74
		$credential = $this->encryptService->encryptCredential($credential);
75
		return $this->credentialMapper->updateCredential($credential, $useRawUser);
76
	}
77
78
	/**
79
	 * Update credential
80
	 *
81
	 * @param Credential $credential
82
	 * @return Credential|Entity
83
	 * @throws DoesNotExistException
84
	 * @throws MultipleObjectsReturnedException
85
	 */
86
	public function upd(Credential $credential) {
87
		$credential = $this->encryptService->encryptCredential($credential);
88
		return $this->credentialMapper->updateCredential($credential->jsonSerialize(), false);
89
	}
90
91
	/**
92
	 * Delete credential
93
	 *
94
	 * @param Credential $credential
95
	 * @return Entity
96
	 */
97
	public function deleteCredential(Credential $credential) {
98
		$this->shareService->unshareCredential($credential->getGuid());
99
		return $this->credentialMapper->deleteCredential($credential);
100
	}
101
102
	/**
103
	 * Get credentials by vault id
104
	 *
105
	 * @param int $vault_id
106
	 * @param string $user_id
107
	 * @return Entity[]
108
	 * @throws \Exception
109
	 */
110
	public function getCredentialsByVaultId(int $vault_id, string $user_id) {
111
		$credentials = $this->credentialMapper->getCredentialsByVaultId($vault_id, $user_id);
112
		foreach ($credentials as $index => $credential) {
113
			$credentials[$index] = $this->encryptService->decryptCredential($credential);
114
		}
115
		return $credentials;
116
	}
117
118
	/**
119
	 * Get a random credential from given vault
120
	 *
121
	 * @param int $vault_id
122
	 * @param string $user_id
123
	 * @return mixed
124
	 */
125
	public function getRandomCredentialByVaultId(int $vault_id, string $user_id) {
126
		$credentials = $this->credentialMapper->getRandomCredentialByVaultId($vault_id, $user_id);
127
		foreach ($credentials as $index => $credential) {
128
			$credentials[$index] = $this->encryptService->decryptCredential($credential);
129
		}
130
		return array_pop($credentials);
131
	}
132
133
	/**
134
	 * Get expired credentials.
135
	 *
136
	 * @param int $timestamp
137
	 * @return Entity[]
138
	 * @throws \Exception
139
	 */
140
	public function getExpiredCredentials(int $timestamp) {
141
		$credentials = $this->credentialMapper->getExpiredCredentials($timestamp);
142
		foreach ($credentials as $index => $credential) {
143
			$credentials[$index] = $this->encryptService->decryptCredential($credential);
144
		}
145
		return $credentials;
146
	}
147
148
	/**
149
	 * Get a single credential.
150
	 *
151
	 * @param int $credential_id
152
	 * @param string $user_id
153
	 * @return array|Credential
154
	 * @throws DoesNotExistException
155
	 * @throws MultipleObjectsReturnedException
156
	 */
157
	public function getCredentialById(int $credential_id, string $user_id) {
158
		$credential = $this->credentialMapper->getCredentialById($credential_id);
159
		if ($credential->getUserId() === $user_id) {
160
			return $this->encryptService->decryptCredential($credential);
161
		} else {
162
			$acl = $this->sharingACL->getItemACL($user_id, $credential->getGuid());
163
			if ($acl->hasPermission(SharingACL::READ)) {
164
				return $this->encryptService->decryptCredential($credential);
165
			} else {
166
				throw new DoesNotExistException("Did expect one result but found none when executing");
167
			}
168
		}
169
	}
170
171
	/**
172
	 * Get credential label by credential id.
173
	 *
174
	 * @param int $credential_id
175
	 * @return array|Credential
176
	 * @throws DoesNotExistException
177
	 * @throws MultipleObjectsReturnedException
178
	 */
179
	public function getCredentialLabelById(int $credential_id) {
180
		$credential = $this->credentialMapper->getCredentialLabelById($credential_id);
181
		return $this->encryptService->decryptCredential($credential);
182
	}
183
184
	/**
185
	 * Get credential by guid
186
	 *
187
	 * @param string $credential_guid
188
	 * @param string|null $user_id
189
	 * @return array|Credential
190
	 * @throws DoesNotExistException
191
	 * @throws MultipleObjectsReturnedException
192
	 */
193
	public function getCredentialByGUID(string $credential_guid, string $user_id = null) {
194
		$credential = $this->credentialMapper->getCredentialByGUID($credential_guid, $user_id);
195
		return $this->encryptService->decryptCredential($credential);
196
	}
197
}
198