Passed
Push — master ( 17cdcf...aa80aa )
by John
09:42 queued 11s
created

CertificateController::addSystemRootCertificate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Björn Schießle <[email protected]>
6
 * @author Lukas Reschke <[email protected]>
7
 * @author Robin Appelman <[email protected]>
8
 * @author Roeland Jago Douma <[email protected]>
9
 * @author Vincent Petry <[email protected]>
10
 *
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
27
namespace OCA\Settings\Controller;
28
29
use OCP\App\IAppManager;
30
use OCP\AppFramework\Controller;
31
use OCP\AppFramework\Http;
32
use OCP\AppFramework\Http\DataResponse;
33
use OCP\ICertificateManager;
34
use OCP\IL10N;
35
use OCP\IRequest;
36
37
class CertificateController extends Controller {
38
	/** @var ICertificateManager */
39
	private $userCertificateManager;
40
	/** @var ICertificateManager  */
41
	private $systemCertificateManager;
42
	/** @var IL10N */
43
	private $l10n;
44
	/** @var IAppManager */
45
	private $appManager;
46
47
	/**
48
	 * @param string $appName
49
	 * @param IRequest $request
50
	 * @param ICertificateManager $userCertificateManager
51
	 * @param ICertificateManager $systemCertificateManager
52
	 * @param IL10N $l10n
53
	 * @param IAppManager $appManager
54
	 */
55
	public function __construct($appName,
56
								IRequest $request,
57
								ICertificateManager $userCertificateManager,
58
								ICertificateManager $systemCertificateManager,
59
								IL10N $l10n,
60
								IAppManager $appManager) {
61
		parent::__construct($appName, $request);
62
		$this->userCertificateManager = $userCertificateManager;
63
		$this->systemCertificateManager = $systemCertificateManager;
64
		$this->l10n = $l10n;
65
		$this->appManager = $appManager;
66
	}
67
68
	/**
69
	 * Add a new personal root certificate to the users' trust store
70
	 *
71
	 * @NoAdminRequired
72
	 * @NoSubadminRequired
73
	 * @return DataResponse
74
	 */
75
	public function addPersonalRootCertificate() {
76
		return $this->addCertificate($this->userCertificateManager);
77
	}
78
79
	/**
80
	 * Add a new root certificate to a trust store
81
	 *
82
	 * @param ICertificateManager $certificateManager
83
	 * @return DataResponse
84
	 */
85
	private function addCertificate(ICertificateManager $certificateManager) {
86
		$headers = [];
87
88
		if ($this->isCertificateImportAllowed() === false) {
89
			return new DataResponse(['message' => 'Individual certificate management disabled'], Http::STATUS_FORBIDDEN, $headers);
90
		}
91
92
		$file = $this->request->getUploadedFile('rootcert_import');
93
		if (empty($file)) {
94
			return new DataResponse(['message' => 'No file uploaded'], Http::STATUS_UNPROCESSABLE_ENTITY, $headers);
95
		}
96
97
		try {
98
			$certificate = $certificateManager->addCertificate(file_get_contents($file['tmp_name']), $file['name']);
99
			return new DataResponse(
100
				[
101
					'name' => $certificate->getName(),
102
					'commonName' => $certificate->getCommonName(),
103
					'organization' => $certificate->getOrganization(),
104
					'validFrom' => $certificate->getIssueDate()->getTimestamp(),
105
					'validTill' => $certificate->getExpireDate()->getTimestamp(),
106
					'validFromString' => $this->l10n->l('date', $certificate->getIssueDate()),
107
					'validTillString' => $this->l10n->l('date', $certificate->getExpireDate()),
108
					'issuer' => $certificate->getIssuerName(),
109
					'issuerOrganization' => $certificate->getIssuerOrganization(),
110
				],
111
				Http::STATUS_OK,
112
				$headers
113
			);
114
		} catch (\Exception $e) {
115
			return new DataResponse(['An error occurred.'], Http::STATUS_UNPROCESSABLE_ENTITY, $headers);
116
		}
117
	}
118
119
	/**
120
	 * Removes a personal root certificate from the users' trust store
121
	 *
122
	 * @NoAdminRequired
123
	 * @NoSubadminRequired
124
	 * @param string $certificateIdentifier
125
	 * @return DataResponse
126
	 */
127
	public function removePersonalRootCertificate($certificateIdentifier) {
128
129
		if ($this->isCertificateImportAllowed() === false) {
130
			return new DataResponse(['Individual certificate management disabled'], Http::STATUS_FORBIDDEN);
131
		}
132
133
		$this->userCertificateManager->removeCertificate($certificateIdentifier);
134
		return new DataResponse();
135
	}
136
137
	/**
138
	 * check if certificate import is allowed
139
	 *
140
	 * @return bool
141
	 */
142
	protected function isCertificateImportAllowed() {
143
		$externalStorageEnabled = $this->appManager->isEnabledForUser('files_external');
144
		if ($externalStorageEnabled) {
145
			/** @var \OCA\Files_External\Service\BackendService $backendService */
146
			$backendService = \OC_Mount_Config::$app->getContainer()->query('\OCA\Files_External\Service\BackendService');
147
			if ($backendService->isUserMountingAllowed()) {
148
				return true;
149
			}
150
		}
151
		return false;
152
	}
153
154
	/**
155
	 * Add a new personal root certificate to the system's trust store
156
	 *
157
	 * @return DataResponse
158
	 */
159
	public function addSystemRootCertificate() {
160
		return $this->addCertificate($this->systemCertificateManager);
161
	}
162
163
	/**
164
	 * Removes a personal root certificate from the users' trust store
165
	 *
166
	 * @param string $certificateIdentifier
167
	 * @return DataResponse
168
	 */
169
	public function removeSystemRootCertificate($certificateIdentifier) {
170
171
		if ($this->isCertificateImportAllowed() === false) {
172
			return new DataResponse(['Individual certificate management disabled'], Http::STATUS_FORBIDDEN);
173
		}
174
175
		$this->systemCertificateManager->removeCertificate($certificateIdentifier);
176
		return new DataResponse();
177
	}
178
}
179