SetPasswordColumn.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Files_Sharing\Migration;
  7. use OCP\IConfig;
  8. use OCP\IDBConnection;
  9. use OCP\Migration\IOutput;
  10. use OCP\Migration\IRepairStep;
  11. use OCP\Share\IShare;
  12. /**
  13. * Class SetPasswordColumn
  14. *
  15. * @package OCA\Files_Sharing\Migration
  16. */
  17. class SetPasswordColumn implements IRepairStep {
  18. public function __construct(
  19. private IDBConnection $connection,
  20. private IConfig $config,
  21. ) {
  22. }
  23. /**
  24. * Returns the step's name
  25. *
  26. * @return string
  27. * @since 9.1.0
  28. */
  29. public function getName() {
  30. return 'Copy the share password into the dedicated column';
  31. }
  32. /**
  33. * @param IOutput $output
  34. */
  35. public function run(IOutput $output) {
  36. if (!$this->shouldRun()) {
  37. return;
  38. }
  39. $query = $this->connection->getQueryBuilder();
  40. $query
  41. ->update('share')
  42. ->set('password', 'share_with')
  43. ->where($query->expr()->eq('share_type', $query->createNamedParameter(IShare::TYPE_LINK)))
  44. ->andWhere($query->expr()->isNotNull('share_with'));
  45. $result = $query->execute();
  46. if ($result === 0) {
  47. // No link updated, no need to run the second query
  48. return;
  49. }
  50. $clearQuery = $this->connection->getQueryBuilder();
  51. $clearQuery
  52. ->update('share')
  53. ->set('share_with', $clearQuery->createNamedParameter(null))
  54. ->where($clearQuery->expr()->eq('share_type', $clearQuery->createNamedParameter(IShare::TYPE_LINK)));
  55. $clearQuery->execute();
  56. }
  57. protected function shouldRun() {
  58. $appVersion = $this->config->getAppValue('files_sharing', 'installed_version', '0.0.0');
  59. return version_compare($appVersion, '1.4.0', '<');
  60. }
  61. }