Bug 7310: DB revision for Improving list permissions
authorMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Thu, 8 Dec 2011 15:10:57 +0000 (16:10 +0100)
committerPaul Poulain <paul.poulain@biblibre.com>
Wed, 21 Mar 2012 15:46:05 +0000 (16:46 +0100)
DB revision for improving list permissions.
Adds new table virtualshelfshares for maintaining shared private lists.
Adds three columns to virtualshelves for permissions per list.
Adds column borrowernumber to virtualshelfcontents.

Feb 16, 2012: Rebased.

Signed-off-by: Jared Camins-Esakov <jcamins@cpbibliography.com>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Feb 28, 2012: Added three FK constraints for QA (for owner/borrowernumber in virtualshelves, shelfcontents and shelfshares). Tested update again.
Also removed the note about public lists, made obsolete by earlier adjustments for comments from Owen.
Feb 29, 2012: Rebased. Added an update statement to replace invalid borrowernumbers with null in virtualshelves before adding constraint and added a delete statement for private lists without owner now (as a result of the first step). Tested it again.
March 5, 2012: Rebased.
March 19: Rebased.
March 21: Rebased.

installer/data/mysql/updatedatabase.pl

index eb5f58a..1c8be0f 100755 (executable)
@@ -4967,6 +4967,33 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
     SetVersion ($DBversion);
 }
 
+$DBversion = "3.07.99.032";
+if ( C4::Context->preference("Version") lt TransformToNum($DBversion) ) {
+    $dbh->do("ALTER TABLE virtualshelves MODIFY COLUMN owner int"); #should have been int already (fk to borrowers)
+    $dbh->do("UPDATE virtualshelves vi LEFT JOIN borrowers bo ON bo.borrowernumber=vi.owner SET vi.owner=NULL where bo.borrowernumber IS NULL"); #before adding the constraint on borrowernumber, we need to get rid of deleted owners
+    $dbh->do("DELETE FROM virtualshelves WHERE owner IS NULL and category=1"); #delete private lists without owner (cascades to shelfcontents)
+    $dbh->do("ALTER TABLE virtualshelves ADD COLUMN allow_add tinyint(1) DEFAULT 0, ADD COLUMN allow_delete_own tinyint(1) DEFAULT 1, ADD COLUMN allow_delete_other tinyint(1) DEFAULT 0, ADD CONSTRAINT `virtualshelves_ibfk_1` FOREIGN KEY (`owner`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE SET NULL ON UPDATE SET NULL");
+    $dbh->do("UPDATE virtualshelves SET allow_add=0, allow_delete_own=1, allow_delete_other=0 WHERE category=1");
+    $dbh->do("UPDATE virtualshelves SET allow_add=0, allow_delete_own=1, allow_delete_other=0 WHERE category=2");
+    $dbh->do("UPDATE virtualshelves SET allow_add=1, allow_delete_own=1, allow_delete_other=1 WHERE category=3");
+    $dbh->do("UPDATE virtualshelves SET category=2 WHERE category=3");
+
+    $dbh->do("ALTER TABLE virtualshelfcontents ADD COLUMN borrowernumber int, ADD CONSTRAINT `shelfcontents_ibfk_3` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE SET NULL ON UPDATE SET NULL");
+    $dbh->do("UPDATE virtualshelfcontents co LEFT JOIN virtualshelves sh USING (shelfnumber) SET co.borrowernumber=sh.owner");
+
+    $dbh->do("CREATE TABLE virtualshelfshares
+    (id int AUTO_INCREMENT PRIMARY KEY, shelfnumber int NOT NULL,
+    borrowernumber int, invitekey varchar(10), sharedate datetime,
+    CONSTRAINT `virtualshelfshares_ibfk_1` FOREIGN KEY (`shelfnumber`) REFERENCES `virtualshelves` (`shelfnumber`) ON DELETE CASCADE ON UPDATE CASCADE,
+        CONSTRAINT `virtualshelfshares_ibfk_2` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE SET NULL ON UPDATE SET NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8");
+
+    $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('OpacAllowPublicListCreation',1,'If set, allows opac users to create public lists',NULL,'YesNo');");
+    $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('OpacAllowSharingPrivateLists',0,'If set, allows opac users to share private lists with other patrons',NULL,'YesNo');");
+
+    print "Upgrade to $DBversion done (BZ7310: Improving list permissions)\n";
+    SetVersion($DBversion);
+}
+
 =head1 FUNCTIONS
 
 =head2 DropAllForeignKeys($table)