SetPasswordColumn.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Files_Sharing\Migration;
  24. use OCP\IConfig;
  25. use OCP\IDBConnection;
  26. use OCP\Migration\IOutput;
  27. use OCP\Migration\IRepairStep;
  28. use OCP\Share\IShare;
  29. /**
  30. * Class SetPasswordColumn
  31. *
  32. * @package OCA\Files_Sharing\Migration
  33. */
  34. class SetPasswordColumn implements IRepairStep {
  35. /** @var IDBConnection */
  36. private $connection;
  37. /** @var IConfig */
  38. private $config;
  39. public function __construct(IDBConnection $connection, IConfig $config) {
  40. $this->connection = $connection;
  41. $this->config = $config;
  42. }
  43. /**
  44. * Returns the step's name
  45. *
  46. * @return string
  47. * @since 9.1.0
  48. */
  49. public function getName() {
  50. return 'Copy the share password into the dedicated column';
  51. }
  52. /**
  53. * @param IOutput $output
  54. */
  55. public function run(IOutput $output) {
  56. if (!$this->shouldRun()) {
  57. return;
  58. }
  59. $query = $this->connection->getQueryBuilder();
  60. $query
  61. ->update('share')
  62. ->set('password', 'share_with')
  63. ->where($query->expr()->eq('share_type', $query->createNamedParameter(IShare::TYPE_LINK)))
  64. ->andWhere($query->expr()->isNotNull('share_with'));
  65. $result = $query->execute();
  66. if ($result === 0) {
  67. // No link updated, no need to run the second query
  68. return;
  69. }
  70. $clearQuery = $this->connection->getQueryBuilder();
  71. $clearQuery
  72. ->update('share')
  73. ->set('share_with', $clearQuery->createNamedParameter(null))
  74. ->where($clearQuery->expr()->eq('share_type', $clearQuery->createNamedParameter(IShare::TYPE_LINK)));
  75. $clearQuery->execute();
  76. }
  77. protected function shouldRun() {
  78. $appVersion = $this->config->getAppValue('files_sharing', 'installed_version', '0.0.0');
  79. return version_compare($appVersion, '1.4.0', '<');
  80. }
  81. }