Bug 16782: Use uri filter instead of html
[koha.git] / opac / svc / auth / googleopenidconnect
index ab71918..e3d7df4 100755 (executable)
@@ -34,7 +34,9 @@ use Modern::Perl;
 use CGI qw ( -utf8 escape );
 use C4::Auth qw{ checkauth get_session get_template_and_user };
 use C4::Context;
+use C4::Members;
 use C4::Output;
+use Koha::Patrons;
 
 use LWP::UserAgent;
 use HTTP::Request::Common qw{ POST };
@@ -179,18 +181,56 @@ elsif ( defined $query->param('code') ) {
                       . ' .' );
             }
             else {
+                my $error_feedback =
+'The email address you are trying to use is not associated with a borrower at this library.';
+                my $auto_registration = C4::Context->preference('GoogleOpenIDConnectAutoRegister') // q{0};
+                my $borrower = Koha::Patrons->find( { email => $email } );
+                if (! $borrower && $auto_registration==1) {
+                    my $cardnumber = fixup_cardnumber();
+                    my $firstname = $claims_json->{'given_name'} // q{};
+                    my $surname = $claims_json->{'family_name'} // q{};
+                    my $delimiter = $firstname ? q{.} : q{};
+                    my $userid = $firstname . $delimiter . $surname;
+                    my $categorycode = C4::Context->preference('GoogleOpenIDConnectDefaultCategory') // q{};
+                    my $patron_category = Koha::Patron::Categories->find( $categorycode );
+                    my $branchcode = C4::Context->preference('GoogleOpenIDConnectDefaultBranch') // q{};
+                    my $library = Koha::Libraries->find( $branchcode );
+                    if (defined $patron_category && defined $library) {
+                        my $password = undef;
+                        my $borrowernumber = C4::Members::AddMember(
+                            cardnumber   => $cardnumber,
+                            firstname    => $firstname,
+                            surname      => $surname,
+                            email        => $email,
+                            categorycode => $categorycode,
+                            branchcode   => $branchcode,
+                            userid       => $userid,
+                            password     => $password
+                        );
+                        $borrower = Koha::Patrons->find( {
+                            borrowernumber => $borrowernumber } );
+                    } else {
+                        $error_feedback = 'The GoogleOpenIDConnectDefaultBranch or GoogleOpenIDConnectDefaultCategory system preferences are not configured properly. Please contact the library with this error message.';
+                    }
+                }
                 my ( $userid, $cookie, $session_id ) =
                   checkauth( $query, 1, {}, 'opac', $email );
                 if ($userid) {    # A user with this email is registered in koha
+
+                    #handle redirect to main.pl, for private opac
+                    my $uri;
+                    if (C4::Context->preference('OpacPublic') ) {
+                        $uri    =  '/cgi-bin/koha/opac-user.pl';
+                    } else {
+                        $uri    =  '/cgi-bin/koha/opac-main.pl';
+                    }
                     print $query->redirect(
-                        -uri    => '/cgi-bin/koha/opac-user.pl',
+                        -uri    => $uri,
                         -cookie => $cookie
                     );
                 }
                 else {
-                    loginfailed( $query,
-'The email address you are trying to use is not associated with a borrower at this library.'
-                    );
+                    loginfailed( $query, $error_feedback );
                 }
             }
         }