Skip to content

Commit

Permalink
Allow passing names/ids/models directly in an abilities map
Browse files Browse the repository at this point in the history
Fixes #197
  • Loading branch information
JosephSilber committed Sep 1, 2017
1 parent cc73893 commit d6b4073
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/Conductors/Concerns/FindsAndCreatesAbilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ protected function getAbilityIds($abilities, $model, array $attributes)
*/
protected function getAbilityIdsFromMap(array $map, array $attributes)
{
return (new Collection($map))
->map(function ($entity, $ability) use ($attributes) {
return $this->getAbilityIds($ability, $entity, $attributes);
})->flatten(1)->all();
list($map, $list) = Helpers::partition($map, function ($value, $key) {
return ! is_int($key);
});

return $map->map(function ($entity, $ability) use ($attributes) {
return $this->getAbilityIds($ability, $entity, $attributes);
})->collapse()->merge($this->getAbilityIdsFromArray($list, $attributes))->all();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static function partition($items, callable $callback)
$partitions = [new Collection, new Collection];

foreach ($items as $key => $item) {
$partitions[(int) ! $callback($item)][$key] = $item;
$partitions[(int) ! $callback($item, $key)][$key] = $item;
}

return new Collection($partitions);
Expand Down
3 changes: 3 additions & 0 deletions tests/MultipleAbilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function test_allowing_multiple_abilities_via_a_map()
'delete' => $user1,
'view' => Account::class,
'update' => $account1,
'access-dashboard',
]);

$this->assertTrue($bouncer->allows('edit', User::class));
Expand All @@ -90,6 +91,8 @@ public function test_allowing_multiple_abilities_via_a_map()
$this->assertTrue($bouncer->denies('update', Account::class));
$this->assertTrue($bouncer->allows('update', $account1));
$this->assertTrue($bouncer->denies('update', $account2));

$this->assertTrue($bouncer->allows('access-dashboard'));
}

public function test_disallowing_multiple_abilties()
Expand Down

0 comments on commit d6b4073

Please sign in to comment.