2
0
Fork 0
mirror of https://github.com/koel/koel synced 2024-12-30 06:23:05 +00:00
koel/app/Rules/AllPlaylistsAreAccessibleBy.php
2024-07-06 17:45:04 +02:00

33 lines
907 B
PHP

<?php
namespace App\Rules;
use App\Facades\License;
use App\Models\User;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Arr;
final class AllPlaylistsAreAccessibleBy implements ValidationRule
{
public function __construct(private readonly User $user)
{
}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$accessiblePlaylists = $this->user->playlists;
if (License::isPlus()) {
$accessiblePlaylists = $accessiblePlaylists->merge($this->user->collaboratedPlaylists);
}
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'
);
}
}
}