Skip to content
Open
Show file tree
Hide file tree
Changes from all 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: 3 additions & 3 deletions src/CP/Navigation/CoreNav.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected function makeContentSection()
Nav::content('Collections')
->route('collections.index')
->icon('collections')
->can('index', Collection::class)
->can('index', [Collection::class, Site::selected()])
->extra([
'breadcrumbs' => [
'create_label' => 'Create Collection',
Expand Down Expand Up @@ -103,7 +103,7 @@ protected function makeContentSection()
Nav::content('Navigation')
->route('navigation.index')
->icon('navigation')
->can('index', NavContract::class)
->can('index', [NavContract::class, Site::selected()])
->extra([
'breadcrumbs' => [
'create_label' => 'Create Navigation',
Expand Down Expand Up @@ -181,7 +181,7 @@ protected function makeContentSection()
Nav::content('Globals')
->route('globals.index')
->icon('globals')
->can('index', GlobalSet::class)
->can('index', [GlobalSet::class, Site::selected()])
->extra([
'breadcrumbs' => [
'create_label' => 'Create Global Set',
Expand Down
10 changes: 6 additions & 4 deletions src/Policies/CollectionPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Statamic\Facades\Collection;
use Statamic\Facades\User;
use Statamic\Sites\Site;

class CollectionPolicy
{
Expand All @@ -18,17 +19,18 @@ public function before($user)
}
}

public function index($user)
public function index($user, ?Site $site = null)
{
$user = User::fromUser($user);

if ($this->create($user)) {
return true;
}

return ! Collection::all()->filter(function ($collection) use ($user) {
return $this->view($user, $collection);
})->isEmpty();
return Collection::all()
->filter(fn ($collection) => $this->view($user, $collection))
->filter(fn ($collection) => ! $site || $collection->sites()->contains($site->handle()))
->isNotEmpty();
}

public function create($user)
Expand Down
10 changes: 6 additions & 4 deletions src/Policies/GlobalSetPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Statamic\Facades\GlobalSet;
use Statamic\Facades\User;
use Statamic\Sites\Site;

class GlobalSetPolicy
{
Expand All @@ -18,17 +19,18 @@ public function before($user)
}
}

public function index($user)
public function index($user, ?Site $site = null)
{
$user = User::fromUser($user);

if ($this->create($user)) {
return true;
}

return ! GlobalSet::all()->filter(function ($set) use ($user) {
return $this->view($user, $set);
})->isEmpty();
return GlobalSet::all()
->filter(fn ($set) => $this->view($user, $set))
->filter(fn ($set) => ! $site || $set->existsIn($site->handle()))
->isNotEmpty();
}

public function create($user)
Expand Down
10 changes: 6 additions & 4 deletions src/Policies/NavPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Statamic\Facades\Nav;
use Statamic\Facades\User;
use Statamic\Sites\Site;

class NavPolicy
{
Expand All @@ -18,17 +19,18 @@ public function before($user)
}
}

public function index($user)
public function index($user, ?Site $site = null)
{
$user = User::fromUser($user);

if ($this->create($user)) {
return true;
}

return ! Nav::all()->filter(function ($nav) use ($user) {
return $this->view($user, $nav);
})->isEmpty();
return Nav::all()
->filter(fn ($nav) => $this->view($user, $nav))
->filter(fn ($nav) => ! $site || $nav->existsIn($site->handle()))
->isNotEmpty();
}

public function create($user)
Expand Down
78 changes: 78 additions & 0 deletions tests/CP/Navigation/CoreNavTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,84 @@ public function it_doesnt_build_globals_children_from_sites_that_the_user_is_not
$this->assertEqualsCanonicalizing($expected, $actual);
}

#[Test]
public function it_doesnt_build_collections_item_when_the_user_cant_view_any_collections_in_the_selected_site()
{
$this->setSites([
'en' => ['url' => '/', 'locale' => 'en_US', 'name' => 'English'],
'fr' => ['url' => '/', 'locale' => 'fr_FR', 'name' => 'French'],
]);

Facades\Collection::make('only_english')->sites(['en'])->save();

$this->setTestRoles(['test' => [
'access cp',
'view only_english entries',
'access en site',
'access fr site',
]]);

$this->actingAs(tap(User::make()->assignRole('test'))->save());

Facades\Site::setSelected('en');
$this->assertContains('Collections', $this->build()->get('Content', collect())->map->display()->all());

Facades\Site::setSelected('fr');
$this->assertNotContains('Collections', $this->build()->get('Content', collect())->map->display()->all());
}

#[Test]
public function it_doesnt_build_navigation_item_when_the_user_cant_view_any_navs_in_the_selected_site()
{
$this->setSites([
'en' => ['url' => '/', 'locale' => 'en_US', 'name' => 'English'],
'fr' => ['url' => '/', 'locale' => 'fr_FR', 'name' => 'French'],
]);

tap(Facades\Nav::make()->handle('only_english'))->save()->makeTree('en')->save();

$this->setTestRoles(['test' => [
'access cp',
'view only_english nav',
'access en site',
'access fr site',
]]);

$this->actingAs(tap(User::make()->assignRole('test'))->save());

Facades\Site::setSelected('en');
$this->assertContains('Navigation', $this->build()->get('Content', collect())->map->display()->all());

Facades\Site::setSelected('fr');
$this->assertNotContains('Navigation', $this->build()->get('Content', collect())->map->display()->all());
}

#[Test]
public function it_doesnt_build_globals_item_when_the_user_cant_view_any_globals_in_the_selected_site()
{
$this->setSites([
'en' => ['url' => '/', 'locale' => 'en_US', 'name' => 'English'],
'fr' => ['url' => '/', 'locale' => 'fr_FR', 'name' => 'French'],
]);

Facades\GlobalSet::make('only_english')->sites(['en'])->save();

$this->setTestRoles(['test' => [
'access cp',
'edit only_english globals',
'access en site',
'access fr site',
]]);

$this->actingAs(tap(User::make()->assignRole('test'))->save());

Facades\Site::setSelected('en');
$this->assertContains('Globals', $this->build()->get('Content', collect())->map->display()->all());

Facades\Site::setSelected('fr');
$this->assertNotContains('Globals', $this->build()->get('Content', collect())->map->display()->all());
}

#[Test]
public function it_builds_the_nav_when_a_form_has_no_title()
{
Expand Down
18 changes: 18 additions & 0 deletions tests/Policies/CollectionPolicyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\Attributes\Test;
use Statamic\Contracts\Entries\Collection as CollectionContract;
use Statamic\Facades\Collection;
use Statamic\Facades\Site;

class CollectionPolicyTest extends PolicyTestCase
{
Expand Down Expand Up @@ -45,6 +46,23 @@ public function index_is_allowed_if_any_collection_is_viewable_with_site_permiss
$this->assertFalse($userWithDePermission->can('index', CollectionContract::class));
}

#[Test]
public function index_is_allowed_for_a_site_if_any_collection_is_viewable_in_that_site()
{
$this->withSites(['en', 'fr']);

$user = $this->userWithPermissions([
'view test entries',
'access en site',
'access fr site',
]);

Collection::make('test')->sites(['en'])->save();

$this->assertTrue($user->can('index', [CollectionContract::class, Site::get('en')]));
$this->assertFalse($user->can('index', [CollectionContract::class, Site::get('fr')]));
}

#[Test]
public function collections_are_viewable_with_view_permissions()
{
Expand Down
19 changes: 19 additions & 0 deletions tests/Policies/GlobalSetPolicyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\Attributes\Test;
use Statamic\Contracts\Globals\GlobalSet;
use Statamic\Facades\GlobalSet as GlobalSets;
use Statamic\Facades\Site;

class GlobalSetPolicyTest extends PolicyTestCase
{
Expand Down Expand Up @@ -49,6 +50,24 @@ public function index_is_allowed_if_any_set_is_viewable_with_site_permissions()
$this->assertFalse($userWithDePermission->can('index', GlobalSet::class));
}

#[Test]
public function index_is_allowed_for_a_site_if_any_set_is_viewable_in_that_site()
{
$this->withSites(['en', 'fr']);

$user = $this->userWithPermissions([
'edit test globals',
'access en site',
'access fr site',
]);

$global = GlobalSets::make('test')->sites(['en' => null])->save();
$global->in('en')->save();

$this->assertTrue($user->can('index', [GlobalSet::class, Site::get('en')]));
$this->assertFalse($user->can('index', [GlobalSet::class, Site::get('fr')]));
}

#[Test]
public function globals_are_viewable_with_edit_permissions()
{
Expand Down
19 changes: 19 additions & 0 deletions tests/Policies/NavPolicyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\Attributes\Test;
use Statamic\Contracts\Structures\Nav as NavContract;
use Statamic\Facades\Nav;
use Statamic\Facades\Site;

class NavPolicyTest extends PolicyTestCase
{
Expand Down Expand Up @@ -47,6 +48,24 @@ public function index_is_allowed_if_any_nav_is_viewable_with_site_permissions()
$this->assertFalse($userWithDePermission->can('index', NavContract::class));
}

#[Test]
public function index_is_allowed_for_a_site_if_any_nav_is_viewable_in_that_site()
{
$this->withSites(['en', 'fr']);

$user = $this->userWithPermissions([
'view test nav',
'access en site',
'access fr site',
]);

$nav = tap(Nav::make('test'))->save();
$nav->makeTree('en')->save();

$this->assertTrue($user->can('index', [NavContract::class, Site::get('en')]));
$this->assertFalse($user->can('index', [NavContract::class, Site::get('fr')]));
}

#[Test]
public function navs_are_viewable_with_view_permissions()
{
Expand Down
Loading