From dd7e6016dcd84cf858edef3b80267a60459d33f0 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Tue, 30 Nov 2010 15:07:49 -0800 Subject: [PATCH] Add a workflow activity that reverifies that all subscribers to a CS can still view the CS, otherwise, remove them. Hook up the workflow to the update method in Collaboriation.pm and create an instance for it if the groupIdView changes. Add tests for the activity. Add a new workflow. Update the default WebGUI.conf. --- docs/changelog/7.x.x.txt | 1 + docs/upgrades/upgrade_7.10.5-7.10.6.pl | 23 +++- etc/WebGUI.conf.original | 3 + lib/WebGUI/Asset/Wobject/Collaboration.pm | 25 +++++ .../Activity/UpdateAssetSubscribers.pm | 100 ++++++++++++++++++ ...orkflow_Activity_UpdateAssetSubscribers.pm | 14 +++ t/Workflow.t | 2 +- t/Workflow/Activity/UpdateAssetSubscribers.t | 86 +++++++++++++++ t/lib/WebGUI/Test.pm | 1 + 9 files changed, 253 insertions(+), 2 deletions(-) create mode 100644 lib/WebGUI/Workflow/Activity/UpdateAssetSubscribers.pm create mode 100644 lib/WebGUI/i18n/English/Workflow_Activity_UpdateAssetSubscribers.pm create mode 100644 t/Workflow/Activity/UpdateAssetSubscribers.t diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 9ec5de45e..4436aff85 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -2,6 +2,7 @@ - fixed #11974: Toolbar icons unclickable in Webkit using HTML5 - fixed #11978: Pasting links into TinyMCE - fixed #11980: DataForm broken + - fixed #11971: Still subscribed to forums you no longer have privilege to view 7.10.5 - fixed #11950: Username set to 0 when edit user diff --git a/docs/upgrades/upgrade_7.10.5-7.10.6.pl b/docs/upgrades/upgrade_7.10.5-7.10.6.pl index 6e91275f2..0b394295f 100644 --- a/docs/upgrades/upgrade_7.10.5-7.10.6.pl +++ b/docs/upgrades/upgrade_7.10.5-7.10.6.pl @@ -22,7 +22,7 @@ use Getopt::Long; use WebGUI::Session; use WebGUI::Storage; use WebGUI::Asset; - +use WebGUI::Workflow; my $toVersion = '7.10.6'; my $quiet; # this line required @@ -31,10 +31,31 @@ my $quiet; # this line required my $session = start(); # this line required # upgrade functions go here +addCollaborationSubscriptionWorkflow($session); finish($session); # this line required +#---------------------------------------------------------------------------- +sub addCollaborationSubscriptionWorkflow { + my $session = shift; + print "\tWe're doing some stuff here that you should know about... " unless $quiet; + # and here's our code + $session->config->addToArray('workflowActivities/WebGUI::Asset', qw/WebGUI::Workflow::Activity::UpdateAssetSubscribers/); + my $workflow = WebGUI::Workflow->create($session, + { + mode => 'parallel', + enabled => 1, + title => 'Update CS Subscription members', + description => "This workflow will be run whenever the viewing permissions are changed on an Asset. It will update the members of the subscription group, and remove members who can no longer view the Asset.", + type => 'WebGUI::Asset', + }, + 'xR-_GRRbjBojgLsFx3dEMA' + ); + $workflow->addActivity('WebGUI::Workflow::Activity::UpdateAssetSubscribers'); + print "DONE!\n" unless $quiet; +} + #---------------------------------------------------------------------------- # Describe what our function does #sub exampleFunction { diff --git a/etc/WebGUI.conf.original b/etc/WebGUI.conf.original index 501042387..c20e726d4 100644 --- a/etc/WebGUI.conf.original +++ b/etc/WebGUI.conf.original @@ -900,6 +900,9 @@ "WebGUI::Asset::Wobject::Thingy" : [ "WebGUI::Workflow::Activity::NotifyAboutThing" ], + "WebGUI::Asset" : [ + "WebGUI::Workflow::Activity::UpdateAssetSubscribers" + ], "WebGUI::User" : [ "WebGUI::Workflow::Activity::CreateCronJob", "WebGUI::Workflow::Activity::NotifyAboutUser", diff --git a/lib/WebGUI/Asset/Wobject/Collaboration.pm b/lib/WebGUI/Asset/Wobject/Collaboration.pm index 7bc75692b..9650db67a 100644 --- a/lib/WebGUI/Asset/Wobject/Collaboration.pm +++ b/lib/WebGUI/Asset/Wobject/Collaboration.pm @@ -1566,6 +1566,31 @@ sub unsubscribe { } +#------------------------------------------------------------------- + +=head2 update ( ) + +Update the base method to handle checking valid users in the subscription group if +view permissions have changed. + +=cut + +sub update { + my $self = shift; + my $origGroupIdView = $self->get('groupIdView'); + $self->next::method(@_); + return if $self->get('groupIdView') eq $origGroupIdView; + my $instance_data = { + workflowId => 'xR-_GRRbjBojgLsFx3dEMA', + className => 'WebGUI::Asset', + methodName => 'newByDynamicClass', + parameters => $self->getId, + }; + my $instance = WebGUI::Workflow::Instance->create($self->session, $instance_data); + $instance->start('skipRealtime'); +} + + #------------------------------------------------------------------- =head2 view diff --git a/lib/WebGUI/Workflow/Activity/UpdateAssetSubscribers.pm b/lib/WebGUI/Workflow/Activity/UpdateAssetSubscribers.pm new file mode 100644 index 000000000..cfc0e24c2 --- /dev/null +++ b/lib/WebGUI/Workflow/Activity/UpdateAssetSubscribers.pm @@ -0,0 +1,100 @@ +package WebGUI::Workflow::Activity::UpdateAssetSubscribers; + + +=head1 LEGAL + + ------------------------------------------------------------------- + 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 + ------------------------------------------------------------------- + +=cut + +use strict; +use base 'WebGUI::Workflow::Activity'; +use WebGUI::User; +use WebGUI::Group; + +=head1 NAME + +Package WebGUI::Workflow::Activity::UpdateCollaborationSubscribers + +=head1 DESCRIPTION + +This workflow activity should be called whenever permissions to view a Collaboration System +are changed. It will remove users who are no longer able to view the CS. + +=head1 SYNOPSIS + +See WebGUI::Workflow::Activity for details on how to use any activity. + +=head1 METHODS + +These methods are available from this class: + +=cut + +#------------------------------------------------------------------- + +=head2 definition ( session, definition ) + +See WebGUI::Workflow::Activity::definition() for details. + +=cut + +sub definition { + my $class = shift; + my $session = shift; + my $definition = shift; + my $i18n = WebGUI::International->new($session, "Workflow_Activity_UpdateAssetSubscribers"); + push(@{$definition}, { + name => $i18n->get("name"), + properties => { } + }); + return $class->SUPER::definition($session,$definition); +} + + +#------------------------------------------------------------------- + +=head2 execute ( ) + +See WebGUI::Workflow::Activity::execute() for details. + +=cut + +sub execute { + my $self = shift; + my $asset = shift; + + return unless $asset->get('subscriptionGroupId'); + + my $expireTime = time() + $self->getTTL(); + my $subscriptionGroup = WebGUI::Group->new($self->session, $asset->get('subscriptionGroupId')); + + ##Deserialize from scratch + my @users = @{ $subscriptionGroup->getUsers }; ##Cache + my @usersToDelete = (); ##Cache + ##Note, we could use grep here, but we can't interrupt if the workflow runs too long + USER: foreach my $userId (@users) { + if (time() > $expireTime) { + #return $self->WAITING(1); + } + next USER if $asset->canView($userId); + push @usersToDelete, $userId; + } + if (@usersToDelete) { + $subscriptionGroup->deleteUsers(\@usersToDelete); + } + #Clear scratch + return $self->COMPLETE; +} + + + +1; diff --git a/lib/WebGUI/i18n/English/Workflow_Activity_UpdateAssetSubscribers.pm b/lib/WebGUI/i18n/English/Workflow_Activity_UpdateAssetSubscribers.pm new file mode 100644 index 000000000..63412d08c --- /dev/null +++ b/lib/WebGUI/i18n/English/Workflow_Activity_UpdateAssetSubscribers.pm @@ -0,0 +1,14 @@ +package WebGUI::i18n::English::Workflow_Activity_UpdateAssetSubscribers; +use strict; + +our $I18N = { + 'name' => { + message => q|Update Collaboration Subscribers|, + context => q|Name for the Workflow activity|, + lastUpdated => 1165363730, + }, + +}; + +1; + diff --git a/t/Workflow.t b/t/Workflow.t index 5c091377a..642ca05e7 100644 --- a/t/Workflow.t +++ b/t/Workflow.t @@ -48,7 +48,7 @@ is_deeply($wf->getCrons, [], 'workflow has no crons'); isa_ok(WebGUI::Workflow->getList($session), 'HASH', 'getList returns a hashref'); ok(!isIn($wfId, keys %{WebGUI::Workflow->getList($session)}), 'workflow not in enabled list'); -is(scalar keys %{WebGUI::Workflow->getList($session)}, 12, 'There are twelve enabled, default workflows, of all types, shipped with WebGUI'); +is(scalar keys %{WebGUI::Workflow->getList($session)}, 13, 'There are twelve enabled, default workflows, of all types, shipped with WebGUI'); $wf->set({enabled => 1}); ok($wf->get('enabled'), 'workflow is enabled'); diff --git a/t/Workflow/Activity/UpdateAssetSubscribers.t b/t/Workflow/Activity/UpdateAssetSubscribers.t new file mode 100644 index 000000000..285dd8e86 --- /dev/null +++ b/t/Workflow/Activity/UpdateAssetSubscribers.t @@ -0,0 +1,86 @@ +#------------------------------------------------------------------- +# 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 +#------------------------------------------------------------------- + +use FindBin; +use strict; +use lib "$FindBin::Bin/../../lib"; + +use WebGUI::Test; +use WebGUI::Session; +use WebGUI::Utility; +use WebGUI::Workflow::Activity::UpdateAssetSubscribers; +use WebGUI::Asset; + +use Test::More; +use Test::Deep; + +plan tests => 4; # increment this value for each test you create + +my $session = WebGUI::Test->session; +$session->user({userId => 3}); + +my $subscriberGroup = WebGUI::Group->new($session, "new"); ##Group to hold subscribers +my $oldGroup = WebGUI::Group->new($session, "new"); ##Pretend group, old groupIdView +my $betterGroup = WebGUI::Group->new($session, "new"); ##New group for groupIdView +my $oldUser = WebGUI::User->create($session); ##User who should be unsubscribed +my $betterUser = WebGUI::User->create($session); ##User who should remain subscribed +my $otherUser = WebGUI::User->create($session); ##Just a user, we should never see him +my $root = WebGUI::Asset->getRoot($session); +my $cs = $root->addChild({ + className => 'WebGUI::Asset::Wobject::Collaboration', + title => 'Test Calendar', + subscriptionGroupId => $subscriberGroup->getId, + groupIdView => $betterGroup->getId, +}); +my $tag = WebGUI::VersionTag->getWorking($session); +$tag->commit; +WebGUI::Test->addToCleanup($tag, $subscriberGroup, $betterGroup, $oldUser, $betterUser, $otherUser); + +$subscriberGroup->addUsers([$oldUser->getId, $betterUser->getId, ]); +$betterGroup->addUsers([$betterUser->getId, ]); + +##Plan, since spectre isn't running, we manually simulate an update event and run the +##workflow activity by hand. + +cmp_bag( + $subscriberGroup->getUsers, + [$oldUser->getId, $betterUser->getId], + 'Initial subscribers are correct' +); + +##This workflowId needs to exist, since it's hardcoded in the CS asset +my $workflow = WebGUI::Workflow->new($session, 'xR-_GRRbjBojgLsFx3dEMA'); + +my $instance1 = WebGUI::Workflow::Instance->create($session, + { + workflowId => $workflow->getId, + skipSpectreNotification => 1, + className => 'WebGUI::Asset', + methodName => 'newByDynamicClass', + parameters => $cs->getId, + } +); +WebGUI::Test->addToCleanup($instance1); + +my $retVal; + +$retVal = $instance1->run(); +is($retVal, 'complete', 'cleanup: activity complete'); +$retVal = $instance1->run(); +is($retVal, 'done', 'cleanup: activity is done'); +$instance1->delete('skipNotify'); + +$subscriberGroup->clearCaches; + +cmp_bag( + $subscriberGroup->getUsers, + [$betterUser->getId], + 'Corrent user removed' +); diff --git a/t/lib/WebGUI/Test.pm b/t/lib/WebGUI/Test.pm index 07d440e18..98e7b87bb 100644 --- a/t/lib/WebGUI/Test.pm +++ b/t/lib/WebGUI/Test.pm @@ -835,6 +835,7 @@ Example call: pbworkflow000000000006 pbworkflow000000000007 send_webgui_statistics + xR-_GRRbjBojgLsFx3dEMA }; }, );