1
0

Util.php 4.3 KB

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