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) {
135 $template->param( loggedinusername => $user );
136 $template->param( sessionID => $sessionID );
138 $borrowernumber = getborrowernumber($user);
139 my ( $borr, $alternativeflags ) =
140 GetMemberDetails( $borrowernumber );
143 $template->param( "USER_INFO" => \@bordat );
145 # We are going to use the $flags returned by checkauth
146 # to create the template's parameters that will indicate
147 # which menus the user can access.
148 if (( $flags && $flags->{superlibrarian}==1) or $insecure==1) {
149 $template->param( CAN_user_circulate => 1 );
150 $template->param( CAN_user_catalogue => 1 );
151 $template->param( CAN_user_parameters => 1 );
152 $template->param( CAN_user_borrowers => 1 );
153 $template->param( CAN_user_permission => 1 );
154 $template->param( CAN_user_reserveforothers => 1 );
155 $template->param( CAN_user_borrow => 1 );
156 $template->param( CAN_user_editcatalogue => 1 );
157 $template->param( CAN_user_updatecharge => 1 );
158 $template->param( CAN_user_acquisition => 1 );
159 $template->param( CAN_user_management => 1 );
160 $template->param( CAN_user_tools => 1 );
161 $template->param( CAN_user_editauthorities => 1 );
162 $template->param( CAN_user_serials => 1 );
163 $template->param( CAN_user_reports => 1 );
166 if ( $flags && $flags->{circulate} == 1 ) {
167 $template->param( CAN_user_circulate => 1 );
170 if ( $flags && $flags->{catalogue} == 1 ) {
171 $template->param( CAN_user_catalogue => 1 );
174 if ( $flags && $flags->{parameters} == 1 ) {
175 $template->param( CAN_user_parameters => 1 );
176 $template->param( CAN_user_management => 1 );
179 if ( $flags && $flags->{borrowers} == 1 ) {
180 $template->param( CAN_user_borrowers => 1 );
183 if ( $flags && $flags->{permissions} == 1 ) {
184 $template->param( CAN_user_permission => 1 );
187 if ( $flags && $flags->{reserveforothers} == 1 ) {
188 $template->param( CAN_user_reserveforothers => 1 );
191 if ( $flags && $flags->{borrow} == 1 ) {
192 $template->param( CAN_user_borrow => 1 );
195 if ( $flags && $flags->{editcatalogue} == 1 ) {
196 $template->param( CAN_user_editcatalogue => 1 );
199 if ( $flags && $flags->{updatecharges} == 1 ) {
200 $template->param( CAN_user_updatecharge => 1 );
203 if ( $flags && $flags->{acquisition} == 1 ) {
204 $template->param( CAN_user_acquisition => 1 );
207 if ( $flags && $flags->{tools} == 1 ) {
208 $template->param( CAN_user_tools => 1 );
211 if ( $flags && $flags->{editauthorities} == 1 ) {
212 $template->param( CAN_user_editauthorities => 1 );
215 if ( $flags && $flags->{serials} == 1 ) {
216 $template->param( CAN_user_serials => 1 );
219 if ( $flags && $flags->{reports} == 1 ) {
220 $template->param( CAN_user_reports => 1 );
223 if ( $in->{'type'} eq "intranet" ) {
225 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
226 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
227 IntranetNav => C4::Context->preference("IntranetNav"),
228 intranetuserjs => C4::Context->preference("intranetuserjs"),
229 TemplateEncoding => C4::Context->preference("TemplateEncoding"),
230 AmazonContent => C4::Context->preference("AmazonContent"),
231 LibraryName => C4::Context->preference("LibraryName"),
232 LoginBranchcode => (C4::Context->userenv?C4::Context->userenv->{"branch"}:"insecure"),
233 LoginBranchname => (C4::Context->userenv?C4::Context->userenv->{"branchname"}:"insecure"),
234 LoginBranchnameShort => substr((C4::Context->userenv?C4::Context->userenv->{"branchname"}:"insecure"),0,10),
235 AutoLocation => C4::Context->preference("AutoLocation"),
236 hide_marc => C4::Context->preference("hide_marc"),
237 patronimages => C4::Context->preference("patronimages"),
238 "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
239 advancedMARCEditor => C4::Context->preference("advancedMARCEditor"),
240 suggestion => C4::Context->preference("suggestion"),
241 virtualshelves => C4::Context->preference("virtualshelves"),
242 LibraryName => C4::Context->preference("LibraryName"),
243 KohaAdminEmailAddress => "" . C4::Context->preference("KohaAdminEmailAddress"),
247 warn "template type should be OPAC, here it is=[" . $in->{'type'} . "]"
248 unless ( $in->{'type'} eq 'opac' );
249 my $LibraryNameTitle = C4::Context->preference("LibraryName");
250 $LibraryNameTitle =~ s/<(?:\/?)(?:br|p)\s*(?:\/?)>/ /sgi;
251 $LibraryNameTitle =~ s/<(?:[^<>'"]|'(?:[^']*)'|"(?:[^"]*)")*>//sg;
253 KohaAdminEmailAddress => "" . C4::Context->preference("KohaAdminEmailAddress"),
254 suggestion => "" . C4::Context->preference("suggestion"),
255 virtualshelves => "" . C4::Context->preference("virtualshelves"),
256 OpacNav => "" . C4::Context->preference("OpacNav"),
257 opacheader => "" . C4::Context->preference("opacheader"),
258 opaccredits => "" . C4::Context->preference("opaccredits"),
259 opacsmallimage => "" . C4::Context->preference("opacsmallimage"),
260 opaclargeimage => "" . C4::Context->preference("opaclargeimage"),
261 opaclayoutstylesheet => "". C4::Context->preference("opaclayoutstylesheet"),
262 opaccolorstylesheet => "". C4::Context->preference("opaccolorstylesheet"),
263 opaclanguagesdisplay => "". C4::Context->preference("opaclanguagesdisplay"),
264 opacuserlogin => "" . C4::Context->preference("opacuserlogin"),
265 opacbookbag => "" . C4::Context->preference("opacbookbag"),
266 TemplateEncoding => "". C4::Context->preference("TemplateEncoding"),
267 AmazonContent => "" . C4::Context->preference("AmazonContent"),
268 LibraryName => "" . C4::Context->preference("LibraryName"),
269 LibraryNameTitle => "" . $LibraryNameTitle,
270 LoginBranchcode => (C4::Context->userenv?C4::Context->userenv->{"branch"}:"insecure"),
271 LoginBranchname => C4::Context->userenv?C4::Context->userenv->{"branchname"}:"",
272 OpacPasswordChange => C4::Context->preference("OpacPasswordChange"),
273 opacreadinghistory => C4::Context->preference("opacreadinghistory"),
274 opacuserjs => C4::Context->preference("opacuserjs"),
275 OpacCloud => C4::Context->preference("OpacCloud"),
276 OpacTopissue => C4::Context->preference("OpacTopissue"),
277 OpacAuthorities => C4::Context->preference("OpacAuthorities"),
278 OpacBrowser => C4::Context->preference("OpacBrowser"),
279 RequestOnOpac => C4::Context->preference("RequestOnOpac"),
280 reviewson => C4::Context->preference("reviewson"),
281 hide_marc => C4::Context->preference("hide_marc"),
282 patronimages => C4::Context->preference("patronimages"),
283 "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
286 return ( $template, $borrowernumber, $cookie );
291 ($userid, $cookie, $sessionID) = &checkauth($query, $noauth, $flagsrequired, $type);
293 Verifies that the user is authorized to run this script. If
294 the user is authorized, a (userid, cookie, session-id, flags)
295 quadruple is returned. If the user is not authorized but does
296 not have the required privilege (see $flagsrequired below), it
297 displays an error page and exits. Otherwise, it displays the
298 login page and exits.
300 Note that C<&checkauth> will return if and only if the user
301 is authorized, so it should be called early on, before any
302 unfinished operations (e.g., if you've opened a file, then
303 C<&checkauth> won't close it for you).
305 C<$query> is the CGI object for the script calling C<&checkauth>.
307 The C<$noauth> argument is optional. If it is set, then no
308 authorization is required for the script.
310 C<&checkauth> fetches user and session information from C<$query> and
311 ensures that the user is authorized to run scripts that require
314 The C<$flagsrequired> argument specifies the required privileges
315 the user must have if the username and password are correct.
316 It should be specified as a reference-to-hash; keys in the hash
317 should be the "flags" for the user, as specified in the Members
318 intranet module. Any key specified must correspond to a "flag"
319 in the userflags table. E.g., { circulate => 1 } would specify
320 that the user must have the "circulate" privilege in order to
321 proceed. To make sure that access control is correct, the
322 C<$flagsrequired> parameter must be specified correctly.
324 The C<$type> argument specifies whether the template should be
325 retrieved from the opac or intranet directory tree. "opac" is
326 assumed if it is not specified; however, if C<$type> is specified,
327 "intranet" is assumed if it is not "opac".
329 If C<$query> does not have a valid session ID associated with it
330 (i.e., the user has not logged in) or if the session has expired,
331 C<&checkauth> presents the user with a login page (from the point of
332 view of the original script, C<&checkauth> does not return). Once the
333 user has authenticated, C<&checkauth> restarts the original script
334 (this time, C<&checkauth> returns).
336 The login page is provided using a HTML::Template, which is set in the
337 systempreferences table or at the top of this file. The variable C<$type>
338 selects which template to use, either the opac or the intranet
339 authentification template.
341 C<&checkauth> returns a user ID, a cookie, and a session ID. The
342 cookie should be sent back to the browser; it verifies that the user
350 # $authnotrequired will be set for scripts which will run without authentication
351 my $authnotrequired = shift;
352 my $flagsrequired = shift;
354 $type = 'opac' unless $type;
356 my $dbh = C4::Context->dbh;
357 # check that database and koha version are the same
358 unless (C4::Context->preference('Version')){
359 if ($type ne 'opac'){
360 warn "Install required, redirecting to Installer";
361 print $query->redirect("/cgi-bin/koha/installer/install.pl");
363 warn "OPAC Install required, redirecting to maintenance";
364 print $query->redirect("/cgi-bin/koha/maintenance.pl");
368 if (C4::Context->preference('Version') < C4::Context->config("kohaversion")){
369 if ($type ne 'opac'){
370 warn "Database update needed, redirecting to Installer. Database is ".C4::Context->preference('Version')." and Koha is : ".C4::Context->config("kohaversion");
371 print $query->redirect("/cgi-bin/koha/installer/install.pl?step=3");
373 warn "OPAC :Database update needed, redirecting to maintenance. Database is ".C4::Context->preference('Version')." and Koha is : ".C4::Context->config("kohaversion");
374 print $query->redirect("/cgi-bin/koha/maintenance.pl");
378 my $timeout = C4::Context->preference('timeout');
379 $timeout = 600 unless $timeout;
382 if ( $type eq 'opac' ) {
383 $template_name = "opac-auth.tmpl";
386 $template_name = "auth.tmpl";
392 my ( $userid, $cookie, $sessionID, $flags, $envcookie );
393 my $logout = $query->param('logout.x');
394 if ( $userid = $ENV{'REMOTE_USER'} ) {
396 # Using Basic Authentication, no cookies required
397 $cookie = $query->cookie(
398 -name => 'CGISESSID',
404 elsif ( $sessionID = $query->cookie("CGISESSID")) {
405 my $session = new CGI::Session("driver:MySQL", $sessionID, {Handle=>$dbh});
407 C4::Context->_new_userenv($sessionID);
409 C4::Context::set_userenv(
410 $session->param('number'), $session->param('id'),
411 $session->param('cardnumber'), $session->param('firstname'),
412 $session->param('surname'), $session->param('branch'),
413 $session->param('branchname'), $session->param('flags'),
414 $session->param('emailaddress'), $session->param('branchprinter')
417 my $ip=$session->param('ip');
418 $userid = $session->param('id');
419 my $lasttime = $session->param('lasttime');
422 # voluntary logout the user
424 C4::Context->_unset_userenv($sessionID);
427 open L, ">>/tmp/sessionlog";
428 my $time = localtime( time() );
429 printf L "%20s from %16s logged out at %30s (manually).\n", $userid,
435 if ( $lasttime < time() - $timeout ) {
438 $info{'timed_out'} = 1;
440 C4::Context->_unset_userenv($sessionID);
443 open L, ">>/tmp/sessionlog";
444 my $time = localtime( time() );
445 printf L "%20s from %16s logged out at %30s (inactivity).\n",
449 elsif ( $ip ne $ENV{'REMOTE_ADDR'} ) {
451 # Different ip than originally logged in from
452 $info{'oldip'} = $ip;
453 $info{'newip'} = $ENV{'REMOTE_ADDR'};
454 $info{'different_ip'} = 1;
456 C4::Context->_unset_userenv($sessionID);
459 open L, ">>/tmp/sessionlog";
460 my $time = localtime( time() );
462 "%20s from logged out at %30s (ip changed from %16s to %16s).\n",
463 $userid, $time, $ip, $info{'newip'};
467 $cookie = $query->cookie(CGISESSID => $session->id
469 $session->param('lasttime',time());
470 $flags = haspermission( $dbh, $userid, $flagsrequired );
475 $info{'nopermission'} = 1;
481 my $session = new CGI::Session("driver:MySQL", undef, {Handle=>$dbh});
482 my $sessionID = $session->id;
483 $userid = $query->param('userid');
484 C4::Context->_new_userenv($sessionID);
485 my $password = $query->param('password');
486 C4::Context->_new_userenv($sessionID);
487 my ( $return, $cardnumber ) = checkpw( $dbh, $userid, $password );
489 open L, ">>/tmp/sessionlog";
490 my $time = localtime( time() );
491 printf L "%20s from %16s logged in at %30s.\n", $userid,
492 $ENV{'REMOTE_ADDR'}, $time;
494 $cookie = $query->cookie(CGISESSID => $sessionID);
495 if ( $flags = haspermission( $dbh, $userid, $flagsrequired ) ) {
499 $info{'nopermission'} = 1;
500 C4::Context->_unset_userenv($sessionID);
502 if ( $return == 1 ) {
504 $borrowernumber, $firstname, $surname,
505 $userflags, $branchcode, $branchname,
506 $branchprinter, $emailaddress
510 "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=?"
512 $sth->execute($userid);
514 $borrowernumber, $firstname, $surname,
515 $userflags, $branchcode, $branchname,
516 $branchprinter, $emailaddress
521 # warn "$cardnumber,$borrowernumber,$userid,$firstname,$surname,$userflags,$branchcode,$emailaddress";
522 unless ( $sth->rows ) {
525 "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=?"
527 $sth->execute($cardnumber);
529 $borrowernumber, $firstname, $surname,
530 $userflags, $branchcode, $branchname,
531 $branchprinter, $emailaddress
536 # warn "$cardnumber,$borrowernumber,$userid,$firstname,$surname,$userflags,$branchcode,$emailaddress";
537 unless ( $sth->rows ) {
538 $sth->execute($userid);
540 $borrowernumber, $firstname, $surname, $userflags,
541 $branchcode, $branchname, $branchprinter, $emailaddress
547 # warn "$cardnumber,$borrowernumber,$userid,$firstname,$surname,$userflags,$branchcode,$emailaddress";
550 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
552 # 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.
553 my $ip = $ENV{'REMOTE_ADDR'};
554 my $branches = GetBranches();
556 foreach my $br ( keys %$branches ) {
558 # now we work with the treatment of ip
559 my $domain = $branches->{$br}->{'branchip'};
560 if ( $domain && $ip =~ /^$domain/ ) {
561 $branchcode = $branches->{$br}->{'branchcode'};
563 # new op dev : add the branchprinter and branchname in the cookie
564 $branchprinter = $branches->{$br}->{'branchprinter'};
565 $branchname = $branches->{$br}->{'branchname'};
569 $session->param('number',$borrowernumber);
570 $session->param('id',$userid);
571 $session->param('cardnumber',$cardnumber);
572 $session->param('firstname',$firstname);
573 $session->param('surname',$surname);
574 $session->param('branch',$branchcode);
575 $session->param('branchname',$branchname);
576 $session->param('flags',$userflags);
577 $session->param('emailaddress',$emailaddress);
578 $session->param('ip',$session->remote_addr());
579 $session->param('lasttime',time());
581 elsif ( $return == 2 ) {
583 #We suppose the user is the superlibrarian
584 $session->param('number',0);
585 $session->param('id',C4::Context->config('user'));
586 $session->param('cardnumber',C4::Context->config('user'));
587 $session->param('firstname',C4::Context->config('user'));
588 $session->param('surname',C4::Context->config('user'),);
589 $session->param('branch','NO_LIBRARY_SET');
590 $session->param('branchname','NO_LIBRARY_SET');
591 $session->param('flags',1);
592 $session->param('emailaddress', C4::Context->preference('KohaAdminEmailAddress'));
593 $session->param('ip',$session->remote_addr());
594 $session->param('lasttime',time());
599 $info{'invalid_username_or_password'} = 1;
600 C4::Context->_unset_userenv($sessionID);
604 my $insecure = C4::Context->boolean_preference('insecure');
606 # finished authentification, now respond
607 if ( $loggedin || $authnotrequired || ( defined($insecure) && $insecure ) )
611 $cookie = $query->cookie( CGISESSID => ''
614 return ( $userid, $cookie, $sessionID, $flags );
618 # else we have a problem...
619 # get the inputs from the incoming query
621 foreach my $name ( param $query) {
622 (next) if ( $name eq 'userid' || $name eq 'password' );
623 my $value = $query->param($name);
624 push @inputs, { name => $name, value => $value };
627 my $template = gettemplate( $template_name, $type, $query );
630 suggestion => C4::Context->preference("suggestion"),
631 virtualshelves => C4::Context->preference("virtualshelves"),
632 opaclargeimage => C4::Context->preference("opaclargeimage"),
633 LibraryName => C4::Context->preference("LibraryName"),
634 OpacNav => C4::Context->preference("OpacNav"),
635 opaccredits => C4::Context->preference("opaccredits"),
636 opacreadinghistory => C4::Context->preference("opacreadinghistory"),
637 opacsmallimage => C4::Context->preference("opacsmallimage"),
638 opaclayoutstylesheet => C4::Context->preference("opaclayoutstylesheet"),
639 opaccolorstylesheet => C4::Context->preference("opaccolorstylesheet"),
640 opaclanguagesdisplay => C4::Context->preference("opaclanguagesdisplay"),
641 opacuserjs => C4::Context->preference("opacuserjs"),
643 intranetcolorstylesheet =>
644 C4::Context->preference("intranetcolorstylesheet"),
645 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
646 IntranetNav => C4::Context->preference("IntranetNav"),
647 intranetuserjs => C4::Context->preference("intranetuserjs"),
648 TemplateEncoding => C4::Context->preference("TemplateEncoding"),
651 $template->param( loginprompt => 1 ) unless $info{'nopermission'};
653 my $self_url = $query->url( -absolute => 1 );
656 LibraryName => => C4::Context->preference("LibraryName"),
658 $template->param( \%info );
659 # $cookie = $query->cookie(CGISESSID => $session->id
661 print $query->header(
671 my ( $dbh, $userid, $password ) = @_;
676 "select password,cardnumber,borrowernumber,userid,firstname,surname,branchcode,flags from borrowers where userid=?"
678 $sth->execute($userid);
680 my ( $md5password, $cardnumber, $borrowernumber, $userid, $firstname,
681 $surname, $branchcode, $flags )
683 if ( md5_base64($password) eq $md5password ) {
685 C4::Context->set_userenv( "$borrowernumber", $userid, $cardnumber,
686 $firstname, $surname, $branchcode, $flags );
687 return 1, $cardnumber;
692 "select password,cardnumber,borrowernumber,userid, firstname,surname,branchcode,flags from borrowers where cardnumber=?"
694 $sth->execute($userid);
696 my ( $md5password, $cardnumber, $borrowernumber, $userid, $firstname,
697 $surname, $branchcode, $flags )
699 if ( md5_base64($password) eq $md5password ) {
701 C4::Context->set_userenv( $borrowernumber, $userid, $cardnumber,
702 $firstname, $surname, $branchcode, $flags );
706 if ( $userid && $userid eq C4::Context->config('user')
707 && "$password" eq C4::Context->config('pass') )
710 # Koha superuser account
711 # C4::Context->set_userenv(0,0,C4::Context->config('user'),C4::Context->config('user'),C4::Context->config('user'),"",1);
714 if ( $userid && $userid eq 'demo'
715 && "$password" eq 'demo'
716 && C4::Context->config('demo') )
719 # DEMO => the demo user is allowed to do everything (if demo set to 1 in koha.conf
720 # some features won't be effective : modify systempref, modify MARC structure,
727 my $cardnumber = shift;
730 my $sth = $dbh->prepare("SELECT flags FROM borrowers WHERE cardnumber=?");
731 $sth->execute($cardnumber);
732 my ($flags) = $sth->fetchrow;
733 $flags = 0 unless $flags;
734 $sth = $dbh->prepare("SELECT bit, flag, defaulton FROM userflags");
737 while ( my ( $bit, $flag, $defaulton ) = $sth->fetchrow ) {
738 if ( ( $flags & ( 2**$bit ) ) || $defaulton ) {
739 $userflags->{$flag} = 1;
742 $userflags->{$flag} = 0;
749 my ( $dbh, $userid, $flagsrequired ) = @_;
750 my $sth = $dbh->prepare("SELECT cardnumber FROM borrowers WHERE userid=?");
751 $sth->execute($userid);
752 my ($cardnumber) = $sth->fetchrow;
753 ($cardnumber) || ( $cardnumber = $userid );
754 my $flags = getuserflags( $cardnumber, $dbh );
756 if ( $userid eq C4::Context->config('user') ) {
758 # Super User Account from /etc/koha.conf
759 $flags->{'superlibrarian'} = 1;
761 if ( $userid eq 'demo' && C4::Context->config('demo') ) {
763 # Demo user that can do "anything" (demo=1 in /etc/koha.conf)
764 $flags->{'superlibrarian'} = 1;
766 return $flags if $flags->{superlibrarian};
767 foreach ( keys %$flagsrequired ) {
768 return $flags if $flags->{$_};
773 sub getborrowernumber {
775 my $dbh = C4::Context->dbh;
776 for my $field ( 'userid', 'cardnumber' ) {
778 $dbh->prepare("select borrowernumber from borrowers where $field=?");
779 $sth->execute($userid);
781 my ($bnumber) = $sth->fetchrow;
788 END { } # module clean-up code here (global destructor)