Skip to content

Commit

Permalink
Fix #687
Browse files Browse the repository at this point in the history
  • Loading branch information
santigarcor committed Feb 24, 2025
1 parent 27aea0a commit ca3f84b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/Laratrust.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ public function hasPermission(
return false;
}

/**
* Check if the current user has a permission by its name.
* Alias to hasPermission.
*/
public function isAbleTo(
string|array|BackedEnum $permission,
mixed $team = null,
bool $requireAll = false
): bool {
return $this->hasPermission($permission, $team, $requireAll);
}

/**
* Check if the current user has a role or permission by its name.
*
Expand Down
17 changes: 15 additions & 2 deletions tests/LaratrustFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

declare(strict_types=1);

use Mockery as m;
use Laratrust\Laratrust;
use Laratrust\Tests\Models\User;
use Laratrust\Tests\LaratrustTestCase;
use Laratrust\Tests\Models\User;
use Mockery as m;

class LaratrustFacadeTest extends LaratrustTestCase
{
protected $laratrust;

protected $user;

protected function setUp(): void
Expand Down Expand Up @@ -45,6 +46,18 @@ public function testHasPermission()
$this->assertFalse($this->laratrust->hasPermission('any_permission'));
}

public function testIsAbleTo()
{
$this->laratrust->shouldReceive('user')->andReturn($this->user)->twice()->ordered();
$this->laratrust->shouldReceive('user')->andReturn(null)->once()->ordered();
$this->user->shouldReceive('hasPermission')->with('user_can', null, false)->andReturn(true)->once();
$this->user->shouldReceive('hasPermission')->with('user_cannot', null, false)->andReturn(false)->once();

$this->assertTrue($this->laratrust->isAbleTo('user_can'));
$this->assertFalse($this->laratrust->isAbleTo('user_cannot'));
$this->assertFalse($this->laratrust->isAbleTo('any_permission'));
}

public function testAbility()
{
$this->laratrust->shouldReceive('user')->andReturn($this->user)->twice()->ordered();
Expand Down

0 comments on commit ca3f84b

Please sign in to comment.