Util.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Clark Tomlinson <fallen013@gmail.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Phil Davis <phil.davis@inf.org>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Encryption;
  28. use OC\Files\View;
  29. use OCA\Encryption\Crypto\Crypt;
  30. use OCP\IConfig;
  31. use OCP\IUser;
  32. use OCP\IUserManager;
  33. use OCP\IUserSession;
  34. use OCP\PreConditionNotMetException;
  35. class Util {
  36. private IUser|false $user;
  37. public function __construct(
  38. private View $files,
  39. private Crypt $crypt,
  40. IUserSession $userSession,
  41. private IConfig $config,
  42. private IUserManager $userManager,
  43. ) {
  44. $this->files = $files;
  45. $this->crypt = $crypt;
  46. $this->user = $userSession->isLoggedIn() ? $userSession->getUser() : false;
  47. $this->config = $config;
  48. $this->userManager = $userManager;
  49. }
  50. /**
  51. * check if recovery key is enabled for user
  52. *
  53. * @param string $uid
  54. * @return bool
  55. */
  56. public function isRecoveryEnabledForUser($uid) {
  57. $recoveryMode = $this->config->getUserValue($uid,
  58. 'encryption',
  59. 'recoveryEnabled',
  60. '0');
  61. return ($recoveryMode === '1');
  62. }
  63. /**
  64. * check if the home storage should be encrypted
  65. *
  66. * @return bool
  67. */
  68. public function shouldEncryptHomeStorage() {
  69. $encryptHomeStorage = $this->config->getAppValue(
  70. 'encryption',
  71. 'encryptHomeStorage',
  72. '1'
  73. );
  74. return ($encryptHomeStorage === '1');
  75. }
  76. /**
  77. * set the home storage encryption on/off
  78. *
  79. * @param bool $encryptHomeStorage
  80. */
  81. public function setEncryptHomeStorage($encryptHomeStorage) {
  82. $value = $encryptHomeStorage ? '1' : '0';
  83. $this->config->setAppValue(
  84. 'encryption',
  85. 'encryptHomeStorage',
  86. $value
  87. );
  88. }
  89. /**
  90. * check if master key is enabled
  91. */
  92. public function isMasterKeyEnabled(): bool {
  93. $userMasterKey = $this->config->getAppValue('encryption', 'useMasterKey', '1');
  94. return ($userMasterKey === '1');
  95. }
  96. /**
  97. * @param $enabled
  98. * @return bool
  99. */
  100. public function setRecoveryForUser($enabled) {
  101. $value = $enabled ? '1' : '0';
  102. try {
  103. $this->config->setUserValue($this->user->getUID(),
  104. 'encryption',
  105. 'recoveryEnabled',
  106. $value);
  107. return true;
  108. } catch (PreConditionNotMetException $e) {
  109. return false;
  110. }
  111. }
  112. /**
  113. * @param string $uid
  114. * @return bool
  115. */
  116. public function userHasFiles($uid) {
  117. return $this->files->file_exists($uid . '/files');
  118. }
  119. /**
  120. * get owner from give path, path relative to data/ expected
  121. *
  122. * @param string $path relative to data/
  123. * @return string
  124. * @throws \BadMethodCallException
  125. */
  126. public function getOwner($path) {
  127. $owner = '';
  128. $parts = explode('/', $path, 3);
  129. if (count($parts) > 1) {
  130. $owner = $parts[1];
  131. if ($this->userManager->userExists($owner) === false) {
  132. throw new \BadMethodCallException('Unknown user: ' .
  133. 'method expects path to a user folder relative to the data folder');
  134. }
  135. }
  136. return $owner;
  137. }
  138. /**
  139. * get storage of path
  140. *
  141. * @param string $path
  142. * @return \OC\Files\Storage\Storage|null
  143. */
  144. public function getStorage($path) {
  145. return $this->files->getMount($path)->getStorage();
  146. }
  147. }