AcceptController.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Files_Sharing\Controller;
  25. use OCA\Files_Sharing\AppInfo\Application;
  26. use OCP\AppFramework\Controller;
  27. use OCP\AppFramework\Http\NotFoundResponse;
  28. use OCP\AppFramework\Http\RedirectResponse;
  29. use OCP\AppFramework\Http\Response;
  30. use OCP\IRequest;
  31. use OCP\IURLGenerator;
  32. use OCP\IUserSession;
  33. use OCP\Share\Exceptions\ShareNotFound;
  34. use OCP\Share\IManager as ShareManager;
  35. class AcceptController extends Controller {
  36. /** @var ShareManager */
  37. private $shareManager;
  38. /** @var IUserSession */
  39. private $userSession;
  40. /** @var IURLGenerator */
  41. private $urlGenerator;
  42. public function __construct(IRequest $request, ShareManager $shareManager, IUserSession $userSession, IURLGenerator $urlGenerator) {
  43. parent::__construct(Application::APP_ID, $request);
  44. $this->shareManager = $shareManager;
  45. $this->userSession = $userSession;
  46. $this->urlGenerator = $urlGenerator;
  47. }
  48. /**
  49. * @NoAdminRequired
  50. * @NoCSRFRequired
  51. */
  52. public function accept(string $shareId): Response {
  53. try {
  54. $share = $this->shareManager->getShareById($shareId);
  55. } catch (ShareNotFound $e) {
  56. return new NotFoundResponse();
  57. }
  58. $user = $this->userSession->getUser();
  59. if ($user === null) {
  60. return new NotFoundResponse();
  61. }
  62. try {
  63. $share = $this->shareManager->acceptShare($share, $user->getUID());
  64. } catch (\Exception $e) {
  65. // Just ignore
  66. }
  67. $url = $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $share->getNode()->getId()]);
  68. return new RedirectResponse($url);
  69. }
  70. }