diff --git a/lib/WebGUI/Admin.pm b/lib/WebGUI/Admin.pm index 2a93c885d..fcf87d8c3 100644 --- a/lib/WebGUI/Admin.pm +++ b/lib/WebGUI/Admin.pm @@ -72,19 +72,20 @@ sub getAdminPluginTemplateVars { # If we have a class name, we've got a new WebGUI::Admin::Plugin if ( $funcDef->{className} ) { - my $plugin = $funcDef->{className}->new( $session, $funcId, $funcDef ); - next unless $plugin->canUse; + my $plugin = $funcDef->{className}->new( $session, id => $funcId, $funcDef ); + next unless $plugin->canView; $var = { - title => $plugin->getTitle, - icon => $plugin->getIcon, - 'icon.small' => $plugin->getIconSmall, - url => $plugin->getUrl, + id => $funcId, + title => $plugin->title, + icon => $plugin->icon, + 'icon.small' => $plugin->iconSmall, }; # build the list of processed items - $processed{$plugin->getTitle} = $var; + $processed{$plugin->title} = $var; } # Don't know what we have (old admin console functions) + # NOTE: This usage is deprecated and will be removed in a future version else { # make title my $title = $funcDef->{title}; @@ -390,7 +391,7 @@ sub getSqlFromQueryString { $dbh->quote($value), ; } - elsif { + elsif ( $isValidOp{ $part->{op} } ) { push @parts, join " ", $dbh->quote_identifier($part->{field}), $part->{op}, @@ -668,6 +669,28 @@ sub www_processAssetHelper { #---------------------------------------------------------------------- +=head2 www_processPlugin ( ) + +Process the given admin console plugin + +=cut + +sub www_processPlugin { + my ( $self ) = @_; + my $session = $self->session; + my ( $form ) = $session->quick(qw{ form }); + + my $id = $form->get('id'); + my $def = $session->config->get('adminConsole/' . $id ); + return JSON->new->encode( { error => 'No such admin plugin: ' . $id } ) + unless $def; + my $class = $def->{className}; + WebGUI::Pluggable::load( $class ); + return JSON->new->encode( $class->process( $session ) ); +} + +#---------------------------------------------------------------------- + =head2 www_searchAssets ( ) Search the asset tree for the given keywords and filters @@ -804,7 +827,13 @@ __DATA__
diff --git a/lib/WebGUI/Admin/Plugin.pm b/lib/WebGUI/Admin/Plugin.pm new file mode 100644 index 000000000..415f9b030 --- /dev/null +++ b/lib/WebGUI/Admin/Plugin.pm @@ -0,0 +1,44 @@ +package WebGUI::Admin::Plugin; + +use Moose; +use Scalar::Util qw(blessed); + +has 'id' => ( + is => 'ro', + isa => 'Str', + required => 1, +); + +has 'title' => ( + is => 'rw', + isa => 'Str', +); + +has 'icon' => ( + is => 'rw', + isa => 'Str', + default => '', # Find a good default +); + +has 'iconSmall' => ( + is => 'rw', + isa => 'Str', + default => '', # Find a good default +); + +sub BUILDARGS { + my ( $class, $session, %args ) = @_; + return { session => $session, %args }; +} + +sub canUse { + return 1; +} + +sub getUrl { + my ( $self, $method, $params ) = @_; + $method ||= "view"; + return '?op=admin;plugin=' . $self->id . ';method=' . $method . ';' . $params; +} + +1; diff --git a/lib/WebGUI/Content/Admin.pm b/lib/WebGUI/Content/Admin.pm index ec91692a3..4aef62c12 100644 --- a/lib/WebGUI/Content/Admin.pm +++ b/lib/WebGUI/Content/Admin.pm @@ -49,9 +49,24 @@ sub handler { if ( $session->form->get("op") eq "admin" ) { if ( $session->form->get("plugin") ) { - # Load the requested plugin if necessary - # Default page is "view" - # Pass control to the right page + my $id = $session->form->get('id'); + my $props = $session->config->get('adminConsole')->{ $id }; + + if ( !$props ) { + return "ERROR"; # die here + } + + my $class = $props->{ className }; + WebGUI::Pluggable::load( $class ); + my $method = $session->form->get('method') || "view"; + + if ( $class->can( "www_" . $method ) ) { + return $class->can( "www_" . $method )->($session); + } + else { + return "ERROR"; # die here + } + } else { my $admin = WebGUI::Admin->new( $session ); @@ -61,7 +76,7 @@ sub handler { return $admin->can( "www_" . $method )->($admin); } else { - return $admin->www_view; + return "ERROR"; # die here } } } diff --git a/t/Admin.t b/t/Admin.t new file mode 100644 index 000000000..39b149cf5 --- /dev/null +++ b/t/Admin.t @@ -0,0 +1,114 @@ +# vim:syntax=perl +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2009 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------ +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------ + +# Write a little about what this script tests. +# +# + +use FindBin; +use strict; +use lib "$FindBin::Bin/lib"; +use JSON; +use Test::More; +use Test::Deep; +use Monkey::Patch; +use WebGUI::Test; # Must use this before any other WebGUI modules +use WebGUI::Session; +use WebGUI::Test::Mechanize; + +#---------------------------------------------------------------------------- +# Init + +# Create a new admin plugin +package WebGUI::Admin::Plugin::Test; + +use Moose; +use base 'WebGUI::Admin::Plugin'; + +has '+title' => ( default => "title" ); +has '+icon' => ( default => "icon" ); +has '+iconSmall' => ( default => "iconSmall" ); +has 'test_config' => ( is => 'rw', default => 'default' ); + +sub canView { return 1; } +sub process { return { message => 'success' } } +sub www_view { return "view" } +sub www_test { return "test" } +sub www_config { return $_[0]->test_config } + +package main; +BEGIN { $INC{'WebGUI/Admin/Plugin/Test.pm'} = __FILE__; } + +my $session = WebGUI::Test->session; +$session->user({ userId => 3 }); + +# Add a couple admin plugins to the config file +WebGUI::Test->originalConfig( "adminConsole" ); +$session->config->addToHash('adminConsole', 'test', { + className => 'WebGUI::Admin::Plugin::Test', +} ); +$session->config->addToHash('adminConsole', 'test2', { + url => '?op=admin;plugin=test;method=config', +} ); + +# Add some assets +my $snip = WebGUI::Asset->getImportNode( $session )->addChild( { + className => 'WebGUI::Asset::Snippet', + title => 'test', + groupIdEdit => '3', +} ); + +# Commit the tag +my $tag = WebGUI::VersionTag->getWorking( $session ); +$tag->commit; +addToCleanup( $tag ); + +#---------------------------------------------------------------------------- +# Tests + +my $output; + +# Test www_ methods +my $mech = WebGUI::Test::Mechanize->new( config => WebGUI::Test->config ); +$mech->get('/'); # Start a session +$mech->session->user({ userId => '3' }); + +# www_processAssetHelper +$mech->get_ok( '/?op=admin;method=processAssetHelper;className=WebGUI::AssetHelper::Cut;assetId=' . $snip->getId ); +cmp_deeply( + JSON->new->decode( $mech->content ), + WebGUI::AssetHelper::Cut->process( $snip ), + 'www_processAssetHelper', +); + +# www_processPlugin +$mech->get_ok( '/?op=admin;method=processPlugin;id=test' ); +$output = $mech->content; +cmp_deeply( + JSON->new->decode( $output ), + WebGUI::Admin::Plugin::Test->process( $session ), + 'Test plugin process()', +) || diag( $output ); + +# www_findUser +$mech->get_ok( '/?op=admin;method=findUser;query=Adm' ); +$output = $mech->content; +cmp_deeply( + JSON->new->decode( $output ), + { results => superbagof( superhashof( { + userId => 3, + } ) ) }, + 'found the Admin user', +) || diag( $output ); + +done_testing; + +#vim:ft=perl