From 72d98fba238187254587d9a031b038f5a324e6c6 Mon Sep 17 00:00:00 2001
From: Blou <philippe.blouin@inlibro.com>
Date: Tue, 19 Apr 2016 11:20:00 -0400
Subject: [PATCH] Bug 11317 - Move file access under Tools and add specific
 permission

Moves the .pl and .tt under tools/ instead of reports/ and adds tools/access_files permission.

Testing
-1) Having done requirements of previous patches, like adding 'accessdir' to koha-conf...
0) apply patch, update database to add permission option.
1) Login as NON-superadmin.  Go under tools, see no Report/Log under the third column
2) Go to add tools/access_file permission to user
3) See new entry under tools third column.
4) validate link is ok.

Sponsored by : CCSR
---
 .../bz11317-add-permission-for-tools-access-file.sql          |  1 +
 installer/data/mysql/userpermissions.sql                      |  1 +
 koha-tmpl/intranet-tmpl/prog/en/includes/permissions.inc      |  1 +
 .../{reports/report_files.tt => tools/access_files.tt}        |  6 +++---
 koha-tmpl/intranet-tmpl/prog/en/modules/tools/tools-home.tt   |  4 +++-
 reports/report_files.pl => tools/access_files.pl              | 11 ++++++-----
 6 files changed, 15 insertions(+), 9 deletions(-)
 create mode 100644 installer/data/mysql/atomicupdate/bz11317-add-permission-for-tools-access-file.sql
 rename koha-tmpl/intranet-tmpl/prog/en/modules/{reports/report_files.tt => tools/access_files.tt} (88%)
 rename reports/report_files.pl => tools/access_files.pl (93%)

diff --git a/installer/data/mysql/atomicupdate/bz11317-add-permission-for-tools-access-file.sql b/installer/data/mysql/atomicupdate/bz11317-add-permission-for-tools-access-file.sql
new file mode 100644
index 0000000..dc49a78
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bz11317-add-permission-for-tools-access-file.sql
@@ -0,0 +1 @@
+INSERT INTO permissions (module_bit, code, description) VALUES (13, 'access_files', 'Access to the files stored on the server');
diff --git a/installer/data/mysql/userpermissions.sql b/installer/data/mysql/userpermissions.sql
index abeeb3a..181ba33 100644
--- a/installer/data/mysql/userpermissions.sql
+++ b/installer/data/mysql/userpermissions.sql
@@ -57,6 +57,7 @@ INSERT INTO permissions (module_bit, code, description) VALUES
    (13, 'records_batchmod', 'Perform batch modification of records (biblios or authorities)'),
    (13, 'marc_modification_templates', 'Manage marc modification templates'),
    (13, 'records_batchdel', 'Perform batch deletion of records (bibliographic or authority)'),
+   (13, 'access_files', 'Access to the files stored on the server'),
    (15, 'check_expiration', 'Check the expiration of a serial'),
    (15, 'claim_serials', 'Claim missing serials'),
    (15, 'create_subscription', 'Create a new subscription'),
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/permissions.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/permissions.inc
index 4a262b9..4c91d28 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/permissions.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/permissions.inc
@@ -100,5 +100,6 @@
     [%- CASE 'report' -%]<span>Use report plugins</span>
     [%- CASE 'tool' -%]<span>Use tool plugins</span>
     [%- CASE 'delete_public_lists' -%]<span>Delete public lists</span>
+    [%- CASE 'access_files' -%]<span>Access to the files stored on the server</span>
     [%- END -%]
 [%- END -%]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/reports/report_files.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/access_files.tt
similarity index 88%
rename from koha-tmpl/intranet-tmpl/prog/en/modules/reports/report_files.tt
rename to koha-tmpl/intranet-tmpl/prog/en/modules/tools/access_files.tt
index 06de9d1..6c6ac8f 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/reports/report_files.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/access_files.tt
@@ -22,7 +22,7 @@
 [% INCLUDE 'header.inc' %]
 [% INCLUDE 'cat-search.inc' %]
 
-<div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/reports/reports-home.pl">Reports</a> &rsaquo; Report/log files</div>
+<div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/tools/tools-home.pl">Tools</a> &rsaquo; Report/log files</div>
 
 <div id="doc3" class="yui-t2">
    <div id="bd">
@@ -46,7 +46,7 @@
             <tbody>
                 [% FOREACH file IN files_loop %]
                 <tr>
-                    <td><a href="/cgi-bin/koha/reports/report_files.pl?id=[% file.id |url %]">[% file.name %]</a></td>
+                    <td><a href="/cgi-bin/koha/tools/access_files.pl?id=[% file.id |url %]">[% file.name %]</a></td>
                     <td align="right">[% file.size %]</td>
                     <td>[% file.date %]</td>
                 </tr>
@@ -61,7 +61,7 @@
 </div>
 </div>
 <div class="yui-b">
-[% INCLUDE 'reports-menu.inc' %]
+[% INCLUDE 'tools-menu.inc' %]
 </div>
 </div>
 [% INCLUDE 'intranet-bottom.inc' %]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/tools-home.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/tools-home.tt
index d9ed604..a81bfc7 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/tools-home.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/tools-home.tt
@@ -109,8 +109,10 @@
     <dd>Manage EDIfact transmissions</dd>
     [% END %]
 
-    <dt><a href="/cgi-bin/koha/reports/report_files.pl">Report/log files </a></dt>
+    [% IF CAN_user_tools_access_files %]
+    <dt><a href="/cgi-bin/koha/tools/access_files.pl">Report/log files</a></dt>
     <dd>Report/log files</dd>
+    [% END %]
 
 </dl>
 </div>
diff --git a/reports/report_files.pl b/tools/access_files.pl
similarity index 93%
rename from reports/report_files.pl
rename to tools/access_files.pl
index aea95a0..a0b8bef 100755
--- a/reports/report_files.pl
+++ b/tools/access_files.pl
@@ -4,7 +4,7 @@
 #
 # Show a list of all the files in the directory specified by the option
 # "accessdir" in koha-conf.xml so they can be downloaded by users with the
-# "reports" permission.
+# "access_files" permission.
 #
 # This file is part of Koha.
 #
@@ -23,12 +23,13 @@
 
 use strict;
 use warnings;
+
 use C4::Auth;
 use CGI;
 use C4::Context;
 use C4::Output;
 use C4::Koha;
-use File::stat;
+use File::stat qw(stat);
 use Digest::MD5 qw(md5_hex);
 
 my $input = new CGI;
@@ -37,11 +38,11 @@ my $accessdir = C4::Context->config('accessdir');
 my @directories = $accessdir ? (ref $accessdir ? @{$accessdir} : ($accessdir)) : ();
 
 my ($template, $borrowernumber, $cookie)
-    = get_template_and_user({template_name => "reports/report_files.tt",
+    = get_template_and_user({template_name => "tools/access_files.tt",
                 query => $input,
                 type => "intranet",
                 authnotrequired => 0,
-                flagsrequired => {reports => '*'},
+                flagsrequired => { tools => 'access_files' },
                 });
 
 unless(@directories) {
@@ -76,7 +77,7 @@ else {
 
     my %files_hash = map { $_->{id} => $_ } @files_list;
     # If we received a file_id and it is valid, send the file to the browser
-    if(defined $file_id and $file_id >= 0 and exists $files_hash{$file_id} ){
+    if(defined $file_id and exists $files_hash{$file_id} ){
         my $filename = $files_hash{$file_id}->{name};
         my $dir = $files_hash{$file_id}->{accessdir};
         binmode STDOUT;
-- 
2.1.0