rework how validParent works, and where it is checked. Add an explicit check for it in addChild, and remove a bunch of overridden addChild methods.

This commit is contained in:
Colin Kuskie 2010-03-19 14:31:49 -07:00
parent a8496c4d15
commit 88ec09d279
12 changed files with 78 additions and 110 deletions

View file

@ -2430,18 +2430,14 @@ sub urlExists {
#-------------------------------------------------------------------
=head2 validParent ( )
=head2 valid_parent_classes ( )
Make sure that the current session asset is a valid parent for the child and return true or false.
For example, a WikiPage would check for a WikiMaster. It should be overridden by those children
that need to perform that kind of check.
This is a class method.
The default view method for any asset that doesn't define one. Under all normal circumstances this should be overridden or your asset won't have any output.
=cut
sub validParent {
return 1;
sub valid_parent_classes {
return [qw/WebGUI::Asset/];
}
#-------------------------------------------------------------------

View file

@ -1901,18 +1901,14 @@ sub setRelatedLinks {
#-------------------------------------------------------------------
=head2 validParent
=head2 valid_parent_classes
Make sure that the current session asset is a Calendar for pasting and adding checks.
This is a class method.
=cut
sub validParent {
my $class = shift;
my $session = shift;
return $session->asset->isa('WebGUI::Asset::Wobject::Calendar');
sub valid_parent_classes {
return [qw/WebGUI::Asset::Wobject::Calendar/];
}
####################################################################

View file

@ -708,6 +708,18 @@ sub setComment {
);
}
#-------------------------------------------------------------------
=head2 valid_parent_classes
Restrict valid parents to GalleryAlbums and Shortcuts.
=cut
sub valid_parent_classes {
return [qw/WebGUI::Asset::Wobject::GalleryAlbum WebGUI::Asset::Shortcut/];
}
#----------------------------------------------------------------------------
=head2 view ( )

View file

@ -1348,7 +1348,7 @@ sub prepareView {
#-------------------------------------------------------------------
=head2 validParent
=head2 valid_parent_classes
Make sure that the current session asset is a Thread or Post for pasting and adding checks.
@ -1356,7 +1356,7 @@ This is a class method.
=cut
sub validParent {
sub valid_parent_classes {
my $class = shift;
my $session = shift;
return $session->asset->isa('WebGUI::Asset::Post');

View file

@ -975,7 +975,7 @@ sub updateThreadRating {
#-------------------------------------------------------------------
=head2 validParent
=head2 valid_parent_classes
Make sure that the current session asset is a CS for pasting and adding checks.
@ -983,10 +983,8 @@ This is a class method.
=cut
sub validParent {
my $class = shift;
my $session = shift;
return $session->asset->isa('WebGUI::Asset::Wobject::Collaboration');
sub valid_parent_classes {
return [qw/WebGUI::Asset::Wobject::Collaboration/];
}
#-------------------------------------------------------------------

View file

@ -779,7 +779,7 @@ sub topic {
#-------------------------------------------------------------------
=head2 validParent
=head2 valid_parent_classes
Make sure that the current session asset is a StoryArchive for pasting and adding checks.
@ -787,13 +787,8 @@ This is a class method.
=cut
sub validParent {
my $class = shift;
my $session = shift;
return $session->asset
&& ( $session->asset->isa('WebGUI::Asset::Wobject::StoryArchive')
|| ($session->asset->isa('WebGUI::Asset::Wobject::Folder') && $session->asset->getParent->isa('WebGUI::Asset::Wobject::StoryArchive') )
);
sub valid_parent_classes {
return [qw/WebGUI::Asset::Wobject::StoryArchive WebGUI::Asset::Wobject::Folder/];
}
#-------------------------------------------------------------------

View file

@ -448,7 +448,7 @@ sub scrubContent {
#-------------------------------------------------------------------
=head2 validParent
=head2 valid_parent_classes
Make sure that the current session asset is a WikiMaster for pasting and adding checks.
@ -456,10 +456,8 @@ This is a class method.
=cut
sub validParent {
my $class = shift;
my $session = shift;
return $session->asset->isa('WebGUI::Asset::Wobject::WikiMaster');
sub valid_parent_classes {
return [qw/WebGUI::Asset::Wobject::WikiMaster/];
}
#-------------------------------------------------------------------

View file

@ -302,27 +302,6 @@ use Text::Wrap;
#----------------------------------------------------------------------------
=head2 addChild ( properties [, more ] )
Only allows Events to be added as a child of this asset.
=cut
sub addChild {
my $self = shift;
my $properties = shift;
my @other = @_;
if ($properties->{className} ne "WebGUI::Asset::Event") {
$self->session->errorHandler->security("add a ".$properties->{className}." to a ".$self->get("className"));
return undef;
}
return $self->SUPER::addChild($properties, @other);
}
#----------------------------------------------------------------------------
=head2 addFeed ( $feedParams )
Adds a new Feed to this calendar. This is a wrapper around WebGUI::JSONCollateral's setJSONCollateral

View file

@ -514,26 +514,6 @@ sub _visitorCacheOk {
&& !$self->session->form->process('sortBy'));
}
#-------------------------------------------------------------------
=head2 addChild
Extend the base method to allow only Threads as children.
=cut
sub addChild {
my $self = shift;
my $properties = shift;
my @other = @_;
if ($properties->{className} ne "WebGUI::Asset::Post::Thread") {
$self->session->errorHandler->security("add a ".$properties->{className}." to a ".$self->className);
return undef;
}
return $self->next::method($properties, @other);
}
#-------------------------------------------------------------------
=head2 appendPostListTemplateVars ($var, $p)

View file

@ -130,37 +130,6 @@ sub addArchive {
#----------------------------------------------------------------------------
=head2 addChild ( properties [, ... ] )
Add a child to this GalleryAlbum. See C<WebGUI::AssetLineage> for more info.
Override to ensure only appropriate classes get added to GalleryAlbums.
=cut
sub addChild {
my $self = shift;
my $properties = shift;
my $fileClass = 'WebGUI::Asset::File::GalleryFile';
# Load the class
WebGUI::Pluggable::load( $properties->{className} );
# Make sure we only add appropriate child classes
if ( !$properties->{className}->isa( $fileClass )
&& !$properties->{ className }->isa( "WebGUI::Asset::Shortcut" )
) {
$self->session->errorHandler->security(
"add a ".$properties->{className}." to a ".$self->className
);
return undef;
}
return $self->next::method( $properties, @_ );
}
#----------------------------------------------------------------------------
=head2 appendTemplateVarsFileLoop ( vars, assetIds )
Append template vars for a file loop for the specified assetIds. C<vars> is
@ -764,6 +733,20 @@ sub sendChunkedContent {
return "chunked";
}
#-------------------------------------------------------------------
=head2 valid_parent_classes
Make sure that the current session asset is a Gallery for pasting and adding checks.
This is a class method.
=cut
sub valid_parent_classes {
return [qw/WebGUI::Asset::Wobject::Gallery/];
}
#----------------------------------------------------------------------------
=head2 view ( )

View file

@ -118,19 +118,19 @@ does not exist, then make it.
=cut
sub addChild {
override addChild => sub {
my $self = shift;
my ($properties) = @_;
##Allow subclassing
if ($properties->{className} eq 'WebGUI::Asset::Wobject::Folder') {
return $self->SUPER::addChild(@_);
return super();
}
return undef unless $properties->{className} =~ /^WebGUI::Asset::Story/;
my $todayFolder = $self->getFolder;
return undef unless $todayFolder;
my $story = $todayFolder->addChild(@_);
return $story;
}
};
#-------------------------------------------------------------------

View file

@ -71,12 +71,17 @@ sub addChild {
my $id = shift || $session->id->generate();
my $now = shift || $session->datetime->time();
my $options = shift;
# Check for valid parentage using validParent on child's class
if (! $properties->{className}->validParent($session, $self)) {
return undef;
}
# Check if it is possible to add a child to this asset. If not add it as a sibling of this asset.
if (length($self->lineage) >= 252) {
$session->errorHandler->warn('Tried to add child to asset "'.$self->getId.'" which is already on the deepest level. Adding it as a sibling instead.');
return $self->getParent->addChild($properties, $id, $now, $options);
}
my $lineage = $self->lineage.$self->getNextChildRank;
$self->{_hasChildren} = 1;
$session->db->beginTransaction;
@ -973,6 +978,32 @@ sub swapRank {
}
#-------------------------------------------------------------------
=head2 validParent ([$asset])
Find out whether a potential parent can have this asset as a child.
This is a class method.
=head3 $asset
The potential parent. If not passed, uses $session->asset;
=cut
sub validParent {
my $class = shift;
my $session = shift;
my $asset = shift || $session->asset;
my $parent_classes = $class->valid_parent_classes;
my $valid_parent = 0;
foreach my $parentClass (@{ $class->valid_parent_classes}) {
return 1 if $class->isa($parentClass);
}
return 0;
}
#-------------------------------------------------------------------
=head2 www_demote ( )