AcceptController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. * @author Kate Döen <kate.doeen@nextcloud.com>
  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 OCA\Files_Sharing\Controller;
  26. use OCA\Files_Sharing\AppInfo\Application;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Http\Attribute\OpenAPI;
  29. use OCP\AppFramework\Http\NotFoundResponse;
  30. use OCP\AppFramework\Http\RedirectResponse;
  31. use OCP\AppFramework\Http\Response;
  32. use OCP\IRequest;
  33. use OCP\IURLGenerator;
  34. use OCP\IUserSession;
  35. use OCP\Share\Exceptions\ShareNotFound;
  36. use OCP\Share\IManager as ShareManager;
  37. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  38. class AcceptController extends Controller {
  39. /** @var ShareManager */
  40. private $shareManager;
  41. /** @var IUserSession */
  42. private $userSession;
  43. /** @var IURLGenerator */
  44. private $urlGenerator;
  45. public function __construct(IRequest $request, ShareManager $shareManager, IUserSession $userSession, IURLGenerator $urlGenerator) {
  46. parent::__construct(Application::APP_ID, $request);
  47. $this->shareManager = $shareManager;
  48. $this->userSession = $userSession;
  49. $this->urlGenerator = $urlGenerator;
  50. }
  51. /**
  52. * @NoAdminRequired
  53. * @NoCSRFRequired
  54. */
  55. public function accept(string $shareId): Response {
  56. try {
  57. $share = $this->shareManager->getShareById($shareId);
  58. } catch (ShareNotFound $e) {
  59. return new NotFoundResponse();
  60. }
  61. $user = $this->userSession->getUser();
  62. if ($user === null) {
  63. return new NotFoundResponse();
  64. }
  65. try {
  66. $share = $this->shareManager->acceptShare($share, $user->getUID());
  67. } catch (\Exception $e) {
  68. // Just ignore
  69. }
  70. $url = $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $share->getNode()->getId()]);
  71. return new RedirectResponse($url);
  72. }
  73. }