diff --git a/app/Conductors/ArticleConductor.php b/app/Conductors/ArticleConductor.php index a1a10ea..7fe68f6 100644 --- a/app/Conductors/ArticleConductor.php +++ b/app/Conductors/ArticleConductor.php @@ -125,7 +125,7 @@ class ArticleConductor extends Conductor public function includeAttachments(Model $model) { return $model->getAttachments()->map(function ($attachment) { - return MediaConductor::includeModel(request(), 'attachments', $attachment->getMedia()); + return MediaConductor::includeModel(request(), 'attachments', $attachment->media); }); } @@ -138,7 +138,7 @@ class ArticleConductor extends Conductor public function includeGallery(Model $model) { return $model->getGallery()->map(function ($item) { - return MediaConductor::includeModel(request(), 'gallery', $item->getMedia()); + return MediaConductor::includeModel(request(), 'gallery', $item->media); }); } diff --git a/app/Conductors/EventConductor.php b/app/Conductors/EventConductor.php index 1f3d442..7558a40 100644 --- a/app/Conductors/EventConductor.php +++ b/app/Conductors/EventConductor.php @@ -110,7 +110,7 @@ class EventConductor extends Conductor return $model->getAttachments()->map(function ($attachment) use ($user) { if ($attachment->private === false || ($user !== null && $user->hasPermission('admin/events') === true)) { - return MediaConductor::includeModel(request(), 'attachments', $attachment->getMedia()); + return MediaConductor::includeModel(request(), 'attachments', $attachment->media); } }); } diff --git a/app/Models/Attachment.php b/app/Models/Attachment.php index 7bf6574..37848f8 100644 --- a/app/Models/Attachment.php +++ b/app/Models/Attachment.php @@ -46,10 +46,41 @@ class Attachment extends Model * * @return null|Media */ - public function getMedia(): ?Media + public function getMediaAttribute(): ?Media { - return Cache::remember("attachment:{$this->id}:media", now()->addDays(28), function () { + $mediaId = '0'; + $media = null; + + if (Cache::has("attachment:{$this->id}:media") === true) { + $mediaId = Cache::get("attachment:{$this->id}:media"); + } else { + $media = $this->media()->first(); + if ($media === null) { + return null; + } + + $mediaId = $media->id; + Cache::put("attachment:{$this->id}:media", $mediaId, now()->addDays(28)); + } + + return Cache::remember("media:{$mediaId}", now()->addDays(28), function () use ($media) { + if ($media !== null) { + return $media; + } + return $this->media()->first(); }); } + + /** + * Set the media for this item. + * + * @param Media $media The media model. + * @return void + */ + public function setMediaAttribute(Media $media): void + { + $this->media()->associate($media)->save(); + Cache::put("attachment:{$this->id}:media", $media->id, now()->addDays(28)); + } } diff --git a/app/Models/Gallery.php b/app/Models/Gallery.php index 855a3ff..c453af8 100644 --- a/app/Models/Gallery.php +++ b/app/Models/Gallery.php @@ -67,7 +67,7 @@ class Gallery extends Model * * @return null|Media The media model. */ - public function getMedia(): ?Media + public function getMediaAttribute(): ?Media { $mediaId = '0'; $media = null; @@ -92,4 +92,16 @@ class Gallery extends Model return $this->media()->first(); }); } + + /** + * Set the media for this item. + * + * @param Media $media The media model. + * @return void + */ + public function setMediaAttribute(Media $media): void + { + $this->media()->associate($media)->save(); + Cache::put("gallery:{$this->id}:media", $media->id, now()->addDays(28)); + } } diff --git a/app/Models/User.php b/app/Models/User.php index d1538ad..ea17138 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -103,9 +103,9 @@ class User extends Authenticatable implements Auditable /** * Get the list of permissions of the user * - * @return Illuminate\Database\Eloquent\Collection + * @return Illuminate\Database\Eloquent\Relations\HasMany */ - public function permissions(): Collection + public function permissions(): HasMany { return $this->hasMany(Permission::class); } @@ -123,6 +123,23 @@ class User extends Authenticatable implements Auditable }); } + /** + * Set the permission attribute + * + * @param array $newPermissions The new permissions to set to the user. + * @return void + */ + public function setPermissionsAttribute(array $newPermissions): void + { + $existingPermissions = $this->permissions->pluck('permission')->toArray(); + + $this->revokePermission(array_diff($this->permissions, $newPermissions)); + $this->givePermission(array_diff($newPermissions, $this->permissions)); + + $cacheKey = "user:{$this->id}:permissions"; + Cache::delete($cacheKey); + } + /** * Test if user has permission * @@ -146,19 +163,14 @@ class User extends Authenticatable implements Auditable $permissions = [$permissions]; } - $permissions = collect($permissions)->map(function ($permission) { + $newPermissions = array_map(function ($permission) { return ['permission' => $permission]; - }); - - $existingPermissions = $this->permissions; - $newPermissions = $permissions->reject(function ($permission) use ($existingPermissions) { - return $existingPermissions->contains('permission', $permission['permission']); - }); + }, array_diff($permissions, $this->permissions)); $cacheKey = "user:{$this->id}:permissions"; Cache::forget($cacheKey); - return $this->permissions()->createMany($newPermissions->toArray()); + return $this->permissions()->createMany($newPermissions); }