View | Details | Raw Unified | Return to bug 17501
Collapse All | Expand All

(-)a/Koha/Upload.pm (-4 / +4 lines)
Lines 1-4 Link Here
1
package Koha::Upload;
1
package Koha::Uploader;
2
2
3
# Copyright 2007 LibLime, Galen Charlton
3
# Copyright 2007 LibLime, Galen Charlton
4
# Copyright 2011-2012 BibLibre
4
# Copyright 2011-2012 BibLibre
Lines 21-37 package Koha::Upload; Link Here
21
21
22
=head1 NAME
22
=head1 NAME
23
23
24
Koha::Upload - Facilitate file uploads (temporary and permanent)
24
Koha::Uploader - Facilitate file uploads (temporary and permanent)
25
25
26
=head1 SYNOPSIS
26
=head1 SYNOPSIS
27
27
28
    use Koha::Upload;
28
    use Koha::Uploader;
29
    use Koha::UploadedFile;
29
    use Koha::UploadedFile;
30
    use Koha::UploadedFiles;
30
    use Koha::UploadedFiles;
31
31
32
    # add an upload (see tools/upload-file.pl)
32
    # add an upload (see tools/upload-file.pl)
33
    # the public flag allows retrieval via OPAC
33
    # the public flag allows retrieval via OPAC
34
    my $upload = Koha::Upload->new( public => 1, category => 'A' );
34
    my $upload = Koha::Uploader->new( public => 1, category => 'A' );
35
    my $cgi = $upload->cgi;
35
    my $cgi = $upload->cgi;
36
    # Do something with $upload->count, $upload->result or $upload->err
36
    # Do something with $upload->count, $upload->result or $upload->err
37
37
(-)a/t/db_dependent/Upload.t (-11 / +11 lines)
Lines 10-18 use t::lib::TestBuilder; Link Here
10
10
11
use C4::Context;
11
use C4::Context;
12
use Koha::Database;
12
use Koha::Database;
13
use Koha::Upload;
14
use Koha::UploadedFile;
13
use Koha::UploadedFile;
15
use Koha::UploadedFiles;
14
use Koha::UploadedFiles;
15
use Koha::Uploader;
16
16
17
my $schema  = Koha::Database->new->schema;
17
my $schema  = Koha::Database->new->schema;
18
$schema->storage->txn_begin;
18
$schema->storage->txn_begin;
Lines 95-101 sub test01 { Link Here
95
    is( Koha::UploadedFile->temporary_directory, $tempdir,
95
    is( Koha::UploadedFile->temporary_directory, $tempdir,
96
        'Check temporary directory' );
96
        'Check temporary directory' );
97
97
98
    my $upl = Koha::Upload->new({
98
    my $upl = Koha::Uploader->new({
99
        category => $uploads->[$current_upload]->[0]->{cat},
99
        category => $uploads->[$current_upload]->[0]->{cat},
100
    });
100
    });
101
    my $cgi= $upl->cgi;
101
    my $cgi= $upl->cgi;
Lines 118-124 sub test01 { Link Here
118
}
118
}
119
119
120
sub test02 {
120
sub test02 {
121
    my $upl = Koha::Upload->new({
121
    my $upl = Koha::Uploader->new({
122
        category => $uploads->[$current_upload]->[0]->{cat},
122
        category => $uploads->[$current_upload]->[0]->{cat},
123
        public => 1,
123
        public => 1,
124
    });
124
    });
Lines 138-144 sub test02 { Link Here
138
}
138
}
139
139
140
sub test03 {
140
sub test03 {
141
    my $upl = Koha::Upload->new({ tmp => 1 }); #temporary
141
    my $upl = Koha::Uploader->new({ tmp => 1 }); #temporary
142
    my $cgi= $upl->cgi;
142
    my $cgi= $upl->cgi;
143
    is( $upl->count, 1, 'Upload 3 includes one temporary file' );
143
    is( $upl->count, 1, 'Upload 3 includes one temporary file' );
144
    my $rec = Koha::UploadedFiles->find( $upl->result );
144
    my $rec = Koha::UploadedFiles->find( $upl->result );
Lines 146-152 sub test03 { Link Here
146
}
146
}
147
147
148
sub test04 { # Fail on a file already there
148
sub test04 { # Fail on a file already there
149
    my $upl = Koha::Upload->new({
149
    my $upl = Koha::Uploader->new({
150
        category => $uploads->[$current_upload]->[0]->{cat},
150
        category => $uploads->[$current_upload]->[0]->{cat},
151
    });
151
    });
152
    my $cgi= $upl->cgi;
152
    my $cgi= $upl->cgi;
Lines 157-163 sub test04 { # Fail on a file already there Link Here
157
}
157
}
158
158
159
sub test05 { # add temporary file with same name and contents, delete it
159
sub test05 { # add temporary file with same name and contents, delete it
160
    my $upl = Koha::Upload->new({ tmp => 1 });
160
    my $upl = Koha::Uploader->new({ tmp => 1 });
161
    my $cgi= $upl->cgi;
161
    my $cgi= $upl->cgi;
162
    is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' );
162
    is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' );
163
    my $id = $upl->result;
163
    my $id = $upl->result;
Lines 171-177 sub test05 { # add temporary file with same name and contents, delete it Link Here
171
171
172
    # testing delete via UploadedFile (singular)
172
    # testing delete via UploadedFile (singular)
173
    # Note that find returns a Koha::Object
173
    # Note that find returns a Koha::Object
174
    $upl = Koha::Upload->new({ tmp => 1 });
174
    $upl = Koha::Uploader->new({ tmp => 1 });
175
    $upl->cgi;
175
    $upl->cgi;
176
    my $kohaobj = Koha::UploadedFiles->find( $upl->result );
176
    my $kohaobj = Koha::UploadedFiles->find( $upl->result );
177
    my $name = $kohaobj->filename;
177
    my $name = $kohaobj->filename;
Lines 208-226 sub test08 { # allows_add_by Link Here
208
        value  => { flags => 0 }, #no permissions
208
        value  => { flags => 0 }, #no permissions
209
    });
209
    });
210
    my $patronid = $patron->{borrowernumber};
210
    my $patronid = $patron->{borrowernumber};
211
    is( Koha::Upload->allows_add_by( $patron->{userid} ),
211
    is( Koha::Uploader->allows_add_by( $patron->{userid} ),
212
        undef, 'Patron is not allowed to do anything' );
212
        undef, 'Patron is not allowed to do anything' );
213
213
214
    # add some permissions: edit_catalogue
214
    # add some permissions: edit_catalogue
215
    my $fl = 2**9; # edit_catalogue
215
    my $fl = 2**9; # edit_catalogue
216
    $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
216
    $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
217
    is( Koha::Upload->allows_add_by( $patron->{userid} ),
217
    is( Koha::Uploader->allows_add_by( $patron->{userid} ),
218
        undef, 'Patron is still not allowed to add uploaded files' );
218
        undef, 'Patron is still not allowed to add uploaded files' );
219
219
220
    # replace flags by all tools
220
    # replace flags by all tools
221
    $fl = 2**13; # tools
221
    $fl = 2**13; # tools
222
    $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
222
    $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
223
    is( Koha::Upload->allows_add_by( $patron->{userid} ),
223
    is( Koha::Uploader->allows_add_by( $patron->{userid} ),
224
        1, 'Patron should be allowed now to add uploaded files' );
224
        1, 'Patron should be allowed now to add uploaded files' );
225
225
226
    # remove all tools and add upload_general_files only
226
    # remove all tools and add upload_general_files only
Lines 234-240 sub test08 { # allows_add_by Link Here
234
            code           => 'upload_general_files',
234
            code           => 'upload_general_files',
235
        },
235
        },
236
    });
