Skip to content

Commit

Permalink
Ensure {{ user:is }} and {{ user:isnt }} work with roles fieldtype
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell committed Nov 21, 2023
1 parent 2dbbb0f commit f57d12c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/Auth/UserTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Statamic\Auth;

use Illuminate\Support\Collection;
use Statamic\Facades\URL;
use Statamic\Facades\User;
use Statamic\Fields\Field;
Expand Down Expand Up @@ -505,7 +506,11 @@ public function is()
return $this->parser ? null : false;
}

$roles = Arr::wrap($this->params->explode(['role', 'roles']));
$roles = $this->params->get(['role', 'roles']);

if (! $roles instanceof Collection) {
$roles = Arr::wrap($this->params->explode(['role', 'roles']));
}

foreach ($roles as $role) {
if ($user->hasRole($role)) {
Expand All @@ -529,7 +534,11 @@ public function isnt()
return $this->parser ? $this->parse() : true;
}

$roles = Arr::wrap($this->params->explode(['roles', 'role']));
$roles = $this->params->get(['role', 'roles']);

if (! $roles instanceof Collection) {
$roles = Arr::wrap($this->params->explode(['roles', 'role']));
}

$is = false;

Expand Down
9 changes: 7 additions & 2 deletions tests/Tags/User/UserTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Exceptions\HttpResponseException;
use Statamic\Facades\Parse;
use Statamic\Facades\Role;
use Statamic\Facades\User;
use Tests\FakesRoles;
use Tests\FakesUserGroups;
Expand All @@ -16,9 +17,9 @@ class UserTagsTest extends TestCase
FakesUserGroups,
PreventSavingStacheItemsToDisk;

private function tag($tag)
private function tag($tag, $params = [])
{
return Parse::template($tag, []);
return Parse::template($tag, $params);
}

/** @test */
Expand Down Expand Up @@ -68,6 +69,10 @@ public function it_renders_user_is_tag_content()
// Test if user is assigned any of these roles
$this->assertEquals('yes', $this->tag('{{ user:is role="webmaster|admin" }}yes{{ /user:is }}'));
$this->assertEquals('', $this->tag('{{ user:isnt role="webmaster|admin" }}yes{{ /user:isnt }}'));

// test if it handles the value of a user_roles tag
$this->assertEquals('yes', $this->tag('{{ user:is :roles="roles" }}yes{{ /user:is }}', ['roles' => Role::all()]));
$this->assertEquals('', $this->tag('{{ user:isnt :roles="roles" }}yes{{ /user:isnt }}', ['roles' => Role::all()]));
}

/** @test */
Expand Down

0 comments on commit f57d12c

Please sign in to comment.