From 5f69c47630dd581a410ecf646e15c2b4f4b184f3 Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Mon, 11 Jul 2016 00:42:40 -0400 Subject: [PATCH] Bug 16892: Add automatic patron registration via OAuth2 login 10988 added the ability to log into the OPAC authenticating with Google Open ID Connect. This extends it, by allowing an unregistered patron to have an account automatically created with default category code and branch. This is accomplished by adding 3 system preferences. - GoogleOpenIDConnectAutoRegister whether it will attempt to auto-register the patron. - GoogleOpenIDConnectDefaultCategory This category code will be used to create Google OpenID Connect patrons. - GoogleOpenIDConnectDefaultBranch' This branch code will be used to create Google OpenID Connect patrons. Sponsored-by: Tulong Aklatan --- .../bug_16892_AutomaticPatronSystemPreferences.sql | 4 ++++ .../prog/en/modules/admin/preferences/admin.pref | 12 +++++++++++ opac/svc/auth/googleopenidconnect | 25 ++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 installer/data/mysql/atomicupdate/bug_16892_AutomaticPatronSystemPreferences.sql diff --git a/installer/data/mysql/atomicupdate/bug_16892_AutomaticPatronSystemPreferences.sql b/installer/data/mysql/atomicupdate/bug_16892_AutomaticPatronSystemPreferences.sql new file mode 100644 index 0000000..229a8c9 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_16892_AutomaticPatronSystemPreferences.sql @@ -0,0 +1,4 @@ +INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type` ) VALUES +('GoogleOpenIDConnectAutoRegister', '0',NULL,' Google OpenID Connect logins to auto-register patrons.','YesNo'), +('GoogleOpenIDConnectDefaultCategory','','','This category code will be used to create Google OpenID Connect patrons.','Textarea'), +('GoogleOpenIDConnectDefaultBranch', '','','This branch code will be used to create Google OpenID Connect patrons.','Textarea'); 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 5d8091c..5cc64a0 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 @@ -156,6 +156,18 @@ Administration: - "Google OpenID Connect Restrict to domain (or subdomain of this domain): " - pref: GoogleOpenIDConnectDomain - Leave blank for all google domains + - + - pref: GoogleOpenIDConnectAutoRegister + choices: + yes: Allow + no: "Don't Allow" + - users logging in with Google Open ID to automatically register. + - + - pref: GoogleOpenIDConnectDefaultCategory + - Use this category code when automatically registering a Google Open ID patron. + - + - pref: GoogleOpenIDConnectDefaultBranch + - Use this branch code when automatically registering a Google Open ID patron. Mozilla Persona: - - "Allow Mozilla persona for login: " diff --git a/opac/svc/auth/googleopenidconnect b/opac/svc/auth/googleopenidconnect index ab71918..eae7b34 100755 --- a/opac/svc/auth/googleopenidconnect +++ b/opac/svc/auth/googleopenidconnect @@ -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,6 +181,29 @@ elsif ( defined $query->param('code') ) { . ' .' ); } else { + 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 $branchcode = C4::Context->preference('GoogleOpenIDConnectDefaultBranch') // q{}; + my $password = undef; + $borrower = Koha::Patron->new( { + cardnumber => $cardnumber, + firstname => $firstname, + surname => $surname, + email => $email, + categorycode => $categorycode, + branchcode => $branchcode, + userid => $userid, + password => $password, + } ); + $borrower->store(); + } my ( $userid, $cookie, $session_id ) = checkauth( $query, 1, {}, 'opac', $email ); if ($userid) { # A user with this email is registered in koha -- 2.1.4