koel/app/Rules/AllPlaylistsAreAccessibleBy.php

34 lines
907 B
PHP
Raw Normal View History

<?php
namespace App\Rules;
use App\Facades\License;
use App\Models\User;
2024-05-19 05:49:42 +00:00
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Arr;
2024-05-19 05:49:42 +00:00
final class AllPlaylistsAreAccessibleBy implements ValidationRule
{
2024-04-18 14:36:28 +00:00
public function __construct(private readonly User $user)
{
}
2024-05-19 05:49:42 +00:00
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$accessiblePlaylists = $this->user->playlists;
if (License::isPlus()) {
$accessiblePlaylists = $accessiblePlaylists->merge($this->user->collaboratedPlaylists);
}
2024-05-19 05:49:42 +00:00
if (array_diff(Arr::wrap($value), $accessiblePlaylists->pluck('id')->toArray())) {
$fail(
License::isPlus()
? 'Not all playlists are accessible by the user'
: 'Not all playlists belong to the user'
);
}
}
}