From e56d7a0761c00aa2f89641b799c9f1b1ef685eee Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Mon, 24 Jun 2013 10:11:45 -0400 Subject: [PATCH] Bug 10502 - Add independent branches option for sql reports This patch adds the ability to hide reports created by other libraries based on the new IndependentBranchesReports system preference. This is accomplished by adding a branchcode field to the sql reports, which is set on report creation. Test Plan: 1) Apply this patch 2) Run updatedatabase.pl 3) Enable IndependentBranchesReports 4) Log in with a non-superlibrarian staff account 5) View existing sql reports, you should see no difference. Existing reports are 'grandfathered' in, assuming that existing reports may be used by multiple libraries or library groups. 6) Ensure you have selected a library to act as via the "set library" link. 7) Create a new sql report. 8) Verify it was created and you can view and edit it. 9) Change the logged in library to a difference branch. 10) Observe that the report is no longer visible in the reports list. --- C4/Reports/Guided.pm | 14 ++++++++++- installer/data/mysql/kohastructure.sql | 1 + installer/data/mysql/sysprefs.sql | 1 + installer/data/mysql/updatedatabase.pl | 22 ++++++++++++++++++++ .../prog/en/modules/admin/preferences/admin.pref | 7 ++++++ reports/guided_reports.pl | 15 +++++++++++++ 6 files changed, 58 insertions(+), 2 deletions(-) diff --git a/C4/Reports/Guided.pm b/C4/Reports/Guided.pm index 22a23dd..2b20de0 100644 --- a/C4/Reports/Guided.pm +++ b/C4/Reports/Guided.pm @@ -534,10 +534,12 @@ sub save_report { my $cache_expiry = $fields->{cache_expiry} || 300; my $public = $fields->{public}; + my $branchcode = ( C4::Context->userenv && C4::Context->userenv->{branch} ne 'NO_LIBRARY_SET' ) ? C4::Context->userenv->{branch} : undef; + my $dbh = C4::Context->dbh(); $sql =~ s/(\s*\;\s*)$//; # removes trailing whitespace and /;/ - my $query = "INSERT INTO saved_sql (borrowernumber,date_created,last_modified,savedsql,report_name,report_area,report_group,report_subgroup,type,notes,cache_expiry,public) VALUES (?,now(),now(),?,?,?,?,?,?,?,?,?)"; - $dbh->do($query, undef, $borrowernumber, $sql, $name, $area, $group, $subgroup, $type, $notes, $cache_expiry, $public); + my $query = "INSERT INTO saved_sql (borrowernumber,branchcode,date_created,last_modified,savedsql,report_name,report_area,report_group,report_subgroup,type,notes,cache_expiry,public) VALUES (?,?,now(),now(),?,?,?,?,?,?,?,?,?)"; + $dbh->do($query, undef, $borrowernumber, $branchcode, $sql, $name, $area, $group, $subgroup, $type, $notes, $cache_expiry, $public); my $id = $dbh->selectrow_array("SELECT max(id) FROM saved_sql WHERE borrowernumber=? AND report_name=?", undef, $borrowernumber, $name); @@ -669,6 +671,14 @@ sub get_saved_reports { push @args, $filter->{subgroup}; } } + + if ( C4::Context->preference('IndependentBranchesReports') && !C4::Context->IsSuperLibrarian() ) { + my $branches = + C4::Branch::GetIndependentGroupModificationRights( { stringify => 1 } ); + + push( @cond, "( s.branchcode IN ( $branches ) OR s.branchcode IS NULL )" ); + } + $query .= " WHERE ".join( " AND ", map "($_)", @cond ) if @cond; $query .= " ORDER by date_created"; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 90f57f3..a6864c0 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -1865,6 +1865,7 @@ DROP TABLE IF EXISTS `saved_sql`; CREATE TABLE saved_sql ( -- saved sql reports `id` int(11) NOT NULL auto_increment, -- unique id and primary key assigned by Koha `borrowernumber` int(11) default NULL, -- the staff member who created this report (borrowers.borrowernumber) + `branchcode` VARCHAR(10) NULL DEFAULT NULL, -- the branchcode of the staff member who created this report `date_created` datetime default NULL, -- the date this report was created `last_modified` datetime default NULL, -- the date this report was last edited `savedsql` text, -- the SQL for this report diff --git a/installer/data/mysql/sysprefs.sql b/installer/data/mysql/sysprefs.sql index 6438483..5175d0f 100644 --- a/installer/data/mysql/sysprefs.sql +++ b/installer/data/mysql/sysprefs.sql @@ -136,6 +136,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('ImageLimit','5','','Limit images stored in the database by the Patron Card image manager to this number.','Integer'), ('IncludeSeeFromInSearches','0','','Include see-from references in searches.','YesNo'), ('IndependentBranches','0',NULL,'If ON, increases security between libraries','YesNo'), +('IndependentBranchesReports','0', NULL, 'If enabled, custom reports will only be visible to librarians of the same library or library group as the report creator.','YesNo'), ('InProcessingToShelvingCart','0','','If set, when any item with a location code of PROC is \'checked in\', it\'s location code will be changed to CART.','YesNo'), ('INTRAdidyoumean',NULL,NULL,'Did you mean? configuration for the Intranet. Do not change, as this is controlled by /cgi-bin/koha/admin/didyoumean.pl.','Free'), ('IntranetBiblioDefaultView','normal','normal|marc|isbd|labeled_marc','Choose the default detail view in the staff interface; choose between normal, labeled_marc, marc or isbd','Choice'), diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index d83b68b..7749679 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -7889,6 +7889,28 @@ if ( CheckVersion($DBversion) ) { SetVersion ($DBversion); } +$DBversion = "3.13.00.XXX"; +if ( CheckVersion($DBversion) ) { + $dbh->do(q{ + INSERT INTO systempreferences ( + variable, + value, + explanation, + type + ) VALUES ( + 'IndependentBranchesReports', + '0', + 'If enabled, custom reports will only be visible to librarians of the same library or library group as the report creator.', + 'YesNo' + ); + }); + $dbh->do(q{ + ALTER TABLE saved_sql ADD branchcode VARCHAR( 10 ) NULL DEFAULT NULL AFTER borrowernumber; + }); + print "Upgrade to $DBversion done (Bug 10502 - Add independent branches option for sql reports)\n"; + SetVersion($DBversion); +} + =head1 FUNCTIONS =head2 TableExists($table) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref index 97376ed..4e26953 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref @@ -84,6 +84,13 @@ Administration: yes: Prevent no: "Don't prevent" - the logged in library from editing bibliographic records of other libraries. The owning library is defined by the branchcode in biblio.branchcode which should be mapped to a MARC field ( e.g. 951$o ). + - + - pref: IndependentBranchesReports + default: 0 + choices: + yes: Prevent + no: "Don't prevent" + - staff (but not superlibrarians) from accessing reports created by other libraries. CAS Authentication: - - pref: casAuthentication diff --git a/reports/guided_reports.pl b/reports/guided_reports.pl index b9c49ad..fe73458 100755 --- a/reports/guided_reports.pl +++ b/reports/guided_reports.pl @@ -140,6 +140,21 @@ elsif ( $phase eq 'Show SQL'){ elsif ( $phase eq 'Edit SQL'){ my $id = $input->param('reports'); my $report = get_saved_report($id); + + if ( $report->{branchcode} + && C4::Context->preference('IndependentBranchesReports') + && !C4::Context->IsSuperLibrarian() ) + { + unless ( + C4::Branch::GetIndependentGroupModificationRights( + { for => $report->{branchcode} } + ) + ) + { + print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved"); + } + } + my $group = $report->{report_group}; my $subgroup = $report->{report_subgroup}; $template->param( -- 1.7.2.5