CheckForUserCertificates.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 OCP\AppFramework\Utility\ITimeFactory;
  27. use OCP\BackgroundJob\QueuedJob;
  28. use OCP\Files\Folder;
  29. use OCP\Files\IRootFolder;
  30. use OCP\Files\NotFoundException;
  31. use OCP\IConfig;
  32. use OCP\IUser;
  33. use OCP\IUserManager;
  34. class CheckForUserCertificates extends QueuedJob {
  35. protected IConfig $config;
  36. private IUserManager $userManager;
  37. private IRootFolder $rootFolder;
  38. public function __construct(IConfig $config, IUserManager $userManager, IRootFolder $rootFolder, ITimeFactory $time) {
  39. parent::__construct($time);
  40. $this->config = $config;
  41. $this->userManager = $userManager;
  42. $this->rootFolder = $rootFolder;
  43. }
  44. /**
  45. * Checks all user directories for old user uploaded certificates
  46. */
  47. public function run($arguments): void {
  48. $uploadList = [];
  49. $this->userManager->callForSeenUsers(function (IUser $user) use (&$uploadList) {
  50. $userId = $user->getUID();
  51. try {
  52. \OC_Util::setupFS($userId);
  53. $filesExternalUploadsFolder = $this->rootFolder->get($userId . '/files_external/uploads');
  54. } catch (NotFoundException $e) {
  55. \OC_Util::tearDownFS();
  56. return;
  57. }
  58. if ($filesExternalUploadsFolder instanceof Folder) {
  59. $files = $filesExternalUploadsFolder->getDirectoryListing();
  60. foreach ($files as $file) {
  61. $filename = $file->getName();
  62. $uploadList[] = "$userId/files_external/uploads/$filename";
  63. }
  64. }
  65. \OC_Util::tearDownFS();
  66. });
  67. if (empty($uploadList)) {
  68. $this->config->deleteAppValue('files_external', 'user_certificate_scan');
  69. } else {
  70. $this->config->setAppValue('files_external', 'user_certificate_scan', json_encode($uploadList));
  71. }
  72. }
  73. }