Add the Lock Asset AssetHelper
This commit is contained in:
parent
e17a783efa
commit
3e11f711b3
3 changed files with 165 additions and 0 deletions
61
lib/WebGUI/AssetHelper/Lock.pm
Normal file
61
lib/WebGUI/AssetHelper/Lock.pm
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
package WebGUI::AssetHelper::Lock;
|
||||
|
||||
use strict;
|
||||
use Class::C3;
|
||||
use base qw/WebGUI::AssetHelper/;
|
||||
|
||||
=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
|
||||
-------------------------------------------------------------------
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Package WebGUI::AssetHelper::Locks
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
Puts an edit lock on an Asset.
|
||||
|
||||
=head1 METHODS
|
||||
|
||||
These methods are available from this class:
|
||||
|
||||
=cut
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 process ( $class, $asset )
|
||||
|
||||
Locks the asset with a version tag. If the user cannot edit the asset, or the asset is
|
||||
already locked, it returns an error message.
|
||||
|
||||
=cut
|
||||
|
||||
sub process {
|
||||
my ($class, $asset) = @_;
|
||||
my $session = $asset->session;
|
||||
|
||||
my $i18n = WebGUI::International->new($session, 'Asset');
|
||||
if (! $asset->canEdit) {
|
||||
return { error => $i18n->get('38', 'WebGUI'), };
|
||||
}
|
||||
elsif ( $asset->isLocked ) {
|
||||
return { error => sprintf $i18n->get('already locked'), $asset->getTitle};
|
||||
}
|
||||
|
||||
$asset = $asset->addRevision;
|
||||
|
||||
return {
|
||||
message => sprintf($i18n->get('locked asset'), $asset->getTitle),
|
||||
};
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
@ -1418,6 +1418,18 @@ Couldn't open %-s because %-s <br />
|
|||
context => q{%s will be replaced by the title of the asset.},
|
||||
},
|
||||
|
||||
'already locked' => {
|
||||
message => q{The asset %s is already locked.},
|
||||
lastUpdated => 0,
|
||||
context => q{%s will be replaced by the title of the asset.},
|
||||
},
|
||||
|
||||
'locked asset' => {
|
||||
message => q{Locked the asset %s.},
|
||||
lastUpdated => 0,
|
||||
context => q{%s will be replaced by the title of the asset.},
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
1;
|
||||
|
|
|
|||
92
t/AssetHelper/Lock.t
Normal file
92
t/AssetHelper/Lock.t
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
# 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 Test::More;
|
||||
use Test::Deep;
|
||||
use WebGUI::Test; # Must use this before any other WebGUI modules
|
||||
use WebGUI::Session;
|
||||
use WebGUI::Asset;
|
||||
use WebGUI::AssetHelper::Lock;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Init
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
|
||||
plan tests => 3; # Increment this number for each test you create
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# put your tests here
|
||||
|
||||
my $output;
|
||||
my $home = WebGUI::Asset->getDefault($session);
|
||||
|
||||
my $editor = WebGUI::User->create($session);
|
||||
$editor->addToGroups([4]);
|
||||
addToCleanup($editor);
|
||||
|
||||
$session->user({userId => 3});
|
||||
my $newPage = $home->addChild({
|
||||
className => 'WebGUI::Asset::Wobject::Layout',
|
||||
title => 'Test page',
|
||||
groupIdEdit => '4',
|
||||
ownerUserId => '3',
|
||||
}, undef, WebGUI::Test->webguiBirthday, { skipAutoCommitWorkflows => 1, });
|
||||
my $versionTag = WebGUI::VersionTag->getWorking($session);
|
||||
$versionTag->commit;
|
||||
addToCleanup($versionTag);
|
||||
|
||||
$newPage = $newPage->cloneFromDb;
|
||||
|
||||
$session->user({userId => 1});
|
||||
$output = WebGUI::AssetHelper::Lock->process($newPage);
|
||||
cmp_deeply(
|
||||
$output,
|
||||
{
|
||||
error => re('You do not have sufficient privileges'),
|
||||
},
|
||||
'AssetHelper/Lock checks for editing privileges'
|
||||
);
|
||||
|
||||
$session->user({userId => 3});
|
||||
$output = WebGUI::AssetHelper::Lock->process($newPage);
|
||||
cmp_deeply(
|
||||
$output,
|
||||
{
|
||||
message => 'Locked the asset Test page.',
|
||||
},
|
||||
'... locks the asset'
|
||||
);
|
||||
|
||||
$newPage = $newPage->cloneFromDb;
|
||||
|
||||
my $versionTag2 = WebGUI::VersionTag->getWorking($session);
|
||||
addToCleanup($versionTag2);
|
||||
|
||||
$session->user({userId => $editor->getId});
|
||||
$output = WebGUI::AssetHelper::Lock->process($newPage);
|
||||
cmp_deeply(
|
||||
$output,
|
||||
{
|
||||
error => 'The asset Test page is already locked.',
|
||||
},
|
||||
'... returns an error message if the asset is already locked'
|
||||
);
|
||||
Loading…
Add table
Add a link
Reference in a new issue