Skip to content

Instantly share code, notes, and snippets.

@jenky
Last active September 6, 2018 11:47
Show Gist options
  • Save jenky/216f0bf75641cd062bf52d8a6c4f1da7 to your computer and use it in GitHub Desktop.
Save jenky/216f0bf75641cd062bf52d8a6c4f1da7 to your computer and use it in GitHub Desktop.
Authorizes 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)
{
list($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