Files
projectsend/app/Modules/Files/Access/ViewableFileScope.php
T
ignacionelson 6e47d76ba6 ProjectSend 2.0.0
Client file sharing, rebuilt from the ground up: a private area per
client, resumable uploads, folders, groups and categories, sharing with
expiry dates and download limits, comments, file versions, an activity
log, a REST API, and sixteen languages.

This repository begins here. ProjectSend 2 was developed privately, and
that development history is not published — the previous generation
remains available, with its own history, at projectsend/legacy.

Free software under the GNU General Public License v2, or (at your
option) any later version.
2026-08-14 01:38:12 -03:00

55 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Files\Access;
use App\Models\User;
use App\Modules\Files\Models\File;
use Illuminate\Database\Eloquent\Builder;
/**
* FilePolicy::view() expressed as a query instead of a per-model check —
* the same rules, evaluated in SQL so a caller can ask "every file this
* user may see" without loading candidates first.
*
* This exists because a folder is not a bag of uniformly-visible files: a
* user may hold a folder while individual files inside it are not theirs
* to read (a client's expired file is the live case — see
* File::scopeVisibleToClient, which ends in notExpired()). Anything that
* expands a folder into its contents must therefore re-derive visibility
* per file rather than inherit it from the folder, or the folder becomes a
* way around the per-file rule. BuildZipDownloadJob is the first such
* caller; any future bulk operation over a subtree is the next.
*
* Keep this in lockstep with FilePolicy::view(). The policy stays the
* authority for a single known file; this is its set-shaped twin.
*/
class ViewableFileScope
{
public function __construct(
private readonly StaffLibraryScope $scope,
) {}
/**
* @return Builder<File>
*/
public function for(User $user): Builder
{
if (! $user->isStaff()) {
return File::query()->visibleToClient($user);
}
// Mirrors FilePolicy::view()'s staff branch: the permission half is
// a property of the viewer, not the row, so it either opens the
// whole scope or closes it entirely.
$permitted = $user->can('upload') || $user->can('edit_files') || $user->can('edit_others_files');
if (! $permitted) {
return File::query()->whereRaw('1 = 0');
}
return $this->scope->files($user);
}
}