The shelf now checks for viewing permissions of the products and shelves that it displays.

Template with i18n feedback for users.
This commit is contained in:
Colin Kuskie 2009-07-17 21:01:55 +00:00
parent 9a6d5d6eb0
commit f2bd5d8e48
5 changed files with 123 additions and 38 deletions

View file

@ -288,8 +288,9 @@ sub view {
# get other shelves # get other shelves
my @childShelves = (); my @childShelves = ();
foreach my $child (@{$self->getLineage(['children'],{returnObjects=>1,includeOnlyClasses=>['WebGUI::Asset::Wobject::Shelf']})}) { SHELF: foreach my $child (@{$self->getLineage(['children'],{returnObjects=>1,includeOnlyClasses=>['WebGUI::Asset::Wobject::Shelf']})}) {
my $properties = $child->get; next SHELF unless $child->canView;
my $properties = $child->get;
$child->{url} = $child->getUrl; $child->{url} = $child->getUrl;
$child->{title} = $child->getTitle; $child->{title} = $child->getTitle;
push @childShelves, $child; push @childShelves, $child;
@ -305,30 +306,38 @@ sub view {
isa => 'WebGUI::Asset::Sku', isa => 'WebGUI::Asset::Sku',
}); });
##Prescreen to only paginate viewable products
my @productIds = List::MoreUtils::uniq(@childSkus, @{$keywordBasedAssetIds});
my @products = ();
PRODUCT: foreach my $id (@productIds) {
my $asset = WebGUI::Asset->newByDynamicClass($session, $id);
if (defined $asset && $asset->canView) {
push @products, $asset;
}
else {
$session->errorHandler->error(q|Couldn't instanciate SKU with assetId |.$id.q| on shelf with assetId |.$self->getId);
}
}
# create paginator # create paginator
my @products = List::MoreUtils::uniq(@childSkus, @{$keywordBasedAssetIds});
my $p = WebGUI::Paginator->new($session, $self->getUrl('func=view')); my $p = WebGUI::Paginator->new($session, $self->getUrl('func=view'));
$p->setDataByArrayRef(\@products); $p->setDataByArrayRef(\@products);
# generate template variables # generate template variables
my @skus = (); my @skus = ();
foreach my $id (@{$p->getPageData}) { foreach my $asset (@{$p->getPageData}) {
my $asset = WebGUI::Asset->newByDynamicClass($session, $id); my $sku = $asset->get;
if (defined $asset) { $sku->{url} = $asset->getUrl;
my $sku = $asset->get; $sku->{thumbnailUrl} = $asset->getThumbnailUrl;
$sku->{url} = $asset->getUrl; $sku->{price} = sprintf("%.2f", $asset->getPrice);
$sku->{thumbnailUrl} = $asset->getThumbnailUrl; $sku->{addToCartForm} = $asset->getAddToCartForm;
$sku->{price} = sprintf("%.2f", $asset->getPrice); push @skus, $sku;
$sku->{addToCartForm} = $asset->getAddToCartForm; }
push @skus, $sku;
}
else {
$session->errorHandler->error(q|Couldn't instanciate SKU with assetId |.$id.q| on shelf with assetId |.$self->getId);
}
}
my %var = ( my %var = (
shelves => \@childShelves, shelves => \@childShelves,
products => \@skus, products => \@skus,
noViewableSkus => scalar(@skus) ? 0 : 1,
emptyShelf => scalar(@productIds) ? 0 : 1,
); );
$p->appendTemplateVars(\%var); $p->appendTemplateVars(\%var);

View file

@ -29,6 +29,8 @@ our $HELP = {
], ],
}, },
{ name => "templateId", description=>"shelf template help" }, { name => "templateId", description=>"shelf template help" },
{ name => "noViewableSkus", },
{ name => "emptyShelf", },
], ],
related => [ related => [
], ],

View file

