From 5aab9bd5fd6a84f751934a7ad9036988ae0b84e7 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Fri, 1 Apr 2016 11:14:15 +0200 Subject: [PATCH] Bug 14686: [QA Follow-up] Move allows_add_by to Upload.pm Content-Type: text/plain; charset=utf-8 As requested by QA, the sub should be moved. Test plan: See previous patches. Signed-off-by: Marcel de Rooy --- Koha/Schema/Result/UploadedFile.pm | 16 +--------------- Koha/Upload.pm | 21 +++++++++++++++++++++ t/db_dependent/Upload.t | 12 ++++++------ tools/upload-file.pl | 3 +-- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/Koha/Schema/Result/UploadedFile.pm b/Koha/Schema/Result/UploadedFile.pm index 8e2106b..1eaab71 100644 --- a/Koha/Schema/Result/UploadedFile.pm +++ b/Koha/Schema/Result/UploadedFile.pm @@ -124,19 +124,5 @@ __PACKAGE__->set_primary_key("id"); # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:Cq/HSuTaBxZH2qSGEddoaQ -sub allows_add_by { - my ( $self, $userid ) = @_; # do not confuse with borrowernumber - my $flags = [ - { tools => 'upload_general_files' }, - { circulate => 'circulate_remaining_permissions' }, - { tools => 'stage_marc_import' }, - { tools => 'upload_local_cover_images' }, - ]; - require C4::Auth; - foreach( @$flags ) { - return 1 if C4::Auth::haspermission( $userid, $_ ); - } - return; -} - +# You can replace this text with custom code or comments, and it will be preserved on regeneration 1; diff --git a/Koha/Upload.pm b/Koha/Upload.pm index 5cd1c62..ceff56a 100644 --- a/Koha/Upload.pm +++ b/Koha/Upload.pm @@ -240,6 +240,27 @@ sub httpheaders { ); } +=head2 allows_add_by + + allows_add_by checks if $userid has permission to add uploaded files + +=cut + +sub allows_add_by { + my ( $class, $userid ) = @_; # do not confuse with borrowernumber + my $flags = [ + { tools => 'upload_general_files' }, + { circulate => 'circulate_remaining_permissions' }, + { tools => 'stage_marc_import' }, + { tools => 'upload_local_cover_images' }, + ]; + require C4::Auth; + foreach( @$flags ) { + return 1 if C4::Auth::haspermission( $userid, $_ ); + } + return; +} + =head1 INTERNAL ROUTINES =cut diff --git a/t/db_dependent/Upload.t b/t/db_dependent/Upload.t index 31d8767..3e67f8e 100644 --- a/t/db_dependent/Upload.t +++ b/t/db_dependent/Upload.t @@ -84,7 +84,7 @@ subtest 'Test07' => sub { plan tests => 2; test07(); }; -subtest 'Test08: UploadedFile->allows_add_by' => sub { +subtest 'Test08: allows_add_by' => sub { plan tests => 4; test08(); }; @@ -176,26 +176,26 @@ sub test07 { #simple test for httpheaders and getCategories is( @$cat >= 1, 1, 'getCategories returned at least one category' ); } -sub test08 { # UploadedFile->allows_add_by +sub test08 { # allows_add_by my $builder = t::lib::TestBuilder->new; my $patron = $builder->build({ source => 'Borrower', value => { flags => 0 }, #no permissions }); my $patronid = $patron->{borrowernumber}; - is( Koha::Schema::Result::UploadedFile->allows_add_by( $patron->{userid} ), + is( Koha::Upload->allows_add_by( $patron->{userid} ), undef, 'Patron is not allowed to do anything' ); # add some permissions: edit_catalogue my $fl = 2**9; # edit_catalogue $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl }); - is( Koha::Schema::Result::UploadedFile->allows_add_by( $patron->{userid} ), + is( Koha::Upload->allows_add_by( $patron->{userid} ), undef, 'Patron is still not allowed to add uploaded files' ); # replace flags by all tools $fl = 2**13; # tools $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl }); - is( Koha::Schema::Result::UploadedFile->allows_add_by( $patron->{userid} ), + is( Koha::Upload->allows_add_by( $patron->{userid} ), 1, 'Patron should be allowed now to add uploaded files' ); # remove all tools and add upload_general_files only @@ -209,7 +209,7 @@ sub test08 { # UploadedFile->allows_add_by code => 'upload_general_files', }, }); - is( Koha::Schema::Result::UploadedFile->allows_add_by( $patron->{userid} ), + is( Koha::Upload->allows_add_by( $patron->{userid} ), 1, 'Patron is still allowed to add uploaded files' ); } diff --git a/tools/upload-file.pl b/tools/upload-file.pl index b410934..a64ce28 100755 --- a/tools/upload-file.pl +++ b/tools/upload-file.pl @@ -28,7 +28,6 @@ use URI::Escape; use C4::Context; use C4::Auth qw/check_cookie_auth haspermission/; use Koha::Upload; -use Koha::Schema::Result::UploadedFile; # upload-file.pl must authenticate the user # before processing the POST request, @@ -42,7 +41,7 @@ my %cookies = CGI::Cookie->fetch; my $sid = $cookies{'CGISESSID'}->value; my ( $auth_status, $sessionID ) = check_cookie_auth( $sid ); my $uid = C4::Auth::get_session($sid)->param('id'); -my $allowed = Koha::Schema::Result::UploadedFile->allows_add_by( $uid ); +my $allowed = Koha::Upload->allows_add_by( $uid ); if( $auth_status ne 'ok' || !$allowed ) { send_reply( 'denied' ); -- 1.7.10.4