3 # NOTE: This file uses 8-character tabs; do not change the tab size!
7 # Copyright 2000-2002 Katipo Communications
9 # This file is part of Koha.
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA 02111-1307 USA
25 use Digest::MD5 qw(md5_base64);
30 use C4::Output; # to get the template
33 use C4::Branch; # GetBranches
37 # use Net::LDAP qw(:all);
39 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
41 # set the version for version checking
46 C4::Auth - Authenticates Koha users
55 my ($template, $borrowernumber, $cookie)
56 = get_template_and_user(
58 template_name => "opac-main.tmpl",
62 flagsrequired => {borrow => 1},
74 The main function of this module is to provide
75 authentification. However the get_template_and_user function has
76 been provided so that a users login information is passed along
77 automatically. This gets loaded into the template.
88 &get_template_and_user
96 =item get_template_and_user
98 my ($template, $borrowernumber, $cookie)
99 = get_template_and_user(
101 template_name => "opac-main.tmpl",
104 authnotrequired => 1,
105 flagsrequired => {borrow => 1},
109 This call passes the C<query>, C<flagsrequired> and C<authnotrequired>
110 to C<&checkauth> (in this module) to perform authentification.
111 See C<&checkauth> for an explanation of these parameters.
113 The C<template_name> is then used to find the correct template for
114 the page. The authenticated users details are loaded onto the
115 template in the HTML::Template LOOP variable C<USER_INFO>. Also the
116 C<sessionID> is passed to the template. This can be used in templates
117 if cookies are disabled. It needs to be put as and input to every
120 More information on the C<gettemplate> sub can be found in the
125 sub get_template_and_user {
128 gettemplate( $in->{'template_name'}, $in->{'type'}, $in->{'query'} );
129 my ( $user, $cookie, $sessionID, $flags ) = checkauth(
131 $in->{'authnotrequired'},
132 $in->{'flagsrequired'},
134 ) unless ($in->{'template_name'}=~/maintenance/);
137 my $insecure = C4::Context->preference('insecure');
138 if ($user or $insecure) {
140 # load the template variables for stylesheets and JavaScript
141 $template->param( css_libs => $in->{'css_libs'} );
142 $template->param( css_module => $in->{'css_module'} );
143 $template->param( css_page => $in->{'css_page'} );
144 $template->param( css_widgets => $in->{'css_widgets'} );
146 $template->param( js_libs => $in->{'js_libs'} );
147 $template->param( js_module => $in->{'js_module'} );
148 $template->param( js_page => $in->{'js_page'} );
149 $template->param( js_widgets => $in->{'js_widgets'} );
152 $template->param( loggedinusername => $user );
153 $template->param( sessionID => $sessionID );
155 $borrowernumber = getborrowernumber($user);
156 my ( $borr, $alternativeflags ) =
157 GetMemberDetails( $borrowernumber );
160 $template->param( "USER_INFO" => \@bordat );
162 my @flagroots = qw(circulate catalogue parameters borrowers permissions reserveforothers borrow
163 editcatalogue updatecharge management tools editauthorities serials reports);
164 # We are going to use the $flags returned by checkauth
165 # to create the template's parameters that will indicate
166 # which menus the user can access.
167 if (( $flags && $flags->{superlibrarian}==1) or $insecure==1) {
168 $template->param( CAN_user_circulate => 1 );
169 $template->param( CAN_user_catalogue => 1 );
170 $template->param( CAN_user_parameters => 1 );
171 $template->param( CAN_user_borrowers => 1 );
172 $template->param( CAN_user_permission => 1 );
173 $template->param( CAN_user_reserveforothers => 1 );
174 $template->param( CAN_user_borrow => 1 );
175 $template->param( CAN_user_editcatalogue => 1 );
176 $template->param( CAN_user_updatecharge => 1 );
177 $template->param( CAN_user_acquisition => 1 );
178 $template->param( CAN_user_management => 1 );
179 $template->param( CAN_user_tools => 1 );
180 $template->param( CAN_user_editauthorities => 1 );
181 $template->param( CAN_user_serials => 1 );
182 $template->param( CAN_user_reports => 1 );
183 $template->param( CAN_user_staffaccess => 1 );
186 if ( $flags && $flags->{circulate} == 1 ) {
187 $template->param( CAN_user_circulate => 1 );
190 if ( $flags && $flags->{catalogue} == 1 ) {
191 $template->param( CAN_user_catalogue => 1 );
194 if ( $flags && $flags->{parameters} == 1 ) {
195 $template->param( CAN_user_parameters => 1 );
196 $template->param( CAN_user_management => 1 );
199 if ( $flags && $flags->{borrowers} == 1 ) {
200 $template->param( CAN_user_borrowers => 1 );
203 if ( $flags && $flags->{permissions} == 1 ) {
204 $template->param( CAN_user_permission => 1 );
207 if ( $flags && $flags->{reserveforothers} == 1 ) {
208 $template->param( CAN_user_reserveforothers => 1 );
211 if ( $flags && $flags->{borrow} == 1 ) {
212 $template->param( CAN_user_borrow => 1 );
215 if ( $flags && $flags->{editcatalogue} == 1 ) {
216 $template->param( CAN_user_editcatalogue => 1 );
219 if ( $flags && $flags->{updatecharges} == 1 ) {
220 $template->param( CAN_user_updatecharge => 1 );
223 if ( $flags && $flags->{acquisition} == 1 ) {
224 $template->param( CAN_user_acquisition => 1 );
227 if ( $flags && $flags->{tools} == 1 ) {
228 $template->param( CAN_user_tools => 1 );
231 if ( $flags && $flags->{editauthorities} == 1 ) {
232 $template->param( CAN_user_editauthorities => 1 );
235 if ( $flags && $flags->{serials} == 1 ) {
236 $template->param( CAN_user_serials => 1 );
239 if ( $flags && $flags->{reports} == 1 ) {
240 $template->param( CAN_user_reports => 1 );
242 if ( $flags && $flags->{staffaccess} == 1 ) {
243 $template->param( CAN_user_staffaccess => 1 );
246 if ( $in->{'type'} eq "intranet" ) {
248 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
249 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
250 IntranetNav => C4::Context->preference("IntranetNav"),
251 intranetuserjs => C4::Context->preference("intranetuserjs"),
252 TemplateEncoding => C4::Context->preference("TemplateEncoding"),
253 AmazonContent => C4::Context->preference("AmazonContent"),
254 LibraryName => C4::Context->preference("LibraryName"),
255 LoginBranchcode => (C4::Context->userenv?C4::Context->userenv->{"branch"}:"insecure"),
256 LoginBranchname => (C4::Context->userenv?C4::Context->userenv->{"branchname"}:"insecure"),
257 LoginFirstname => (C4::Context->userenv?C4::Context->userenv->{"firstname"}:"Bel"),
258 LoginSurname => C4::Context->userenv?C4::Context->userenv->{"surname"}:"Inconnu",
259 AutoLocation => C4::Context->preference("AutoLocation"),
260 hide_marc => C4::Context->preference("hide_marc"),
261 patronimages => C4::Context->preference("patronimages"),
262 "BiblioDefaultView".C4::Context->preference("IntranetBiblioDefaultView") => 1,
263 advancedMARCEditor => C4::Context->preference("advancedMARCEditor"),
264 suggestion => C4::Context->preference("suggestion"),
265 virtualshelves => C4::Context->preference("virtualshelves"),
266 LibraryName => C4::Context->preference("LibraryName"),
267 KohaAdminEmailAddress => "" . C4::Context->preference("KohaAdminEmailAddress"),
268 IntranetmainUserblock => C4::Context->preference("IntranetmainUserblock"),
269 IndependantBranches => C4::Context->preference("IndependantBranches"),
270 CircAutocompl => C4::Context->preference("CircAutocompl"),
271 yuipath => C4::Context->preference("yuipath"),
272 FRBRizeEditions => C4::Context->preference("FRBRizeEditions"),
273 AmazonSimilarItems => C4::Context->preference("AmazonSimilarItems"),
277 warn "template type should be OPAC, here it is=[" . $in->{'type'} . "]" unless ( $in->{'type'} eq 'opac' );
278 my $LibraryNameTitle = C4::Context->preference("LibraryName");
279 $LibraryNameTitle =~ s/<(?:\/?)(?:br|p)\s*(?:\/?)>/ /sgi;
280 $LibraryNameTitle =~ s/<(?:[^<>'"]|'(?:[^']*)'|"(?:[^"]*)")*>//sg;
282 KohaAdminEmailAddress => "" . C4::Context->preference("KohaAdminEmailAddress"),
283 AnonSuggestions => "" . C4::Context->preference("AnonSuggestions"),
284 suggestion => "" . C4::Context->preference("suggestion"),
285 virtualshelves => "" . C4::Context->preference("virtualshelves"),
286 OpacNav => "" . C4::Context->preference("OpacNav"),
287 opacheader => "" . C4::Context->preference("opacheader"),
288 opaccredits => "" . C4::Context->preference("opaccredits"),
289 opacsmallimage => "" . C4::Context->preference("opacsmallimage"),
290 opaclargeimage => "" . C4::Context->preference("opaclargeimage"),
291 opaclayoutstylesheet => "". C4::Context->preference("opaclayoutstylesheet"),
292 opaccolorstylesheet => "". C4::Context->preference("opaccolorstylesheet"),
293 opaclanguagesdisplay => "". C4::Context->preference("opaclanguagesdisplay"),
294 opacuserlogin => "" . C4::Context->preference("opacuserlogin"),
295 opacbookbag => "" . C4::Context->preference("opacbookbag"),
296 TemplateEncoding => "". C4::Context->preference("TemplateEncoding"),
297 AmazonContent => "" . C4::Context->preference("AmazonContent"),
298 LibraryName => "" . C4::Context->preference("LibraryName"),
299 LibraryNameTitle => "" . $LibraryNameTitle,
300 LoginBranchcode => (C4::Context->userenv?C4::Context->userenv->{"branch"}:"insecure"),
301 LoginBranchname => C4::Context->userenv?C4::Context->userenv->{"branchname"}:"",
302 LoginFirstname => (C4::Context->userenv?C4::Context->userenv->{"firstname"}:"Bel"),
303 LoginSurname => C4::Context->userenv?C4::Context->userenv->{"surname"}:"Inconnu",
304 OpacPasswordChange => C4::Context->preference("OpacPasswordChange"),
305 opacreadinghistory => C4::Context->preference("opacreadinghistory"),
306 opacuserjs => C4::Context->preference("opacuserjs"),
307 OpacCloud => C4::Context->preference("OpacCloud"),
308 OpacTopissue => C4::Context->preference("OpacTopissue"),
309 OpacAuthorities => C4::Context->preference("OpacAuthorities"),
310 OpacBrowser => C4::Context->preference("OpacBrowser"),
311 RequestOnOpac => C4::Context->preference("RequestOnOpac"),
312 reviewson => C4::Context->preference("reviewson"),
313 hide_marc => C4::Context->preference("hide_marc"),
314 patronimages => C4::Context->preference("patronimages"),
315 mylibraryfirst => C4::Context->preference("SearchMyLibraryFirst"),
316 "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
317 OPACFRBRizeEditions => C4::Context->preference("OPACFRBRizeEditions"),
320 return ( $template, $borrowernumber, $cookie );
325 ($userid, $cookie, $sessionID) = &checkauth($query, $noauth, $flagsrequired, $type);
327 Verifies that the user is authorized to run this script. If
328 the user is authorized, a (userid, cookie, session-id, flags)
329 quadruple is returned. If the user is not authorized but does
330 not have the required privilege (see $flagsrequired below), it
331 displays an error page and exits. Otherwise, it displays the
332 login page and exits.
334 Note that C<&checkauth> will return if and only if the user
335 is authorized, so it should be called early on, before any
336 unfinished operations (e.g., if you've opened a file, then
337 C<&checkauth> won't close it for you).
339 C<$query> is the CGI object for the script calling C<&checkauth>.
341 The C<$noauth> argument is optional. If it is set, then no
342 authorization is required for the script.
344 C<&checkauth> fetches user and session information from C<$query> and
345 ensures that the user is authorized to run scripts that require
348 The C<$flagsrequired> argument specifies the required privileges
349 the user must have if the username and password are correct.
350 It should be specified as a reference-to-hash; keys in the hash
351 should be the "flags" for the user, as specified in the Members
352 intranet module. Any key specified must correspond to a "flag"
353 in the userflags table. E.g., { circulate => 1 } would specify
354 that the user must have the "circulate" privilege in order to
355 proceed. To make sure that access control is correct, the
356 C<$flagsrequired> parameter must be specified correctly.
358 The C<$type> argument specifies whether the template should be
359 retrieved from the opac or intranet directory tree. "opac" is
360 assumed if it is not specified; however, if C<$type> is specified,
361 "intranet" is assumed if it is not "opac".
363 If C<$query> does not have a valid session ID associated with it
364 (i.e., the user has not logged in) or if the session has expired,
365 C<&checkauth> presents the user with a login page (from the point of
366 view of the original script, C<&checkauth> does not return). Once the
367 user has authenticated, C<&checkauth> restarts the original script
368 (this time, C<&checkauth> returns).
370 The login page is provided using a HTML::Template, which is set in the
371 systempreferences table or at the top of this file. The variable C<$type>
372 selects which template to use, either the opac or the intranet
373 authentification template.
375 C<&checkauth> returns a user ID, a cookie, and a session ID. The
376 cookie should be sent back to the browser; it verifies that the user
381 sub _version_check ($$) {
385 # If Version syspref is unavailable, it means Koha is beeing installed,
386 # and so we must redirect to OPAC maintenance page or to the WebInstaller
387 #warn "about to check version";
388 unless ($version = C4::Context->preference('Version')) { # assignment, not comparison
389 if ($type ne 'opac') {
390 warn "Install required, redirecting to Installer";
391 print $query->redirect("/cgi-bin/koha/installer/install.pl");
394 warn "OPAC Install required, redirecting to maintenance";
395 print $query->redirect("/cgi-bin/koha/maintenance.pl");
400 # check that database and koha version are the same
401 # there is no DB version, it's a fresh install,
402 # go to web installer
403 # there is a DB version, compare it to the code version
404 my $kohaversion=C4::Context::KOHAVERSION;
405 # remove the 3 last . to have a Perl number
406 $kohaversion =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
407 # warn "kohaversion : $kohaversion";
408 if ($version < $kohaversion){
409 my $warning = "Database update needed, redirecting to %s. Database is $version and Koha is "
410 . C4::Context->config("kohaversion");
411 if ($type ne 'opac'){
412 warn sprintf($warning, 'Installer');
413 print $query->redirect("/cgi-bin/koha/installer/install.pl?step=3");
415 warn sprintf("OPAC: " . $warning, 'maintenance');
416 print $query->redirect("/cgi-bin/koha/maintenance.pl");
424 open L, ">>/tmp/sessionlog";
425 printf L join("\n",@_);
431 # warn "Checking Auth";
432 # $authnotrequired will be set for scripts which will run without authentication
433 my $authnotrequired = shift;
434 my $flagsrequired = shift;
436 $type = 'opac' unless $type;
438 my $dbh = C4::Context->dbh;
439 my $timeout = C4::Context->preference('timeout');
441 if ($timeout =~ /(\d*)[dD]/) {
442 $timeout = $1 * 86400;
444 $timeout = 600 unless $timeout;
446 _version_check($type,$query);
450 my ( $userid, $cookie, $sessionID, $flags );
451 my $logout = $query->param('logout.x');
452 if ( $userid = $ENV{'REMOTE_USER'} ) {
453 # Using Basic Authentication, no cookies required
454 $cookie = $query->cookie(
455 -name => 'CGISESSID',
461 elsif ( $sessionID = $query->cookie("CGISESSID")) { # assignment, not comparison (?)
462 my $session = get_session($sessionID);
463 C4::Context->_new_userenv($sessionID);
465 C4::Context::set_userenv(
466 $session->param('number'), $session->param('id'),
467 $session->param('cardnumber'), $session->param('firstname'),
468 $session->param('surname'), $session->param('branch'),
469 $session->param('branchname'), $session->param('flags'),
470 $session->param('emailaddress'), $session->param('branchprinter')
472 # warn "".$session->param('cardnumber').", ".$session->param('firstname').",
473 # ".$session->param('surname').", ".$session->param('branch');
478 $ip = $session->param('ip');
479 $lasttime = $session->param('lasttime');
480 $userid = $session->param('id');
484 # voluntary logout the user
487 C4::Context->_unset_userenv($sessionID);
490 _session_log(sprintf "%20s from %16s logged out at %30s (manually).\n", $userid,$ip,localtime);
493 if ( $lasttime < time() - $timeout ) {
495 $info{'timed_out'} = 1;
497 C4::Context->_unset_userenv($sessionID);
500 _session_log(sprintf "%20s from %16s logged out at %30s (inactivity).\n", $userid,$ip,localtime);
502 elsif ( $ip ne $ENV{'REMOTE_ADDR'} ) {
503 # Different ip than originally logged in from
504 $info{'oldip'} = $ip;
505 $info{'newip'} = $ENV{'REMOTE_ADDR'};
506 $info{'different_ip'} = 1;
508 C4::Context->_unset_userenv($sessionID);
511 _session_log(sprintf "%20s from %16s logged out at %30s (ip changed to %16s).\n", $userid,$ip,localtime, $info{'newip'});
514 $cookie = $query->cookie( CGISESSID => $session->id );
515 $session->param('lasttime',time());
516 $flags = haspermission( $dbh, $userid, $flagsrequired );
521 $info{'nopermission'} = 1;
527 my $session = get_session("");
530 $sessionID = $session->id;
532 $userid = $query->param('userid');
533 C4::Context->_new_userenv($sessionID);
534 my $password = $query->param('password');
535 C4::Context->_new_userenv($sessionID);
536 my ( $return, $cardnumber ) = checkpw( $dbh, $userid, $password );
538 _session_log(sprintf "%20s from %16s logged in at %30s.\n", $userid,$ENV{'REMOTE_ADDR'},localtime);
539 $cookie = $query->cookie(CGISESSID => $sessionID);
540 if ( $flags = haspermission( $dbh, $userid, $flagsrequired ) ) {
544 $info{'nopermission'} = 1;
545 C4::Context->_unset_userenv($sessionID);
547 if ( $return == 1 ) {
549 $borrowernumber, $firstname, $surname, $userflags,
550 $branchcode, $branchname, $branchprinter, $emailaddress
553 SELECT borrowernumber, firstname, surname, flags, borrowers.branchcode,
554 branches.branchname as branchname,
555 branches.branchprinter as branchprinter,
558 LEFT JOIN branches on borrowers.branchcode=branches.branchcode
560 my $sth = $dbh->prepare("$select where userid=?");
561 $sth->execute($userid);
563 $borrowernumber, $firstname, $surname, $userflags,
564 $branchcode, $branchname, $branchprinter, $emailaddress
567 # warn "$cardnumber,$borrowernumber,$userid,$firstname,$surname,$userflags,$branchcode,$emailaddress";
568 unless ( $sth->rows ) {
569 my $sth = $dbh->prepare("$select where cardnumber=?");
570 $sth->execute($cardnumber);
572 $borrowernumber, $firstname, $surname, $userflags,
573 $branchcode, $branchname, $branchprinter, $emailaddress
576 # warn "$cardnumber,$borrowernumber,$userid,$firstname,$surname,$userflags,$branchcode,$emailaddress";
577 unless ( $sth->rows ) {
578 $sth->execute($userid);
580 $borrowernumber, $firstname, $surname, $userflags,
581 $branchcode, $branchname, $branchprinter, $emailaddress
586 # launch a sequence to check if we have a ip for the branch, i
587 # if we have one we replace the branchcode of the userenv by the branch bound in the ip.
589 my $ip = $ENV{'REMOTE_ADDR'};
590 # if they specify at login, use that
591 if ($query->param('branch')) {
592 $branchcode = $query->param('branch');
593 $branchname = GetBranchName($branchcode);
595 my $branches = GetBranches();
597 foreach my $br ( keys %$branches ) {
598 # now we work with the treatment of ip
599 my $domain = $branches->{$br}->{'branchip'};
600 if ( $domain && $ip =~ /^$domain/ ) {
601 $branchcode = $branches->{$br}->{'branchcode'};
603 # new op dev : add the branchprinter and branchname in the cookie
604 $branchprinter = $branches->{$br}->{'branchprinter'};
605 $branchname = $branches->{$br}->{'branchname'};
608 $session->param('number',$borrowernumber);
609 $session->param('id',$userid);
610 $session->param('cardnumber',$cardnumber);
611 $session->param('firstname',$firstname);
612 $session->param('surname',$surname);
613 $session->param('branch',$branchcode);
614 $session->param('branchname',$branchname);
615 $session->param('flags',$userflags);
616 $session->param('emailaddress',$emailaddress);
617 $session->param('ip',$session->remote_addr());
618 $session->param('lasttime',time());
619 # warn "".$session->param('cardnumber').", ".$session->param('firstname').",
620 # ".$session->param('surname').", ".$session->param('branch');
622 elsif ( $return == 2 ) {
623 #We suppose the user is the superlibrarian
624 $session->param('number',0);
625 $session->param('id',C4::Context->config('user'));
626 $session->param('cardnumber',C4::Context->config('user'));
627 $session->param('firstname',C4::Context->config('user'));
628 $session->param('surname',C4::Context->config('user'));
629 $session->param('branch','NO_LIBRARY_SET');
630 $session->param('branchname','NO_LIBRARY_SET');
631 $session->param('flags',1);
632 $session->param('emailaddress', C4::Context->preference('KohaAdminEmailAddress'));
633 $session->param('ip',$session->remote_addr());
634 $session->param('lasttime',time());
637 C4::Context::set_userenv(
638 $session->param('number'), $session->param('id'),
639 $session->param('cardnumber'), $session->param('firstname'),
640 $session->param('surname'), $session->param('branch'),
641 $session->param('branchname'), $session->param('flags'),
642 $session->param('emailaddress'), $session->param('branchprinter')
648 $info{'invalid_username_or_password'} = 1;
649 C4::Context->_unset_userenv($sessionID);
653 my $insecure = C4::Context->boolean_preference('insecure');
655 # finished authentification, now respond
656 if ( $loggedin || $authnotrequired || ( defined($insecure) && $insecure ) )
660 $cookie = $query->cookie( CGISESSID => '' );
662 return ( $userid, $cookie, $sessionID, $flags );
667 # AUTH rejected, show the login/password template, after checking the DB.
671 # get the inputs from the incoming query
673 foreach my $name ( param $query) {
674 (next) if ( $name eq 'userid' || $name eq 'password' );
675 my $value = $query->param($name);
676 push @inputs, { name => $name, value => $value };
678 # get the branchloop, which we need for authentication
679 my $branches = GetBranches();
681 for my $branch_hash (keys %$branches) {
682 push @branch_loop, {branchcode => "$branch_hash", branchname => $branches->{$branch_hash}->{'branchname'}, };
685 my $template_name = ( $type eq 'opac' ) ? 'opac-auth.tmpl' : 'auth.tmpl';
686 my $template = gettemplate( $template_name, $type, $query );
687 $template->param(branchloop => \@branch_loop,);
691 suggestion => C4::Context->preference("suggestion"),
692 virtualshelves => C4::Context->preference("virtualshelves"),
693 opaclargeimage => C4::Context->preference("opaclargeimage"),
694 LibraryName => C4::Context->preference("LibraryName"),
695 OpacNav => C4::Context->preference("OpacNav"),
696 opaccredits => C4::Context->preference("opaccredits"),
697 opacreadinghistory => C4::Context->preference("opacreadinghistory"),
698 opacsmallimage => C4::Context->preference("opacsmallimage"),
699 opaclayoutstylesheet => C4::Context->preference("opaclayoutstylesheet"),
700 opaccolorstylesheet => C4::Context->preference("opaccolorstylesheet"),
701 opaclanguagesdisplay => C4::Context->preference("opaclanguagesdisplay"),
702 opacuserjs => C4::Context->preference("opacuserjs"),
704 intranetcolorstylesheet =>
705 C4::Context->preference("intranetcolorstylesheet"),
706 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
707 IntranetNav => C4::Context->preference("IntranetNav"),
708 intranetuserjs => C4::Context->preference("intranetuserjs"),
709 TemplateEncoding => C4::Context->preference("TemplateEncoding"),
710 IndependantBranches => C4::Context->preference("IndependantBranches"),
711 AutoLocation => C4::Context->preference("AutoLocation"),
713 $template->param( loginprompt => 1 ) unless $info{'nopermission'};
715 my $self_url = $query->url( -absolute => 1 );
718 LibraryName => => C4::Context->preference("LibraryName"),
720 $template->param( \%info );
721 # $cookie = $query->cookie(CGISESSID => $session->id
723 print $query->header(
724 -type => 'text/html',
734 ($status, $cookie, $sessionId) = check_api_auth($query, $userflags);
736 Given a CGI query containing the parameters 'userid' and 'password' and/or a session
737 cookie, determine if the user has the privileges specified by C<$userflags>.
739 C<check_api_auth> is is meant for authenticating users of web services, and
740 consequently will always return and will not attempt to redirect the user
743 If a valid session cookie is already present, check_api_auth will return a status
744 of "ok", the cookie, and the Koha session ID.
746 If no session cookie is present, check_api_auth will check the 'userid' and 'password
747 parameters and create a session cookie and Koha session if the supplied credentials
750 Possible return values in C<$status> are:
754 =item "ok" -- user authenticated; C<$cookie> and C<$sessionid> have valid values.
756 =item "failed" -- credentials are not correct; C<$cookie> and C<$sessionid> are undef
758 =item "maintenance" -- DB is in maintenance mode; no login possible at the moment
760 =item "expired -- session cookie has expired; API user should resubmit userid and password
768 my $flagsrequired = shift;
770 my $dbh = C4::Context->dbh;
771 my $timeout = C4::Context->preference('timeout');
772 $timeout = 600 unless $timeout;
774 unless (C4::Context->preference('Version')) {
775 # database has not been installed yet
776 return ("maintenance", undef, undef);
778 my $kohaversion=C4::Context::KOHAVERSION;
779 $kohaversion =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
780 if (C4::Context->preference('Version') < $kohaversion) {
781 # database in need of version update; assume that
782 # no API should be called while databsae is in
784 return ("maintenance", undef, undef);
787 # FIXME -- most of what follows is a copy-and-paste
788 # of code from checkauth. There is an obvious need
789 # for refactoring to separate the various parts of
790 # the authentication code, but as of 2007-11-19 this
791 # is deferred so as to not introduce bugs into the
792 # regular authentication code for Koha 3.0.
794 # see if we have a valid session cookie already
795 # however, if a userid parameter is present (i.e., from
796 # a form submission, assume that any current cookie
798 my $sessionID = undef;
799 unless ($query->param('userid')) {
800 $sessionID = $query->cookie("CGISESSID");
803 my $session = get_session($sessionID);
804 C4::Context->_new_userenv($sessionID);
806 C4::Context::set_userenv(
807 $session->param('number'), $session->param('id'),
808 $session->param('cardnumber'), $session->param('firstname'),
809 $session->param('surname'), $session->param('branch'),
810 $session->param('branchname'), $session->param('flags'),
811 $session->param('emailaddress'), $session->param('branchprinter')
814 my $ip = $session->param('ip');
815 my $lasttime = $session->param('lasttime');
816 my $userid = $session->param('id');
817 if ( $lasttime < time() - $timeout ) {
820 C4::Context->_unset_userenv($sessionID);
823 return ("expired", undef, undef);
824 } elsif ( $ip ne $ENV{'REMOTE_ADDR'} ) {
827 C4::Context->_unset_userenv($sessionID);
830 return ("expired", undef, undef);
832 my $cookie = $query->cookie( CGISESSID => $session->id );
833 $session->param('lasttime',time());
834 my $flags = haspermission( $dbh, $userid, $flagsrequired );
836 return ("ok", $cookie, $sessionID);
839 C4::Context->_unset_userenv($sessionID);
842 return ("failed", undef, undef);
846 return ("expired", undef, undef);
850 my $userid = $query->param('userid');
851 my $password = $query->param('password');
852 unless ($userid and $password) {
853 # caller did something wrong, fail the authenticateion
854 return ("failed", undef, undef);
856 my ( $return, $cardnumber ) = checkpw( $dbh, $userid, $password );
857 if ($return and haspermission( $dbh, $userid, $flagsrequired)) {
858 my $session = get_session("");
859 return ("failed", undef, undef) unless $session;
861 my $sessionID = $session->id;
862 C4::Context->_new_userenv($sessionID);
863 my $cookie = $query->cookie(CGISESSID => $sessionID);
864 if ( $return == 1 ) {
866 $borrowernumber, $firstname, $surname,
867 $userflags, $branchcode, $branchname,
868 $branchprinter, $emailaddress
872 "select borrowernumber, firstname, surname, flags, borrowers.branchcode, branches.branchname as branchname,branches.branchprinter as branchprinter, email from borrowers left join branches on borrowers.branchcode=branches.branchcode where userid=?"
874 $sth->execute($userid);
876 $borrowernumber, $firstname, $surname,
877 $userflags, $branchcode, $branchname,
878 $branchprinter, $emailaddress
879 ) = $sth->fetchrow if ( $sth->rows );
881 unless ($sth->rows ) {
882 my $sth = $dbh->prepare(
883 "select borrowernumber, firstname, surname, flags, borrowers.branchcode, branches.branchname as branchname, branches.branchprinter as branchprinter, email from borrowers left join branches on borrowers.branchcode=branches.branchcode where cardnumber=?"
885 $sth->execute($cardnumber);
887 $borrowernumber, $firstname, $surname,
888 $userflags, $branchcode, $branchname,
889 $branchprinter, $emailaddress
890 ) = $sth->fetchrow if ( $sth->rows );
892 unless ( $sth->rows ) {
893 $sth->execute($userid);
895 $borrowernumber, $firstname, $surname, $userflags,
896 $branchcode, $branchname, $branchprinter, $emailaddress
897 ) = $sth->fetchrow if ( $sth->rows );
901 my $ip = $ENV{'REMOTE_ADDR'};
902 # if they specify at login, use that
903 if ($query->param('branch')) {
904 $branchcode = $query->param('branch');
905 $branchname = GetBranchName($branchcode);
907 my $branches = GetBranches();
909 foreach my $br ( keys %$branches ) {
910 # now we work with the treatment of ip
911 my $domain = $branches->{$br}->{'branchip'};
912 if ( $domain && $ip =~ /^$domain/ ) {
913 $branchcode = $branches->{$br}->{'branchcode'};
915 # new op dev : add the branchprinter and branchname in the cookie
916 $branchprinter = $branches->{$br}->{'branchprinter'};
917 $branchname = $branches->{$br}->{'branchname'};
920 $session->param('number',$borrowernumber);
921 $session->param('id',$userid);
922 $session->param('cardnumber',$cardnumber);
923 $session->param('firstname',$firstname);
924 $session->param('surname',$surname);
925 $session->param('branch',$branchcode);
926 $session->param('branchname',$branchname);
927 $session->param('flags',$userflags);
928 $session->param('emailaddress',$emailaddress);
929 $session->param('ip',$session->remote_addr());
930 $session->param('lasttime',time());
931 } elsif ( $return == 2 ) {
932 #We suppose the user is the superlibrarian
933 $session->param('number',0);
934 $session->param('id',C4::Context->config('user'));
935 $session->param('cardnumber',C4::Context->config('user'));
936 $session->param('firstname',C4::Context->config('user'));
937 $session->param('surname',C4::Context->config('user'));
938 $session->param('branch','NO_LIBRARY_SET');
939 $session->param('branchname','NO_LIBRARY_SET');
940 $session->param('flags',1);
941 $session->param('emailaddress', C4::Context->preference('KohaAdminEmailAddress'));
942 $session->param('ip',$session->remote_addr());
943 $session->param('lasttime',time());
945 C4::Context::set_userenv(
946 $session->param('number'), $session->param('id'),
947 $session->param('cardnumber'), $session->param('firstname'),
948 $session->param('surname'), $session->param('branch'),
949 $session->param('branchname'), $session->param('flags'),
950 $session->param('emailaddress'), $session->param('branchprinter')
952 return ("ok", $cookie, $sessionID);
954 return ("failed", undef, undef);
959 =item check_cookie_auth
961 ($status, $sessionId) = check_api_auth($cookie, $userflags);
963 Given a CGISESSID cookie set during a previous login to Koha, determine
964 if the user has the privileges specified by C<$userflags>.
966 C<check_cookie_auth> is meant for authenticating special services
967 such as tools/upload-file.pl that are invoked by other pages that
968 have been authenticated in the usual way.
970 Possible return values in C<$status> are:
974 =item "ok" -- user authenticated; C<$sessionID> have valid values.
976 =item "failed" -- credentials are not correct; C<$sessionid> are undef
978 =item "maintenance" -- DB is in maintenance mode; no login possible at the moment
980 =item "expired -- session cookie has expired; API user should resubmit userid and password
986 sub check_cookie_auth {
988 my $flagsrequired = shift;
990 my $dbh = C4::Context->dbh;
991 my $timeout = C4::Context->preference('timeout');
992 $timeout = 600 unless $timeout;
994 unless (C4::Context->preference('Version')) {
995 # database has not been installed yet
996 return ("maintenance", undef);
998 my $kohaversion=C4::Context::KOHAVERSION;
999 $kohaversion =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
1000 if (C4::Context->preference('Version') < $kohaversion) {
1001 # database in need of version update; assume that
1002 # no API should be called while databsae is in
1004 return ("maintenance", undef);
1007 # FIXME -- most of what follows is a copy-and-paste
1008 # of code from checkauth. There is an obvious need
1009 # for refactoring to separate the various parts of
1010 # the authentication code, but as of 2007-11-23 this
1011 # is deferred so as to not introduce bugs into the
1012 # regular authentication code for Koha 3.0.
1014 # see if we have a valid session cookie already
1015 # however, if a userid parameter is present (i.e., from
1016 # a form submission, assume that any current cookie
1018 unless (defined $cookie and $cookie) {
1019 return ("failed", undef);
1021 my $sessionID = $cookie;
1022 my $session = get_session($sessionID);
1023 C4::Context->_new_userenv($sessionID);
1025 C4::Context::set_userenv(
1026 $session->param('number'), $session->param('id'),
1027 $session->param('cardnumber'), $session->param('firstname'),
1028 $session->param('surname'), $session->param('branch'),
1029 $session->param('branchname'), $session->param('flags'),
1030 $session->param('emailaddress'), $session->param('branchprinter')
1033 my $ip = $session->param('ip');
1034 my $lasttime = $session->param('lasttime');
1035 my $userid = $session->param('id');
1036 if ( $lasttime < time() - $timeout ) {
1039 C4::Context->_unset_userenv($sessionID);
1042 return ("expired", undef);
1043 } elsif ( $ip ne $ENV{'REMOTE_ADDR'} ) {
1044 # IP address changed
1046 C4::Context->_unset_userenv($sessionID);
1049 return ("expired", undef);
1051 $session->param('lasttime',time());
1052 my $flags = haspermission( $dbh, $userid, $flagsrequired );
1054 return ("ok", $sessionID);
1057 C4::Context->_unset_userenv($sessionID);
1060 return ("failed", undef);
1064 return ("expired", undef);
1071 my $session = get_session($sessionID);
1073 Given a session ID, retrieve the CGI::Session object used to store
1074 the session's state. The session object can be used to store
1075 data that needs to be accessed by different scripts during a
1078 If the C<$sessionID> parameter is an empty string, a new session
1084 my $sessionID = shift;
1085 my $storage_method = C4::Context->preference('SessionStorage');
1086 my $dbh = C4::Context->dbh;
1088 if ($storage_method eq 'mysql'){
1089 $session = new CGI::Session("driver:MySQL", $sessionID, {Handle=>$dbh});
1091 elsif ($storage_method eq 'Pg') {
1092 $session = new CGI::Session("driver:PostgreSQL", $sessionID, {Handle=>$dbh});
1095 # catch all defaults to tmp should work on all systems
1096 $session = new CGI::Session("driver:File", $sessionID, {Directory=>'/tmp'});
1103 my ( $dbh, $userid, $password ) = @_;
1108 "select password,cardnumber,borrowernumber,userid,firstname,surname,branchcode,flags from borrowers where userid=?"
1110 $sth->execute($userid);
1112 my ( $md5password, $cardnumber, $borrowernumber, $userid, $firstname,
1113 $surname, $branchcode, $flags )
1115 if ( md5_base64($password) eq $md5password ) {
1117 C4::Context->set_userenv( "$borrowernumber", $userid, $cardnumber,
1118 $firstname, $surname, $branchcode, $flags );
1119 return 1, $cardnumber;
1124 "select password,cardnumber,borrowernumber,userid, firstname,surname,branchcode,flags from borrowers where cardnumber=?"
1126 $sth->execute($userid);
1128 my ( $md5password, $cardnumber, $borrowernumber, $userid, $firstname,
1129 $surname, $branchcode, $flags )
1131 if ( md5_base64($password) eq $md5password ) {
1133 C4::Context->set_userenv( $borrowernumber, $userid, $cardnumber,
1134 $firstname, $surname, $branchcode, $flags );
1138 if ( $userid && $userid eq C4::Context->config('user')
1139 && "$password" eq C4::Context->config('pass') )
1142 # Koha superuser account
1143 # C4::Context->set_userenv(0,0,C4::Context->config('user'),C4::Context->config('user'),C4::Context->config('user'),"",1);
1146 if ( $userid && $userid eq 'demo'
1147 && "$password" eq 'demo'
1148 && C4::Context->config('demo') )
1151 # DEMO => the demo user is allowed to do everything (if demo set to 1 in koha.conf
1152 # some features won't be effective : modify systempref, modify MARC structure,
1160 $authflags = getuserflags($flags,$dbh);
1161 Translates integer flags into permissions strings hash.
1163 C<$flags> is the integer userflags value ( borrowers.userflags )
1164 C<$authflags> is a hashref of permissions
1172 $flags = 0 unless $flags;
1173 my $sth = $dbh->prepare("SELECT bit, flag, defaulton FROM userflags");
1176 while ( my ( $bit, $flag, $defaulton ) = $sth->fetchrow ) {
1177 if ( ( $flags & ( 2**$bit ) ) || $defaulton ) {
1178 $userflags->{$flag} = 1;
1181 $userflags->{$flag} = 0;
1189 $flags = ($dbh,$member,$flagsrequired);
1191 C<$member> may be either userid or overloaded with $borrower hashref from GetMemberDetails.
1192 C<$flags> is a hashref of required flags lik C<$borrower-<{authflags}>
1194 Returns member's flags or 0 if a permission is not met.
1199 my ( $dbh, $userid, $flagsrequired ) = @_;
1200 my ($flags,$intflags);
1201 $dbh=C4::Context->dbh unless($dbh);
1203 $intflags = $userid->{'flags'};
1205 my $sth = $dbh->prepare("SELECT flags FROM borrowers WHERE userid=?");
1206 $sth->execute($userid);
1207 my ($intflags) = $sth->fetchrow;
1208 $flags = getuserflags( $intflags, $dbh );
1210 if ( $userid eq C4::Context->config('user') ) {
1211 # Super User Account from /etc/koha.conf
1212 $flags->{'superlibrarian'} = 1;
1214 if ( $userid eq 'demo' && C4::Context->config('demo') ) {
1215 # Demo user that can do "anything" (demo=1 in /etc/koha.conf)
1216 $flags->{'superlibrarian'} = 1;
1218 return $flags if $flags->{superlibrarian};
1219 foreach ( keys %$flagsrequired ) {
1220 return 0 unless( $flags->{$_} );
1223 #FIXME - This fcn should return the failed permission so a suitable error msg can be delivered.
1227 sub getborrowernumber {
1229 my $dbh = C4::Context->dbh;
1230 for my $field ( 'userid', 'cardnumber' ) {
1232 $dbh->prepare("select borrowernumber from borrowers where $field=?");
1233 $sth->execute($userid);
1235 my ($bnumber) = $sth->fetchrow;
1242 END { } # module clean-up code here (global destructor)