Util.php 2.9 KB

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