CheckForUserCertificates.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Morris Jobke <hey@morrisjobke.de>
  5. *
  6. * @author J0WI <J0WI@users.noreply.github.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  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
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Core\BackgroundJobs;
  26. use OC\BackgroundJob\QueuedJob;
  27. use OCP\Files\Folder;
  28. use OCP\Files\IRootFolder;
  29. use OCP\Files\NotFoundException;
  30. use OCP\IConfig;
  31. use OCP\IUser;
  32. use OCP\IUserManager;
  33. class CheckForUserCertificates extends QueuedJob {
  34. /** @var IConfig */
  35. protected $config;
  36. /** @var IUserManager */
  37. private $userManager;
  38. /** @var IRootFolder */
  39. private $rootFolder;
  40. public function __construct(IConfig $config, IUserManager $userManager, IRootFolder $rootFolder) {
  41. $this->config = $config;
  42. $this->userManager = $userManager;
  43. $this->rootFolder = $rootFolder;
  44. }
  45. /**
  46. * Checks all user directories for old user uploaded certificates
  47. */
  48. public function run($arguments): void {
  49. $uploadList = [];
  50. $this->userManager->callForSeenUsers(function (IUser $user) use (&$uploadList) {
  51. $userId = $user->getUID();
  52. try {
  53. \OC_Util::setupFS($userId);
  54. $filesExternalUploadsFolder = $this->rootFolder->get($userId . '/files_external/uploads');
  55. } catch (NotFoundException $e) {
  56. \OC_Util::tearDownFS();
  57. return;
  58. }
  59. if ($filesExternalUploadsFolder instanceof Folder) {
  60. $files = $filesExternalUploadsFolder->getDirectoryListing();
  61. foreach ($files as $file) {
  62. $filename = $file->getName();
  63. $uploadList[] = "$userId/files_external/uploads/$filename";
  64. }
  65. }
  66. \OC_Util::tearDownFS();
  67. });
  68. if (empty($uploadList)) {
  69. $this->config->deleteAppValue('files_external', 'user_certificate_scan');
  70. } else {
  71. $this->config->setAppValue('files_external', 'user_certificate_scan', json_encode($uploadList));
  72. }
  73. }
  74. }