Bug 22884: Show separator between 440 and 490 fields
[koha.git] / C4 / InstallAuth.pm
blob67319b3db6675dad94fde4223f93b773d551b8ef
1 package C4::InstallAuth;
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 use Digest::MD5 qw(md5_base64);
23 use CGI::Session;
24 use File::Spec;
26 require Exporter;
28 use C4::Context;
29 use C4::Output;
30 use C4::Templates;
31 use C4::Koha;
33 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
36 =head1 NAME
38 InstallAuth - Authenticates Koha users for Install process
40 =head1 SYNOPSIS
42 use CGI qw ( -utf8 );
43 use InstallAuth;
44 use C4::Output;
46 my $query = new CGI;
48 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
49 { template_name => "opac-main.tt",
50 query => $query,
51 type => "opac",
52 authnotrequired => 1,
53 flagsrequired => { acquisition => '*' },
57 output_html_with_http_headers $query, $cookie, $template->output;
59 =head1 DESCRIPTION
61 The main function of this module is to provide
62 authentification. However the get_template_and_user function has
63 been provided so that a users login information is passed along
64 automatically. This gets loaded into the template.
65 This package is different from C4::Auth in so far as
66 C4::Auth uses many preferences which are supposed NOT to be obtainable when installing the database.
68 As in C4::Auth, Authentication is based on cookies.
70 =head1 FUNCTIONS
72 =over 2
74 =cut
76 @ISA = qw(Exporter);
77 @EXPORT = qw(
78 &checkauth
79 &get_template_and_user
82 =item get_template_and_user
84 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
85 { template_name => "opac-main.tt",
86 query => $query,
87 type => "opac",
88 authnotrequired => 1,
89 flagsrequired => { acquisition => '*' },
93 This call passes the C<query>, C<flagsrequired> and C<authnotrequired>
94 to C<&checkauth> (in this module) to perform authentification.
95 See C<&checkauth> for an explanation of these parameters.
97 The C<template_name> is then used to find the correct template for
98 the page. The authenticated users details are loaded onto the
99 template in the logged_in_user variable (which is a Koha::Patron object). Also the
100 C<sessionID> is passed to the template. This can be used in templates
101 if cookies are disabled. It needs to be put as and input to every
102 authenticated page.
104 More information on the C<gettemplate> sub can be found in the
105 Templates.pm module.
107 =cut
109 sub get_template_and_user {
110 my $in = shift;
111 my $query = $in->{'query'};
112 my $language =_get_template_language($query->cookie('KohaOpacLanguage'));
113 my $path = C4::Context->config('intrahtdocs'). "/prog/". $language;
115 my $tmplbase = $in->{template_name};
116 my $filename = "$path/modules/" . $tmplbase;
117 my $interface = 'intranet';
118 my $template = C4::Templates->new( $interface, $filename, $tmplbase, $query);
120 my ( $user, $cookie, $sessionID, $flags ) = checkauth(
121 $in->{'query'},
122 $in->{'authnotrequired'},
123 $in->{'flagsrequired'},
124 $in->{'type'}
127 # use Data::Dumper;warn "utilisateur $user cookie : ".Dumper($cookie);
129 my $borrowernumber;
130 if ($user) {
131 $template->param( loggedinusername => $user );
132 $template->param( sessionID => $sessionID );
134 # We are going to use the $flags returned by checkauth
135 # to create the template's parameters that will indicate
136 # which menus the user can access.
137 if ( ( $flags && $flags->{superlibrarian} == 1 ) ) {
138 $template->param( CAN_user_circulate => 1 );
139 $template->param( CAN_user_catalogue => 1 );
140 $template->param( CAN_user_parameters => 1 );
141 $template->param( CAN_user_borrowers => 1 );
142 $template->param( CAN_user_permission => 1 );
143 $template->param( CAN_user_reserveforothers => 1 );
144 $template->param( CAN_user_editcatalogue => 1 );
145 $template->param( CAN_user_updatecharges => 1 );
146 $template->param( CAN_user_acquisition => 1 );
147 $template->param( CAN_user_tools => 1 );
148 $template->param( CAN_user_editauthorities => 1 );
149 $template->param( CAN_user_serials => 1 );
150 $template->param( CAN_user_reports => 1 );
153 my $minPasswordLength = C4::Context->preference('minPasswordLength');
154 $minPasswordLength = 3 if not $minPasswordLength or $minPasswordLength < 3;
155 $template->param(minPasswordLength => $minPasswordLength,);
157 return ( $template, $borrowernumber, $cookie );
160 sub _get_template_language {
162 #verify if opac language exists in staff (bug 5660)
163 #conditions are 1) dir exists and 2) enabled in prefs
164 my ($opaclang) = @_;
165 return 'en' unless $opaclang;
166 $opaclang =~ s/[^a-zA-Z_-]*//g;
167 my $path = C4::Context->config('intrahtdocs') . "/prog/$opaclang";
168 -d $path ? $opaclang : 'en';
171 =item checkauth
173 ($userid, $cookie, $sessionID) = &checkauth($query, $noauth, $flagsrequired, $type);
175 Verifies that the user is authorized to run this script. If
176 the user is authorized, a (userid, cookie, session-id, flags)
177 quadruple is returned. If the user is not authorized but does
178 not have the required privilege (see $flagsrequired below), it
179 displays an error page and exits. Otherwise, it displays the
180 login page and exits.
182 Note that C<&checkauth> will return if and only if the user
183 is authorized, so it should be called early on, before any
184 unfinished operations (e.g., if you've opened a file, then
185 C<&checkauth> won't close it for you).
187 C<$query> is the CGI object for the script calling C<&checkauth>.
189 The C<$noauth> argument is optional. If it is set, then no
190 authorization is required for the script.
192 C<&checkauth> fetches user and session information from C<$query> and
193 ensures that the user is authorized to run scripts that require
194 authorization.
196 The C<$flagsrequired> argument specifies the required privileges
197 the user must have if the username and password are correct.
198 It should be specified as a reference-to-hash; keys in the hash
199 should be the "flags" for the user, as specified in the Members
200 intranet module. Any key specified must correspond to a "flag"
201 in the userflags table. E.g., { circulate => 1 } would specify
202 that the user must have the "circulate" privilege in order to
203 proceed. To make sure that access control is correct, the
204 C<$flagsrequired> parameter must be specified correctly.
206 The C<$type> argument specifies whether the template should be
207 retrieved from the opac or intranet directory tree. "opac" is
208 assumed if it is not specified; however, if C<$type> is specified,
209 "intranet" is assumed if it is not "opac".
211 If C<$query> does not have a valid session ID associated with it
212 (i.e., the user has not logged in) or if the session has expired,
213 C<&checkauth> presents the user with a login page (from the point of
214 view of the original script, C<&checkauth> does not return). Once the
215 user has authenticated, C<&checkauth> restarts the original script
216 (this time, C<&checkauth> returns).
218 The login page is provided using a HTML::Template, which is set in the
219 systempreferences table or at the top of this file. The variable C<$type>
220 selects which template to use, either the opac or the intranet
221 authentification template.
223 C<&checkauth> returns a user ID, a cookie, and a session ID. The
224 cookie should be sent back to the browser; it verifies that the user
225 has authenticated.
227 =cut
229 sub checkauth {
230 my $query = shift;
232 # $authnotrequired will be set for scripts which will run without authentication
233 my $authnotrequired = shift;
234 my $flagsrequired = shift;
235 my $type = shift;
236 $type = 'intranet' unless $type;
238 my $dbh = C4::Context->dbh();
239 my $template_name;
240 $template_name = "installer/auth.tt";
241 my $sessdir = File::Spec->catdir( C4::Context::temporary_directory, 'cgisess_' . C4::Context->config('database') ); # same construction as in C4/Auth
243 # state variables
244 my $loggedin = 0;
245 my %info;
246 my ( $userid, $cookie, $sessionID, $flags, $envcookie );
247 my $logout = $query->param('logout.x');
248 if ( $sessionID = $query->cookie("CGISESSID") ) {
249 C4::Context->_new_userenv($sessionID);
250 my $session =
251 new CGI::Session( "driver:File;serializer:yaml", $sessionID,
252 { Directory => $sessdir } );
253 if ( $session->param('cardnumber') ) {
254 C4::Context->set_userenv(
255 $session->param('number'),
256 $session->param('id'),
257 $session->param('cardnumber'),
258 $session->param('firstname'),
259 $session->param('surname'),
260 $session->param('branch'),
261 $session->param('branchname'),
262 $session->param('flags'),
263 $session->param('emailaddress'),
264 $session->param('branchprinter')
266 $cookie = $query->cookie(
267 -name => 'CGISESSID',
268 -value => $session->id,
269 -HttpOnly => 1,
271 $loggedin = 1;
272 $userid = $session->param('cardnumber');
274 my ( $ip, $lasttime );
276 if ($logout) {
278 # voluntary logout the user
279 C4::Context->_unset_userenv($sessionID);
280 $sessionID = undef;
281 $userid = undef;
282 # Commented out due to its lack of usefulness
283 # open L, ">>/tmp/sessionlog";
284 # my $time = localtime( time() );
285 # printf L "%20s from %16s logged out at %30s (manually).\n", $userid,
286 # $ip, $time;
287 # close L;
290 unless ($userid) {
291 my $session =
292 new CGI::Session( "driver:File;serializer:yaml", undef, { Directory => $sessdir } );
293 $sessionID = $session->id;
294 $userid = $query->param('userid');
295 C4::Context->_new_userenv($sessionID);
296 my $password = $query->param('password');
297 C4::Context->_new_userenv($sessionID);
298 my ( $return, $cardnumber ) = checkpw( $userid, $password );
299 if ($return) {
300 $loggedin = 1;
301 # open L, ">>/tmp/sessionlog";
302 # my $time = localtime( time() );
303 # printf L "%20s from %16s logged in at %30s.\n", $userid,
304 # $ENV{'REMOTE_ADDR'}, $time;
305 # close L;
306 $cookie = $query->cookie(
307 -name => 'CGISESSID',
308 -value => $sessionID,
309 -HttpOnly => 1,
311 if ( $return == 2 ) {
313 #Only superlibrarian should have access to this page.
314 #Since if it is a user, it is supposed that there is a borrower table
315 #And thus that data structure is loaded.
316 my $hash = C4::Context->set_userenv(
317 0, 0,
318 C4::Context->config('user'), C4::Context->config('user'),
319 C4::Context->config('user'), "",
320 "NO_LIBRARY_SET", 1,
323 $session->param( 'number', 0 );
324 $session->param( 'id', C4::Context->config('user') );
325 $session->param( 'cardnumber', C4::Context->config('user') );
326 $session->param( 'firstname', C4::Context->config('user') );
327 $session->param( 'surname', C4::Context->config('user'), );
328 $session->param( 'branch', 'NO_LIBRARY_SET' );
329 $session->param( 'branchname', 'NO_LIBRARY_SET' );
330 $session->param( 'flags', 1 );
331 $session->param( 'emailaddress',
332 C4::Context->preference('KohaAdminEmailAddress') );
333 $session->param( 'ip', $session->remote_addr() );
334 $session->param( 'lasttime', time() );
335 $userid = C4::Context->config('user');
338 else {
339 if ($userid) {
340 $info{'invalid_username_or_password'} = 1;
341 C4::Context->_unset_userenv($sessionID);
346 # finished authentification, now respond
347 if ($loggedin) {
349 # successful login
350 unless ($cookie) {
351 $cookie = $query->cookie(
352 -name => 'CGISESSID',
353 -value => '',
354 -HttpOnly => 1,
355 -expires => ''
358 if ($envcookie) {
359 return ( $userid, [ $cookie, $envcookie ], $sessionID, $flags );
361 else {
362 return ( $userid, $cookie, $sessionID, $flags );
366 # else we have a problem...
367 # get the inputs from the incoming query
368 my @inputs = ();
369 foreach my $name ( param $query) {
370 (next) if ( $name eq 'userid' || $name eq 'password' );
371 my $value = $query->param($name);
372 push @inputs, { name => $name, value => $value };
375 my $path =
376 C4::Context->config('intrahtdocs') . "/prog/"
377 . ( $query->param('language') ? $query->param('language') : "en" );
378 my $filename = "$path/modules/$template_name";
379 my $interface = 'intranet';
380 my $template = C4::Templates->new( $interface, $filename, '', $query);
381 $template->param(
382 INPUTS => \@inputs,
385 $template->param( login => 1 );
386 $template->param( loginprompt => 1 ) unless $info{'nopermission'};
388 if ($info{'invalid_username_or_password'} == 1) {
389 $template->param( 'invalid_username_or_password' => $info{'invalid_username_or_password'});
392 $template->param( \%info );
393 $cookie = $query->cookie(
394 -name => 'CGISESSID',
395 -value => $sessionID,
396 -HttpOnly => 1,
397 -expires => ''
399 print $query->header(
400 -type => 'text/html; charset=utf-8',
401 -cookie => $cookie
403 $template->output;
404 exit;
407 sub checkpw {
409 my ( $userid, $password ) = @_;
411 if ( $userid
412 && $userid eq C4::Context->config('user')
413 && "$password" eq C4::Context->config('pass') )
416 # Koha superuser account
417 C4::Context->set_userenv(
418 0, 0,
419 C4::Context->config('user'),
420 C4::Context->config('user'),
421 C4::Context->config('user'),
422 "", "NO_LIBRARY_SET", 1
424 return 2;
426 return 0;
429 END { } # module clean-up code here (global destructor)
431 __END__
433 =back
435 =head1 SEE ALSO
437 CGI(3)
439 C4::Output(3)
441 Digest::MD5(3)
443 =cut