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);
28 use C4::Output; # to get the template
31 use C4::Branch; # GetBranches
34 # use Net::LDAP qw(:all);
36 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
38 # set the version for version checking
39 $VERSION = do { my @v = '$Revision$' =~ /\d+/g;
40 shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v );
45 C4::Auth - Authenticates Koha users
54 my ($template, $borrowernumber, $cookie)
55 = get_template_and_user(
57 template_name => "opac-main.tmpl",
61 flagsrequired => {borrow => 1},
73 The main function of this module is to provide
74 authentification. However the get_template_and_user function has
75 been provided so that a users login information is passed along
76 automatically. This gets loaded into the template.
87 &get_template_and_user
90 =item get_template_and_user
92 my ($template, $borrowernumber, $cookie)
93 = get_template_and_user(
95 template_name => "opac-main.tmpl",
99 flagsrequired => {borrow => 1},
103 This call passes the C<query>, C<flagsrequired> and C<authnotrequired>
104 to C<&checkauth> (in this module) to perform authentification.
105 See C<&checkauth> for an explanation of these parameters.
107 The C<template_name> is then used to find the correct template for
108 the page. The authenticated users details are loaded onto the
109 template in the HTML::Template LOOP variable C<USER_INFO>. Also the
110 C<sessionID> is passed to the template. This can be used in templates
111 if cookies are disabled. It needs to be put as and input to every
114 More information on the C<gettemplate> sub can be found in the
119 sub get_template_and_user {
122 gettemplate( $in->{'template_name'}, $in->{'type'}, $in->{'query'} );
123 my ( $user, $cookie, $sessionID, $flags ) = checkauth(
125 $in->{'authnotrequired'},
126 $in->{'flagsrequired'},
128 ) unless ($in->{'template_name'}=~/maintenance/);
131 my $insecure = C4::Context->preference('insecure');
132 if ($user or $insecure) {
133 $template->param( loggedinusername => $user );
134 $template->param( sessionID => $sessionID );
136 $borrowernumber = getborrowernumber($user);
137 my ( $borr, $alternativeflags ) =
138 GetMemberDetails( $borrowernumber );
141 $template->param( "USER_INFO" => \@bordat );
143 # We are going to use the $flags returned by checkauth
144 # to create the template's parameters that will indicate
145 # which menus the user can access.
146 if (( $flags && $flags->{superlibrarian}==1) or $insecure==1) {
147 $template->param( CAN_user_circulate => 1 );
148 $template->param( CAN_user_catalogue => 1 );
149 $template->param( CAN_user_parameters => 1 );
150 $template->param( CAN_user_borrowers => 1 );
151 $template->param( CAN_user_permission => 1 );
152 $template->param( CAN_user_reserveforothers => 1 );
153 $template->param( CAN_user_borrow => 1 );
154 $template->param( CAN_user_editcatalogue => 1 );
155 $template->param( CAN_user_updatecharge => 1 );
156 $template->param( CAN_user_acquisition => 1 );
157 $template->param( CAN_user_management => 1 );
158 $template->param( CAN_user_tools => 1 );
159 $template->param( CAN_user_editauthorities => 1 );
160 $template->param( CAN_user_serials => 1 );
161 $template->param( CAN_user_reports => 1 );
164 if ( $flags && $flags->{circulate} == 1 ) {
165 $template->param( CAN_user_circulate => 1 );
168 if ( $flags && $flags->{catalogue} == 1 ) {
169 $template->param( CAN_user_catalogue => 1 );
172 if ( $flags && $flags->{parameters} == 1 ) {
173 $template->param( CAN_user_parameters => 1 );
174 $template->param( CAN_user_management => 1 );
177 if ( $flags && $flags->{borrowers} == 1 ) {
178 $template->param( CAN_user_borrowers => 1 );
181 if ( $flags && $flags->{permissions} == 1 ) {
182 $template->param( CAN_user_permission => 1 );
185 if ( $flags && $flags->{reserveforothers} == 1 ) {
186 $template->param( CAN_user_reserveforothers => 1 );
189 if ( $flags && $flags->{borrow} == 1 ) {
190 $template->param( CAN_user_borrow => 1 );
193 if ( $flags && $flags->{editcatalogue} == 1 ) {
194 $template->param( CAN_user_editcatalogue => 1 );
197 if ( $flags && $flags->{updatecharges} == 1 ) {
198 $template->param( CAN_user_updatecharge => 1 );
201 if ( $flags && $flags->{acquisition} == 1 ) {
202 $template->param( CAN_user_acquisition => 1 );
205 if ( $flags && $flags->{tools} == 1 ) {
206 $template->param( CAN_user_tools => 1 );
209 if ( $flags && $flags->{editauthorities} == 1 ) {
210 $template->param( CAN_user_editauthorities => 1 );
213 if ( $flags && $flags->{serials} == 1 ) {
214 $template->param( CAN_user_serials => 1 );
217 if ( $flags && $flags->{reports} == 1 ) {
218 $template->param( CAN_user_reports => 1 );
221 if ( $in->{'type'} eq "intranet" ) {
223 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
224 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
225 IntranetNav => C4::Context->preference("IntranetNav"),
226 intranetuserjs => C4::Context->preference("intranetuserjs"),
227 TemplateEncoding => C4::Context->preference("TemplateEncoding"),
228 AmazonContent => C4::Context->preference("AmazonContent"),
229 LibraryName => C4::Context->preference("LibraryName"),
230 LoginBranchcode => (C4::Context->userenv?C4::Context->userenv->{"branch"}:"insecure"),
231 LoginBranchname => (C4::Context->userenv?C4::Context->userenv->{"branchname"}:"insecure"),
232 LoginBranchnameShort => substr((C4::Context->userenv?C4::Context->userenv->{"branchname"}:"insecure"),0,10),
233 AutoLocation => C4::Context->preference("AutoLocation"),
234 hide_marc => C4::Context->preference("hide_marc"),
235 patronimages => C4::Context->preference("patronimages"),
236 "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
237 advancedMARCEditor => C4::Context->preference("advancedMARCEditor"),
238 suggestion => C4::Context->preference("suggestion"),
239 virtualshelves => C4::Context->preference("virtualshelves"),
240 LibraryName => C4::Context->preference("LibraryName"),
241 KohaAdminEmailAddress => "" . C4::Context->preference("KohaAdminEmailAddress"),
245 warn "template type should be OPAC, here it is=[" . $in->{'type'} . "]"
246 unless ( $in->{'type'} eq 'opac' );
247 my $LibraryNameTitle = C4::Context->preference("LibraryName");
248 $LibraryNameTitle =~ s/<(?:\/?)(?:br|p)\s*(?:\/?)>/ /sgi;
249 $LibraryNameTitle =~ s/<(?:[^<>'"]|'(?:[^']*)'|"(?:[^"]*)")*>//sg;
251 KohaAdminEmailAddress => "" . C4::Context->preference("KohaAdminEmailAddress"),
252 suggestion => "" . C4::Context->preference("suggestion"),
253 virtualshelves => "" . C4::Context->preference("virtualshelves"),
254 OpacNav => "" . C4::Context->preference("OpacNav"),
255 opacheader => "" . C4::Context->preference("opacheader"),
256 opaccredits => "" . C4::Context->preference("opaccredits"),
257 opacsmallimage => "" . C4::Context->preference("opacsmallimage"),
258 opaclargeimage => "" . C4::Context->preference("opaclargeimage"),
259 opaclayoutstylesheet => "". C4::Context->preference("opaclayoutstylesheet"),
260 opaccolorstylesheet => "". C4::Context->preference("opaccolorstylesheet"),
261 opaclanguagesdisplay => "". C4::Context->preference("opaclanguagesdisplay"),
262 opacuserlogin => "" . C4::Context->preference("opacuserlogin"),
263 opacbookbag => "" . C4::Context->preference("opacbookbag"),
264 TemplateEncoding => "". C4::Context->preference("TemplateEncoding"),
265 AmazonContent => "" . C4::Context->preference("AmazonContent"),
266 LibraryName => "" . C4::Context->preference("LibraryName"),
267 LibraryNameTitle => "" . $LibraryNameTitle,
268 LoginBranchcode => (C4::Context->userenv?C4::Context->userenv->{"branch"}:"insecure"),
269 LoginBranchname => C4::Context->userenv?C4::Context->userenv->{"branchname"}:"",
270 OpacPasswordChange => C4::Context->preference("OpacPasswordChange"),
271 opacreadinghistory => C4::Context->preference("opacreadinghistory"),
272 opacuserjs => C4::Context->preference("opacuserjs"),
273 OpacCloud => C4::Context->preference("OpacCloud"),
274 OpacTopissue => C4::Context->preference("OpacTopissue"),
275 OpacAuthorities => C4::Context->preference("OpacAuthorities"),
276 OpacBrowser => C4::Context->preference("OpacBrowser"),
277 RequestOnOpac => C4::Context->preference("RequestOnOpac"),
278 reviewson => C4::Context->preference("reviewson"),
279 hide_marc => C4::Context->preference("hide_marc"),
280 patronimages => C4::Context->preference("patronimages"),
281 "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
284 return ( $template, $borrowernumber, $cookie );
289 ($userid, $cookie, $sessionID) = &checkauth($query, $noauth, $flagsrequired, $type);
291 Verifies that the user is authorized to run this script. If
292 the user is authorized, a (userid, cookie, session-id, flags)
293 quadruple is returned. If the user is not authorized but does
294 not have the required privilege (see $flagsrequired below), it
295 displays an error page and exits. Otherwise, it displays the
296 login page and exits.
298 Note that C<&checkauth> will return if and only if the user
299 is authorized, so it should be called early on, before any
300 unfinished operations (e.g., if you've opened a file, then
301 C<&checkauth> won't close it for you).
303 C<$query> is the CGI object for the script calling C<&checkauth>.
305 The C<$noauth> argument is optional. If it is set, then no
306 authorization is required for the script.
308 C<&checkauth> fetches user and session information from C<$query> and
309 ensures that the user is authorized to run scripts that require
312 The C<$flagsrequired> argument specifies the required privileges
313 the user must have if the username and password are correct.
314 It should be specified as a reference-to-hash; keys in the hash
315 should be the "flags" for the user, as specified in the Members
316 intranet module. Any key specified must correspond to a "flag"
317 in the userflags table. E.g., { circulate => 1 } would specify
318 that the user must have the "circulate" privilege in order to
319 proceed. To make sure that access control is correct, the
320 C<$flagsrequired> parameter must be specified correctly.
322 The C<$type> argument specifies whether the template should be
323 retrieved from the opac or intranet directory tree. "opac" is
324 assumed if it is not specified; however, if C<$type> is specified,
325 "intranet" is assumed if it is not "opac".
327 If C<$query> does not have a valid session ID associated with it
328 (i.e., the user has not logged in) or if the session has expired,
329 C<&checkauth> presents the user with a login page (from the point of
330 view of the original script, C<&checkauth> does not return). Once the
331 user has authenticated, C<&checkauth> restarts the original script
332 (this time, C<&checkauth> returns).
334 The login page is provided using a HTML::Template, which is set in the
335 systempreferences table or at the top of this file. The variable C<$type>
336 selects which template to use, either the opac or the intranet
337 authentification template.
339 C<&checkauth> returns a user ID, a cookie, and a session ID. The
340 cookie should be sent back to the browser; it verifies that the user
348 # $authnotrequired will be set for scripts which will run without authentication
349 my $authnotrequired = shift;
350 my $flagsrequired = shift;
352 $type = 'opac' unless $type;
354 my $dbh = C4::Context->dbh;
355 # check that database and koha version are the same
356 unless (C4::Context->preference('Version')){
357 if ($type ne 'opac'){
358 warn "Install required, redirecting to Installer";
359 print $query->redirect("/cgi-bin/koha/installer/install.pl");
361 warn "OPAC Install required, redirecting to maintenance";
362 print $query->redirect("/cgi-bin/koha/maintenance.pl");
366 if (C4::Context->preference('Version') < C4::Context->config("kohaversion")){
367 if ($type ne 'opac'){
368 warn "Database update needed, redirecting to Installer. Database is ".C4::Context->preference('Version')." and Koha is : ".C4::Context->config("kohaversion");
369 print $query->redirect("/cgi-bin/koha/installer/install.pl?step=3");
371 warn "OPAC :Database update needed, redirecting to maintenance. Database is ".C4::Context->preference('Version')." and Koha is : ".C4::Context->config("kohaversion");
372 print $query->redirect("/cgi-bin/koha/maintenance.pl");
376 my $timeout = C4::Context->preference('timeout');
377 $timeout = 600 unless $timeout;
380 if ( $type eq 'opac' ) {
381 $template_name = "opac-auth.tmpl";
384 $template_name = "auth.tmpl";
390 my ( $userid, $cookie, $sessionID, $flags, $envcookie );
391 my $logout = $query->param('logout.x');
392 if ( $userid = $ENV{'REMOTE_USER'} ) {
394 # Using Basic Authentication, no cookies required
395 $cookie = $query->cookie(
396 -name => 'sessionID',
402 elsif ( $sessionID = $query->cookie('sessionID') ) {
403 C4::Context->_new_userenv($sessionID);
404 if ( my %hash = $query->cookie('userenv') ) {
405 C4::Context::set_userenv(
406 $hash{number}, $hash{id},
407 $hash{cardnumber}, $hash{firstname},
408 $hash{surname}, $hash{branch},
409 $hash{branchname}, $hash{flags},
410 $hash{emailaddress}, $hash{branchprinter}
413 my ( $ip, $lasttime );
415 ( $userid, $ip, $lasttime ) =
416 $dbh->selectrow_array(
417 "SELECT userid,ip,lasttime FROM sessions WHERE sessionid=?",
421 # voluntary logout the user
422 $dbh->do( "DELETE FROM sessions WHERE sessionID=?",
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,
434 if ( $lasttime < time() - $timeout ) {
437 $info{'timed_out'} = 1;
438 $dbh->do( "DELETE FROM sessions WHERE sessionID=?",
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;
455 $dbh->do( "DELETE FROM sessions WHERE sessionID=?",
457 C4::Context->_unset_userenv($sessionID);
460 open L, ">>/tmp/sessionlog";
461 my $time = localtime( time() );
463 "%20s from logged out at %30s (ip changed from %16s to %16s).\n",
464 $userid, $time, $ip, $info{'newip'};
468 $cookie = $query->cookie(
469 -name => 'sessionID',
470 -value => $sessionID,
473 $dbh->do( "UPDATE sessions SET lasttime=? WHERE sessionID=?",
474 undef, ( time(), $sessionID ) );
475 $flags = haspermission( $dbh, $userid, $flagsrequired );
480 $info{'nopermission'} = 1;
486 $sessionID = int( rand() * 100000 ) . '-' . time();
487 $userid = $query->param('userid');
488 C4::Context->_new_userenv($sessionID);
489 my $password = $query->param('password');
490 C4::Context->_new_userenv($sessionID);
491 my ( $return, $cardnumber ) = checkpw( $dbh, $userid, $password );
493 $dbh->do( "DELETE FROM sessions WHERE sessionID=? AND userid=?",
494 undef, ( $sessionID, $userid ) );
496 "INSERT INTO sessions (sessionID, userid, ip,lasttime) VALUES (?, ?, ?, ?)",
498 ( $sessionID, $userid, $ENV{'REMOTE_ADDR'}, time() )
500 open L, ">>/tmp/sessionlog";
501 my $time = localtime( time() );
502 printf L "%20s from %16s logged in at %30s.\n", $userid,
503 $ENV{'REMOTE_ADDR'}, $time;
505 $cookie = $query->cookie(
506 -name => 'sessionID',
507 -value => $sessionID,
510 if ( $flags = haspermission( $dbh, $userid, $flagsrequired ) ) {
514 $info{'nopermission'} = 1;
515 C4::Context->_unset_userenv($sessionID);
517 if ( $return == 1 ) {
519 $borrowernumber, $firstname, $surname,
520 $userflags, $branchcode, $branchname,
521 $branchprinter, $emailaddress
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 userid=?"
527 $sth->execute($userid);
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 ) {
540 "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=?"
542 $sth->execute($cardnumber);
544 $borrowernumber, $firstname, $surname,
545 $userflags, $branchcode, $branchname,
546 $branchprinter, $emailaddress
551 # warn "$cardnumber,$borrowernumber,$userid,$firstname,$surname,$userflags,$branchcode,$emailaddress";
552 unless ( $sth->rows ) {
553 $sth->execute($userid);
555 $borrowernumber, $firstname, $surname, $userflags,
556 $branchcode, $branchname, $branchprinter, $emailaddress
562 # warn "$cardnumber,$borrowernumber,$userid,$firstname,$surname,$userflags,$branchcode,$emailaddress";
565 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
567 # 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.
568 my $ip = $ENV{'REMOTE_ADDR'};
569 my $branches = GetBranches();
571 foreach my $br ( keys %$branches ) {
573 # now we work with the treatment of ip
574 my $domain = $branches->{$br}->{'branchip'};
575 if ( $domain && $ip =~ /^$domain/ ) {
576 $branchcode = $branches->{$br}->{'branchcode'};
578 # new op dev : add the branchprinter and branchname in the cookie
579 $branchprinter = $branches->{$br}->{'branchprinter'};
580 $branchname = $branches->{$br}->{'branchname'};
583 my $hash = C4::Context::set_userenv(
584 $borrowernumber, $userid, $cardnumber,
585 $firstname, $surname, $branchcode,
586 $branchname, $userflags, $emailaddress,
590 $envcookie = $query->cookie(
596 elsif ( $return == 2 ) {
598 #We suppose the user is the superlibrarian
599 my $hash = C4::Context::set_userenv(
602 C4::Context->config('user'),
603 C4::Context->config('user'),
604 C4::Context->config('user'),
608 C4::Context->preference('KohaAdminEmailAddress')
610 $envcookie = $query->cookie(
619 $info{'invalid_username_or_password'} = 1;
620 C4::Context->_unset_userenv($sessionID);
624 my $insecure = C4::Context->boolean_preference('insecure');
626 # finished authentification, now respond
627 if ( $loggedin || $authnotrequired || ( defined($insecure) && $insecure ) )
632 $cookie = $query->cookie(
633 -name => 'sessionID',
639 return ( $userid, [ $cookie, $envcookie ], $sessionID, $flags );
642 return ( $userid, $cookie, $sessionID, $flags );
646 # else we have a problem...
647 # get the inputs from the incoming query
649 foreach my $name ( param $query) {
650 (next) if ( $name eq 'userid' || $name eq 'password' );
651 my $value = $query->param($name);
652 push @inputs, { name => $name, value => $value };
655 my $template = gettemplate( $template_name, $type, $query );
658 suggestion => C4::Context->preference("suggestion"),
659 virtualshelves => C4::Context->preference("virtualshelves"),
660 opaclargeimage => C4::Context->preference("opaclargeimage"),
661 LibraryName => C4::Context->preference("LibraryName"),
662 OpacNav => C4::Context->preference("OpacNav"),
663 opaccredits => C4::Context->preference("opaccredits"),
664 opacreadinghistory => C4::Context->preference("opacreadinghistory"),
665 opacsmallimage => C4::Context->preference("opacsmallimage"),
666 opaclayoutstylesheet => C4::Context->preference("opaclayoutstylesheet"),
667 opaccolorstylesheet => C4::Context->preference("opaccolorstylesheet"),
668 opaclanguagesdisplay => C4::Context->preference("opaclanguagesdisplay"),
669 opacuserjs => C4::Context->preference("opacuserjs"),
671 intranetcolorstylesheet =>
672 C4::Context->preference("intranetcolorstylesheet"),
673 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
674 IntranetNav => C4::Context->preference("IntranetNav"),
675 intranetuserjs => C4::Context->preference("intranetuserjs"),
676 TemplateEncoding => C4::Context->preference("TemplateEncoding"),
679 $template->param( loginprompt => 1 ) unless $info{'nopermission'};
681 my $self_url = $query->url( -absolute => 1 );
684 LibraryName => => C4::Context->preference("LibraryName"),
686 $template->param( \%info );
687 $cookie = $query->cookie(
688 -name => 'sessionID',
689 -value => $sessionID,
692 print $query->header(
702 my ( $dbh, $userid, $password ) = @_;
707 "select password,cardnumber,borrowernumber,userid,firstname,surname,branchcode,flags from borrowers where userid=?"
709 $sth->execute($userid);
711 my ( $md5password, $cardnumber, $borrowernumber, $userid, $firstname,
712 $surname, $branchcode, $flags )
714 if ( md5_base64($password) eq $md5password ) {
716 C4::Context->set_userenv( "$borrowernumber", $userid, $cardnumber,
717 $firstname, $surname, $branchcode, $flags );
718 return 1, $cardnumber;
723 "select password,cardnumber,borrowernumber,userid, firstname,surname,branchcode,flags from borrowers where cardnumber=?"
725 $sth->execute($userid);
727 my ( $md5password, $cardnumber, $borrowernumber, $userid, $firstname,
728 $surname, $branchcode, $flags )
730 if ( md5_base64($password) eq $md5password ) {
732 C4::Context->set_userenv( $borrowernumber, $userid, $cardnumber,
733 $firstname, $surname, $branchcode, $flags );
737 if ( $userid && $userid eq C4::Context->config('user')
738 && "$password" eq C4::Context->config('pass') )
741 # Koha superuser account
742 # C4::Context->set_userenv(0,0,C4::Context->config('user'),C4::Context->config('user'),C4::Context->config('user'),"",1);
745 if ( $userid && $userid eq 'demo'
746 && "$password" eq 'demo'
747 && C4::Context->config('demo') )
750 # DEMO => the demo user is allowed to do everything (if demo set to 1 in koha.conf
751 # some features won't be effective : modify systempref, modify MARC structure,
758 my $cardnumber = shift;
761 my $sth = $dbh->prepare("SELECT flags FROM borrowers WHERE cardnumber=?");
762 $sth->execute($cardnumber);
763 my ($flags) = $sth->fetchrow;
764 $flags = 0 unless $flags;
765 $sth = $dbh->prepare("SELECT bit, flag, defaulton FROM userflags");
768 while ( my ( $bit, $flag, $defaulton ) = $sth->fetchrow ) {
769 if ( ( $flags & ( 2**$bit ) ) || $defaulton ) {
770 $userflags->{$flag} = 1;
773 $userflags->{$flag} = 0;
780 my ( $dbh, $userid, $flagsrequired ) = @_;
781 my $sth = $dbh->prepare("SELECT cardnumber FROM borrowers WHERE userid=?");
782 $sth->execute($userid);
783 my ($cardnumber) = $sth->fetchrow;
784 ($cardnumber) || ( $cardnumber = $userid );
785 my $flags = getuserflags( $cardnumber, $dbh );
787 if ( $userid eq C4::Context->config('user') ) {
789 # Super User Account from /etc/koha.conf
790 $flags->{'superlibrarian'} = 1;
792 if ( $userid eq 'demo' && C4::Context->config('demo') ) {
794 # Demo user that can do "anything" (demo=1 in /etc/koha.conf)
795 $flags->{'superlibrarian'} = 1;
797 return $flags if $flags->{superlibrarian};
798 foreach ( keys %$flagsrequired ) {
799 return $flags if $flags->{$_};
804 sub getborrowernumber {
806 my $dbh = C4::Context->dbh;
807 for my $field ( 'userid', 'cardnumber' ) {
809 $dbh->prepare("select borrowernumber from borrowers where $field=?");
810 $sth->execute($userid);
812 my ($bnumber) = $sth->fetchrow;
819 END { } # module clean-up code here (global destructor)