Skip to content

Commit

Permalink
add gate evaluation event
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Feb 19, 2021
1 parent e7207b8 commit 0c6f5f7
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
51 changes: 51 additions & 0 deletions src/Illuminate/Auth/Access/Events/GateEvaluated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Illuminate\Auth\Access\Events;

class GateEvaluated
{
/**
* The authenticatable model.
*
* @var \Illuminate\Contracts\Auth\Authenticatable
*/
public $user;

/**
* The ability being evaluated.
*
* @var string
*/
public $ability;

/**
* The result of the evaluation.
*
* @var bool|null
*/
public $result;

/**
* The arguments given during evaluation.
*
* @var array
*/
public $arguments;

/**
* Create a new event instance.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param string $ability
* @param bool|null $result
* @param array $arguments
* @return void
*/
public function __construct($user, $ability, $result, $arguments)
{
$this->user = $user;
$this->ability = $ability;
$this->result = $result;
$this->arguments = $arguments;
}
}
27 changes: 24 additions & 3 deletions src/Illuminate/Auth/Access/Gate.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -86,7 +87,7 @@ class Gate implements GateContract
*/
public function __construct(Container $container, callable $userResolver, array $abilities = [],
array $policies = [], array $beforeCallbacks = [], array $afterCallbacks = [],
callable $guessPolicyNamesUsingCallback = null)
callable $guessPolicyNamesUsingCallback = null)
{
$this->policies = $policies;
$this->container = $container;
Expand Down Expand Up @@ -374,9 +375,11 @@ public function raw($ability, $arguments = [])
// After calling the authorization callback, we will call the "after" callbacks
// that are registered with the Gate, which allows a developer to do logging
// if that is required for this application. Then we'll return the result.
return $this->callAfterCallbacks(
return tap($this->callAfterCallbacks(
$user, $ability, $arguments, $result
);
), function ($result) use ($user, $ability, $arguments) {
$this->dispatchGateEvaluatedEvent($user, $ability, $arguments, $result);
});
}

/**
Expand Down Expand Up @@ -519,6 +522,24 @@ protected function callAfterCallbacks($user, $ability, array $arguments, $result
return $result;
}

/**
* Dispatch a gate evaluation event.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param string $ability
* @param array $arguments
* @param bool $result
* @return void
*/
protected function dispatchGateEvaluatedEvent($user, $ability, array $arguments, $result)
{
if ($this->container->bound(Dispatcher::class)) {
$this->container->make(Dispatcher::class)->dispatch(
new Events\GateEvaluated($user, $ability, $result, $arguments)
);
}
}

/**
* Resolve the callable for the given ability and arguments.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/Integration/Auth/GatePolicyResolutionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Tests\Integration\Auth;

use Illuminate\Auth\Access\Events\GateEvaluated;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Gate;
use Illuminate\Tests\Integration\Auth\Fixtures\AuthenticationTestUser;
use Illuminate\Tests\Integration\Auth\Fixtures\Policies\AuthenticationTestUserPolicy;
Expand All @@ -12,6 +14,15 @@
*/
class GatePolicyResolutionTest extends TestCase
{
public function testGateEvaluationEventIsFired()
{
Event::fake();

Gate::check('foo');

Event::assertDispatched(GateEvaluated::class);
}

public function testPolicyCanBeGuessedUsingClassConventions()
{
$this->assertInstanceOf(
Expand Down

0 comments on commit 0c6f5f7

Please sign in to comment.