certificatecontroller.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OC\Settings\Controller;
  22. use OCP\App\IAppManager;
  23. use OCP\AppFramework\Controller;
  24. use OCP\AppFramework\Http;
  25. use OCP\AppFramework\Http\DataResponse;
  26. use OCP\ICertificateManager;
  27. use OCP\IL10N;
  28. use OCP\IRequest;
  29. /**
  30. * @package OC\Settings\Controller
  31. */
  32. class CertificateController extends Controller {
  33. /** @var ICertificateManager */
  34. private $certificateManager;
  35. /** @var IL10N */
  36. private $l10n;
  37. /** @var IAppManager */
  38. private $appManager;
  39. /**
  40. * @param string $appName
  41. * @param IRequest $request
  42. * @param ICertificateManager $certificateManager
  43. * @param IL10N $l10n
  44. * @param IAppManager $appManager
  45. */
  46. public function __construct($appName,
  47. IRequest $request,
  48. ICertificateManager $certificateManager,
  49. IL10N $l10n,
  50. IAppManager $appManager) {
  51. parent::__construct($appName, $request);
  52. $this->certificateManager = $certificateManager;
  53. $this->l10n = $l10n;
  54. $this->appManager = $appManager;
  55. }
  56. /**
  57. * Add a new personal root certificate to the users' trust store
  58. *
  59. * @NoAdminRequired
  60. * @NoSubadminRequired
  61. * @return array
  62. */
  63. public function addPersonalRootCertificate() {
  64. if ($this->isCertificateImportAllowed() === false) {
  65. return new DataResponse('Individual certificate management disabled', Http::STATUS_FORBIDDEN);
  66. }
  67. $file = $this->request->getUploadedFile('rootcert_import');
  68. if(empty($file)) {
  69. return new DataResponse(['message' => 'No file uploaded'], Http::STATUS_UNPROCESSABLE_ENTITY);
  70. }
  71. try {
  72. $certificate = $this->certificateManager->addCertificate(file_get_contents($file['tmp_name']), $file['name']);
  73. return new DataResponse([
  74. 'name' => $certificate->getName(),
  75. 'commonName' => $certificate->getCommonName(),
  76. 'organization' => $certificate->getOrganization(),
  77. 'validFrom' => $certificate->getIssueDate()->getTimestamp(),
  78. 'validTill' => $certificate->getExpireDate()->getTimestamp(),
  79. 'validFromString' => $this->l10n->l('date', $certificate->getIssueDate()),
  80. 'validTillString' => $this->l10n->l('date', $certificate->getExpireDate()),
  81. 'issuer' => $certificate->getIssuerName(),
  82. 'issuerOrganization' => $certificate->getIssuerOrganization(),
  83. ]);
  84. } catch (\Exception $e) {
  85. return new DataResponse('An error occurred.', Http::STATUS_UNPROCESSABLE_ENTITY);
  86. }
  87. }
  88. /**
  89. * Removes a personal root certificate from the users' trust store
  90. *
  91. * @NoAdminRequired
  92. * @NoSubadminRequired
  93. * @param string $certificateIdentifier
  94. * @return DataResponse
  95. */
  96. public function removePersonalRootCertificate($certificateIdentifier) {
  97. if ($this->isCertificateImportAllowed() === false) {
  98. return new DataResponse('Individual certificate management disabled', Http::STATUS_FORBIDDEN);
  99. }
  100. $this->certificateManager->removeCertificate($certificateIdentifier);
  101. return new DataResponse();
  102. }
  103. /**
  104. * check if certificate import is allowed
  105. *
  106. * @return bool
  107. */
  108. protected function isCertificateImportAllowed() {
  109. $externalStorageEnabled = $this->appManager->isEnabledForUser('files_external');
  110. if ($externalStorageEnabled) {
  111. $backends = \OC_Mount_Config::getPersonalBackends();
  112. if (!empty($backends)) {
  113. return true;
  114. }
  115. }
  116. return false;
  117. }
  118. }