@ -46,14 +46,14 @@ our $I18N = {
}, },
'shelves' => { 'shelves' => {
message => q|A loop containing the list of shelves that are children of this one in the asset tree. Each record in the loop contains all the properties of a shelf.|, message => q|A loop containing the list of shelves that are children of this one in the asset tree. Each record in the loop contains all the properties of a shelf. Only shelves that the user can see will be in the loop.|,
lastUpdated => 0, lastUpdated => 0,
context => q|a template variable|, context => q|a template variable|,
}, },
'products' => { 'products' => {
message => q|A loop containing the list of products that match the keywords specified in this shelf. Each record in the loop contains all the properties of the matching sku, plus the following variables.|, message => q|A loop containing the list of products that match the keywords specified in this shelf, or that are children of this shelf. Only products that the user can see will be in the loop. Each record in the loop contains all the properties of the matching sku, plus the following variables.|,
lastUpdated => 0, lastUpdated => 1247603018,
context => q|a template variable|, context => q|a template variable|,
}, },
@ -75,6 +75,30 @@ our $I18N = {
context => q|help for a property|, context => q|help for a property|,
}, },
'noViewableSkus' => {
message => q|A boolean which is true if there are no products on this shelf which the current user can view.|,
lastUpdated => 0,
context => q|Template variable help|,
},
'emptyShelf' => {
message => q|A boolean which is true if this shelf has any Products at all.|,
lastUpdated => 0,
context => q|Template variable help|,
},
'this shelf is empty' => {
message => q|This shelf is empty.|,
lastUpdated => 0,
context => q|template label|,
},
'You do not have permission to view the products on this shelf' => {
message => q|You do not have permission to view the products on this shelf.|,
lastUpdated => 0,
context => q|template label|,
},
'assetName' => { 'assetName' => {
message => q|Shelf|, message => q|Shelf|,
lastUpdated => 0, lastUpdated => 0,

View file

@ -20,11 +20,13 @@ use Test::More;
use Test::Deep; use Test::Deep;
use Exception::Class; use Exception::Class;
use Data::Dumper; use Data::Dumper;
use JSON;
use WebGUI::Test; # Must use this before any other WebGUI modules use WebGUI::Test; # Must use this before any other WebGUI modules
use WebGUI::Session; use WebGUI::Session;
use WebGUI::Text; use WebGUI::Text;
use WebGUI::Asset::Sku::Product; use WebGUI::Asset::Sku::Product;
use WebGUI::VersionTag;
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# Init # Init
@ -33,7 +35,7 @@ my $session = WebGUI::Test->session;
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# Tests # Tests
my $tests = 55; my $tests = 61;
plan tests => 1 + $tests; plan tests => 1 + $tests;
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
@ -42,15 +44,14 @@ plan tests => 1 + $tests;
my $class = 'WebGUI::Asset::Wobject::Shelf'; my $class = 'WebGUI::Asset::Wobject::Shelf';
my $loaded = use_ok($class); my $loaded = use_ok($class);
my $storage;
my ($e, $failure); my ($e, $failure);
my ($shelf, $shelf2);
SKIP: { SKIP: {
skip "Unable to load module $class", $tests unless $loaded; skip "Unable to load module $class", $tests unless $loaded;
$shelf = WebGUI::Asset->getRoot($session)->addChild({className => $class}); my $root = WebGUI::Asset->getRoot($session);
my $shelf = $root->addChild({className => $class});
####################################################################### #######################################################################
# #
@ -383,7 +384,7 @@ SKIP: {
# #
####################################################################### #######################################################################
$shelf2 = WebGUI::Asset->getRoot($session)->addChild({className => $class}); my $shelf2 = $root->addChild({className => $class});
$pass = 0; $pass = 0;
eval { eval {
@ -443,15 +444,64 @@ SKIP: {
$shelf2->purge; $shelf2->purge;
undef $shelf2; undef $shelf2;
} ##Clear out this tag so we can do downstream work.
my $tag = WebGUI::VersionTag->getWorking($session);
$tag->commit;
WebGUI::Test->tagsToRollback($tag);
#######################################################################
#
# Template variables
#
#######################################################################
my $tommy = WebGUI::User->create($session);
my $warden = WebGUI::User->create($session);
WebGUI::Test->usersToDelete($tommy, $warden);
my $inGroup = WebGUI::Group->new($session, 'new');
WebGUI::Test->groupsToDelete($inGroup);
$inGroup->addUsers([$tommy->getId]);
my $testTemplate = $root->addChild({
className => 'WebGUI::Asset::Template',
template => q|{ "noViewableSkus":"<tmpl_var noViewableSkus>","emptyShelf":"<tmpl_var emptyShelf>"}|,
});
my $testShelf = $root->addChild({
className => $class,
templateId => $testTemplate->getId,
});
my $tag2 = WebGUI::VersionTag->getWorking($session);
WebGUI::Test->tagsToRollback($tag2);
$tag2->commit;
$session->user({userId => 1});
$testShelf->prepareView;
my $json = $testShelf->view;
my $vars = eval { from_json($json) };
ok( $vars->{emptyShelf}, 'empty shelf: yes');
ok( $vars->{noViewableSkus}, 'viewable skus: none');
my $privateSku = $testShelf->addChild({
className => 'WebGUI::Asset::Sku::Product',
groupIdView => $inGroup->getId,
title => 'Private Product',
});
my $tag3 = WebGUI::VersionTag->getWorking($session);
WebGUI::Test->tagsToRollback($tag3);
$tag3->commit;
$session->user({user => $tommy});
$testShelf->prepareView;
$json = $testShelf->view;
$vars = eval { from_json($json) };
ok( !$vars->{emptyShelf}, 'empty shelf, no');
ok( !$vars->{noViewableSkus}, 'viewable skus: yes for user in group');
$session->user({user => $warden});
$testShelf->prepareView;
$json = $testShelf->view;
$vars = eval { from_json($json) };
ok( !$vars->{emptyShelf}, 'empty shelf, no');
ok( $vars->{noViewableSkus}, 'viewable skus: none for user not in viewable group');
#----------------------------------------------------------------------------
# Cleanup
END {
if (defined $shelf and ref $shelf eq $class) {
$shelf->purge;
}
if (defined $shelf2 and ref $shelf2 eq $class) {
$shelf2->purge;
}
} }