Skip to content

Commit 8a0b535

Browse files
committed
Larastan
1 parent 4416ec7 commit 8a0b535

File tree

7 files changed

+107
-43
lines changed

7 files changed

+107
-43
lines changed

src/Middleware/RedirectToCorrectEditPage.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tapp\FilamentLibrary\Middleware;
44

55
use Closure;
6+
use Filament\Facades\Filament;
67
use Illuminate\Http\Request;
78
use Tapp\FilamentLibrary\Models\LibraryItem;
89

@@ -13,20 +14,28 @@ class RedirectToCorrectEditPage
1314
*/
1415
public function handle(Request $request, Closure $next)
1516
{
16-
// Check if this is an edit route for library items
17-
if ($request->routeIs('filament.admin.resources.library.edit')) {
17+
$panel = Filament::getCurrentPanel();
18+
19+
if (! $panel) {
20+
return $next($request);
21+
}
22+
23+
$panelId = $panel->getId();
24+
25+
// Check if this is an edit route for library items in any panel
26+
if ($request->routeIs("filament.{$panelId}.resources.library.edit")) {
1827
$recordId = $request->route('record');
1928

2029
if ($recordId) {
2130
$libraryItem = LibraryItem::find($recordId);
2231

23-
if ($libraryItem) {
32+
if ($libraryItem && isset($libraryItem->type)) {
2433
// Redirect to the correct edit page based on type
2534
$editUrl = match ($libraryItem->type) {
26-
'folder' => route('filament.admin.resources.library.edit-folder', ['record' => $recordId]),
27-
'file' => route('filament.admin.resources.library.edit-file', ['record' => $recordId]),
28-
'link' => route('filament.admin.resources.library.edit-link', ['record' => $recordId]),
29-
default => route('filament.admin.resources.library.edit-folder', ['record' => $recordId]),
35+
'folder' => route("filament.{$panelId}.resources.library.edit-folder", ['record' => $recordId]),
36+
'file' => route("filament.{$panelId}.resources.library.edit-file", ['record' => $recordId]),
37+
'link' => route("filament.{$panelId}.resources.library.edit-link", ['record' => $recordId]),
38+
default => route("filament.{$panelId}.resources.library.edit-folder", ['record' => $recordId]),
3039
};
3140

3241
return redirect($editUrl);

src/Models/LibraryItem.php

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,27 @@
1313
use Spatie\MediaLibrary\InteractsWithMedia;
1414
use Spatie\MediaLibrary\MediaCollections\Models\Media;
1515

16+
/**
17+
* @property int $id
18+
* @property string $name
19+
* @property string $slug
20+
* @property string $type
21+
* @property int|null $parent_id
22+
* @property int $created_by
23+
* @property int|null $updated_by
24+
* @property string|null $external_url
25+
* @property string|null $link_description
26+
* @property string|null $general_access
27+
* @property \Illuminate\Support\Carbon|null $created_at
28+
* @property \Illuminate\Support\Carbon|null $updated_at
29+
* @property \Illuminate\Support\Carbon|null $deleted_at
30+
* @property-read LibraryItem|null $parent
31+
* @property-read \Illuminate\Database\Eloquent\Collection<int, LibraryItem> $children
32+
* @property-read \App\Models\User $creator
33+
* @property-read \App\Models\User|null $updater
34+
* @property-read \Illuminate\Database\Eloquent\Collection<int, LibraryItemPermission> $permissions
35+
* @property-read \Illuminate\Database\Eloquent\Collection<int, LibraryItemTag> $tags
36+
*/
1637
class LibraryItem extends Model implements HasMedia
1738
{
1839
use HasFactory;
@@ -58,14 +79,16 @@ protected static function boot(): void
5879

5980
static::created(function (self $item) {
6081
// Copy parent folder permissions to the new item
61-
if ($item->parent_id) {
82+
if ($item->parent_id && $item->parent) {
6283
$parentPermissions = $item->parent->permissions()->get();
6384

6485
foreach ($parentPermissions as $permission) {
65-
$item->permissions()->create([
66-
'user_id' => $permission->user_id,
67-
'role' => $permission->role,
68-
]);
86+
if (isset($permission->user_id) && isset($permission->role)) {
87+
$item->permissions()->create([
88+
'user_id' => $permission->user_id,
89+
'role' => $permission->role,
90+
]);
91+
}
6992
}
7093
}
7194
});
@@ -199,7 +222,7 @@ public function getEffectiveRole($user): ?string
199222
->where('user_id', $user->id)
200223
->first();
201224

202-
if ($directPermission) {
225+
if ($directPermission && isset($directPermission->role)) {
203226
return $directPermission->role;
204227
}
205228

@@ -220,14 +243,16 @@ public function getEffectiveRole($user): ?string
220243

221244
/**
222245
* Get the current owner of this item.
246+
*
247+
* @return \App\Models\User|\Illuminate\Database\Eloquent\Model|null
223248
*/
224-
public function getCurrentOwner(): ?\App\Models\User
249+
public function getCurrentOwner()
225250
{
226251
$ownerPermission = $this->permissions()
227252
->where('role', 'owner')
228253
->first();
229254

230-
if ($ownerPermission) {
255+
if ($ownerPermission && $ownerPermission->user) {
231256
return $ownerPermission->user;
232257
}
233258

src/Models/LibraryItemPermission.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Database\Eloquent\Relations\BelongsTo;
88

9+
/**
10+
* @property int $id
11+
* @property int $library_item_id
12+
* @property int $user_id
13+
* @property string $role
14+
* @property \Illuminate\Support\Carbon|null $created_at
15+
* @property \Illuminate\Support\Carbon|null $updated_at
16+
* @property-read LibraryItem $libraryItem
17+
* @property-read \App\Models\User $user
18+
*/
919
class LibraryItemPermission extends Model
1020
{
1121
use HasFactory;

src/Resources/Pages/ListLibraryItems.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ protected function getTableQuery(): \Illuminate\Database\Eloquent\Builder
254254

255255
public function getTitle(): string
256256
{
257-
if ($this->parentFolder) {
257+
if ($this->parentFolder && isset($this->parentFolder->name)) {
258258
return $this->parentFolder->name;
259259
}
260260

@@ -263,7 +263,7 @@ public function getTitle(): string
263263

264264
public function getSubheading(): ?string
265265
{
266-
if ($this->parentFolder && $this->parentFolder->link_description) {
266+
if ($this->parentFolder && isset($this->parentFolder->link_description) && $this->parentFolder->link_description) {
267267
return $this->parentFolder->link_description;
268268
}
269269

@@ -298,7 +298,9 @@ public function getBreadcrumbs(): array
298298
// Generate URLs more efficiently
299299
$baseUrl = static::getResource()::getUrl('index');
300300
foreach ($path as $folder) {
301-
$breadcrumbs[$baseUrl . '?parent=' . $folder->id] = $folder->name;
301+
if (isset($folder->id) && isset($folder->name)) {
302+
$breadcrumbs[$baseUrl . '?parent=' . $folder->id] = $folder->name;
303+
}
302304
}
303305
}
304306

src/Resources/RelationManagers/LibraryItemPermissionsRelationManager.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Filament\Tables\Table;
1616
use Illuminate\Database\Eloquent\Model;
1717
use Illuminate\Support\Facades\Schema as SchemaFacade;
18+
use Tapp\FilamentLibrary\Models\LibraryItem;
1819
use Tapp\FilamentLibrary\Models\LibraryItemPermission;
1920

2021
class LibraryItemPermissionsRelationManager extends RelationManager
@@ -41,7 +42,7 @@ public static function canAccess(): bool
4142

4243
// For non-admins, check if they have share permission on the current record
4344
$record = static::getOwnerRecord();
44-
if ($record && $record->hasPermission($user, 'share')) {
45+
if ($record instanceof LibraryItem && $record->hasPermission($user, 'share')) {
4546
return true;
4647
}
4748

@@ -50,7 +51,7 @@ public static function canAccess(): bool
5051

5152
public static function canViewForRecord(Model $ownerRecord, string $pageClass): bool
5253
{
53-
return $ownerRecord->hasPermission(auth()->user(), 'share');
54+
return $ownerRecord instanceof LibraryItem && $ownerRecord->hasPermission(auth()->user(), 'share');
5455
}
5556

5657
/**
@@ -173,28 +174,28 @@ public function table(Table $table): Table
173174
])
174175
->headerActions([
175176
CreateAction::make()
176-
->visible(fn () => $this->ownerRecord->hasPermission(auth()->user(), 'share')),
177+
->visible(fn () => $this->ownerRecord instanceof LibraryItem && $this->ownerRecord->hasPermission(auth()->user(), 'share')),
177178
])
178179
->heading('User Permissions')
179180
->description('Owner: Share and edit. Editor/Viewer: Standard permissions.')
180-
->actions([
181+
->recordActions([
181182
EditAction::make()
182-
->visible(fn () => $this->ownerRecord->hasPermission(auth()->user(), 'share')),
183+
->visible(fn () => $this->ownerRecord instanceof LibraryItem && $this->ownerRecord->hasPermission(auth()->user(), 'share')),
183184
DeleteAction::make()
184-
->visible(fn () => $this->ownerRecord->hasPermission(auth()->user(), 'share')),
185+
->visible(fn () => $this->ownerRecord instanceof LibraryItem && $this->ownerRecord->hasPermission(auth()->user(), 'share')),
185186
])
186-
->bulkActions([
187+
->toolbarActions([
187188
BulkActionGroup::make([
188189
DeleteBulkAction::make()
189-
->visible(fn () => $this->ownerRecord->hasPermission(auth()->user(), 'share')),
190+
->visible(fn () => $this->ownerRecord instanceof LibraryItem && $this->ownerRecord->hasPermission(auth()->user(), 'share')),
190191
]),
191192
])
192193
->emptyStateHeading('No permissions assigned')
193194
->emptyStateDescription('Add users to grant them specific permissions on this item.')
194195
->emptyStateActions([
195196
CreateAction::make()
196197
->label('Add Permission')
197-
->visible(fn () => $this->ownerRecord->hasPermission(auth()->user(), 'share')),
198+
->visible(fn () => $this->ownerRecord instanceof LibraryItem && $this->ownerRecord->hasPermission(auth()->user(), 'share')),
198199
]);
199200
}
200201
}

src/Services/PermissionService.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public function removePermission($user, LibraryItem $item, string $permission):
8080

8181
/**
8282
* Bulk assign permissions to multiple users for multiple items.
83+
*
84+
* @param \Illuminate\Database\Eloquent\Collection<int, LibraryItem>|array<LibraryItem> $items
8385
*/
8486
public function bulkAssignPermissions($items, array $data): void
8587
{
@@ -88,17 +90,24 @@ public function bulkAssignPermissions($items, array $data): void
8890
$generalAccess = $data['general_access'] ?? 'private';
8991

9092
foreach ($items as $item) {
93+
if (! $item instanceof LibraryItem) {
94+
continue;
95+
}
96+
9197
// Update the general access level for the item
9298
$item->update(['general_access' => $generalAccess]);
9399

94100
// Assign permissions to users
95101
foreach ($userIds as $userId) {
96102
$userModel = $this->getUserModel();
97-
$this->assignPermission(
98-
$userModel::find($userId),
99-
$item,
100-
$permission
101-
);
103+
$user = $userModel::find($userId);
104+
if ($user) {
105+
$this->assignPermission(
106+
$user,
107+
$item,
108+
$permission
109+
);
110+
}
102111
}
103112
}
104113
}
@@ -111,17 +120,24 @@ public function cascadePermissionsToChildren(LibraryItem $folder, array $userIds
111120
$children = $folder->children;
112121

113122
foreach ($children as $child) {
123+
if (! $child instanceof LibraryItem) {
124+
continue;
125+
}
126+
114127
foreach ($userIds as $userId) {
115128
$userModel = $this->getUserModel();
116-
$this->assignPermission(
117-
$userModel::find($userId),
118-
$child,
119-
$permission
120-
);
129+
$user = $userModel::find($userId);
130+
if ($user) {
131+
$this->assignPermission(
132+
$user,
133+
$child,
134+
$permission
135+
);
136+
}
121137
}
122138

123139
// Recursively cascade to grandchildren
124-
if ($child->type === 'folder') {
140+
if (isset($child->type) && $child->type === 'folder') {
125141
$this->cascadePermissionsToChildren($child, $userIds, $permission);
126142
}
127143
}

src/Tables/Actions/BulkManagePermissionsAction.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected function setUp(): void
3030
->icon('heroicon-o-shield-check')
3131
->color('warning')
3232
->visible(fn (): bool => auth()->user() && FilamentLibraryPlugin::isLibraryAdmin(auth()->user()))
33-
->form([
33+
->schema([
3434
Select::make('general_access')
3535
->label('General Access')
3636
->options([
@@ -76,9 +76,10 @@ protected function setUp(): void
7676

7777
public function success(): void
7878
{
79-
$this->successNotification(
80-
title: 'Permissions Updated',
81-
body: 'Permissions have been successfully updated for the selected items.',
82-
);
79+
\Filament\Notifications\Notification::make()
80+
->title('Permissions Updated')
81+
->body('Permissions have been successfully updated for the selected items.')
82+
->success()
83+
->send();
8384
}
8485
}

0 commit comments

Comments
 (0)