236
    });
237
    is( Koha::Upload->allows_add_by( $patron->{userid} ),
237
    is( Koha::Uploader->allows_add_by( $patron->{userid} ),
238
        1, 'Patron is still allowed to add uploaded files' );
238
        1, 'Patron is still allowed to add uploaded files' );
239
}
239
}
240
240
(-)a/tools/upload-file.pl (-5 / +4 lines)
Lines 27-33 use URI::Escape; Link Here
27
27
28
use C4::Context;
28
use C4::Context;
29
use C4::Auth qw/check_cookie_auth haspermission/;
29
use C4::Auth qw/check_cookie_auth haspermission/;
30
use Koha::Upload;
30
use Koha::Uploader;
31
31
32
# upload-file.pl must authenticate the user
32
# upload-file.pl must authenticate the user
33
# before processing the POST request,
33
# before processing the POST request,
Lines 41-54 my %cookies = CGI::Cookie->fetch; Link Here
41
my $sid = $cookies{'CGISESSID'}->value;
41
my $sid = $cookies{'CGISESSID'}->value;
42
my ( $auth_status, $sessionID ) = check_cookie_auth( $sid );
42
my ( $auth_status, $sessionID ) = check_cookie_auth( $sid );
43
my $uid = C4::Auth::get_session($sid)->param('id');
43
my $uid = C4::Auth::get_session($sid)->param('id');
44
my $allowed = Koha::Upload->allows_add_by( $uid );
44
my $allowed = Koha::Uploader->allows_add_by( $uid );
45
45
46
if( $auth_status ne 'ok' || !$allowed ) {
46
if( $auth_status ne 'ok' || !$allowed ) {
47
    send_reply( 'denied' );
47
    send_reply( 'denied' );
48
    exit 0;
48
    exit 0;
49
}
49
}
50
50
51
my $upload = Koha::Upload->new( upload_pars($ENV{QUERY_STRING}) );
51
my $upload = Koha::Uploader->new( upload_pars($ENV{QUERY_STRING}) );
52
if( !$upload || !$upload->cgi || !$upload->count ) {
52
if( !$upload || !$upload->cgi || !$upload->count ) {
53
    # not one upload succeeded
53
    # not one upload succeeded
54
    send_reply( 'failed', undef, $upload? $upload->err: undef );
54
    send_reply( 'failed', undef, $upload? $upload->err: undef );
Lines 70-76 sub send_reply { # response will be sent back as JSON Link Here
70
}
70
}
71
71
72
sub upload_pars { # this sub parses QUERY_STRING in order to build the
72
sub upload_pars { # this sub parses QUERY_STRING in order to build the
73
                  # parameter hash for Koha::Upload
73
                  # parameter hash for Koha::Uploader
74
    my ( $qstr ) = @_;
74
    my ( $qstr ) = @_;
75
    $qstr = Encode::decode_utf8( uri_unescape( $qstr ) );
75
    $qstr = Encode::decode_utf8( uri_unescape( $qstr ) );
76
    # category could include a utf8 character
76
    # category could include a utf8 character
77
- 

Return to bug 17501