util.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Clark Tomlinson <fallen013@gmail.com>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\Encryption;
  23. use OC\Files\View;
  24. use OCA\Encryption\Crypto\Crypt;
  25. use OCP\IConfig;
  26. use OCP\ILogger;
  27. use OCP\IUser;
  28. use OCP\IUserSession;
  29. use OCP\PreConditionNotMetException;
  30. class Util {
  31. /**
  32. * @var View
  33. */
  34. private $files;
  35. /**
  36. * @var Crypt
  37. */
  38. private $crypt;
  39. /**
  40. * @var ILogger
  41. */
  42. private $logger;
  43. /**
  44. * @var bool|IUser
  45. */
  46. private $user;
  47. /**
  48. * @var IConfig
  49. */
  50. private $config;
  51. /**
  52. * Util constructor.
  53. *
  54. * @param View $files
  55. * @param Crypt $crypt
  56. * @param ILogger $logger
  57. * @param IUserSession $userSession
  58. * @param IConfig $config
  59. */
  60. public function __construct(View $files,
  61. Crypt $crypt,
  62. ILogger $logger,
  63. IUserSession $userSession,
  64. IConfig $config
  65. ) {
  66. $this->files = $files;
  67. $this->crypt = $crypt;
  68. $this->logger = $logger;
  69. $this->user = $userSession && $userSession->isLoggedIn() ? $userSession->getUser() : false;
  70. $this->config = $config;
  71. }
  72. /**
  73. * check if recovery key is enabled for user
  74. *
  75. * @param string $uid
  76. * @return bool
  77. */
  78. public function isRecoveryEnabledForUser($uid) {
  79. $recoveryMode = $this->config->getUserValue($uid,
  80. 'encryption',
  81. 'recoveryEnabled',
  82. 0);
  83. return ($recoveryMode === '1');
  84. }
  85. /**
  86. * @param $enabled
  87. * @return bool
  88. */
  89. public function setRecoveryForUser($enabled) {
  90. $value = $enabled ? '1' : '0';
  91. try {
  92. $this->config->setUserValue($this->user->getUID(),
  93. 'encryption',
  94. 'recoveryEnabled',
  95. $value);
  96. return true;
  97. } catch (PreConditionNotMetException $e) {
  98. return false;
  99. }
  100. }
  101. /**
  102. * @param string $uid
  103. * @return bool
  104. */
  105. public function userHasFiles($uid) {
  106. return $this->files->file_exists($uid . '/files');
  107. }
  108. }