From 73f9966a1dc5032f088a866bbbd47094cecc4430 Mon Sep 17 00:00:00 2001
From: Jesse Weaver <pianohacker@gmail.com>
Date: Mon, 21 Oct 2013 10:17:08 -0600
Subject: [PATCH] Bug 8446 - Followup: Remove unnecessary sysprefs, move to
 config

---
 C4/Auth.pm                                               | 11 +++--------
 C4/Auth_with_Shibboleth.pm                               | 11 ++++++++---
 installer/data/mysql/updatedatabase.pl                   |  8 --------
 .../prog/en/modules/admin/preferences/admin.pref         | 11 -----------
 koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-auth.tt    | 16 ++++++++++++++++
 koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt    |  2 +-
 koha-tmpl/opac-tmpl/prog/en/modules/opac-auth.tt         |  6 +++---
 koha-tmpl/opac-tmpl/prog/en/modules/opac-main.tt         |  3 +--
 opac/opac-main.pl                                        |  3 +--
 opac/opac-user.pl                                        |  3 +--
 10 files changed, 34 insertions(+), 40 deletions(-)

diff --git a/C4/Auth.pm b/C4/Auth.pm
index bc8c611..1df90d5 100644
--- a/C4/Auth.pm
+++ b/C4/Auth.pm
@@ -54,7 +54,7 @@ BEGIN {
     %EXPORT_TAGS = ( EditPermissions => [qw(get_all_subpermissions get_user_subpermissions)] );
     $ldap        = C4::Context->config('useldapserver') || 0;
     $cas         = C4::Context->preference('casAuthentication');
-    $shib        = C4::Context->preference('shibbolethAuthentication');
+    $shib        = C4::Context->config('useshibboleth') || 0;
     $caslogout   = C4::Context->preference('casLogout');
     require C4::Auth_with_cas;             # no import
     require C4::Auth_with_Shibboleth;
@@ -805,7 +805,7 @@ sub checkauth {
             || $userid
             || $shib
             || $pki_field ne 'None'
-	    || $persona )
+            || $persona )
         {
             my $password = $query->param('password');
 
@@ -1063,7 +1063,7 @@ sub checkauth {
         login                => 1,
         INPUTS               => \@inputs,
         casAuthentication    => C4::Context->preference("casAuthentication"),
-        shibbolethAuthentication => C4::Context->preference("shibbolethAuthentication"),
+        shibbolethAuthentication => $shib,
         suggestion           => C4::Context->preference("suggestion"),
         virtualshelves       => C4::Context->preference("virtualshelves"),
         LibraryName          => "" . C4::Context->preference("LibraryName"),
@@ -1581,11 +1581,6 @@ sub checkpw {
         # (defined in the shibbolethLoginAttribute) tto contain the login of the
         # shibboleth-authenticated user
 
-        # Shibboleth attributes are mapped into http environmement variables,
-        # so we're getting the login of the user this way
-        my $attributename = C4::Context->preference('shibbolethLoginAttribute');
-        my $attributevalue = $ENV{$attributename};
-
         # Then, we check if it matches a valid koha user
         if ($shib_login) {
             my ( $retval, $retcard, $retuserid ) = C4::Auth_with_Shibboleth::checkpw_shib( $dbh, $shib_login );    # EXTERNAL AUTH
diff --git a/C4/Auth_with_Shibboleth.pm b/C4/Auth_with_Shibboleth.pm
index 13f4b79..5c6c6b3 100644
--- a/C4/Auth_with_Shibboleth.pm
+++ b/C4/Auth_with_Shibboleth.pm
@@ -22,6 +22,7 @@ use warnings;
 
 use C4::Debug;
 use C4::Context;
+use Carp;
 use CGI;
 
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $debug);
@@ -64,11 +65,13 @@ sub get_login_shib {
     # Shibboleth attributes are mapped into http environmement variables,
     # so we're getting the login of the user this way
 
-    my $shibbolethLoginAttribute = C4::Context->preference('shibbolethLoginAttribute');
-    $debug and warn "shibbolethLoginAttribute value: $shibbolethLoginAttribute";
+    my $shib = C4::Context->config('shibboleth') or croak 'No <shibboleth> in koha-conf.xml';
+
+    my $shibbolethLoginAttribute = $shib->{'userid'};
+    $debug and warn "shibboleth->userid value: $shibbolethLoginAttribute";
     $debug and warn "$shibbolethLoginAttribute value: " . $ENV{$shibbolethLoginAttribute};
 
-    return $ENV{$shibbolethLoginAttribute};
+    return $ENV{$shibbolethLoginAttribute} || '';
 }
 
 # Checks for password correctness
@@ -80,6 +83,8 @@ sub checkpw_shib {
     my $retnumber;
     $debug and warn "User Shibboleth-authenticated as: $userid";
 
+    my $shib = C4::Context->config('shibboleth') or croak 'No <shibboleth> in koha-conf.xml';
+
     # Does it match one of our users ?
     my $sth = $dbh->prepare("select cardnumber from borrowers where userid=?");
     $sth->execute($userid);
diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl
index 945c685..125a668 100755
--- a/installer/data/mysql/updatedatabase.pl
+++ b/installer/data/mysql/updatedatabase.pl
@@ -8056,14 +8056,6 @@ if ( CheckVersion($DBversion) ) {
    SetVersion ($DBversion);
 }
 
-$DBversion = "XXX";
-if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
-    $dbh->do("INSERT INTO `systempreferences` (variable,value,options,explanation,type) VALUES('shibbolethAuthentication','','','Enable or disable Shibboleth authentication','YesNo')");
-    $dbh->do("INSERT INTO `systempreferences` (variable,value,options,explanation,type) VALUES('shibbolethLoginAttribute','','','Which shibboleth user attribute should be used to match koha user login?','')");
-    print "Upgrade to $DBversion done (Adds shibbolethAuthentication and shibbolethLoginAttribute preferences)\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 32665b2..1215ac1 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
@@ -105,17 +105,6 @@ Administration:
                   yes: Allow
                   no: "Don't Allow"
             - Mozilla persona for login
-    Shibboleth Authentication:
-        -
-            - pref: shibbolethAuthentication
-              default: 0
-              choices:
-                  yes: Use
-                  no: "Don't use"
-            - Shibboleth for login authentication.
-        -
-            - Which shibboleth user attribute should be used to match koha user login?
-            - pref: shibbolethLoginAttribute
     Search Engine:
         -
             - pref: SearchEngine
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-auth.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-auth.tt
index 5b5e90f..a1cf15e 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-auth.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-auth.tt
@@ -57,6 +57,22 @@
                                 </div>
                             [% END %]
 
+                            [% IF ( shibbolethAuthentication ) %]
+                                <h4>Shibboleth Login</h4>
+
+                                [% IF ( invalidShibLogin ) %]
+                                <!-- This is what is displated if shibboleth login has failed -->
+                                <p>Sorry, the Shibboleth login failed.</p>
+                                [% END %]
+
+                                <p>If you have a Shibboleth account,
+                                please <a href="[% shibbolethLoginUrl %]">click here to login</a>.</p>
+
+                                <h4>Local Login</h4>
+                                <p>If you do not have a Shibboleth account, but a local account, you can still log in : </p>
+
+                            [% END %]
+
                             [% IF ( casAuthentication ) %]
                                 <h4>CAS login</h4>
 
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt
index 5b68c19..eee397b 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt
@@ -65,7 +65,7 @@
             <div class="span3">
                 [% IF Koha.Preference( 'opacuserlogin' ) == 1 %]
                     [% UNLESS ( loggedinusername ) %]
-                        [% UNLESS ( casAuthentication ) %]
+                        [% UNLESS ( casAuthentication || shibbolethAuthentication ) %]
                             <div id="login">
                                 <form action="/cgi-bin/koha/opac-user.pl" method="post" name="auth" id="auth">
                                     <input type="hidden" name="koha_login_context" value="opac" />
diff --git a/koha-tmpl/opac-tmpl/prog/en/modules/opac-auth.tt b/koha-tmpl/opac-tmpl/prog/en/modules/opac-auth.tt
index 7f23b2b..c009a92 100644
--- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-auth.tt
+++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-auth.tt
@@ -51,14 +51,14 @@
 
 [% IF ( invalidShibLogin ) %]
 <!-- This is what is displated if shibboleth login has failed -->
-<p>Sorry, the shibboleth login failed.</p>
+<p>Sorry, the Shibboleth login failed.</p>
 [% END %]
 
-<p>If you have a shibboleth account,
+<p>If you have a Shibboleth account,
 please <a href="[% shibbolethLoginUrl %]">click here to login</a>.</p>
 
 <h4>Local Login</h4>
-<p>If you do not have a shibboleth account, but a local account, you can still log in : </p>
+<p>If you do not have a Shibboleth account, but a local account, you can still log in : </p>
 
 [% END %]
 
diff --git a/koha-tmpl/opac-tmpl/prog/en/modules/opac-main.tt b/koha-tmpl/opac-tmpl/prog/en/modules/opac-main.tt
index 574a196..dd144a8 100644
--- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-main.tt
+++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-main.tt
@@ -46,8 +46,7 @@
     <div class="yui-u">
     [% IF ( opacuserlogin ) %]
     [% UNLESS ( loggedinusername ) %]
-    [% UNLESS ( casAuthentication ) %]
-    [% UNLESS ( shibbolethAuthentication ) %]
+    [% UNLESS ( casAuthentication || shibbolethAuthentication ) %]
     <div id="login" class="container clearfix">
 	<form action="/cgi-bin/koha/opac-user.pl" method="post" name="auth" id="auth">
     <input type="hidden" name="koha_login_context" value="opac" />
diff --git a/opac/opac-main.pl b/opac/opac-main.pl
index 4a0702a..fb9de41 100755
--- a/opac/opac-main.pl
+++ b/opac/opac-main.pl
@@ -43,8 +43,7 @@ $template->param(
     casAuthentication   => $casAuthentication,
 );
 
-my $shibbolethAuthentication = C4::Context->preference('shibbolethAuthentication');
-$template->param( shibbolethAuthentication => $shibbolethAuthentication);
+$template->param( shibbolethAuthentication => C4::Context->config('useshibboleth') );
 
 # display news
 # use cookie setting for language, bug default to syspref if it's not set
diff --git a/opac/opac-user.pl b/opac/opac-user.pl
index 8f67a4c..d1f7a38 100755
--- a/opac/opac-user.pl
+++ b/opac/opac-user.pl
@@ -71,8 +71,7 @@ for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
 my $patronupdate = $query->param('patronupdate');
 my $canrenew = 1;
 
-my $shibbolethAuthentication = C4::Context->preference('shibbolethAuthentication');
-$template->param( shibbolethAuthentication => $shibbolethAuthentication );
+$template->param( shibbolethAuthentication => C4::Context->config('useshibboleth') );
 
 # get borrower information ....
 my ( $borr ) = GetMemberDetails( $borrowernumber );
-- 
1.9.0