You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Util.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. *
  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. namespace OCA\Encryption;
  27. use OC\Files\View;
  28. use OCA\Encryption\Crypto\Crypt;
  29. use OCP\IConfig;
  30. use OCP\ILogger;
  31. use OCP\IUser;
  32. use OCP\IUserManager;
  33. use OCP\IUserSession;
  34. use OCP\PreConditionNotMetException;
  35. class Util {
  36. /**
  37. * @var View
  38. */
  39. private $files;
  40. /**
  41. * @var Crypt
  42. */
  43. private $crypt;
  44. /**
  45. * @var ILogger
  46. */
  47. private $logger;
  48. /**
  49. * @var bool|IUser
  50. */
  51. private $user;
  52. /**
  53. * @var IConfig
  54. */
  55. private $config;
  56. /**
  57. * @var IUserManager
  58. */
  59. private $userManager;
  60. /**
  61. * Util constructor.
  62. *
  63. * @param View $files
  64. * @param Crypt $crypt
  65. * @param ILogger $logger
  66. * @param IUserSession $userSession
  67. * @param IConfig $config
  68. * @param IUserManager $userManager
  69. */
  70. public function __construct(View $files,
  71. Crypt $crypt,
  72. ILogger $logger,
  73. IUserSession $userSession,
  74. IConfig $config,
  75. IUserManager $userManager
  76. ) {
  77. $this->files = $files;
  78. $this->crypt = $crypt;
  79. $this->logger = $logger;
  80. $this->user = $userSession && $userSession->isLoggedIn() ? $userSession->getUser() : false;
  81. $this->config = $config;
  82. $this->userManager = $userManager;
  83. }
  84. /**
  85. * check if recovery key is enabled for user
  86. *
  87. * @param string $uid
  88. * @return bool
  89. */
  90. public function isRecoveryEnabledForUser($uid) {
  91. $recoveryMode = $this->config->getUserValue($uid,
  92. 'encryption',
  93. 'recoveryEnabled',
  94. '0');
  95. return ($recoveryMode === '1');
  96. }
  97. /**
  98. * check if the home storage should be encrypted
  99. *
  100. * @return bool
  101. */
  102. public function shouldEncryptHomeStorage() {
  103. $encryptHomeStorage = $this->config->getAppValue(
  104. 'encryption',
  105. 'encryptHomeStorage',
  106. '1'
  107. );
  108. return ($encryptHomeStorage === '1');
  109. }
  110. /**
  111. * set the home storage encryption on/off
  112. *
  113. * @param bool $encryptHomeStorage
  114. */
  115. public function setEncryptHomeStorage($encryptHomeStorage) {
  116. $value = $encryptHomeStorage ? '1' : '0';
  117. $this->config->setAppValue(
  118. 'encryption',
  119. 'encryptHomeStorage',
  120. $value
  121. );
  122. }
  123. /**
  124. * check if master key is enabled
  125. *
  126. * @return bool
  127. */
  128. public function isMasterKeyEnabled() {
  129. $userMasterKey = $this->config->getAppValue('encryption', 'useMasterKey', '1');
  130. return ($userMasterKey === '1');
  131. }
  132. /**
  133. * @param $enabled
  134. * @return bool
  135. */
  136. public function setRecoveryForUser($enabled) {
  137. $value = $enabled ? '1' : '0';
  138. try {
  139. $this->config->setUserValue($this->user->getUID(),
  140. 'encryption',
  141. 'recoveryEnabled',
  142. $value);
  143. return true;
  144. } catch (PreConditionNotMetException $e) {
  145. return false;
  146. }
  147. }
  148. /**
  149. * @param string $uid
  150. * @return bool
  151. */
  152. public function userHasFiles($uid) {
  153. return $this->files->file_exists($uid . '/files');
  154. }
  155. /**
  156. * get owner from give path, path relative to data/ expected
  157. *
  158. * @param string $path relative to data/
  159. * @return string
  160. * @throws \BadMethodCallException
  161. */
  162. public function getOwner($path) {
  163. $owner = '';
  164. $parts = explode('/', $path, 3);
  165. if (count($parts) > 1) {
  166. $owner = $parts[1];
  167. if ($this->userManager->userExists($owner) === false) {
  168. throw new \BadMethodCallException('Unknown user: ' .
  169. 'method expects path to a user folder relative to the data folder');
  170. }
  171. }
  172. return $owner;
  173. }
  174. /**
  175. * get storage of path
  176. *
  177. * @param string $path
  178. * @return \OC\Files\Storage\Storage
  179. */
  180. public function getStorage($path) {
  181. return $this->files->getMount($path)->getStorage();
  182. }
  183. }