|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OCA\Tables\Controller; |
| 4 | + |
| 5 | +use OCA\Tables\AppInfo\Application; |
| 6 | +use OCA\Tables\Errors\BadRequestError; |
| 7 | +use OCA\Tables\Helper\ConversionHelper; |
| 8 | +use OCA\Tables\Middleware\Attribute\RequirePermission; |
| 9 | +use OCA\Tables\Service\ShareService; |
| 10 | +use OCA\Tables\Service\TableService; |
| 11 | +use OCA\Tables\Service\ViewService; |
| 12 | +use OCP\AppFramework\Http\Attribute\ApiRoute; |
| 13 | +use OCP\AppFramework\Http\Attribute\NoAdminRequired; |
| 14 | +use OCP\AppFramework\Http\DataResponse; |
| 15 | +use OCP\EventDispatcher\IEventDispatcher; |
| 16 | +use OCP\IL10N; |
| 17 | +use OCP\IRequest; |
| 18 | +use OCP\IURLGenerator; |
| 19 | +use OCP\Security\Events\ValidatePasswordPolicyEvent; |
| 20 | +use OCP\Security\PasswordContext; |
| 21 | +use Psr\Log\LoggerInterface; |
| 22 | + |
| 23 | +class ShareOCSController extends AOCSController { |
| 24 | + public function __construct( |
| 25 | + IRequest $request, |
| 26 | + LoggerInterface $logger, |
| 27 | + IL10N $n, |
| 28 | + string $userId, |
| 29 | + protected ShareService $shareService, |
| 30 | + protected TableService $tableService, |
| 31 | + protected ViewService $viewService, |
| 32 | + protected IEventDispatcher $eventDispatcher, |
| 33 | + protected IURLGenerator $urlGenerator, |
| 34 | + ) { |
| 35 | + parent::__construct($request, $logger, $n, $userId); |
| 36 | + } |
| 37 | + |
| 38 | + #[NoAdminRequired] |
| 39 | + #[RequirePermission(permission: Application::PERMISSION_MANAGE, typeParam: 'nodeCollection')] |
| 40 | + #[ApiRoute(verb: 'POST', url: '/api/2/{nodeCollection}/{nodeId}/share')] |
| 41 | + public function createLinkShare( |
| 42 | + string $nodeCollection, |
| 43 | + string $nodeId, |
| 44 | + ?string $password = null, |
| 45 | + ): DataResponse { |
| 46 | + $collection = ConversionHelper::stringNodeType2Const($nodeCollection); |
| 47 | + if ($collection === Application::NODE_TYPE_TABLE) { |
| 48 | + $node = $this->tableService->find($nodeId); |
| 49 | + } else { |
| 50 | + $node = $this->viewService->find($nodeId); |
| 51 | + } |
| 52 | + |
| 53 | + if ($password !== null) { |
| 54 | + $event = new ValidatePasswordPolicyEvent($password, PasswordContext::SHARING); |
| 55 | + try { |
| 56 | + $this->eventDispatcher->dispatchTyped($event); |
| 57 | + } catch (\Exception $e) { |
| 58 | + $error = new BadRequestError($e->getMessage(), $e->getCode(), $e); |
| 59 | + return $this->handleBadRequestError($error); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + $share = $this->shareService->createLinkShare($node, $password); |
| 64 | + return new DataResponse([ |
| 65 | + 'shareToken' => (string)$share->getToken(), |
| 66 | + 'url' => $this->urlGenerator->linkToRouteAbsolute('tables.page.public', ['shareToken' => (string)$share->getToken()]), |
| 67 | + ]); |
| 68 | + } |
| 69 | +} |
0 commit comments