Skip to content

Commit ca3f84b

Browse files
committed
Fix #687
1 parent 27aea0a commit ca3f84b

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/Laratrust.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ public function hasPermission(
5151
return false;
5252
}
5353

54+
/**
55+
* Check if the current user has a permission by its name.
56+
* Alias to hasPermission.
57+
*/
58+
public function isAbleTo(
59+
string|array|BackedEnum $permission,
60+
mixed $team = null,
61+
bool $requireAll = false
62+
): bool {
63+
return $this->hasPermission($permission, $team, $requireAll);
64+
}
65+
5466
/**
5567
* Check if the current user has a role or permission by its name.
5668
*

tests/LaratrustFacadeTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
declare(strict_types=1);
44

5-
use Mockery as m;
65
use Laratrust\Laratrust;
7-
use Laratrust\Tests\Models\User;
86
use Laratrust\Tests\LaratrustTestCase;
7+
use Laratrust\Tests\Models\User;
8+
use Mockery as m;
99

1010
class LaratrustFacadeTest extends LaratrustTestCase
1111
{
1212
protected $laratrust;
13+
1314
protected $user;
1415

1516
protected function setUp(): void
@@ -45,6 +46,18 @@ public function testHasPermission()
4546
$this->assertFalse($this->laratrust->hasPermission('any_permission'));
4647
}
4748

49+
public function testIsAbleTo()
50+
{
51+
$this->laratrust->shouldReceive('user')->andReturn($this->user)->twice()->ordered();
52+
$this->laratrust->shouldReceive('user')->andReturn(null)->once()->ordered();
53+
$this->user->shouldReceive('hasPermission')->with('user_can', null, false)->andReturn(true)->once();
54+
$this->user->shouldReceive('hasPermission')->with('user_cannot', null, false)->andReturn(false)->once();
55+
56+
$this->assertTrue($this->laratrust->isAbleTo('user_can'));
57+
$this->assertFalse($this->laratrust->isAbleTo('user_cannot'));
58+
$this->assertFalse($this->laratrust->isAbleTo('any_permission'));
59+
}
60+
4861
public function testAbility()
4962
{
5063
$this->laratrust->shouldReceive('user')->andReturn($this->user)->twice()->ordered();

0 commit comments

Comments
 (0)