Skip to content

Commit

Permalink
Keep support for 5.1 where arguments were assed in separately
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSilber committed Feb 21, 2016
1 parent 275b13d commit 9a2439e
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/Clipboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ class Clipboard
*/
public function registerAt(Gate $gate)
{
$gate->before(function ($user, $ability, array $arguments) {
if (count($arguments) > 1) {
$gate->before(function ($user, $ability, array $arguments, $additional = null) {
list($model, $additional) = $this->parseGateArguments($arguments, $additional);

if ( ! is_null($additional)) {
return;
}

$model = head($arguments) ?: null;

if ($id = $this->checkGetId($user, $ability, $model)) {
return $this->allow('Bouncer granted permission via ability #'.$id);
}
Expand All @@ -44,6 +44,35 @@ public function registerAt(Gate $gate)
});
}

/**
* Parse the arguments we got from the gate.
*
* @param mixed $arguments
* @param mixed $additional
* @return array
*/
protected function parseGateArguments($arguments, $additional)
{
// The way arguments are passed into the gate's before callback has changed in Laravel
// in the middle of the 5.2 release. Before, arguments were spread out. Now they're
// all supplied in a single array instead. We will normalize it into two values.
if ( ! is_null($additional)) {
return [$arguments, $additional];
}

if (is_array($arguments)) {
return [
isset($arguments[0]) ? $arguments[0] : null,
isset($arguments[1]) ? $arguments[1] : null,
];
}

return [
head($arguments) ?: null,
null
];
}

/**
* Set whether the bouncer is the exclusive authority on gate access.
*
Expand Down

0 comments on commit 9a2439e

Please sign in to comment.