Skip to content

Commit

Permalink
[5.x] Support arguments in user can/cant tags (#11407)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
jacksleight and jasonvarga authored Feb 18, 2025
1 parent d135c55 commit 40d0fb4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Auth/UserTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,10 @@ public function can()
}

$permissions = Arr::wrap($this->params->explode(['permission', 'do']));
$arguments = $this->params->except(['permission', 'do'])->all();

foreach ($permissions as $permission) {
if ($user->can($permission)) {
if ($user->can($permission, $arguments)) {
return $this->parser ? $this->parse() : true;
}
}
Expand All @@ -477,11 +478,12 @@ public function cant()
}

$permissions = Arr::wrap($this->params->explode(['permission', 'do']));
$arguments = $this->params->except(['permission', 'do'])->all();

$can = false;

foreach ($permissions as $permission) {
if ($user->can($permission)) {
if ($user->can($permission, $arguments)) {
$can = true;
break;
}
Expand Down
33 changes: 33 additions & 0 deletions tests/Tags/User/UserTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Tests\Tags\User;

use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\Gate;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Parse;
use Statamic\Facades\Role;
Expand Down Expand Up @@ -52,6 +54,37 @@ public function it_renders_user_can_tag_content()
$this->assertEquals('', $this->tag('{{ user:cant do="access cp|configure collections" }}yes{{ /user:cant }}'));
}

#[DataProvider('userCanProvider')]
#[Test]
public function it_renders_user_can_with_arguments_tag_content($tag, $params, $expectedOne, $expectedTwo, $expectedOutput)
{
$this->actingAs(User::make()->save());

Gate::define('test gate', function ($user, $one, $two) use ($expectedTwo, $expectedOne) {
$this->assertEquals($expectedOne, $one);
$this->assertEquals($expectedTwo, $two);

return $one === 'alfa';
});

$this->assertEquals($expectedOutput, $this->tag('{{ user:'.$tag.' do="test gate" '.$params.' }}yes{{ /user:'.$tag.' }}'));
}

public static function userCanProvider()
{
return [
['can', 'one="alfa" two="bravo"', 'alfa', 'bravo', 'yes'],
['can', 'two="bravo" one="alfa"', 'alfa', 'bravo', 'yes'],
['can', 'one="bravo" two="alfa"', 'bravo', 'alfa', ''],
['can', 'two="alfa" one="bravo"', 'bravo', 'alfa', ''],

['cant', 'one="alfa" two="bravo"', 'alfa', 'bravo', ''],
['cant', 'two="bravo" one="alfa"', 'alfa', 'bravo', ''],
['cant', 'one="bravo" two="alfa"', 'bravo', 'alfa', 'yes'],
['cant', 'two="alfa" one="bravo"', 'bravo', 'alfa', 'yes'],
];
}

#[Test]
public function it_renders_user_is_tag_content()
{
Expand Down

0 comments on commit 40d0fb4

Please sign in to comment.