Files
projectsend/app/Modules/Files/FolderPolicy.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

60 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Files;
use App\Models\User;
use App\Modules\Files\Access\StaffLibraryScope;
use App\Modules\Files\Models\Folder;
/**
* Folder ownership rules, mirroring FilePolicy: own vs others' via the
* v1 permission pairs. Which staff see which folders is the
* StaffLibraryScope's job — and for client-scoped staff the policy AND's
* that scope into every action so direct access stays inside the boundary.
*
* Clients may create/rename/delete only folders they created themselves
* (create_own_folders doubles as the single toggle for the whole client
* folder-management feature, since clients have no edit_files/delete_files
* equivalent) — see MyFoldersController.
*/
class FolderPolicy
{
public function __construct(
private readonly StaffLibraryScope $scope,
) {}
public function view(User $user, Folder $folder): bool
{
if ($user->isStaff()) {
return ($user->can('upload') || $user->can('edit_files') || $user->can('edit_others_files'))
&& $this->scope->allowsFolder($user, $folder);
}
return Folder::query()->whereKey($folder->id)->visibleToClient($user)->exists();
}
public function update(User $user, Folder $folder): bool
{
if (! $user->isStaff()) {
return $folder->isOwnedBy($user) && $user->can('create_own_folders');
}
$permitted = $folder->isOwnedBy($user) ? $user->can('edit_files') : $user->can('edit_others_files');
return $permitted && $this->scope->allowsFolder($user, $folder);
}
public function delete(User $user, Folder $folder): bool
{
if (! $user->isStaff()) {
return $folder->isOwnedBy($user) && $user->can('create_own_folders');
}
$permitted = $folder->isOwnedBy($user) ? $user->can('delete_files') : $user->can('delete_others_files');
return $permitted && $this->scope->allowsFolder($user, $folder);
}
}