1111use OC \Files \Storage \Wrapper \Wrapper ;
1212use OCA \Richdocuments \Middleware \WOPIMiddleware ;
1313use OCA \Richdocuments \PermissionManager ;
14+ use OCA \Richdocuments \Service \SecureViewService ;
1415use OCP \Files \ForbiddenException ;
1516use OCP \Files \IRootFolder ;
16- use OCP \Files \NotFoundException ;
17- use OCP \Files \Storage \ISharedStorage ;
1817use OCP \Files \Storage \IStorage ;
1918use OCP \IUserSession ;
2019use OCP \Server ;
@@ -24,6 +23,7 @@ class SecureViewWrapper extends Wrapper {
2423 private WOPIMiddleware $ wopiMiddleware ;
2524 private IRootFolder $ rootFolder ;
2625 private IUserSession $ userSession ;
26+ private SecureViewService $ secureViewService ;
2727
2828 private string $ mountPoint ;
2929
@@ -34,6 +34,7 @@ public function __construct(array $parameters) {
3434 $ this ->wopiMiddleware = Server::get (WOPIMiddleware::class);
3535 $ this ->rootFolder = Server::get (IRootFolder::class);
3636 $ this ->userSession = Server::get (IUserSession::class);
37+ $ this ->secureViewService = Server::get (SecureViewService::class);
3738
3839 $ this ->mountPoint = $ parameters ['mountPoint ' ];
3940 }
@@ -78,41 +79,15 @@ public function rename(string $source, string $target): bool {
7879 * @throws ForbiddenException
7980 */
8081 private function checkFileAccess (string $ path ): void {
81- if ($ this ->shouldSecure ( $ path ) && ! $ this ->wopiMiddleware -> isWOPIRequest ( )) {
82+ if (! $ this ->wopiMiddleware -> isWOPIRequest ( ) && $ this ->secureViewService -> shouldSecure ( $ path , $ this , false )) {
8283 throw new ForbiddenException ('Download blocked due the secure view policy ' , false );
8384 }
8485 }
8586
86- private function shouldSecure (string $ path , ?IStorage $ sourceStorage = null ): bool {
87- if ($ sourceStorage !== $ this && $ sourceStorage !== null ) {
88- $ fp = $ sourceStorage ->fopen ($ path , 'r ' );
89- fclose ($ fp );
90- }
91-
92- $ storage = $ sourceStorage ?? $ this ;
93- $ cacheEntry = $ storage ->getCache ()->get ($ path );
94- if (!$ cacheEntry ) {
95- $ parent = dirname ($ path );
96- if ($ parent === '. ' ) {
97- $ parent = '' ;
98- }
99- $ cacheEntry = $ storage ->getCache ()->get ($ parent );
100- if (!$ cacheEntry ) {
101- throw new NotFoundException (sprintf ('Could not find cache entry for path and parent of %s within storage %s ' , $ path , $ storage ->getId ()));
102- }
103- }
104-
105- $ isSharedStorage = $ storage ->instanceOfStorage (ISharedStorage::class);
106-
107- $ share = $ isSharedStorage ? $ storage ->getShare () : null ;
108- $ userId = $ this ->userSession ->getUser ()?->getUID();
109-
110- return $ this ->permissionManager ->shouldWatermark ($ cacheEntry , $ userId , $ share , $ storage ->getOwner ($ path ) ?: null );
111- }
112-
113-
11487 private function checkSourceAndTarget (string $ source , string $ target , ?IStorage $ sourceStorage = null ): void {
115- if ($ this ->shouldSecure ($ source , $ sourceStorage ) && !$ this ->shouldSecure ($ target )) {
88+ if ($ this ->secureViewService ->shouldSecure ($ source , $ sourceStorage ?? $ this , $ sourceStorage !== null )
89+ && !$ this ->secureViewService ->shouldSecure ($ target , $ this )
90+ ) {
11691 throw new ForbiddenException ('Download blocked due the secure view policy. The source requires secure view that the target cannot offer. ' , false );
11792 }
11893 }
0 commit comments