SetPasswordColumn.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. /** @var IDBConnection */
  19. private $connection;
  20. /** @var IConfig */
  21. private $config;
  22. public function __construct(IDBConnection $connection, IConfig $config) {
  23. $this->connection = $connection;
  24. $this->config = $config;
  25. }
  26. /**
  27. * Returns the step's name
  28. *
  29. * @return string
  30. * @since 9.1.0
  31. */
  32. public function getName() {
  33. return 'Copy the share password into the dedicated column';
  34. }
  35. /**
  36. * @param IOutput $output
  37. */
  38. public function run(IOutput $output) {
  39. if (!$this->shouldRun()) {
  40. return;
  41. }
  42. $query = $this->connection->getQueryBuilder();
  43. $query
  44. ->update('share')
  45. ->set('password', 'share_with')
  46. ->where($query->expr()->eq('share_type', $query->createNamedParameter(IShare::TYPE_LINK)))
  47. ->andWhere($query->expr()->isNotNull('share_with'));
  48. $result = $query->execute();
  49. if ($result === 0) {
  50. // No link updated, no need to run the second query
  51. return;
  52. }
  53. $clearQuery = $this->connection->getQueryBuilder();
  54. $clearQuery
  55. ->update('share')
  56. ->set('share_with', $clearQuery->createNamedParameter(null))
  57. ->where($clearQuery->expr()->eq('share_type', $clearQuery->createNamedParameter(IShare::TYPE_LINK)));
  58. $clearQuery->execute();
  59. }
  60. protected function shouldRun() {
  61. $appVersion = $this->config->getAppValue('files_sharing', 'installed_version', '0.0.0');
  62. return version_compare($appVersion, '1.4.0', '<');
  63. }
  64. }