Skip to content

Commit 7542dd3

Browse files
committed
Fix: Custom authorization serialization issue (v1.1.3)
1 parent 5b07e54 commit 7542dd3

File tree

4 files changed

+111
-3
lines changed

4 files changed

+111
-3
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ All notable changes to `filament-activity-log` will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.1.3] - 2025-12-15
9+
10+
### Fixed
11+
12+
- **Custom Authorization Serialization** - Fixed `Your configuration files could not be serialized` error when using `custom_authorization` with config caching.
13+
- Added support for Class-Based Authorization (`checkCustomAuthorization` in `ActivityPolicy`).
14+
- Updated documentation with examples and troubleshooting steps.
15+
816
## [1.1.1] - 2025-12-12
917

1018
### Fixed

CONFIGURATION.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,34 @@ Use the `custom_authorization` callback to define your own logic:
154154
],
155155
```
156156

157+
#### Example 5: Class-Based Authorization (Recommended for Config Caching)
158+
159+
If you cache your configuration (`php artisan config:cache`), you cannot use Closures. Instead, use an invokable class:
160+
161+
**config/filament-activity-log.php:**
162+
163+
```php
164+
'permissions' => [
165+
'custom_authorization' => \App\Security\ActivityLogAuthorization::class,
166+
],
167+
```
168+
169+
**app/Security/ActivityLogAuthorization.php:**
170+
171+
```php
172+
namespace App\Security;
173+
174+
use App\Models\User;
175+
176+
class ActivityLogAuthorization
177+
{
178+
public function __invoke(User $user): bool
179+
{
180+
return $user->id === 1;
181+
}
182+
}
183+
```
184+
157185
**Important Notes:**
158186

159187
- `custom_authorization` takes **precedence** over the `enabled` setting
@@ -304,6 +332,16 @@ And assign them through Filament Shield's UI.
304332
},
305333
```
306334

335+
### Issue: Configuration serialization error
336+
337+
**Error:** `Your configuration files could not be serialized because the value at "filament-activity-log.permissions.custom_authorization" is non-serializable`
338+
339+
**Solution:**
340+
341+
You are likely running `php artisan config:cache` while using a Closure in your config file. Closures cannot be serialized.
342+
343+
To fix this, switch to **Class-Based Authorization** (see Example 5 above) or remove the config cache if not needed (`php artisan config:clear`).
344+
307345
---
308346

309347
## Best Practices

config/filament-activity-log.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@
208208
*
209209
* Example: fn($user) => $user->id === 1
210210
* Example: fn($user) => $user->hasRole('super_admin')
211+
* Example: 'App\Support\ActivityLogAuthorization' (class with __invoke method)
211212
*/
212213
'custom_authorization' => null,
213214

src/Policies/ActivityPolicy.php

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,31 @@ class ActivityPolicy
1919
{
2020
use HandlesAuthorization;
2121

22+
/**
23+
* Check for custom authorization.
24+
*
25+
* @param User $user The authenticated user
26+
* @return bool|null Boolean result if custom auth handles it, null otherwise
27+
*/
28+
protected function checkCustomAuthorization(User $user): ?bool
29+
{
30+
$customAuthorization = config('filament-activity-log.permissions.custom_authorization');
31+
32+
if (is_callable($customAuthorization)) {
33+
return $customAuthorization($user);
34+
}
35+
36+
if (is_string($customAuthorization) && class_exists($customAuthorization)) {
37+
$instance = app($customAuthorization);
38+
39+
if (is_callable($instance)) {
40+
return $instance($user);
41+
}
42+
}
43+
44+
return null;
45+
}
46+
2247
/**
2348
* Determine whether the user can view any activities.
2449
*
@@ -31,9 +56,9 @@ class ActivityPolicy
3156
public function viewAny(User $user): bool
3257
{
3358
// Check for custom authorization callback first
34-
$customCallback = config('filament-activity-log.permissions.custom_authorization');
35-
if ($customCallback && is_callable($customCallback)) {
36-
return $customCallback($user);
59+
$result = $this->checkCustomAuthorization($user);
60+
if ($result !== null) {
61+
return $result;
3762
}
3863

3964
if (! config('filament-activity-log.permissions.enabled', false)) {
@@ -57,6 +82,12 @@ public function viewAny(User $user): bool
5782
*/
5883
public function view(User $user, Activity $activity): bool
5984
{
85+
// Check for custom authorization callback first
86+
$result = $this->checkCustomAuthorization($user);
87+
if ($result !== null) {
88+
return $result;
89+
}
90+
6091
if (! config('filament-activity-log.permissions.enabled', false)) {
6192
return true;
6293
}
@@ -77,6 +108,12 @@ public function view(User $user, Activity $activity): bool
77108
*/
78109
public function create(User $user): bool
79110
{
111+
// Check for custom authorization callback first
112+
$result = $this->checkCustomAuthorization($user);
113+
if ($result !== null) {
114+
return $result;
115+
}
116+
80117
if (! config('filament-activity-log.permissions.enabled', false)) {
81118
return false;
82119
}
@@ -98,6 +135,12 @@ public function create(User $user): bool
98135
*/
99136
public function update(User $user, Activity $activity): bool
100137
{
138+
// Check for custom authorization callback first
139+
$result = $this->checkCustomAuthorization($user);
140+
if ($result !== null) {
141+
return $result;
142+
}
143+
101144
if (! config('filament-activity-log.permissions.enabled', false)) {
102145
return false;
103146
}
@@ -119,6 +162,12 @@ public function update(User $user, Activity $activity): bool
119162
*/
120163
public function delete(User $user, Activity $activity): bool
121164
{
165+
// Check for custom authorization callback first
166+
$result = $this->checkCustomAuthorization($user);
167+
if ($result !== null) {
168+
return $result;
169+
}
170+
122171
if (! config('filament-activity-log.permissions.enabled', false)) {
123172
return false;
124173
}
@@ -140,6 +189,12 @@ public function delete(User $user, Activity $activity): bool
140189
*/
141190
public function restore(User $user, Activity $activity): bool
142191
{
192+
// Check for custom authorization callback first
193+
$result = $this->checkCustomAuthorization($user);
194+
if ($result !== null) {
195+
return $result;
196+
}
197+
143198
if (! config('filament-activity-log.permissions.enabled', false)) {
144199
return false;
145200
}
@@ -161,6 +216,12 @@ public function restore(User $user, Activity $activity): bool
161216
*/
162217
public function forceDelete(User $user, Activity $activity): bool
163218
{
219+
// Check for custom authorization callback first
220+
$result = $this->checkCustomAuthorization($user);
221+
if ($result !== null) {
222+
return $result;
223+
}
224+
164225
if (! config('filament-activity-log.permissions.enabled', false)) {
165226
return false;
166227
}

0 commit comments

Comments
 (0)