AcceptController.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files_Sharing\Controller;
  8. use OCA\Files_Sharing\AppInfo\Application;
  9. use OCP\AppFramework\Controller;
  10. use OCP\AppFramework\Http\Attribute\OpenAPI;
  11. use OCP\AppFramework\Http\NotFoundResponse;
  12. use OCP\AppFramework\Http\RedirectResponse;
  13. use OCP\AppFramework\Http\Response;
  14. use OCP\IRequest;
  15. use OCP\IURLGenerator;
  16. use OCP\IUserSession;
  17. use OCP\Share\Exceptions\ShareNotFound;
  18. use OCP\Share\IManager as ShareManager;
  19. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  20. class AcceptController extends Controller {
  21. /** @var ShareManager */
  22. private $shareManager;
  23. /** @var IUserSession */
  24. private $userSession;
  25. /** @var IURLGenerator */
  26. private $urlGenerator;
  27. public function __construct(IRequest $request, ShareManager $shareManager, IUserSession $userSession, IURLGenerator $urlGenerator) {
  28. parent::__construct(Application::APP_ID, $request);
  29. $this->shareManager = $shareManager;
  30. $this->userSession = $userSession;
  31. $this->urlGenerator = $urlGenerator;
  32. }
  33. /**
  34. * @NoAdminRequired
  35. * @NoCSRFRequired
  36. */
  37. public function accept(string $shareId): Response {
  38. try {
  39. $share = $this->shareManager->getShareById($shareId);
  40. } catch (ShareNotFound $e) {
  41. return new NotFoundResponse();
  42. }
  43. $user = $this->userSession->getUser();
  44. if ($user === null) {
  45. return new NotFoundResponse();
  46. }
  47. try {
  48. $share = $this->shareManager->acceptShare($share, $user->getUID());
  49. } catch (\Exception $e) {
  50. // Just ignore
  51. }
  52. $url = $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $share->getNode()->getId()]);
  53. return new RedirectResponse($url);
  54. }
  55. }