[Koha-patches] [PATCH 2/2] Bug 5630 : MT4057: Let the user choose the CAS server to authenticate against in case of multiple CAS servers

Chris Cormack chrisc at catalyst.net.nz
Wed Mar 30 23:12:43 CEST 2011


From: Matthias Meusburger <matthias.meusburger at biblibre.com>

---
 C4/Auth.pm                                         |   31 ++++++++++++++++---
 C4/Auth_with_cas.pm                                |   21 ++++++++++---
 koha-tmpl/opac-tmpl/prog/en/modules/opac-auth.tmpl |   14 ++++++++-
 3 files changed, 55 insertions(+), 11 deletions(-)

diff --git a/C4/Auth.pm b/C4/Auth.pm
index 3870692..fa4fd04 100644
--- a/C4/Auth.pm
+++ b/C4/Auth.pm
@@ -943,12 +943,33 @@ sub checkauth {
     $template->param( OpacPublic => C4::Context->preference("OpacPublic"));
     $template->param( loginprompt => 1 ) unless $info{'nopermission'};
 
-    if ($cas) { 
+    if ($cas) {
+
+	# Is authentication against multiple CAS servers enabled?
+
+        if (C4::Auth_with_cas::multipleAuth && !$casparam) {
+	    my $casservers = C4::Auth_with_cas::getMultipleAuth();		    
+#	    warn Data::Dumper::Dumper($casservers);
+	    my @tmplservers;
+	    foreach my $key (keys %$casservers) {
+	    warn $key, $casservers->{$key};
+		push @tmplservers, {name => $key, value => login_cas_url($query, $key)};
+	    }
+	    #warn Data::Dumper::Dumper(\@tmplservers);
+	    $template->param(
+		casServersLoop => \@tmplservers
+	    );
+	} else {
+        $template->param(
+            casServerUrl    => login_cas_url($query),
+	    );
+	}
+
 	$template->param(
-        casServerUrl    => login_cas_url($query),
-	    invalidCasLogin => $info{'invalidCasLogin'}
-	);
-   }
+            invalidCasLogin => $info{'invalidCasLogin'}
+        );
+    }
+
     my $self_url = $query->url( -absolute => 1 );
     $template->param(
         url         => $self_url,
diff --git a/C4/Auth_with_cas.pm b/C4/Auth_with_cas.pm
index 6c69fb5..b096232 100644
--- a/C4/Auth_with_cas.pm
+++ b/C4/Auth_with_cas.pm
@@ -40,11 +40,12 @@ BEGIN {
 my $context = C4::Context->new() or die 'C4::Context->new failed';
 my $defaultcasserver;
 my $casservers;
+my $yamlauthfile = "../C4/Auth_cas_servers.yaml";
 
 
 # If there's a configuration for multiple cas servers, then we get it
-if (-e qq($FindBin::Bin/../C4/Auth_cas_servers.yaml)) {
-    ($defaultcasserver, $casservers) = YAML::LoadFile(qq($FindBin::Bin/../C4/Auth_cas_servers.yaml));
+if (multipleAuth()) {
+    ($defaultcasserver, $casservers) = YAML::LoadFile(qq($FindBin::Bin/$yamlauthfile));
     $defaultcasserver = $defaultcasserver->{'default'};
 } else {
 # Else, we fall back to casServerUrl syspref
@@ -52,6 +53,16 @@ if (-e qq($FindBin::Bin/../C4/Auth_cas_servers.yaml)) {
     $casservers = { 'default' => C4::Context->preference('casServerUrl') };
 }
 
+# Is there a configuration file for multiple cas servers?
+sub multipleAuth {
+    return (-e qq($FindBin::Bin/$yamlauthfile));
+}
+
+# Returns configured CAS servers' list if multiple authentication is enabled
+sub getMultipleAuth {
+   return $casservers; 
+}
+
 # Logout from CAS
 sub logout_cas {
     my ($query) = @_;
@@ -71,7 +82,6 @@ sub login_cas {
     my $casparam = $query->param('cas');
     # FIXME: This should be more generic and handle whatever parameters there might be
     $uri .= "?cas=" . $casparam if (defined $casparam);
-    warn $defaultcasserver;
     $casparam = $defaultcasserver if (not defined $casparam);
     my $cas = Authen::CAS::Client->new($casservers->{$casparam});
     print $query->redirect( $cas->login_url($uri));
@@ -79,13 +89,14 @@ sub login_cas {
 
 # Returns CAS login URL with callback to the requesting URL
 sub login_cas_url {
-    my ($query) = @_;
+
+    my ($query, $key) = @_;
     my $uri = $ENV{'SCRIPT_URI'};
     my $casparam = $query->param('cas');
     # FIXME: This should be more generic and handle whatever parameters there might be
     $uri .= "?cas=" . $casparam if (defined $casparam);
     $casparam = $defaultcasserver if (not defined $casparam);
-    warn $defaultcasserver;
+    $casparam = $key if (defined $key);
     my $cas = Authen::CAS::Client->new($casservers->{$casparam});
     return $cas->login_url($uri);
 }
diff --git a/koha-tmpl/opac-tmpl/prog/en/modules/opac-auth.tmpl b/koha-tmpl/opac-tmpl/prog/en/modules/opac-auth.tmpl
index bef863d..fcb9cab 100644
--- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-auth.tmpl
+++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-auth.tmpl
@@ -53,7 +53,19 @@
 <p>Sorry, the CAS login failed.</p>
 <!-- /TMPL_IF -->
 
-<p>If you have a <acronym title="Central Authentication Service">CAS</acronym> account, please <a href="<!-- TMPL_VAR NAME="casServerUrl" -->">click here to login</a>.<p>
+<p>If you have a <acronym title="Central Authentication Service">CAS</acronym> account, 
+<!-- TMPL_IF name="casServerUrl" -->
+    please <a href="<!-- TMPL_VAR NAME="casServerUrl" -->">click here to login</a>.<p>
+<!-- /TMPL_IF -->
+
+<!-- TMPL_IF name="casServersLoop" -->
+please choose against which one you would like to authenticate: </p>
+<ul>
+    <!-- TMPL_LOOP name="casServersLoop" -->
+	<li><a href="<!-- TMPL_VAR name="value" -->"><!-- TMPL_VAR name="name" --></a></li>
+    <!-- /TMPL_LOOP -->
+<!-- /TMPL_IF -->
+</ul>
 
 <h4>Local Login</h4>
 <p>If you do not have a CAS account, but a local account, you can still log in : </p>
-- 
1.7.1



More information about the Koha-patches mailing list