mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-05-14 01:05:02 +00:00
TeamFoldersController@show refactored
This commit is contained in:
@@ -18,7 +18,7 @@ class AuthServiceProvider extends ServiceProvider
|
||||
* @var array
|
||||
*/
|
||||
protected $policies = [
|
||||
// 'App\Model' => 'App\Policies\ModelPolicy',
|
||||
//
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -55,9 +55,7 @@ class AuthServiceProvider extends ServiceProvider
|
||||
|
||||
private function share_guard(Share $share, Folder | File $item): bool
|
||||
{
|
||||
$isOwner = $share->user_id === $item->user_id;
|
||||
|
||||
if (! $share->is_protected && $isOwner) {
|
||||
if (! $share->is_protected && $share->user_id === $item->user_id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -80,7 +78,7 @@ class AuthServiceProvider extends ServiceProvider
|
||||
return false;
|
||||
}
|
||||
|
||||
return $isOwner;
|
||||
return $share->user_id === $item->user_id;
|
||||
}
|
||||
|
||||
private function team_member_guard(Folder | File $item, ?User $user, $ability): bool
|
||||
|
||||
@@ -201,11 +201,6 @@ class Folder extends Model
|
||||
return $this->hasMany(Folder::class, 'id', 'parent_id');
|
||||
}
|
||||
|
||||
public function teamRoot(): HasMany
|
||||
{
|
||||
return $this->parents()->with('teamRoot');
|
||||
}
|
||||
|
||||
public function getLatestParent()
|
||||
{
|
||||
if ($this->parent) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace Domain\Teams\Controllers;
|
||||
|
||||
use Gate;
|
||||
use Illuminate\Support\Str;
|
||||
use Domain\Files\Models\File;
|
||||
use Illuminate\Http\Response;
|
||||
@@ -28,33 +29,16 @@ class TeamFoldersController extends Controller
|
||||
|
||||
public function show($id): array
|
||||
{
|
||||
$files = [];
|
||||
$teamRootFolder = null;
|
||||
$id = Str::isUuid($id) ? $id : null;
|
||||
|
||||
$rootId = Str::isUuid($id) ? $id : null;
|
||||
$requestedFolder = $rootId ? Folder::findOrFail($rootId) : null;
|
||||
|
||||
$folders = Folder::where('parent_id', $rootId)
|
||||
$folders = Folder::where('parent_id', $id)
|
||||
->where('team_folder', ! Str::isUuid($id))
|
||||
->where('user_id', Auth::id())
|
||||
->sortable()
|
||||
->get();
|
||||
|
||||
if ($requestedFolder) {
|
||||
// Get root team folder
|
||||
$teamRootIdResults = recursiveFind(
|
||||
$requestedFolder->teamRoot->toArray(),
|
||||
'id'
|
||||
);
|
||||
|
||||
$teamRootId = end($teamRootIdResults);
|
||||
|
||||
$teamRootFolder = $teamRootId
|
||||
? Folder::findOrFail($teamRootId)
|
||||
: $requestedFolder;
|
||||
|
||||
// Get files
|
||||
$files = File::where('parent_id', $rootId)
|
||||
if ($id) {
|
||||
$files = File::where('parent_id', $id)
|
||||
->where('user_id', Auth::id())
|
||||
->sortable()
|
||||
->get();
|
||||
@@ -63,9 +47,9 @@ class TeamFoldersController extends Controller
|
||||
// Collect folders and files to single array
|
||||
return [
|
||||
'folders' => new FolderCollection($folders),
|
||||
'files' => new FilesCollection($files),
|
||||
'root' => $requestedFolder ? new FolderResource($requestedFolder) : null,
|
||||
'teamFolder' => $teamRootFolder ? new FolderResource($teamRootFolder) : null,
|
||||
'files' => isset($files) ? new FilesCollection($files) : new FilesCollection([]),
|
||||
'root' => $id ? new FolderResource(Folder::findOrFail($id)) : null,
|
||||
'teamFolder' => $id ? new FolderResource(Folder::findOrFail($id)->getLatestParent()) : null,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -32,9 +32,9 @@ class TeamsTest extends TestCase
|
||||
'parent_id' => $level_1->id,
|
||||
]);
|
||||
|
||||
$teamRoot = recursiveFind($level_2->teamRoot->toArray(), 'id');
|
||||
$teamRoot = $level_2->getLatestParent();
|
||||
|
||||
$this->assertEquals($teamFolder->id, end($teamRoot));
|
||||
$this->assertEquals($teamFolder->id, $teamRoot->id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user