Util.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Encryption;
  8. use OC\Files\View;
  9. use OCA\Encryption\Crypto\Crypt;
  10. use OCP\IConfig;
  11. use OCP\IUser;
  12. use OCP\IUserManager;
  13. use OCP\IUserSession;
  14. use OCP\PreConditionNotMetException;
  15. class Util {
  16. private IUser|false $user;
  17. public function __construct(
  18. private View $files,
  19. private Crypt $crypt,
  20. IUserSession $userSession,
  21. private IConfig $config,
  22. private IUserManager $userManager,
  23. ) {
  24. $this->files = $files;
  25. $this->crypt = $crypt;
  26. $this->user = $userSession->isLoggedIn() ? $userSession->getUser() : false;
  27. $this->config = $config;
  28. $this->userManager = $userManager;
  29. }
  30. /**
  31. * check if recovery key is enabled for user
  32. *
  33. * @param string $uid
  34. * @return bool
  35. */
  36. public function isRecoveryEnabledForUser($uid) {
  37. $recoveryMode = $this->config->getUserValue($uid,
  38. 'encryption',
  39. 'recoveryEnabled',
  40. '0');
  41. return ($recoveryMode === '1');
  42. }
  43. /**
  44. * check if the home storage should be encrypted
  45. *
  46. * @return bool
  47. */
  48. public function shouldEncryptHomeStorage() {
  49. $encryptHomeStorage = $this->config->getAppValue(
  50. 'encryption',
  51. 'encryptHomeStorage',
  52. '1'
  53. );
  54. return ($encryptHomeStorage === '1');
  55. }
  56. /**
  57. * set the home storage encryption on/off
  58. *
  59. * @param bool $encryptHomeStorage
  60. */
  61. public function setEncryptHomeStorage($encryptHomeStorage) {
  62. $value = $encryptHomeStorage ? '1' : '0';
  63. $this->config->setAppValue(
  64. 'encryption',
  65. 'encryptHomeStorage',
  66. $value
  67. );
  68. }
  69. /**
  70. * check if master key is enabled
  71. */
  72. public function isMasterKeyEnabled(): bool {
  73. $userMasterKey = $this->config->getAppValue('encryption', 'useMasterKey', '1');
  74. return ($userMasterKey === '1');
  75. }
  76. /**
  77. * @param $enabled
  78. * @return bool
  79. */
  80. public function setRecoveryForUser($enabled) {
  81. $value = $enabled ? '1' : '0';
  82. try {
  83. $this->config->setUserValue($this->user->getUID(),
  84. 'encryption',
  85. 'recoveryEnabled',
  86. $value);
  87. return true;
  88. } catch (PreConditionNotMetException $e) {
  89. return false;
  90. }
  91. }
  92. /**
  93. * @param string $uid
  94. * @return bool
  95. */
  96. public function userHasFiles($uid) {
  97. return $this->files->file_exists($uid . '/files');
  98. }
  99. /**
  100. * get owner from give path, path relative to data/ expected
  101. *
  102. * @param string $path relative to data/
  103. * @return string
  104. * @throws \BadMethodCallException
  105. */
  106. public function getOwner($path) {
  107. $owner = '';
  108. $parts = explode('/', $path, 3);
  109. if (count($parts) > 1) {
  110. $owner = $parts[1];
  111. if ($this->userManager->userExists($owner) === false) {
  112. throw new \BadMethodCallException('Unknown user: ' .
  113. 'method expects path to a user folder relative to the data folder');
  114. }
  115. }
  116. return $owner;
  117. }
  118. /**
  119. * get storage of path
  120. *
  121. * @param string $path
  122. * @return \OC\Files\Storage\Storage|null
  123. */
  124. public function getStorage($path) {
  125. return $this->files->getMount($path)->getStorage();
  126. }
  127. }