Skip to content

Commit

Permalink
Allow giving the gate permissions teams and require all parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
santigarcor committed Apr 17, 2023
1 parent 194a8f5 commit a745962
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
12 changes: 10 additions & 2 deletions src/LaratrustServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Contracts\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
Expand Down Expand Up @@ -162,9 +163,16 @@ protected function registerPermissionsToGate()
return;
}

app(Gate::class)->before(function (Authorizable $user, mixed $ability) {
app(Gate::class)->before(function (Authorizable $user, mixed $ability, $attributes) {
if (method_exists($user, 'hasPermission')) {
return $user->hasPermission($ability) ?: null;
$team = Collection::make($attributes)
->filter(fn ($attr) => ! is_bool($attr))
->first();
$requireAll = Collection::make($attributes)
->filter(fn ($attr) => is_bool($attr))
->first() || false;

return $user->hasPermission($ability, $team, $requireAll) ?: null;
}
});
}
Expand Down
26 changes: 0 additions & 26 deletions tests/Checkers/User/CanAbilityDefaultCheckerTest.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
use Laratrust\Tests\LaratrustTestCase;
use Laratrust\Tests\Models\Permission;
use Laratrust\Tests\Models\Role;
use Laratrust\Tests\Models\Team;
use Laratrust\Tests\Models\User;

abstract class CanCheckerTestCase extends LaratrustTestCase
class CanCheckerTest extends LaratrustTestCase
{
protected $user;

Expand All @@ -19,7 +20,7 @@ protected function setUp(): void
parent::setUp();

$this->migrate();

Team::create(['name' => 'team_a']);
$permissionA = Permission::create(['name' => 'permission_a']);
$permissionB = Permission::create(['name' => 'permission_b']);

Expand All @@ -31,7 +32,16 @@ protected function setUp(): void
$this->user->addRole($role);
}

protected function canShouldReturnBooleanAssertions()
protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);

$app['config']->set('laratrust.teams.enabled', true);
$app['config']->set('laratrust.checker', 'default');
$app['config']->set('laratrust.permissions_as_gates', true);
}

public function testCanShouldReturnBoolean()
{
/*
|------------------------------------------------------------
Expand All @@ -41,10 +51,14 @@ protected function canShouldReturnBooleanAssertions()
// Case: User has everything.
$this->assertTrue(
$this->user->can(
[EnumsPermission::PERM_A, 'permission_b']
[EnumsPermission::PERM_A->value, 'permission_b']
)
);

$this->assertFalse(
$this->user->can(EnumsPermission::PERM_A->value, ['team_a'])
);

// Case: User lacks a permission.
if (method_exists($this->user, 'canAny')) {
$this->assertTrue(
Expand Down

0 comments on commit a745962

Please sign in to comment.