Skip to content

Commit 11b9004

Browse files
committed
Merge cmsperms cast into user objects automatically
1 parent 8bf3159 commit 11b9004

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

core/src/Concerns/Benchmarks.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ protected function user(): \Illuminate\Foundation\Auth\User
157157
throw new \RuntimeException( 'User model must extend Illuminate\Foundation\Auth\User' );
158158
}
159159

160-
$user->mergeCasts( ['cmsperms' => 'array'] );
161160
$user->forceFill( [
162161
'name' => 'Benchmark User',
163162
'email' => 'benchmark@example.com',

core/src/CoreServiceProvider.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function boot(): void
1818
$this->publishes( [$basedir . '/config/cms.php' => config_path( 'cms.php' )], 'cms-core-config' );
1919

2020
$this->rateLimiter();
21+
$this->userCasts();
2122
$this->console();
2223

2324
// Set null Scout driver as fallback if none configured
@@ -69,6 +70,32 @@ protected function scout() : void
6970
}
7071
}
7172

73+
protected function userCasts() : void
74+
{
75+
$this->app->booted( function() {
76+
$userClass = config( 'auth.providers.users.model', 'App\\Models\\User' );
77+
78+
if( !$userClass || !class_exists( $userClass ) || !method_exists( $userClass, 'mergeCasts' ) ) {
79+
return;
80+
}
81+
82+
$casts = ['cmsperms' => 'array'];
83+
84+
$userClass::retrieved( function( $model ) use ( $casts ) {
85+
$model->mergeCasts( $casts );
86+
} );
87+
88+
$userClass::saving( function( $model ) use ( $casts ) {
89+
$model->mergeCasts( $casts );
90+
91+
if( is_array( $model->getAttributes()['cmsperms'] ?? null ) ) {
92+
$model->setAttribute( 'cmsperms', $model->getAttributes()['cmsperms'] );
93+
}
94+
} );
95+
} );
96+
}
97+
98+
7299
protected function rateLimiter(): void
73100
{
74101
RateLimiter::for( 'cms-admin', fn( $request ) =>

0 commit comments

Comments
 (0)