Failed Conditions
Pull Request — master (#682)
by
unknown
09:41
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 Credential[]
108
	 */
109
	public function getCredentialsByVaultId(int $vault_id, string $user_id) {
110
		$credentials = $this->credentialMapper->getCredentialsByVaultId($vault_id, $user_id);
111
		foreach ($credentials as $index => $credential) {
112
			$credentials[$index] = $this->encryptService->decryptCredential($credential);
113
		}
114
		return $credentials;
115
	}
116
117
	/**
118
	 * Get a random credential from given vault
119
	 *
120
	 * @param int $vault_id
121
	 * @param string $user_id
122
	 * @return mixed
123
	 */
124
	public function getRandomCredentialByVaultId(int $vault_id, string $user_id) {
125
		$credentials = $this->credentialMapper->getRandomCredentialByVaultId($vault_id, $user_id);
126
		foreach ($credentials as $index => $credential) {
127
			$credentials[$index] = $this->encryptService->decryptCredential($credential);
128
		}
129
		return array_pop($credentials);
130
	}
131
132
	/**
133
	 * Get expired credentials.
134
	 *
135
	 * @param int $timestamp
136
	 * @return Credential[]
137
	 */
138
	public function getExpiredCredentials(int $timestamp) {
139
		$credentials = $this->credentialMapper->getExpiredCredentials($timestamp);
140
		foreach ($credentials as $index => $credential) {
141
			$credentials[$index] = $this->encryptService->decryptCredential($credential);
142
		}
143
		return $credentials;
144
	}
145
146
	/**
147
	 * Get a single credential.
148
	 *
149
	 * @param int $credential_id
150
	 * @param string $user_id
151
	 * @return array|Credential
152
	 * @throws DoesNotExistException
153
	 * @throws MultipleObjectsReturnedException
154
	 */
155
	public function getCredentialById(int $credential_id, string $user_id) {
156
		$credential = $this->credentialMapper->getCredentialById($credential_id);
157
		if ($credential->getUserId() === $user_id) {
158
			return $this->encryptService->decryptCredential($credential);
159
		} else {
160
			$acl = $this->sharingACL->getItemACL($user_id, $credential->getGuid());
161
			if ($acl->hasPermission(SharingACL::READ)) {
162
				return $this->encryptService->decryptCredential($credential);
163
			} else {
164
				throw new DoesNotExistException("Did expect one result but found none when executing");
165
			}
166
		}
167
	}
168
169
	/**
170
	 * Get credential label by credential id.
171
	 *
172
	 * @param int $credential_id
173
	 * @return array|Credential
174
	 * @throws DoesNotExistException
175
	 * @throws MultipleObjectsReturnedException
176
	 */
177
	public function getCredentialLabelById(int $credential_id) {
178
		$credential = $this->credentialMapper->getCredentialLabelById($credential_id);
179
		return $this->encryptService->decryptCredential($credential);
180
	}
181
182
	/**
183
	 * Get credential by guid
184
	 *
185
	 * @param string $credential_guid
186
	 * @param string|null $user_id
187
	 * @return array|Credential
188
	 * @throws DoesNotExistException
189
	 * @throws MultipleObjectsReturnedException
190
	 */
191
	public function getCredentialByGUID(string $credential_guid, string $user_id = null) {
192
		$credential = $this->credentialMapper->getCredentialByGUID($credential_guid, $user_id);
193
		return $this->encryptService->decryptCredential($credential);
194
	}
195
}
196