From 99cb12b973fb773ab3bc0afbc93a3a2bd436760d Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Fri, 17 Jan 2014 11:42:45 -0500 Subject: [PATCH] Extend FileRepo::findFile private option to accept a User object Callers may want to test visibility for a user other than $wgUser when specifying the 'private' option. It seems a natural extension to allow 'private' to be a User object, with boolean true retaining its old meaning of $wgUser. Change-Id: Idbed0f3055c0135b5c11068de1bf1ef668e13456 --- includes/filerepo/FileRepo.php | 14 +++++++++++--- includes/filerepo/LocalRepo.php | 5 ++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/includes/filerepo/FileRepo.php b/includes/filerepo/FileRepo.php index cab5690f9b..81529e77e5 100644 --- a/includes/filerepo/FileRepo.php +++ b/includes/filerepo/FileRepo.php @@ -416,7 +416,7 @@ class FileRepo { * ignoreRedirect: If true, do not follow file redirects * private: If true, return restricted (deleted) files if the current * user is allowed to view them. Otherwise, such files will not - * be found. + * be found. If a User object, use that user instead of the current. * @return File|bool False on failure */ public function findFile( $title, $options = array() ) { @@ -439,7 +439,11 @@ class FileRepo { if ( $img && $img->exists() ) { if ( !$img->isDeleted( File::DELETED_FILE ) ) { return $img; // always OK - } elseif ( !empty( $options['private'] ) && $img->userCan( File::DELETED_FILE ) ) { + } elseif ( !empty( $options['private'] ) && + $img->userCan( File::DELETED_FILE, + $options['private'] instanceof User ? $options['private'] : null + ) + ) { return $img; } } @@ -536,7 +540,11 @@ class FileRepo { if ( $img && $img->exists() ) { if ( !$img->isDeleted( File::DELETED_FILE ) ) { return $img; // always OK - } elseif ( !empty( $options['private'] ) && $img->userCan( File::DELETED_FILE ) ) { + } elseif ( !empty( $options['private'] ) && + $img->userCan( File::DELETED_FILE, + $options['private'] instanceof User ? $options['private'] : null + ) + ) { return $img; } } diff --git a/includes/filerepo/LocalRepo.php b/includes/filerepo/LocalRepo.php index 1399c2bad9..99c0e09af6 100644 --- a/includes/filerepo/LocalRepo.php +++ b/includes/filerepo/LocalRepo.php @@ -260,6 +260,9 @@ class LocalRepo extends FileRepo { $fileMatchesSearch = function( File $file, array $search ) { // Note: file name comparison done elsewhere (to handle redirects) + $user = ( !empty( $search['private'] ) && $search['private'] instanceof User ) + ? $search['private'] + : null; return ( $file->exists() && ( @@ -267,7 +270,7 @@ class LocalRepo extends FileRepo { ( !empty( $search['time'] ) && $search['time'] === $file->getTimestamp() ) ) && ( !empty( $search['private'] ) || !$file->isDeleted( File::DELETED_FILE ) ) && - $file->userCan( File::DELETED_FILE ) + $file->userCan( File::DELETED_FILE, $user ) ); }; -- 2.20.1