diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 439abe5c4..e3f775aef 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -3,6 +3,7 @@ - rfe #2123: Layouts related for export purposes - fixed #12125: Recaptcha API url - rfe #12127: AssetProperty macro + - fixed #12129: AdminBar calls canAdd as an object method 7.10.15 - fixed #12117: Thingy - www_searchViaAjax broken diff --git a/lib/WebGUI/Asset.pm b/lib/WebGUI/Asset.pm index d3274617c..96d52b164 100644 --- a/lib/WebGUI/Asset.pm +++ b/lib/WebGUI/Asset.pm @@ -226,6 +226,10 @@ The "turn admin on" group which is group id 12. sub canAdd { my $className = shift; + + # just in case we get called as object method + $className = $className->get('className') if blessed $className; + my $session = shift; my $userId = shift || $session->user->userId; my $user = WebGUI::User->new($session, $userId); diff --git a/lib/WebGUI/Macro/AdminBar.pm b/lib/WebGUI/Macro/AdminBar.pm index 7408a0a14..a19d80017 100644 --- a/lib/WebGUI/Macro/AdminBar.pm +++ b/lib/WebGUI/Macro/AdminBar.pm @@ -160,12 +160,13 @@ sub process { # prototypes foreach my $prototype (@{ $session->asset->getPrototypeList }) { - next unless ($prototype->canView && $prototype->canAdd($session) && $prototype->getUiLevel <= $userUiLevel); + my $class = $prototype->get('className'); + next unless ($prototype->canView && $class->canAdd($session) && $prototype->getUiLevel <= $userUiLevel); $categories{prototypes}{items} ||= []; push @{$categories{prototypes}{items}}, { title => $prototype->getTitle, url => $asset->getUrl( - "func=add;class=".$prototype->get('className').";prototype=".$prototype->getId.$proceed + "func=add;class=$class;prototype=".$prototype->getId.$proceed ), icon => $prototype->getIcon(1), }; diff --git a/t/Asset/Asset.t b/t/Asset/Asset.t index fee63d6c2..f232fd5cb 100644 --- a/t/Asset/Asset.t +++ b/t/Asset/Asset.t @@ -23,6 +23,7 @@ use WebGUI::Asset::Sku; use WebGUI::Asset::Sku::Product; use WebGUI::AssetVersioning; use WebGUI::VersionTag; +use Monkey::Patch; use Test::More; use Test::Deep; @@ -172,7 +173,7 @@ sub definition { package main; -plan tests => 137 +plan tests => 138 + scalar(@fixIdTests) + scalar(@fixTitleTests) + 2*scalar(@getTitleTests) #same tests used for getTitle and getMenuTitle @@ -1212,4 +1213,25 @@ sub getTitleTests { ); } +subtest 'canAdd tolerates being called as an object method', sub { + my $class = 'WebGUI::Asset::Snippet'; + my $snip = $tempNode->addChild({className => $class}); + # Make a test user who's just in Turn Admin On + my $u = WebGUI::User->create($session); + WebGUI::Test->addToCleanup($u); + $u->addToGroups(['12']); + $session->user({ user => $u }); + + # default addGroup is Turn Admin On + ok $class->canAdd($session), 'can add when called as a class method'; + ok $snip->canAdd($session), '...or an object method'; + + my $key = "assets/$class/addGroup"; + WebGUI::Test->originalConfig($key); + $session->config->set($key, 3); + + # now only admins can add snippets, so canAdd should return false + ok !$class->canAdd($session), 'Cannot add when called as a class method'; + ok !$snip->canAdd($session), '...or an object method'; +};