Skip to content

Instantly share code, notes, and snippets.

@jesseleite
Last active March 5, 2016 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesseleite/0899f8642d687bdcd17b to your computer and use it in GitHub Desktop.
Save jesseleite/0899f8642d687bdcd17b to your computer and use it in GitHub Desktop.
Authorize without authenticated user.
<?php
namespace App\Helpers;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Auth\Access\AuthorizationException;
trait AuthorizesRequestsWithoutUser
{
/**
* Authorize a given action without an authenticated user.
*
* @param mixed $ability
* @param mixed|array $arguments
* @return \Illuminate\Auth\Access\Response
*
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function authorizeWithoutUser($ability, $arguments = [])
{
list($ability, $arguments) = $this->parseAbilityAndArguments($ability, $arguments);
$policy = app(Gate::class)->getPolicyFor($arguments[0]);
if (! call_user_func_array([$policy, $ability], $arguments)) {
throw new AuthorizationException('This action is unauthorized.');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment