Util.php 4.2 KB

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