AvatarPermissions.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Repair;
  23. use Doctrine\DBAL\Platforms\OraclePlatform;
  24. use OCP\IDBConnection;
  25. use OCP\Migration\IOutput;
  26. use OCP\Migration\IRepairStep;
  27. /**
  28. * Class AvatarPermissions
  29. *
  30. * @package OC\Repair
  31. */
  32. class AvatarPermissions implements IRepairStep {
  33. /** @var IDBConnection */
  34. private $connection;
  35. /**
  36. * AvatarPermissions constructor.
  37. *
  38. * @param IDBConnection $connection
  39. */
  40. public function __construct(IDBConnection $connection) {
  41. $this->connection = $connection;
  42. }
  43. /**
  44. * @return string
  45. */
  46. public function getName() {
  47. return 'Fix permissions so avatars can be stored again';
  48. }
  49. /**
  50. * @param IOutput $output
  51. */
  52. public function run(IOutput $output) {
  53. $output->startProgress(2);
  54. $this->fixUserRootPermissions();
  55. $output->advance();
  56. $this->fixAvatarPermissions();
  57. $output->finishProgress();
  58. }
  59. /**
  60. * Make sure all user roots have permissions 23 (all but share)
  61. */
  62. protected function fixUserRootPermissions() {
  63. $qb = $this->connection->getQueryBuilder();
  64. $qb2 = $this->connection->getQueryBuilder();
  65. $qb->select('numeric_id')
  66. ->from('storages')
  67. ->where($qb->expr()->like('id', $qb2->createParameter('like')));
  68. if ($this->connection->getDatabasePlatform() instanceof OraclePlatform) {
  69. // '' is null on oracle
  70. $path = $qb2->expr()->isNull('path');
  71. } else {
  72. $path = $qb2->expr()->eq('path', $qb2->createNamedParameter(''));
  73. }
  74. $qb2->update('filecache')
  75. ->set('permissions', $qb2->createNamedParameter(23))
  76. ->where($path)
  77. ->andWhere($qb2->expr()->in('storage', $qb2->createFunction($qb->getSQL())))
  78. ->andWhere($qb2->expr()->neq('permissions', $qb2->createNamedParameter(23)))
  79. ->setParameter('like', 'home::%');
  80. $qb2->execute();
  81. }
  82. /**
  83. * Make sure all avatar files in the user roots have permission 27
  84. */
  85. protected function fixAvatarPermissions() {
  86. $qb = $this->connection->getQueryBuilder();
  87. $qb2 = $this->connection->getQueryBuilder();
  88. $qb->select('numeric_id')
  89. ->from('storages')
  90. ->where($qb->expr()->like('id', $qb2->createParameter('like')));
  91. $qb2->update('filecache')
  92. ->set('permissions', $qb2->createNamedParameter(27))
  93. ->where($qb2->expr()->like('path', $qb2->createNamedParameter('avatar.%')))
  94. ->andWhere($qb2->expr()->in('storage', $qb2->createFunction($qb->getSQL())))
  95. ->andWhere($qb2->expr()->neq('permissions', $qb2->createNamedParameter(27)))
  96. ->setParameter('like', 'home::%');
  97. $qb2->execute();
  98. }
  99. }