Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Support arguments in user can/cant tags #11407

Merged
merged 4 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 17 additions & 0 deletions tests/Tags/User/UserTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Tags\User;

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

#[Test]
public function it_renders_user_can_with_aurguments_tag_content()
{
$this->actingAs(User::make()->save());

Gate::define('test gate', function ($user, $value) {
return $value === 'foo';
});

$this->assertEquals('yes', $this->tag('{{ user:can do="test gate" value="foo" }}yes{{ /user:can }}'));
$this->assertEquals('', $this->tag('{{ user:cant do="test gate" value="foo" }}yes{{ /user:cant }}'));

$this->assertEquals('', $this->tag('{{ user:can do="test gate" value="bar" }}yes{{ /user:can }}'));
$this->assertEquals('yes', $this->tag('{{ user:cant do="test gate" value="bar" }}yes{{ /user:cant }}'));
}

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