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

VaultController::updateSharingKeys()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
nc 4
nop 3
dl 0
loc 14
ccs 6
cts 8
cp 0.75
crap 3.1406
rs 9.4285
c 1
b 0
f 0
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
12
namespace OCA\Passman\Controller;
13
14
use OCA\Passman\Service\EncryptService;
15
use OCA\Passman\Service\SettingsService;
16
use OCA\Passman\Utility\NotFoundJSONResponse;
17
use OCP\AppFramework\Db\DoesNotExistException;
18
use OCP\IRequest;
19
use OCP\AppFramework\Http\JSONResponse;
20
use OCP\AppFramework\ApiController;
21
use OCA\Passman\Service\VaultService;
22
use OCA\Passman\Service\CredentialService;
23
24
25
class VaultController extends ApiController {
26
	private $userId;
27
	private $vaultService;
28
	private $credentialService;
29
	private $settings;
30
31
	public function __construct($AppName,
32
								IRequest $request,
33
								$UserId,
34
								VaultService $vaultService,
35
								CredentialService $credentialService,
36
								SettingsService $settings) {
37
		parent::__construct(
38
			$AppName,
39
			$request,
40
			'GET, POST, DELETE, PUT, PATCH',
41
			'Authorization, Content-Type, Accept',
42
			86400);
43
		$this->userId = $UserId;
44
		$this->vaultService = $vaultService;
45
		$this->credentialService = $credentialService;
46
		$this->settings = $settings;
47
	}
48
49
	/**
50
	 * @NoAdminRequired
51
	 * @NoCSRFRequired
52
	 */
53 1
	public function listVaults() {
54 1
		$result = array();
55 1
		$vaults = $this->vaultService->getByUser($this->userId);
56
57 1
		$protected_credential_fields = array('getDescription', 'getEmail', 'getUsername', 'getPassword');
58 1
		if ($vaults) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $vaults of type OCA\Passman\Db\Vault[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
59
			foreach ($vaults as $vault) {
60
				$credential = $this->credentialService->getRandomCredentialByVaultId($vault->getId(), $this->userId);
61
				$secret_field = $protected_credential_fields[array_rand($protected_credential_fields)];
62
				array_push($result, array(
63
					'vault_id' => $vault->getId(),
64
					'guid' => $vault->getGuid(),
65
					'name' => $vault->getName(),
66
					'created' => $vault->getCreated(),
67
					'public_sharing_key' => $vault->getPublicSharingKey(),
68
					'last_access' => $vault->getlastAccess(),
69
					'challenge_password' => $credential->{$secret_field}(),
70
				));
71
			}
72
		}
73
74 1
		return new JSONResponse($result);
75
	}
76
77
	/**
78
	 * @NoAdminRequired
79
	 * @NoCSRFRequired
80
	 */
81 1
	public function create($vault_name) {
82 1
		$vault = $this->vaultService->createVault($vault_name, $this->userId);
83 1
		return new JSONResponse($vault);
84
	}
85
86
	/**
87
	 * @NoAdminRequired
88
	 * @NoCSRFRequired
89
	 */
90 1
	public function get($vault_guid) {
91 1
		$vault = null;
92
		try {
93 1
			$vault = $this->vaultService->getByGuid($vault_guid, $this->userId);
94 1
		} catch (\Exception $e) {
95
			return new NotFoundJSONResponse();
96
		}
97 1
		$result = array();
98 1
		if ($vault) {
99
			$credentials = $this->credentialService->getCredentialsByVaultId($vault->getId(), $this->userId);
100
101
			$result = array(
102
				'vault_id' => $vault->getId(),
103
				'guid' => $vault->getGuid(),
104
				'name' => $vault->getName(),
105
				'created' => $vault->getCreated(),
106
				'private_sharing_key' => $vault->getPrivateSharingKey(),
107
				'public_sharing_key' => $vault->getPublicSharingKey(),
108
				'sharing_keys_generated' => $vault->getSharingKeysGenerated(),
109
				'vault_settings' => $vault->getVaultSettings(),
110
				'last_access' => $vault->getlastAccess()
111
			);
112
			$result['credentials'] = $credentials;
113
114
			$this->vaultService->setLastAccess($vault->getId(), $this->userId);
115
		}
116
117
118 1
		return new JSONResponse($result);
119
	}
120
121
	/**
122
	 * @NoAdminRequired
123
	 * @NoCSRFRequired
124
	 */
125 1
	public function update($vault_guid, $name, $vault_settings) {
126 1
		$vault = $this->vaultService->getByGuid($vault_guid, $this->userId);
127 1
		if ($name && $vault) {
128
			$vault->setName($name);
129
		}
130 1
		if ($vault_settings && $vault) {
131
			$vault->setVaultSettings($vault_settings);
132
		}
133 1
		$this->vaultService->updateVault($vault);
134 1
	}
135
136
	/**
137
	 * @NoAdminRequired
138
	 * @NoCSRFRequired
139
	 */
140 1
	public function updateSharingKeys($vault_guid, $private_sharing_key, $public_sharing_key) {
141 1
		$vault = null;
142
		try {
143 1
			$vault = $this->vaultService->getByGuid($vault_guid, $this->userId);
144 1
		} catch (\Exception $e) {
145
			// No need to catch the execption
146
		}
147
148 1
		if ($vault) {
149
			$this->vaultService->updateSharingKeys($vault->getId(), $private_sharing_key, $public_sharing_key);
150
		}
151
152 1
		return;
153
	}
154
155
	/**
156
	 * @NoAdminRequired
157
	 * @NoCSRFRequired
158
	 */
159 1
	public function delete($vault_id) {
160 1
		return new JSONResponse($vault_id);
161
	}
162
}