2727namespace OCA \Text \Service ;
2828
2929use OC \User \NoUserException ;
30+ use OCA \DAV \Connector \Sabre \PublicAuth ;
3031use OCA \Files_Sharing \SharedStorage ;
3132use OCA \Text \Controller \AttachmentController ;
3233use OCA \Text \Db \Session ;
4041use OCP \Files \NotPermittedException ;
4142use OCP \Files \SimpleFS \ISimpleFile ;
4243use OCP \IPreview ;
44+ use OCP \ISession ;
4345use OCP \IURLGenerator ;
4446use OCP \Lock \LockedException ;
4547use OCP \Share \Exceptions \ShareNotFound ;
@@ -52,7 +54,8 @@ public function __construct(private IRootFolder $rootFolder,
5254 private ShareManager $ shareManager ,
5355 private IPreview $ previewManager ,
5456 private IMimeTypeDetector $ mimeTypeDetector ,
55- private IURLGenerator $ urlGenerator ) {
57+ private IURLGenerator $ urlGenerator ,
58+ private ISession $ session ) {
5659 }
5760
5861 /**
@@ -310,9 +313,33 @@ public function uploadAttachment(int $documentId, string $newFileName, $newFileR
310313 * @throws NoUserException
311314 */
312315 public function uploadAttachmentPublic (?int $ documentId , string $ newFileName , $ newFileResource , string $ shareToken ): array {
313- if (!$ this ->hasUpdatePermissions ($ shareToken )) {
316+ try {
317+ $ share = $ this ->shareManager ->getShareByToken ($ shareToken );
318+ } catch (ShareNotFound ) {
319+ throw new NotFoundException ('Share not found ' );
320+ }
321+
322+ if (!$ this ->hasUpdatePermissions ($ share )) {
314323 throw new NotPermittedException ('No write permissions ' );
315324 }
325+
326+ if ($ share ->getPassword () !== null ) {
327+ $ key = PublicAuth::DAV_AUTHENTICATED ;
328+
329+ if (!$ this ->session ->exists ($ key )) {
330+ throw new NotPermittedException ('Share not authenticated ' );
331+ }
332+
333+ $ allowedShareIds = $ this ->session ->get ($ key );
334+ if (!is_array ($ allowedShareIds )) {
335+ throw new NotPermittedException ('Share not authenticated ' );
336+ }
337+
338+ if (!in_array ($ share ->getId (), $ allowedShareIds , true )) {
339+ throw new NotPermittedException ('Share not authenticated ' );
340+ }
341+ }
342+
316343 $ textFile = $ this ->getTextFilePublic ($ documentId , $ shareToken );
317344 $ saveDir = $ this ->getAttachmentDirectoryForFile ($ textFile , true );
318345 $ fileName = self ::getUniqueFileName ($ saveDir , $ newFileName );
@@ -398,25 +425,16 @@ public static function getUniqueFileName(Folder $dir, string $fileName): string
398425
399426 /**
400427 * Check if the shared access has write permissions
401- *
402- * @param string $shareToken
403- *
404- * @return bool
405428 */
406- private function hasUpdatePermissions (string $ shareToken ): bool {
407- try {
408- $ share = $ this ->shareManager ->getShareByToken ($ shareToken );
409- return (
410- in_array (
411- $ share ->getShareType (),
412- [IShare::TYPE_LINK , IShare::TYPE_EMAIL , IShare::TYPE_ROOM ],
413- true
414- )
415- && $ share ->getPermissions () & Constants::PERMISSION_UPDATE
416- && $ share ->getNode ()->getPermissions () & Constants::PERMISSION_UPDATE );
417- } catch (ShareNotFound |NotFoundException $ e ) {
418- return false ;
419- }
429+ private function hasUpdatePermissions (IShare $ share ): bool {
430+ return (
431+ in_array (
432+ $ share ->getShareType (),
433+ [IShare::TYPE_LINK , IShare::TYPE_EMAIL , IShare::TYPE_ROOM ],
434+ true
435+ )
436+ && $ share ->getPermissions () & Constants::PERMISSION_UPDATE
437+ && $ share ->getNode ()->getPermissions () & Constants::PERMISSION_UPDATE );
420438 }
421439
422440 /**
0 commit comments