Skip to content

Instantly share code, notes, and snippets.

@jenky
Created November 28, 2018 04:20
Show Gist options
  • Save jenky/bc03f3a421c8d61267122713e6c9ece3 to your computer and use it in GitHub Desktop.
Save jenky/bc03f3a421c8d61267122713e6c9ece3 to your computer and use it in GitHub Desktop.
Authorize multiple guard in policy
<?php
namespace App\Policies;
use Illuminate\Foundation\Auth\User;
use Illuminate\Support\Str;
trait AuthorizesMultipleGuard
{
protected function authorizeFor(User $user, ...$args)
{
[$one, $two, $three] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
$ability = $two['function'];
$method = 'authorize'.Str::studly($ability).'For'.Str::studly(class_basename($user));
if (method_exists($this, $method)) {
return call_user_func_array([$this, $method], array_prepend($args, $user));
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment