1
0

AcceptController.php 1.8 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\NoAdminRequired;
  11. use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
  12. use OCP\AppFramework\Http\Attribute\OpenAPI;
  13. use OCP\AppFramework\Http\NotFoundResponse;
  14. use OCP\AppFramework\Http\RedirectResponse;
  15. use OCP\AppFramework\Http\Response;
  16. use OCP\IRequest;
  17. use OCP\IURLGenerator;
  18. use OCP\IUserSession;
  19. use OCP\Share\Exceptions\ShareNotFound;
  20. use OCP\Share\IManager as ShareManager;
  21. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  22. class AcceptController extends Controller {
  23. /** @var ShareManager */
  24. private $shareManager;
  25. /** @var IUserSession */
  26. private $userSession;
  27. /** @var IURLGenerator */
  28. private $urlGenerator;
  29. public function __construct(IRequest $request, ShareManager $shareManager, IUserSession $userSession, IURLGenerator $urlGenerator) {
  30. parent::__construct(Application::APP_ID, $request);
  31. $this->shareManager = $shareManager;
  32. $this->userSession = $userSession;
  33. $this->urlGenerator = $urlGenerator;
  34. }
  35. #[NoAdminRequired]
  36. #[NoCSRFRequired]
  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. }