2 # NOTE: This file uses 8-character tabs; do not change the tab size!
6 # Copyright 2000-2002 Katipo Communications
8 # This file is part of Koha.
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA 02111-1307 USA
24 use Digest::MD5 qw(md5_base64);
30 use C4::Output; # to get the template
33 use C4::Branch; # GetBranches
36 # use Net::LDAP qw(:all);
38 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
40 # set the version for version checking
41 $VERSION = do { my @v = '$Revision$' =~ /\d+/g;
42 shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v );
47 C4::Auth - Authenticates Koha users
56 my ($template, $borrowernumber, $cookie)
57 = get_template_and_user(
59 template_name => "opac-main.tmpl",
63 flagsrequired => {borrow => 1},
75 The main function of this module is to provide
76 authentification. However the get_template_and_user function has
77 been provided so that a users login information is passed along
78 automatically. This gets loaded into the template.
89 &get_template_and_user
92 =item get_template_and_user
94 my ($template, $borrowernumber, $cookie)
95 = get_template_and_user(
97 template_name => "opac-main.tmpl",
100 authnotrequired => 1,
101 flagsrequired => {borrow => 1},
105 This call passes the C<query>, C<flagsrequired> and C<authnotrequired>
106 to C<&checkauth> (in this module) to perform authentification.
107 See C<&checkauth> for an explanation of these parameters.
109 The C<template_name> is then used to find the correct template for
110 the page. The authenticated users details are loaded onto the
111 template in the HTML::Template LOOP variable C<USER_INFO>. Also the
112 C<sessionID> is passed to the template. This can be used in templates
113 if cookies are disabled. It needs to be put as and input to every
116 More information on the C<gettemplate> sub can be found in the
121 sub get_template_and_user {
124 gettemplate( $in->{'template_name'}, $in->{'type'}, $in->{'query'} );
125 my ( $user, $cookie, $sessionID, $flags ) = checkauth(
127 $in->{'authnotrequired'},
128 $in->{'flagsrequired'},
130 ) unless ($in->{'template_name'}=~/maintenance/);
133 my $insecure = C4::Context->preference('insecure');
134 if ($user or $insecure) {
136 # load the template variables for stylesheets and JavaScript
137 $template->param( css_libs => $in->{'css_libs'} );
138 $template->param( css_module => $in->{'css_module'} );
139 $template->param( css_page => $in->{'css_page'} );
140 $template->param( css_widgets => $in->{'css_widgets'} );
142 $template->param( js_libs => $in->{'js_libs'} );
143 $template->param( js_module => $in->{'js_module'} );
144 $template->param( js_page => $in->{'js_page'} );
145 $template->param( js_widgets => $in->{'js_widgets'} );
148 $template->param( loggedinusername => $user );
149 $template->param( sessionID => $sessionID );
151 $borrowernumber = getborrowernumber($user);
152 my ( $borr, $alternativeflags ) =
153 GetMemberDetails( $borrowernumber );
156 $template->param( "USER_INFO" => \@bordat );
158 # We are going to use the $flags returned by checkauth
159 # to create the template's parameters that will indicate
160 # which menus the user can access.
161 if (( $flags && $flags->{superlibrarian}==1) or $insecure==1) {
162 $template->param( CAN_user_circulate => 1 );
163 $template->param( CAN_user_catalogue => 1 );
164 $template->param( CAN_user_parameters => 1 );
165 $template->param( CAN_user_borrowers => 1 );
166 $template->param( CAN_user_permission => 1 );
167 $template->param( CAN_user_reserveforothers => 1 );
168 $template->param( CAN_user_borrow => 1 );
169 $template->param( CAN_user_editcatalogue => 1 );
170 $template->param( CAN_user_updatecharge => 1 );
171 $template->param( CAN_user_acquisition => 1 );
172 $template->param( CAN_user_management => 1 );
173 $template->param( CAN_user_tools => 1 );
174 $template->param( CAN_user_editauthorities => 1 );
175 $template->param( CAN_user_serials => 1 );
176 $template->param( CAN_user_reports => 1 );
179 if ( $flags && $flags->{circulate} == 1 ) {
180 $template->param( CAN_user_circulate => 1 );
183 if ( $flags && $flags->{catalogue} == 1 ) {
184 $template->param( CAN_user_catalogue => 1 );
187 if ( $flags && $flags->{parameters} == 1 ) {
188 $template->param( CAN_user_parameters => 1 );
189 $template->param( CAN_user_management => 1 );
192 if ( $flags && $flags->{borrowers} == 1 ) {
193 $template->param( CAN_user_borrowers => 1 );
196 if ( $flags && $flags->{permissions} == 1 ) {
197 $template->param( CAN_user_permission => 1 );
200 if ( $flags && $flags->{reserveforothers} == 1 ) {
201 $template->param( CAN_user_reserveforothers => 1 );
204 if ( $flags && $flags->{borrow} == 1 ) {
205 $template->param( CAN_user_borrow => 1 );
208 if ( $flags && $flags->{editcatalogue} == 1 ) {
209 $template->param( CAN_user_editcatalogue => 1 );
212 if ( $flags && $flags->{updatecharges} == 1 ) {
213 $template->param( CAN_user_updatecharge => 1 );
216 if ( $flags && $flags->{acquisition} == 1 ) {
217 $template->param( CAN_user_acquisition => 1 );
220 if ( $flags && $flags->{tools} == 1 ) {
221 $template->param( CAN_user_tools => 1 );
224 if ( $flags && $flags->{editauthorities} == 1 ) {
225 $template->param( CAN_user_editauthorities => 1 );
228 if ( $flags && $flags->{serials} == 1 ) {
229 $template->param( CAN_user_serials => 1 );
232 if ( $flags && $flags->{reports} == 1 ) {
233 $template->param( CAN_user_reports => 1 );
236 if ( $in->{'type'} eq "intranet" ) {
238 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
239 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
240 IntranetNav => C4::Context->preference("IntranetNav"),
241 intranetuserjs => C4::Context->preference("intranetuserjs"),
242 TemplateEncoding => C4::Context->preference("TemplateEncoding"),
243 AmazonContent => C4::Context->preference("AmazonContent"),
244 LibraryName => C4::Context->preference("LibraryName"),
245 LoginBranchcode => (C4::Context->userenv?C4::Context->userenv->{"branch"}:"insecure"),
246 LoginBranchname => (C4::Context->userenv?C4::Context->userenv->{"branchname"}:"insecure"),
247 AutoLocation => C4::Context->preference("AutoLocation"),
248 hide_marc => C4::Context->preference("hide_marc"),
249 patronimages => C4::Context->preference("patronimages"),
250 "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
251 advancedMARCEditor => C4::Context->preference("advancedMARCEditor"),
252 suggestion => C4::Context->preference("suggestion"),
253 virtualshelves => C4::Context->preference("virtualshelves"),
254 LibraryName => C4::Context->preference("LibraryName"),
255 KohaAdminEmailAddress => "" . C4::Context->preference("KohaAdminEmailAddress"),
256 IntranetmainUserblock => C4::Context->preference("IntranetmainUserblock"),
260 warn "template type should be OPAC, here it is=[" . $in->{'type'} . "]"
261 unless ( $in->{'type'} eq 'opac' );
262 my $LibraryNameTitle = C4::Context->preference("LibraryName");
263 $LibraryNameTitle =~ s/<(?:\/?)(?:br|p)\s*(?:\/?)>/ /sgi;
264 $LibraryNameTitle =~ s/<(?:[^<>'"]|'(?:[^']*)'|"(?:[^"]*)")*>//sg;
266 KohaAdminEmailAddress => "" . C4::Context->preference("KohaAdminEmailAddress"),
267 suggestion => "" . C4::Context->preference("suggestion"),
268 virtualshelves => "" . C4::Context->preference("virtualshelves"),
269 OpacNav => "" . C4::Context->preference("OpacNav"),
270 opacheader => "" . C4::Context->preference("opacheader"),
271 opaccredits => "" . C4::Context->preference("opaccredits"),
272 opacsmallimage => "" . C4::Context->preference("opacsmallimage"),
273 opaclargeimage => "" . C4::Context->preference("opaclargeimage"),
274 opaclayoutstylesheet => "". C4::Context->preference("opaclayoutstylesheet"),
275 opaccolorstylesheet => "". C4::Context->preference("opaccolorstylesheet"),
276 opaclanguagesdisplay => "". C4::Context->preference("opaclanguagesdisplay"),
277 opacuserlogin => "" . C4::Context->preference("opacuserlogin"),
278 opacbookbag => "" . C4::Context->preference("opacbookbag"),
279 TemplateEncoding => "". C4::Context->preference("TemplateEncoding"),
280 AmazonContent => "" . C4::Context->preference("AmazonContent"),
281 LibraryName => "" . C4::Context->preference("LibraryName"),
282 LibraryNameTitle => "" . $LibraryNameTitle,
283 LoginBranchcode => (C4::Context->userenv?C4::Context->userenv->{"branch"}:"insecure"),
284 LoginBranchname => C4::Context->userenv?C4::Context->userenv->{"branchname"}:"",
285 OpacPasswordChange => C4::Context->preference("OpacPasswordChange"),
286 opacreadinghistory => C4::Context->preference("opacreadinghistory"),
287 opacuserjs => C4::Context->preference("opacuserjs"),
288 OpacCloud => C4::Context->preference("OpacCloud"),
289 OpacTopissue => C4::Context->preference("OpacTopissue"),
290 OpacAuthorities => C4::Context->preference("OpacAuthorities"),
291 OpacBrowser => C4::Context->preference("OpacBrowser"),
292 RequestOnOpac => C4::Context->preference("RequestOnOpac"),
293 reviewson => C4::Context->preference("reviewson"),
294 hide_marc => C4::Context->preference("hide_marc"),
295 patronimages => C4::Context->preference("patronimages"),
296 "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
299 return ( $template, $borrowernumber, $cookie );
304 ($userid, $cookie, $sessionID) = &checkauth($query, $noauth, $flagsrequired, $type);
306 Verifies that the user is authorized to run this script. If
307 the user is authorized, a (userid, cookie, session-id, flags)
308 quadruple is returned. If the user is not authorized but does
309 not have the required privilege (see $flagsrequired below), it
310 displays an error page and exits. Otherwise, it displays the
311 login page and exits.
313 Note that C<&checkauth> will return if and only if the user
314 is authorized, so it should be called early on, before any
315 unfinished operations (e.g., if you've opened a file, then
316 C<&checkauth> won't close it for you).
318 C<$query> is the CGI object for the script calling C<&checkauth>.
320 The C<$noauth> argument is optional. If it is set, then no
321 authorization is required for the script.
323 C<&checkauth> fetches user and session information from C<$query> and
324 ensures that the user is authorized to run scripts that require
327 The C<$flagsrequired> argument specifies the required privileges
328 the user must have if the username and password are correct.
329 It should be specified as a reference-to-hash; keys in the hash
330 should be the "flags" for the user, as specified in the Members
331 intranet module. Any key specified must correspond to a "flag"
332 in the userflags table. E.g., { circulate => 1 } would specify
333 that the user must have the "circulate" privilege in order to
334 proceed. To make sure that access control is correct, the
335 C<$flagsrequired> parameter must be specified correctly.
337 The C<$type> argument specifies whether the template should be
338 retrieved from the opac or intranet directory tree. "opac" is
339 assumed if it is not specified; however, if C<$type> is specified,
340 "intranet" is assumed if it is not "opac".
342 If C<$query> does not have a valid session ID associated with it
343 (i.e., the user has not logged in) or if the session has expired,
344 C<&checkauth> presents the user with a login page (from the point of
345 view of the original script, C<&checkauth> does not return). Once the
346 user has authenticated, C<&checkauth> restarts the original script
347 (this time, C<&checkauth> returns).
349 The login page is provided using a HTML::Template, which is set in the
350 systempreferences table or at the top of this file. The variable C<$type>
351 selects which template to use, either the opac or the intranet
352 authentification template.
354 C<&checkauth> returns a user ID, a cookie, and a session ID. The
355 cookie should be sent back to the browser; it verifies that the user
362 # warn "Checking Auth";
363 # $authnotrequired will be set for scripts which will run without authentication
364 my $authnotrequired = shift;
365 my $flagsrequired = shift;
367 $type = 'opac' unless $type;
369 my $dbh = C4::Context->dbh;
370 my $timeout = C4::Context->preference('timeout');
371 $timeout = 600 unless $timeout;
376 my ( $userid, $cookie, $sessionID, $flags );
377 my $logout = $query->param('logout.x');
378 if ( $userid = $ENV{'REMOTE_USER'} ) {
379 # Using Basic Authentication, no cookies required
380 $cookie = $query->cookie(
381 -name => 'CGISESSID',
387 elsif ( $sessionID = $query->cookie("CGISESSID")) {
388 my $session = new CGI::Session("driver:MySQL", $sessionID, {Handle=>$dbh});
389 C4::Context->_new_userenv($sessionID);
391 C4::Context::set_userenv(
392 $session->param('number'), $session->param('id'),
393 $session->param('cardnumber'), $session->param('firstname'),
394 $session->param('surname'), $session->param('branch'),
395 $session->param('branchname'), $session->param('flags'),
396 $session->param('emailaddress'), $session->param('branchprinter')
402 $ip = $session->param('ip');
403 $lasttime = $session->param('lasttime');
404 $userid = $session->param('id');
410 # voluntary logout the user
412 C4::Context->_unset_userenv($sessionID);
415 open L, ">>/tmp/sessionlog";
416 my $time = localtime( time() );
417 printf L "%20s from %16s logged out at %30s (manually).\n", $userid,
422 if ( $lasttime < time() - $timeout ) {
424 $info{'timed_out'} = 1;
426 C4::Context->_unset_userenv($sessionID);
429 open L, ">>/tmp/sessionlog";
430 my $time = localtime( time() );
431 printf L "%20s from %16s logged out at %30s (inactivity).\n",
435 elsif ( $ip ne $ENV{'REMOTE_ADDR'} ) {
436 # Different ip than originally logged in from
437 $info{'oldip'} = $ip;
438 $info{'newip'} = $ENV{'REMOTE_ADDR'};
439 $info{'different_ip'} = 1;
441 C4::Context->_unset_userenv($sessionID);
444 open L, ">>/tmp/sessionlog";
445 my $time = localtime( time() );
447 "%20s from logged out at %30s (ip changed from %16s to %16s).\n",
448 $userid, $time, $ip, $info{'newip'};
452 $cookie = $query->cookie( CGISESSID => $session->id );
453 $session->param('lasttime',time());
454 $flags = haspermission( $dbh, $userid, $flagsrequired );
459 $info{'nopermission'} = 1;
465 my $session = new CGI::Session("driver:MySQL", undef, {Handle=>$dbh});
468 $sessionID = $session->id;
470 $userid = $query->param('userid');
471 C4::Context->_new_userenv($sessionID);
472 my $password = $query->param('password');
473 C4::Context->_new_userenv($sessionID);
474 my ( $return, $cardnumber ) = checkpw( $dbh, $userid, $password );
476 open L, ">>/tmp/sessionlog";
477 my $time = localtime( time() );
478 printf L "%20s from %16s logged in at %30s.\n", $userid,
479 $ENV{'REMOTE_ADDR'}, $time;
481 $cookie = $query->cookie(CGISESSID => $sessionID);
482 if ( $flags = haspermission( $dbh, $userid, $flagsrequired ) ) {
486 $info{'nopermission'} = 1;
487 C4::Context->_unset_userenv($sessionID);
489 if ( $return == 1 ) {
491 $borrowernumber, $firstname, $surname,
492 $userflags, $branchcode, $branchname,
493 $branchprinter, $emailaddress
497 "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=?"
499 $sth->execute($userid);
501 $borrowernumber, $firstname, $surname,
502 $userflags, $branchcode, $branchname,
503 $branchprinter, $emailaddress
508 # warn "$cardnumber,$borrowernumber,$userid,$firstname,$surname,$userflags,$branchcode,$emailaddress";
509 unless ( $sth->rows ) {
512 "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=?"
514 $sth->execute($cardnumber);
516 $borrowernumber, $firstname, $surname,
517 $userflags, $branchcode, $branchname,
518 $branchprinter, $emailaddress
523 # warn "$cardnumber,$borrowernumber,$userid,$firstname,$surname,$userflags,$branchcode,$emailaddress";
524 unless ( $sth->rows ) {
525 $sth->execute($userid);
527 $borrowernumber, $firstname, $surname, $userflags,
528 $branchcode, $branchname, $branchprinter, $emailaddress
535 # launch a sequence to check if we have a ip for the branch, if we have one we replace the branchcode of the userenv by the branch bound in the ip.
536 my $ip = $ENV{'REMOTE_ADDR'};
537 # if they specify at login, use that
538 if ($query->param('branch')) {
539 $branchcode = $query->param('branch');
540 $branchname = GetBranchName($branchcode);
542 my $branches = GetBranches();
544 foreach my $br ( keys %$branches ) {
545 # now we work with the treatment of ip
546 my $domain = $branches->{$br}->{'branchip'};
547 if ( $domain && $ip =~ /^$domain/ ) {
548 $branchcode = $branches->{$br}->{'branchcode'};
550 # new op dev : add the branchprinter and branchname in the cookie
551 $branchprinter = $branches->{$br}->{'branchprinter'};
552 $branchname = $branches->{$br}->{'branchname'};
555 $session->param('number',$borrowernumber);
556 $session->param('id',$userid);
557 $session->param('cardnumber',$cardnumber);
558 $session->param('firstname',$firstname);
559 $session->param('surname',$surname);
560 $session->param('branch',$branchcode);
561 $session->param('branchname',$branchname);
562 $session->param('flags',$userflags);
563 $session->param('emailaddress',$emailaddress);
564 $session->param('ip',$session->remote_addr());
565 $session->param('lasttime',time());
566 $session->param('branchprinter',$branchprinter);
568 elsif ( $return == 2 ) {
569 #We suppose the user is the superlibrarian
570 $session->param('number',0);
571 $session->param('id',C4::Context->config('user'));
572 $session->param('cardnumber',C4::Context->config('user'));
573 $session->param('firstname',C4::Context->config('user'));
574 $session->param('surname',C4::Context->config('user'),);
575 $session->param('branch','NO_LIBRARY_SET');
576 $session->param('branchname','NO_LIBRARY_SET');
577 $session->param('flags',1);
578 $session->param('emailaddress', C4::Context->preference('KohaAdminEmailAddress'));
579 $session->param('ip',$session->remote_addr());
580 $session->param('lasttime',time());
583 C4::Context::set_userenv(
584 $session->param('number'), $session->param('id'),
585 $session->param('cardnumber'), $session->param('firstname'),
586 $session->param('surname'), $session->param('branch'),
587 $session->param('branchname'), $session->param('flags'),
588 $session->param('emailaddress'), $session->param('branchprinter')
595 $info{'invalid_username_or_password'} = 1;
596 C4::Context->_unset_userenv($sessionID);
600 my $insecure = C4::Context->boolean_preference('insecure');
602 # finished authentification, now respond
603 if ( $loggedin || $authnotrequired || ( defined($insecure) && $insecure ) )
607 $cookie = $query->cookie( CGISESSID => ''
610 return ( $userid, $cookie, $sessionID, $flags );
616 # AUTH rejected, show the login/password template, after checking the DB.
620 # get the inputs from the incoming query
622 foreach my $name ( param $query) {
623 (next) if ( $name eq 'userid' || $name eq 'password' );
624 my $value = $query->param($name);
625 push @inputs, { name => $name, value => $value };
627 # get the branchloop, which we need for authentication
628 my $branches = GetBranches();
630 for my $branch_hash (keys %$branches) {
631 push @branch_loop, {branchcode => "$branch_hash", branchname => $branches->{$branch_hash}->{'branchname'}, };
634 # check that database and koha version are the same
635 # there is no DB version, it's a fresh install,
636 # go to web installer
637 warn "about to check version";
638 unless (C4::Context->preference('Version')){
639 if ($type ne 'opac'){
640 warn "Install required, redirecting to Installer";
641 print $query->redirect("/cgi-bin/koha/installer/install.pl");
643 warn "OPAC Install required, redirecting to maintenance";
644 print $query->redirect("/cgi-bin/koha/maintenance.pl");
648 # there is a DB version, compare it to the code version
649 my $kohaversion=C4::Context::KOHAVERSION;
650 # remove the 3 last . to have a Perl number
651 $kohaversion =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
652 # warn "kohaversion : $kohaversion";
653 if (C4::Context->preference('Version') < $kohaversion){
654 if ($type ne 'opac'){
655 warn "Database update needed, redirecting to Installer. Database is ".C4::Context->preference('Version')." and Koha is : ".C4::Context->config("kohaversion");
656 print $query->redirect("/cgi-bin/koha/installer/install.pl?step=3");
658 warn "OPAC :Database update needed, redirecting to maintenance. Database is ".C4::Context->preference('Version')." and Koha is : ".C4::Context->config("kohaversion");
659 print $query->redirect("/cgi-bin/koha/maintenance.pl");
664 if ( $type eq 'opac' ) {
665 $template_name = "opac-auth.tmpl";
668 $template_name = "auth.tmpl";
670 my $template = gettemplate( $template_name, $type, $query );
671 $template->param(branchloop => \@branch_loop,);
675 suggestion => C4::Context->preference("suggestion"),
676 virtualshelves => C4::Context->preference("virtualshelves"),
677 opaclargeimage => C4::Context->preference("opaclargeimage"),
678 LibraryName => C4::Context->preference("LibraryName"),
679 OpacNav => C4::Context->preference("OpacNav"),
680 opaccredits => C4::Context->preference("opaccredits"),
681 opacreadinghistory => C4::Context->preference("opacreadinghistory"),
682 opacsmallimage => C4::Context->preference("opacsmallimage"),
683 opaclayoutstylesheet => C4::Context->preference("opaclayoutstylesheet"),
684 opaccolorstylesheet => C4::Context->preference("opaccolorstylesheet"),
685 opaclanguagesdisplay => C4::Context->preference("opaclanguagesdisplay"),
686 opacuserjs => C4::Context->preference("opacuserjs"),
688 intranetcolorstylesheet =>
689 C4::Context->preference("intranetcolorstylesheet"),
690 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
691 IntranetNav => C4::Context->preference("IntranetNav"),
692 intranetuserjs => C4::Context->preference("intranetuserjs"),
693 TemplateEncoding => C4::Context->preference("TemplateEncoding"),
696 $template->param( loginprompt => 1 ) unless $info{'nopermission'};
698 my $self_url = $query->url( -absolute => 1 );
701 LibraryName => => C4::Context->preference("LibraryName"),
703 $template->param( \%info );
704 # $cookie = $query->cookie(CGISESSID => $session->id
706 print $query->header(
707 -type => 'text/html',
717 my ( $dbh, $userid, $password ) = @_;
722 "select password,cardnumber,borrowernumber,userid,firstname,surname,branchcode,flags from borrowers where userid=?"
724 $sth->execute($userid);
726 my ( $md5password, $cardnumber, $borrowernumber, $userid, $firstname,
727 $surname, $branchcode, $flags )
729 if ( md5_base64($password) eq $md5password ) {
731 C4::Context->set_userenv( "$borrowernumber", $userid, $cardnumber,
732 $firstname, $surname, $branchcode, $flags );
733 return 1, $cardnumber;
738 "select password,cardnumber,borrowernumber,userid, firstname,surname,branchcode,flags from borrowers where cardnumber=?"
740 $sth->execute($userid);
742 my ( $md5password, $cardnumber, $borrowernumber, $userid, $firstname,
743 $surname, $branchcode, $flags )
745 if ( md5_base64($password) eq $md5password ) {
747 C4::Context->set_userenv( $borrowernumber, $userid, $cardnumber,
748 $firstname, $surname, $branchcode, $flags );
752 if ( $userid && $userid eq C4::Context->config('user')
753 && "$password" eq C4::Context->config('pass') )
756 # Koha superuser account
757 # C4::Context->set_userenv(0,0,C4::Context->config('user'),C4::Context->config('user'),C4::Context->config('user'),"",1);
760 if ( $userid && $userid eq 'demo'
761 && "$password" eq 'demo'
762 && C4::Context->config('demo') )
765 # DEMO => the demo user is allowed to do everything (if demo set to 1 in koha.conf
766 # some features won't be effective : modify systempref, modify MARC structure,
773 my $cardnumber = shift;
776 my $sth = $dbh->prepare("SELECT flags FROM borrowers WHERE cardnumber=?");
777 $sth->execute($cardnumber);
778 my ($flags) = $sth->fetchrow;
779 $flags = 0 unless $flags;
780 $sth = $dbh->prepare("SELECT bit, flag, defaulton FROM userflags");
783 while ( my ( $bit, $flag, $defaulton ) = $sth->fetchrow ) {
784 if ( ( $flags & ( 2**$bit ) ) || $defaulton ) {
785 $userflags->{$flag} = 1;
788 $userflags->{$flag} = 0;
795 my ( $dbh, $userid, $flagsrequired ) = @_;
796 my $sth = $dbh->prepare("SELECT cardnumber FROM borrowers WHERE userid=?");
797 $sth->execute($userid);
798 my ($cardnumber) = $sth->fetchrow;
799 ($cardnumber) || ( $cardnumber = $userid );
800 my $flags = getuserflags( $cardnumber, $dbh );
802 if ( $userid eq C4::Context->config('user') ) {
804 # Super User Account from /etc/koha.conf
805 $flags->{'superlibrarian'} = 1;
807 if ( $userid eq 'demo' && C4::Context->config('demo') ) {
809 # Demo user that can do "anything" (demo=1 in /etc/koha.conf)
810 $flags->{'superlibrarian'} = 1;
812 return $flags if $flags->{superlibrarian};
813 foreach ( keys %$flagsrequired ) {
814 return $flags if $flags->{$_};
819 sub getborrowernumber {
821 my $dbh = C4::Context->dbh;
822 for my $field ( 'userid', 'cardnumber' ) {
824 $dbh->prepare("select borrowernumber from borrowers where $field=?");
825 $sth->execute($userid);
827 my ($bnumber) = $sth->fetchrow;
834 END { } # module clean-up code here (global destructor)