Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

koel/koel

16858 stars
3 code files
View koel/koel on GitHub

app/Policies/PlaylistPolicy.php

Open in GitHub
use App\Models\Playlist;
use App\Models\User;
 
class PlaylistPolicy
{
public function owner(User $user, Playlist $playlist): bool
{
return $user->id === $playlist->user_id;
}
}

app/Providers/AuthServiceProvider.php

Open in GitHub
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
 
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
Playlist::class => PlaylistPolicy::class,
//
];
//
}

app/Http/Controllers/API/PlaylistController.php

Open in GitHub
use App\Models\Playlist;
use Illuminate\Http\Request;
use App\Http\Requests\API\PlaylistSyncRequest;
 
class PlaylistController extends Controller
{
public function update(Request $request, Playlist $playlist)
{
$this->authorize('owner', $playlist);
 
$playlist->update($request->only('name', 'rules'));
 
return response()->json($playlist);
}
 
public function sync(PlaylistSyncRequest $request, Playlist $playlist)
{
$this->authorize('owner', $playlist);
 
abort_if($playlist->is_smart, 403, 'A smart playlist\'s content cannot be updated manually.');
 
$playlist->songs()->sync((array) $request->songs);
 
return response()->json();
}
 
public function getSongs(Playlist $playlist)
{
$this->authorize('owner', $playlist);
 
return response()->json(
$playlist->is_smart
? $this->smartPlaylistService->getSongs($playlist)->pluck('id')
: $playlist->songs->pluck('id')
);
}
 
public function destroy(Playlist $playlist)
{
$this->authorize('owner', $playlist);
 
$playlist->delete();
 
return response()->json();
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.