1010namespace OCA \Text \Service ;
1111
1212use OC \User \NoUserException ;
13+ use OCA \DAV \Connector \Sabre \PublicAuth ;
1314use OCA \Files_Sharing \SharedStorage ;
1415use OCA \Text \Controller \AttachmentController ;
1516use OCA \Text \Db \Session ;
2526use OCP \Files \SimpleFS \ISimpleFile ;
2627use OCP \FilesMetadata \IFilesMetadataManager ;
2728use OCP \IPreview ;
29+ use OCP \ISession ;
2830use OCP \IURLGenerator ;
2931use OCP \Lock \LockedException ;
3032use OCP \Share \Exceptions \ShareNotFound ;
@@ -41,6 +43,7 @@ public function __construct(
4143 private IURLGenerator $ urlGenerator ,
4244 private IFilenameValidator $ filenameValidator ,
4345 private IFilesMetadataManager $ filesMetadataManager ,
46+ private ISession $ session ,
4447 ) {
4548 }
4649
@@ -311,9 +314,33 @@ public function uploadAttachment(int $documentId, string $newFileName, $newFileR
311314 * @throws NoUserException
312315 */
313316 public function uploadAttachmentPublic (?int $ documentId , string $ newFileName , $ newFileResource , string $ shareToken ): array {
314- if (!$ this ->hasUpdatePermissions ($ shareToken )) {
317+ try {
318+ $ share = $ this ->shareManager ->getShareByToken ($ shareToken );
319+ } catch (ShareNotFound ) {
320+ throw new NotFoundException ('Share not found ' );
321+ }
322+
323+ if (!$ this ->hasUpdatePermissions ($ share )) {
315324 throw new NotPermittedException ('No write permissions ' );
316325 }
326+
327+ if ($ share ->getPassword () !== null ) {
328+ $ key = PublicAuth::DAV_AUTHENTICATED ;
329+
330+ if (!$ this ->session ->exists ($ key )) {
331+ throw new NotPermittedException ('Share not authenticated ' );
332+ }
333+
334+ $ allowedShareIds = $ this ->session ->get ($ key );
335+ if (!is_array ($ allowedShareIds )) {
336+ throw new NotPermittedException ('Share not authenticated ' );
337+ }
338+
339+ if (!in_array ($ share ->getId (), $ allowedShareIds , true )) {
340+ throw new NotPermittedException ('Share not authenticated ' );
341+ }
342+ }
343+
317344 $ textFile = $ this ->getTextFilePublic ($ documentId , $ shareToken );
318345 $ saveDir = $ this ->getAttachmentDirectoryForFile ($ textFile , true );
319346 $ fileName = self ::getUniqueFileName ($ saveDir , $ newFileName );
@@ -429,25 +456,16 @@ public static function getUniqueFileName(Folder $dir, string $fileName): string
429456
430457 /**
431458 * Check if the shared access has write permissions
432- *
433- * @param string $shareToken
434- *
435- * @return bool
436459 */
437- private function hasUpdatePermissions (string $ shareToken ): bool {
438- try {
439- $ share = $ this ->shareManager ->getShareByToken ($ shareToken );
440- return (
441- in_array (
442- $ share ->getShareType (),
443- [IShare::TYPE_LINK , IShare::TYPE_EMAIL , IShare::TYPE_ROOM ],
444- true
445- )
446- && $ share ->getPermissions () & Constants::PERMISSION_UPDATE
447- && $ share ->getNode ()->getPermissions () & Constants::PERMISSION_UPDATE );
448- } catch (ShareNotFound |NotFoundException $ e ) {
449- return false ;
450- }
460+ private function hasUpdatePermissions (IShare $ share ): bool {
461+ return (
462+ in_array (
463+ $ share ->getShareType (),
464+ [IShare::TYPE_LINK , IShare::TYPE_EMAIL , IShare::TYPE_ROOM ],
465+ true
466+ )
467+ && $ share ->getPermissions () & Constants::PERMISSION_UPDATE
468+ && $ share ->getNode ()->getPermissions () & Constants::PERMISSION_UPDATE );
451469 }
452470
453471 /**
0 commit comments