2222use OCP \AppFramework \Http \DataDownloadResponse ;
2323use OCP \AppFramework \Http \DataResponse ;
2424use OCP \AppFramework \Http \RedirectResponse ;
25+ use OCP \Constants ;
2526use OCP \Files \IMimeTypeDetector ;
2627use OCP \Files \InvalidPathException ;
28+ use OCP \Files \NotPermittedException ;
2729use OCP \IL10N ;
2830use OCP \IRequest ;
31+ use OCP \IUser ;
32+ use OCP \Share \Exceptions \ShareNotFound ;
33+ use OCP \Share \IManager as ShareManager ;
34+ use OCP \Share \IShare ;
2935use OCP \Util ;
3036use Psr \Log \LoggerInterface ;
3137
@@ -63,6 +69,7 @@ public function __construct(
6369 private LoggerInterface $ logger ,
6470 private IMimeTypeDetector $ mimeTypeDetector ,
6571 private AttachmentService $ attachmentService ,
72+ private ShareManager $ shareManager ,
6673 ) {
6774 parent ::__construct ($ appName , $ request );
6875 }
@@ -77,25 +84,19 @@ public function getAttachmentList(string $shareToken = ''): DataResponse {
7784 } catch (InvalidSessionException ) {
7885 $ session = null ;
7986 }
80-
81- if ($ shareToken ) {
82- $ attachments = $ this ->attachmentService ->getAttachmentList ($ documentId , null , $ session , $ shareToken );
83- } else {
84- $ userId = $ this ->getUserId ();
85- $ attachments = $ this ->attachmentService ->getAttachmentList ($ documentId , $ userId , $ session );
86- }
87-
87+ $ auth = $ this ->getAuth ($ shareToken , false );
88+ $ attachments = $ this ->attachmentService ->getAttachmentList ($ documentId , $ auth , $ session );
8889 return new DataResponse ($ attachments );
8990 }
9091
9192 #[NoAdminRequired]
9293 #[PublicPage]
9394 #[RequireDocumentSession]
9495 public function insertAttachmentFile (string $ filePath ): DataResponse {
95- $ userId = $ this ->getSession ()-> getUserId ();
96+ $ user = $ this ->getUser ();
9697
9798 try {
98- $ insertResult = $ this ->attachmentService ->insertAttachmentFile ($ this ->getSession ()->getDocumentId (), $ filePath , $ userId );
99+ $ insertResult = $ this ->attachmentService ->insertAttachmentFile ($ this ->getSession ()->getDocumentId (), $ filePath , $ user );
99100 if (isset ($ insertResult ['error ' ])) {
100101 return new DataResponse ($ insertResult , Http::STATUS_BAD_REQUEST );
101102 } else {
@@ -121,12 +122,8 @@ public function uploadAttachment(string $token = ''): DataResponse {
121122 throw new Exception ('Could not read file ' );
122123 }
123124 $ newFileName = $ file ['name ' ];
124- if ($ token ) {
125- $ uploadResult = $ this ->attachmentService ->uploadAttachmentPublic ($ documentId , $ newFileName , $ newFileResource , $ token );
126- } else {
127- $ userId = $ this ->getSession ()->getUserId ();
128- $ uploadResult = $ this ->attachmentService ->uploadAttachment ($ documentId , $ newFileName , $ newFileResource , $ userId );
129- }
125+ $ auth = $ this ->getAuth ($ token );
126+ $ uploadResult = $ this ->attachmentService ->uploadAttachment ($ documentId , $ newFileName , $ newFileResource , $ auth );
130127 if (isset ($ uploadResult ['error ' ])) {
131128 return new DataResponse ($ uploadResult , Http::STATUS_BAD_REQUEST );
132129 } else {
@@ -147,12 +144,12 @@ public function uploadAttachment(string $token = ''): DataResponse {
147144 #[NoAdminRequired]
148145 #[PublicPage]
149146 #[RequireDocumentSession]
150- public function createAttachment (string $ token = '' ): DataResponse {
147+ public function createAttachment (): DataResponse {
151148 $ documentId = $ this ->getSession ()->getDocumentId ();
152149 try {
153- $ userId = $ this ->getSession ()-> getUserId ();
150+ $ user = $ this ->getUser ();
154151 $ newFileName = $ this ->request ->getParam ('fileName ' , 'text.md ' );
155- $ createResult = $ this ->attachmentService ->createAttachmentFile ($ documentId , $ newFileName , $ userId );
152+ $ createResult = $ this ->attachmentService ->createAttachmentFile ($ documentId , $ newFileName , $ user );
156153 if (isset ($ createResult ['error ' ])) {
157154 return new DataResponse ($ createResult , Http::STATUS_BAD_REQUEST );
158155 } else {
@@ -208,13 +205,8 @@ public function getImageFile(string $imageFileName, string $shareToken = '',
208205 $ documentId = $ this ->getDocumentId ();
209206
210207 try {
211- if ($ shareToken ) {
212- $ imageFile = $ this ->attachmentService ->getImageFilePublic ($ documentId , $ imageFileName , $ shareToken , $ preferRawImage === 1 );
213- } else {
214- $ userId = $ this ->getUserId ();
215- $ imageFile = $ this ->attachmentService ->getImageFile ($ documentId , $ imageFileName , $ userId , $ preferRawImage === 1 );
216- }
217-
208+ $ auth = $ this ->getAuth ($ shareToken , false );
209+ $ imageFile = $ this ->attachmentService ->getImageFile ($ documentId , $ imageFileName , $ auth , $ preferRawImage === 1 );
218210 if ($ imageFile !== null ) {
219211 $ response = new DataDownloadResponse (
220212 $ imageFile ->getContent (),
@@ -247,12 +239,8 @@ public function getMediaFile(string $mediaFileName, string $shareToken = ''): Da
247239 $ documentId = $ this ->getDocumentId ();
248240
249241 try {
250- if ($ shareToken ) {
251- $ mediaFile = $ this ->attachmentService ->getMediaFilePublic ($ documentId , $ mediaFileName , $ shareToken );
252- } else {
253- $ userId = $ this ->getUserId ();
254- $ mediaFile = $ this ->attachmentService ->getMediaFile ($ documentId , $ mediaFileName , $ userId );
255- }
242+ $ auth = $ this ->getAuth ($ shareToken , false );
243+ $ mediaFile = $ this ->attachmentService ->getMediaFile ($ documentId , $ mediaFileName , $ auth );
256244 return $ mediaFile !== null
257245 ? new DataDownloadResponse (
258246 $ mediaFile ->getContent (),
@@ -278,12 +266,8 @@ public function getMediaFilePreview(string $mediaFileName, string $shareToken =
278266 $ documentId = $ this ->getDocumentId ();
279267
280268 try {
281- if ($ shareToken ) {
282- $ preview = $ this ->attachmentService ->getMediaFilePreviewPublic ($ documentId , $ mediaFileName , $ shareToken );
283- } else {
284- $ userId = $ this ->getUserId ();
285- $ preview = $ this ->attachmentService ->getMediaFilePreview ($ documentId , $ mediaFileName , $ userId );
286- }
269+ $ auth = $ this ->getAuth ($ shareToken , false );
270+ $ preview = $ this ->attachmentService ->getMediaFilePreview ($ documentId , $ mediaFileName , $ auth );
287271 if ($ preview === null ) {
288272 return new DataResponse ('' , Http::STATUS_NOT_FOUND );
289273 }
@@ -302,6 +286,36 @@ public function getMediaFilePreview(string $mediaFileName, string $shareToken =
302286 return new DataResponse ('' , Http::STATUS_NOT_FOUND );
303287 }
304288
289+ private function getAuth (string $ shareToken , bool $ updatePermissionRequired = true ): IShare |IUser {
290+ if ($ shareToken !== '' ) {
291+ try {
292+ $ share = $ this ->shareManager ->getShareByToken ($ shareToken );
293+ if (!$ updatePermissionRequired && !$ this ->hasUpdatePermissions ($ share )) {
294+ throw new NotPermittedException ('No write permissions ' );
295+ }
296+ return $ share ;
297+ } catch (ShareNotFound ) {
298+ throw new InvalidSessionException ();
299+ }
300+ } else {
301+ return $ this ->getUser ();
302+ }
303+ }
304+
305+ /**
306+ * Check if the shared access has write permissions
307+ */
308+ private function hasUpdatePermissions (IShare $ share ): bool {
309+ return (
310+ in_array (
311+ $ share ->getShareType (),
312+ [IShare::TYPE_LINK , IShare::TYPE_EMAIL , IShare::TYPE_ROOM ],
313+ true
314+ )
315+ && $ share ->getPermissions () & Constants::PERMISSION_UPDATE
316+ && $ share ->getNode ()->getPermissions () & Constants::PERMISSION_UPDATE );
317+ }
318+
305319 /**
306320 * Allow all supported mimetypes
307321 * Use mimetype detector for the other ones
0 commit comments