Auth: Support für Verbindung zur Unit-Test-Datenbank
[kivitendo-erp.git] / SL / Auth.pm
1 package SL::Auth;
2
3 use DBI;
4
5 use Digest::MD5 qw(md5_hex);
6 use IO::File;
7 use Time::HiRes qw(gettimeofday);
8 use List::MoreUtils qw(uniq);
9 use YAML;
10
11 use SL::Auth::ColumnInformation;
12 use SL::Auth::Constants qw(:all);
13 use SL::Auth::DB;
14 use SL::Auth::LDAP;
15 use SL::Auth::Password;
16 use SL::Auth::SessionValue;
17
18 use SL::SessionFile;
19 use SL::User;
20 use SL::DBConnect;
21 use SL::DBUpgrade2;
22 use SL::DBUtils;
23
24 use strict;
25
26 use constant SESSION_KEY_ROOT_AUTH => 'session_auth_status_root';
27 use constant SESSION_KEY_USER_AUTH => 'session_auth_status_user';
28
29 use Rose::Object::MakeMethods::Generic (
30   scalar => [ qw(client) ],
31 );
32
33
34 sub new {
35   $main::lxdebug->enter_sub();
36
37   my ($type, %params) = @_;
38   my $self            = bless {}, $type;
39
40   $self->_read_auth_config(%params);
41   $self->reset;
42
43   $main::lxdebug->leave_sub();
44
45   return $self;
46 }
47
48 sub reset {
49   my ($self, %params) = @_;
50
51   delete $self->{dbh};
52   $self->{SESSION}            = { };
53   $self->{FULL_RIGHTS}        = { };
54   $self->{RIGHTS}             = { };
55   $self->{unique_counter}     = 0;
56   $self->{column_information} = SL::Auth::ColumnInformation->new(auth => $self);
57   $self->{authenticator}->reset;
58
59   $self->client(undef);
60 }
61
62 sub set_client {
63   my ($self, $id_or_name) = @_;
64
65   $self->client(undef);
66
67   return undef unless $id_or_name;
68
69   my $column = $id_or_name =~ m/^\d+$/ ? 'id' : 'name';
70   my $dbh    = $self->dbconnect;
71
72   return undef unless $dbh;
73
74   $self->client($dbh->selectrow_hashref(qq|SELECT * FROM auth.clients WHERE ${column} = ?|, undef, $id_or_name));
75
76   return $self->client;
77 }
78
79 sub DESTROY {
80   my $self = shift;
81
82   $self->{dbh}->disconnect() if ($self->{dbh});
83 }
84
85 # form isn't loaded yet, so auth needs it's own error.
86 sub mini_error {
87   $::lxdebug->show_backtrace();
88
89   my ($self, @msg) = @_;
90   if ($ENV{HTTP_USER_AGENT}) {
91     print Form->create_http_response(content_type => 'text/html');
92     print "<pre>", join ('<br>', @msg), "</pre>";
93   } else {
94     print STDERR "Error: @msg\n";
95   }
96   ::end_of_request();
97 }
98
99 sub _read_auth_config {
100   $main::lxdebug->enter_sub();
101
102   my ($self, %params) = @_;
103
104   map { $self->{$_} = $::lx_office_conf{authentication}->{$_} } keys %{ $::lx_office_conf{authentication} };
105
106   # Prevent password leakage to log files when dumping Auth instances.
107   $self->{admin_password} = sub { $::lx_office_conf{authentication}->{admin_password} };
108
109   if ($params{unit_tests_database}) {
110     $self->{DB_config}   = $::lx_office_conf{'testing/database'};
111     $self->{module}      = 'DB';
112
113   } else {
114     $self->{DB_config}   = $::lx_office_conf{'authentication/database'};
115     $self->{LDAP_config} = $::lx_office_conf{'authentication/ldap'};
116   }
117
118   if ($self->{module} eq 'DB') {
119     $self->{authenticator} = SL::Auth::DB->new($self);
120
121   } elsif ($self->{module} eq 'LDAP') {
122     $self->{authenticator} = SL::Auth::LDAP->new($self);
123   }
124
125   if (!$self->{authenticator}) {
126     my $locale = Locale->new('en');
127     $self->mini_error($locale->text('No or an unknown authenticantion module specified in "config/kivitendo.conf".'));
128   }
129
130   my $cfg = $self->{DB_config};
131
132   if (!$cfg) {
133     my $locale = Locale->new('en');
134     $self->mini_error($locale->text('config/kivitendo.conf: Key "DB_config" is missing.'));
135   }
136
137   if (!$cfg->{host} || !$cfg->{db} || !$cfg->{user}) {
138     my $locale = Locale->new('en');
139     $self->mini_error($locale->text('config/kivitendo.conf: Missing parameters in "authentication/database". Required parameters are "host", "db" and "user".'));
140   }
141
142   $self->{authenticator}->verify_config();
143
144   $self->{session_timeout} *= 1;
145   $self->{session_timeout}  = 8 * 60 if (!$self->{session_timeout});
146
147   $main::lxdebug->leave_sub();
148 }
149
150 sub has_access_to_client {
151   my ($self, $login) = @_;
152
153   return 0 if !$self->client || !$self->client->{id};
154
155   my $sql = <<SQL;
156     SELECT cu.client_id
157     FROM auth.clients_users cu
158     LEFT JOIN auth."user" u ON (cu.user_id = u.id)
159     WHERE (u.login      = ?)
160       AND (cu.client_id = ?)
161 SQL
162
163   my ($has_access) = $self->dbconnect->selectrow_array($sql, undef, $login, $self->client->{id});
164   return $has_access;
165 }
166
167 sub authenticate_root {
168   $main::lxdebug->enter_sub();
169
170   my ($self, $password) = @_;
171
172   my $session_root_auth = $self->get_session_value(SESSION_KEY_ROOT_AUTH());
173   if (defined $session_root_auth && $session_root_auth == OK) {
174     $::lxdebug->leave_sub;
175     return OK;
176   }
177
178   if (!defined $password) {
179     $::lxdebug->leave_sub;
180     return ERR_PASSWORD;
181   }
182
183   $password             = SL::Auth::Password->hash(login => 'root', password => $password);
184   my $admin_password    = SL::Auth::Password->hash_if_unhashed(login => 'root', password => $self->{admin_password}->());
185
186   my $result = $password eq $admin_password ? OK : ERR_PASSWORD;
187   $self->set_session_value(SESSION_KEY_ROOT_AUTH() => $result);
188
189   $::lxdebug->leave_sub;
190   return $result;
191 }
192
193 sub authenticate {
194   $main::lxdebug->enter_sub();
195
196   my ($self, $login, $password) = @_;
197
198   if (!$self->client || !$self->has_access_to_client($login)) {
199     $::lxdebug->leave_sub;
200     return ERR_PASSWORD;
201   }
202
203   my $session_auth = $self->get_session_value(SESSION_KEY_USER_AUTH());
204   if (defined $session_auth && $session_auth == OK) {
205     $::lxdebug->leave_sub;
206     return OK;
207   }
208
209   if (!defined $password) {
210     $::lxdebug->leave_sub;
211     return ERR_PASSWORD;
212   }
213
214   my $result = $login ? $self->{authenticator}->authenticate($login, $password) : ERR_USER;
215   $self->set_session_value(SESSION_KEY_USER_AUTH() => $result, login => $login, client_id => $self->client->{id});
216
217   $::lxdebug->leave_sub;
218   return $result;
219 }
220
221 sub punish_wrong_login {
222   my $failed_login_penalty = ($::lx_office_conf{authentication} || {})->{failed_login_penalty};
223   sleep $failed_login_penalty if $failed_login_penalty;
224 }
225
226 sub get_stored_password {
227   my ($self, $login) = @_;
228
229   my $dbh            = $self->dbconnect;
230
231   return undef unless $dbh;
232
233   my $query             = qq|SELECT password FROM auth."user" WHERE login = ?|;
234   my ($stored_password) = $dbh->selectrow_array($query, undef, $login);
235
236   return $stored_password;
237 }
238
239 sub dbconnect {
240   $main::lxdebug->enter_sub(2);
241
242   my $self     = shift;
243   my $may_fail = shift;
244
245   if ($self->{dbh}) {
246     $main::lxdebug->leave_sub(2);
247     return $self->{dbh};
248   }
249
250   my $cfg = $self->{DB_config};
251   my $dsn = 'dbi:Pg:dbname=' . $cfg->{db} . ';host=' . $cfg->{host};
252
253   if ($cfg->{port}) {
254     $dsn .= ';port=' . $cfg->{port};
255   }
256
257   $main::lxdebug->message(LXDebug->DEBUG1, "Auth::dbconnect DSN: $dsn");
258
259   $self->{dbh} = SL::DBConnect->connect($dsn, $cfg->{user}, $cfg->{password}, { pg_enable_utf8 => 1, AutoCommit => 1 });
260
261   if (!$may_fail && !$self->{dbh}) {
262     $main::form->error($main::locale->text('The connection to the authentication database failed:') . "\n" . $DBI::errstr);
263   }
264
265   $main::lxdebug->leave_sub(2);
266
267   return $self->{dbh};
268 }
269
270 sub dbdisconnect {
271   $main::lxdebug->enter_sub();
272
273   my $self = shift;
274
275   if ($self->{dbh}) {
276     $self->{dbh}->disconnect();
277     delete $self->{dbh};
278   }
279
280   $main::lxdebug->leave_sub();
281 }
282
283 sub check_tables {
284   $main::lxdebug->enter_sub();
285
286   my ($self, $dbh)    = @_;
287
288   $dbh   ||= $self->dbconnect();
289   my $query   = qq|SELECT COUNT(*) FROM pg_tables WHERE (schemaname = 'auth') AND (tablename = 'user')|;
290
291   my ($count) = $dbh->selectrow_array($query);
292
293   $main::lxdebug->leave_sub();
294
295   return $count > 0;
296 }
297
298 sub check_database {
299   $main::lxdebug->enter_sub();
300
301   my $self = shift;
302
303   my $dbh  = $self->dbconnect(1);
304
305   $main::lxdebug->leave_sub();
306
307   return $dbh ? 1 : 0;
308 }
309
310 sub create_database {
311   $main::lxdebug->enter_sub();
312
313   my $self   = shift;
314   my %params = @_;
315
316   my $cfg    = $self->{DB_config};
317
318   if (!$params{superuser}) {
319     $params{superuser}          = $cfg->{user};
320     $params{superuser_password} = $cfg->{password};
321   }
322
323   $params{template} ||= 'template0';
324   $params{template}   =~ s|[^a-zA-Z0-9_\-]||g;
325
326   my $dsn = 'dbi:Pg:dbname=template1;host=' . $cfg->{host};
327
328   if ($cfg->{port}) {
329     $dsn .= ';port=' . $cfg->{port};
330   }
331
332   $main::lxdebug->message(LXDebug->DEBUG1(), "Auth::create_database DSN: $dsn");
333
334   my $dbh = SL::DBConnect->connect($dsn, $params{superuser}, $params{superuser_password}, { pg_enable_utf8 => 1 });
335
336   if (!$dbh) {
337     $main::form->error($main::locale->text('The connection to the template database failed:') . "\n" . $DBI::errstr);
338   }
339
340   my $query = qq|CREATE DATABASE "$cfg->{db}" OWNER "$cfg->{user}" TEMPLATE "$params{template}" ENCODING 'UNICODE'|;
341
342   $main::lxdebug->message(LXDebug->DEBUG1(), "Auth::create_database query: $query");
343
344   $dbh->do($query);
345
346   if ($dbh->err) {
347     my $error = $dbh->errstr();
348
349     $query                 = qq|SELECT pg_encoding_to_char(encoding) FROM pg_database WHERE datname = 'template0'|;
350     my ($cluster_encoding) = $dbh->selectrow_array($query);
351
352     if ($cluster_encoding && ($cluster_encoding !~ m/^(?:UTF-?8|UNICODE)$/i)) {
353       $error = $::locale->text('Your PostgreSQL installationen does not use Unicode as its encoding. This is not supported anymore.');
354     }
355
356     $dbh->disconnect();
357
358     $main::form->error($main::locale->text('The creation of the authentication database failed:') . "\n" . $error);
359   }
360
361   $dbh->disconnect();
362
363   $main::lxdebug->leave_sub();
364 }
365
366 sub create_tables {
367   $main::lxdebug->enter_sub();
368
369   my $self = shift;
370   my $dbh  = $self->dbconnect();
371
372   $dbh->rollback();
373   SL::DBUpgrade2->new(form => $::form)->process_query($dbh, 'sql/auth_db.sql');
374
375   $main::lxdebug->leave_sub();
376 }
377
378 sub save_user {
379   $main::lxdebug->enter_sub();
380
381   my $self   = shift;
382   my $login  = shift;
383   my %params = @_;
384
385   my $form   = $main::form;
386
387   my $dbh    = $self->dbconnect();
388
389   my ($sth, $query, $user_id);
390
391   $dbh->begin_work;
392
393   $query     = qq|SELECT id FROM auth."user" WHERE login = ?|;
394   ($user_id) = selectrow_query($form, $dbh, $query, $login);
395
396   if (!$user_id) {
397     $query     = qq|SELECT nextval('auth.user_id_seq')|;
398     ($user_id) = selectrow_query($form, $dbh, $query);
399
400     $query     = qq|INSERT INTO auth."user" (id, login) VALUES (?, ?)|;
401     do_query($form, $dbh, $query, $user_id, $login);
402   }
403
404   $query = qq|DELETE FROM auth.user_config WHERE (user_id = ?)|;
405   do_query($form, $dbh, $query, $user_id);
406
407   $query = qq|INSERT INTO auth.user_config (user_id, cfg_key, cfg_value) VALUES (?, ?, ?)|;
408   $sth   = prepare_query($form, $dbh, $query);
409
410   while (my ($cfg_key, $cfg_value) = each %params) {
411     next if ($cfg_key eq 'password');
412
413     do_statement($form, $sth, $query, $user_id, $cfg_key, $cfg_value);
414   }
415
416   $dbh->commit();
417
418   $main::lxdebug->leave_sub();
419 }
420
421 sub can_change_password {
422   my $self = shift;
423
424   return $self->{authenticator}->can_change_password();
425 }
426
427 sub change_password {
428   $main::lxdebug->enter_sub();
429
430   my ($self, $login, $new_password) = @_;
431
432   my $result = $self->{authenticator}->change_password($login, $new_password);
433
434   $main::lxdebug->leave_sub();
435
436   return $result;
437 }
438
439 sub read_all_users {
440   $main::lxdebug->enter_sub();
441
442   my $self  = shift;
443
444   my $dbh   = $self->dbconnect();
445   my $query = qq|SELECT u.id, u.login, cfg.cfg_key, cfg.cfg_value, s.mtime AS last_action
446
447                  FROM auth."user" AS  u
448
449                  LEFT JOIN auth.user_config AS cfg
450                    ON (cfg.user_id = u.id)
451
452                  LEFT JOIN auth.session_content AS sc_login
453                    ON (sc_login.sess_key = 'login' AND sc_login.sess_value = ('--- ' \|\| u.login \|\| '\n'))
454
455                  LEFT JOIN auth.session AS s
456                    ON (s.id = sc_login.session_id)
457               |;
458   my $sth   = prepare_execute_query($main::form, $dbh, $query);
459
460   my %users;
461
462   while (my $ref = $sth->fetchrow_hashref()) {
463
464     $users{$ref->{login}}                    ||= {
465                                                 'login' => $ref->{login},
466                                                 'id' => $ref->{id},
467                                                 'last_action' => $ref->{last_action},
468                                              };
469     $users{$ref->{login}}->{$ref->{cfg_key}}   = $ref->{cfg_value} if (($ref->{cfg_key} ne 'login') && ($ref->{cfg_key} ne 'id'));
470   }
471
472   $sth->finish();
473
474   $main::lxdebug->leave_sub();
475
476   return %users;
477 }
478
479 sub read_user {
480   $main::lxdebug->enter_sub();
481
482   my ($self, %params) = @_;
483
484   my $dbh   = $self->dbconnect();
485
486   my (@where, @values);
487   if ($params{login}) {
488     push @where,  'u.login = ?';
489     push @values, $params{login};
490   }
491   if ($params{id}) {
492     push @where,  'u.id = ?';
493     push @values, $params{id};
494   }
495   my $where = join ' AND ', '1 = 1', @where;
496   my $query = qq|SELECT u.id, u.login, cfg.cfg_key, cfg.cfg_value
497                  FROM auth.user_config cfg
498                  LEFT JOIN auth."user" u ON (cfg.user_id = u.id)
499                  WHERE $where|;
500   my $sth   = prepare_execute_query($main::form, $dbh, $query, @values);
501
502   my %user_data;
503
504   while (my $ref = $sth->fetchrow_hashref()) {
505     $user_data{$ref->{cfg_key}} = $ref->{cfg_value};
506     @user_data{qw(id login)}    = @{$ref}{qw(id login)};
507   }
508
509   # The XUL/XML & 'CSS new' backed menus have been removed.
510   my %menustyle_map = ( xml => 'new', v4 => 'v3' );
511   $user_data{menustyle} = $menustyle_map{lc($user_data{menustyle} || '')} || $user_data{menustyle};
512
513   # The 'Win2000.css' stylesheet has been removed.
514   $user_data{stylesheet} = 'kivitendo.css' if ($user_data{stylesheet} || '') =~ m/win2000/i;
515
516   # Set default language if selected language does not exist (anymore).
517   $user_data{countrycode} = $::lx_office_conf{system}->{language} unless $user_data{countrycode} && -d "locale/$user_data{countrycode}";
518
519   $sth->finish();
520
521   $main::lxdebug->leave_sub();
522
523   return %user_data;
524 }
525
526 sub get_user_id {
527   $main::lxdebug->enter_sub();
528
529   my $self  = shift;
530   my $login = shift;
531
532   my $dbh   = $self->dbconnect();
533   my ($id)  = selectrow_query($main::form, $dbh, qq|SELECT id FROM auth."user" WHERE login = ?|, $login);
534
535   $main::lxdebug->leave_sub();
536
537   return $id;
538 }
539
540 sub delete_user {
541   $::lxdebug->enter_sub;
542
543   my $self  = shift;
544   my $login = shift;
545
546   my $dbh   = $self->dbconnect;
547   my $id    = $self->get_user_id($login);
548
549   $dbh->rollback and return $::lxdebug->leave_sub if (!$id);
550
551   $dbh->begin_work;
552
553   do_query($::form, $dbh, qq|DELETE FROM auth.user_group WHERE user_id = ?|, $id);
554   do_query($::form, $dbh, qq|DELETE FROM auth.user_config WHERE user_id = ?|, $id);
555   do_query($::form, $dbh, qq|DELETE FROM auth.user WHERE id = ?|, $id);
556
557   # TODO: SL::Auth::delete_user
558   # do_query($::form, $u_dbh, qq|UPDATE employee SET deleted = 't' WHERE login = ?|, $login) if $u_dbh && $user_db_exists;
559
560   $dbh->commit;
561
562   $::lxdebug->leave_sub;
563 }
564
565 # --------------------------------------
566
567 my $session_id;
568
569 sub restore_session {
570   $main::lxdebug->enter_sub();
571
572   my $self = shift;
573
574   $session_id        =  $::request->{cgi}->cookie($self->get_session_cookie_name());
575   $session_id        =~ s|[^0-9a-f]||g if $session_id;
576
577   $self->{SESSION}   = { };
578
579   if (!$session_id) {
580     $main::lxdebug->leave_sub();
581     return $self->session_restore_result(SESSION_NONE());
582   }
583
584   my ($dbh, $query, $sth, $cookie, $ref, $form);
585
586   $form   = $main::form;
587
588   # Don't fail if the auth DB doesn't yet.
589   if (!( $dbh = $self->dbconnect(1) )) {
590     $::lxdebug->leave_sub;
591     return $self->session_restore_result(SESSION_NONE());
592   }
593
594   # Don't fail if the "auth" schema doesn't exist yet, e.g. if the
595   # admin is creating the session tables at the moment.
596   $query  = qq|SELECT *, (mtime < (now() - '$self->{session_timeout}m'::interval)) AS is_expired FROM auth.session WHERE id = ?|;
597
598   if (!($sth = $dbh->prepare($query)) || !$sth->execute($session_id)) {
599     $sth->finish if $sth;
600     $::lxdebug->leave_sub;
601     return $self->session_restore_result(SESSION_NONE());
602   }
603
604   $cookie = $sth->fetchrow_hashref;
605   $sth->finish;
606
607   # The session ID provided is valid in the following cases:
608   #  1. session ID exists in the database
609   #  2. hasn't expired yet
610   #  3. if form field '{AUTH}api_token' is given: form field must equal database column 'auth.session.api_token' for the session ID
611   #  4. if form field '{AUTH}api_token' is NOT given then: the requestee's IP address must match the stored IP address
612   $self->{api_token}   = $cookie->{api_token} if $cookie;
613   my $api_token_cookie = $self->get_api_token_cookie;
614   my $cookie_is_bad    = !$cookie || $cookie->{is_expired};
615   $cookie_is_bad     ||= $api_token_cookie && ($api_token_cookie ne $cookie->{api_token}) if  $api_token_cookie;
616   $cookie_is_bad     ||= $cookie->{ip_address} ne $ENV{REMOTE_ADDR}                       if !$api_token_cookie;
617   if ($cookie_is_bad) {
618     $self->destroy_session();
619     $main::lxdebug->leave_sub();
620     return $self->session_restore_result($cookie ? SESSION_EXPIRED() : SESSION_NONE());
621   }
622
623   if ($self->{column_information}->has('auto_restore')) {
624     $self->_load_with_auto_restore_column($dbh, $session_id);
625   } else {
626     $self->_load_without_auto_restore_column($dbh, $session_id);
627   }
628
629   $main::lxdebug->leave_sub();
630
631   return $self->session_restore_result(SESSION_OK());
632 }
633
634 sub session_restore_result {
635   my $self = shift;
636   if (@_) {
637     $self->{session_restore_result} = $_[0];
638   }
639   return $self->{session_restore_result};
640 }
641
642 sub _load_without_auto_restore_column {
643   my ($self, $dbh, $session_id) = @_;
644
645   my $query = <<SQL;
646     SELECT sess_key, sess_value
647     FROM auth.session_content
648     WHERE (session_id = ?)
649 SQL
650   my $sth = prepare_execute_query($::form, $dbh, $query, $session_id);
651
652   while (my $ref = $sth->fetchrow_hashref) {
653     my $value = SL::Auth::SessionValue->new(auth  => $self,
654                                             key   => $ref->{sess_key},
655                                             value => $ref->{sess_value},
656                                             raw   => 1);
657     $self->{SESSION}->{ $ref->{sess_key} } = $value;
658
659     next if defined $::form->{$ref->{sess_key}};
660
661     my $data                    = $value->get;
662     $::form->{$ref->{sess_key}} = $data if $value->{auto_restore} || !ref $data;
663   }
664 }
665
666 sub _load_with_auto_restore_column {
667   my ($self, $dbh, $session_id) = @_;
668
669   my $auto_restore_keys = join ', ', map { "'${_}'" } qw(login password rpw);
670
671   my $query = <<SQL;
672     SELECT sess_key, sess_value, auto_restore
673     FROM auth.session_content
674     WHERE (session_id = ?)
675       AND (   auto_restore
676            OR sess_key IN (${auto_restore_keys}))
677 SQL
678   my $sth = prepare_execute_query($::form, $dbh, $query, $session_id);
679
680   while (my $ref = $sth->fetchrow_hashref) {
681     my $value = SL::Auth::SessionValue->new(auth         => $self,
682                                             key          => $ref->{sess_key},
683                                             value        => $ref->{sess_value},
684                                             auto_restore => $ref->{auto_restore},
685                                             raw          => 1);
686     $self->{SESSION}->{ $ref->{sess_key} } = $value;
687
688     next if defined $::form->{$ref->{sess_key}};
689
690     my $data                    = $value->get;
691     $::form->{$ref->{sess_key}} = $data if $value->{auto_restore} || !ref $data;
692   }
693
694   $sth->finish;
695
696   $query = <<SQL;
697     SELECT sess_key
698     FROM auth.session_content
699     WHERE (session_id = ?)
700       AND NOT COALESCE(auto_restore, FALSE)
701       AND (sess_key NOT IN (${auto_restore_keys}))
702 SQL
703   $sth = prepare_execute_query($::form, $dbh, $query, $session_id);
704
705   while (my $ref = $sth->fetchrow_hashref) {
706     my $value = SL::Auth::SessionValue->new(auth => $self,
707                                             key  => $ref->{sess_key});
708     $self->{SESSION}->{ $ref->{sess_key} } = $value;
709   }
710 }
711
712 sub destroy_session {
713   $main::lxdebug->enter_sub();
714
715   my $self = shift;
716
717   if ($session_id) {
718     my $dbh = $self->dbconnect();
719
720     $dbh->begin_work;
721
722     do_query($main::form, $dbh, qq|DELETE FROM auth.session_content WHERE session_id = ?|, $session_id);
723     do_query($main::form, $dbh, qq|DELETE FROM auth.session WHERE id = ?|, $session_id);
724
725     $dbh->commit();
726
727     SL::SessionFile->destroy_session($session_id);
728
729     $session_id      = undef;
730     $self->{SESSION} = { };
731   }
732
733   $main::lxdebug->leave_sub();
734 }
735
736 sub active_session_ids {
737   my $self  = shift;
738   my $dbh   = $self->dbconnect;
739
740   my $query = qq|SELECT id FROM auth.session|;
741
742   my @ids   = selectall_array_query($::form, $dbh, $query);
743
744   return @ids;
745 }
746
747 sub expire_sessions {
748   $main::lxdebug->enter_sub();
749
750   my $self  = shift;
751
752   $main::lxdebug->leave_sub and return if !$self->session_tables_present;
753
754   my $dbh   = $self->dbconnect();
755
756   my $query = qq|SELECT id
757                  FROM auth.session
758                  WHERE (mtime < (now() - '$self->{session_timeout}m'::interval))|;
759
760   my @ids   = selectall_array_query($::form, $dbh, $query);
761
762   if (@ids) {
763     $dbh->begin_work;
764
765     SL::SessionFile->destroy_session($_) for @ids;
766
767     $query = qq|DELETE FROM auth.session_content
768                 WHERE session_id IN (| . join(', ', ('?') x scalar(@ids)) . qq|)|;
769     do_query($main::form, $dbh, $query, @ids);
770
771     $query = qq|DELETE FROM auth.session
772                 WHERE id IN (| . join(', ', ('?') x scalar(@ids)) . qq|)|;
773     do_query($main::form, $dbh, $query, @ids);
774
775     $dbh->commit();
776   }
777
778   $main::lxdebug->leave_sub();
779 }
780
781 sub _create_session_id {
782   $main::lxdebug->enter_sub();
783
784   my @data;
785   map { push @data, int(rand() * 255); } (1..32);
786
787   my $id = md5_hex(pack 'C*', @data);
788
789   $main::lxdebug->leave_sub();
790
791   return $id;
792 }
793
794 sub create_or_refresh_session {
795   $session_id ||= shift->_create_session_id;
796 }
797
798 sub save_session {
799   $::lxdebug->enter_sub;
800   my $self         = shift;
801   my $provided_dbh = shift;
802
803   my $dbh          = $provided_dbh || $self->dbconnect(1);
804
805   $::lxdebug->leave_sub && return unless $dbh && $session_id;
806
807   $dbh->begin_work unless $provided_dbh;
808
809   # If this fails then the "auth" schema might not exist yet, e.g. if
810   # the admin is just trying to create the auth database.
811   if (!$dbh->do(qq|LOCK auth.session_content|)) {
812     $dbh->rollback unless $provided_dbh;
813     $::lxdebug->leave_sub;
814     return;
815   }
816
817   my @unfetched_keys = map     { $_->{key}        }
818                        grep    { ! $_->{fetched}  }
819                        values %{ $self->{SESSION} };
820   # $::lxdebug->dump(0, "unfetched_keys", [ sort @unfetched_keys ]);
821   # $::lxdebug->dump(0, "all keys", [ sort map { $_->{key} } values %{ $self->{SESSION} } ]);
822   my $query          = qq|DELETE FROM auth.session_content WHERE (session_id = ?)|;
823   $query            .= qq| AND (sess_key NOT IN (| . join(', ', ('?') x scalar @unfetched_keys) . qq|))| if @unfetched_keys;
824
825   do_query($::form, $dbh, $query, $session_id, @unfetched_keys);
826
827   my ($id) = selectrow_query($::form, $dbh, qq|SELECT id FROM auth.session WHERE id = ?|, $session_id);
828
829   if ($id) {
830     do_query($::form, $dbh, qq|UPDATE auth.session SET mtime = now() WHERE id = ?|, $session_id);
831   } else {
832     do_query($::form, $dbh, qq|INSERT INTO auth.session (id, ip_address, mtime) VALUES (?, ?, now())|, $session_id, $ENV{REMOTE_ADDR});
833   }
834
835   if ($self->{column_information}->has('api_token', 'session')) {
836     my ($stored_api_token) = $dbh->selectrow_array(qq|SELECT api_token FROM auth.session WHERE id = ?|, undef, $session_id);
837     do_query($::form, $dbh, qq|UPDATE auth.session SET api_token = ? WHERE id = ?|, $self->_create_session_id, $session_id) unless $stored_api_token;
838   }
839
840   my @values_to_save = grep    { $_->{fetched} }
841                        values %{ $self->{SESSION} };
842   if (@values_to_save) {
843     my ($columns, $placeholders) = ('', '');
844     my $auto_restore             = $self->{column_information}->has('auto_restore');
845
846     if ($auto_restore) {
847       $columns      .= ', auto_restore';
848       $placeholders .= ', ?';
849     }
850
851     $query  = qq|INSERT INTO auth.session_content (session_id, sess_key, sess_value ${columns}) VALUES (?, ?, ? ${placeholders})|;
852     my $sth = prepare_query($::form, $dbh, $query);
853
854     foreach my $value (@values_to_save) {
855       my @values = ($value->{key}, $value->get_dumped);
856       push @values, $value->{auto_restore} if $auto_restore;
857
858       do_statement($::form, $sth, $query, $session_id, @values);
859     }
860
861     $sth->finish();
862   }
863
864   $dbh->commit() unless $provided_dbh;
865   $::lxdebug->leave_sub;
866 }
867
868 sub set_session_value {
869   $main::lxdebug->enter_sub();
870
871   my $self   = shift;
872   my @params = @_;
873
874   $self->{SESSION} ||= { };
875
876   while (@params) {
877     my $key = shift @params;
878
879     if (ref $key eq 'HASH') {
880       $self->{SESSION}->{ $key->{key} } = SL::Auth::SessionValue->new(key          => $key->{key},
881                                                                       value        => $key->{value},
882                                                                       auto_restore => $key->{auto_restore});
883
884     } else {
885       my $value = shift @params;
886       $self->{SESSION}->{ $key } = SL::Auth::SessionValue->new(key   => $key,
887                                                                value => $value);
888     }
889   }
890
891   $main::lxdebug->leave_sub();
892
893   return $self;
894 }
895
896 sub delete_session_value {
897   $main::lxdebug->enter_sub();
898
899   my $self = shift;
900
901   $self->{SESSION} ||= { };
902   delete @{ $self->{SESSION} }{ @_ };
903
904   $main::lxdebug->leave_sub();
905
906   return $self;
907 }
908
909 sub get_session_value {
910   $main::lxdebug->enter_sub();
911
912   my $self = shift;
913   my $data = $self->{SESSION} && $self->{SESSION}->{ $_[0] } ? $self->{SESSION}->{ $_[0] }->get : undef;
914
915   $main::lxdebug->leave_sub();
916
917   return $data;
918 }
919
920 sub create_unique_sesion_value {
921   my ($self, $value, %params) = @_;
922
923   $self->{SESSION} ||= { };
924
925   my @now                   = gettimeofday();
926   my $key                   = "$$-" . ($now[0] * 1000000 + $now[1]) . "-";
927   $self->{unique_counter} ||= 0;
928
929   my $hashed_key;
930   do {
931     $self->{unique_counter}++;
932     $hashed_key = md5_hex($key . $self->{unique_counter});
933   } while (exists $self->{SESSION}->{$hashed_key});
934
935   $self->set_session_value($hashed_key => $value);
936
937   return $hashed_key;
938 }
939
940 sub save_form_in_session {
941   my ($self, %params) = @_;
942
943   my $form        = delete($params{form}) || $::form;
944   my $non_scalars = delete $params{non_scalars};
945   my $data        = {};
946
947   my %skip_keys   = map { ( $_ => 1 ) } (qw(login password stylesheet version titlebar), @{ $params{skip_keys} || [] });
948
949   foreach my $key (grep { !$skip_keys{$_} } keys %{ $form }) {
950     $data->{$key} = $form->{$key} if !ref($form->{$key}) || $non_scalars;
951   }
952
953   return $self->create_unique_sesion_value($data, %params);
954 }
955
956 sub restore_form_from_session {
957   my ($self, $key, %params) = @_;
958
959   my $data = $self->get_session_value($key);
960   return $self unless $data;
961
962   my $form    = delete($params{form}) || $::form;
963   my $clobber = exists $params{clobber} ? $params{clobber} : 1;
964
965   map { $form->{$_} = $data->{$_} if $clobber || !exists $form->{$_} } keys %{ $data };
966
967   return $self;
968 }
969
970 sub set_cookie_environment_variable {
971   my $self = shift;
972   $ENV{HTTP_COOKIE} = $self->get_session_cookie_name() . "=${session_id}";
973 }
974
975 sub get_session_cookie_name {
976   my ($self, %params) = @_;
977
978   $params{type}     ||= 'id';
979   my $name            = $self->{cookie_name} || 'lx_office_erp_session_id';
980   $name              .= '_api_token' if $params{type} eq 'api_token';
981
982   return $name;
983 }
984
985 sub get_session_id {
986   return $session_id;
987 }
988
989 sub get_api_token_cookie {
990   my ($self) = @_;
991
992   $::request->{cgi}->cookie($self->get_session_cookie_name(type => 'api_token'));
993 }
994
995 sub session_tables_present {
996   $main::lxdebug->enter_sub();
997
998   my $self = shift;
999
1000   # Only re-check for the presence of auth tables if either the check
1001   # hasn't been done before of if they weren't present.
1002   if ($self->{session_tables_present}) {
1003     $main::lxdebug->leave_sub();
1004     return $self->{session_tables_present};
1005   }
1006
1007   my $dbh  = $self->dbconnect(1);
1008
1009   if (!$dbh) {
1010     $main::lxdebug->leave_sub();
1011     return 0;
1012   }
1013
1014   my $query =
1015     qq|SELECT COUNT(*)
1016        FROM pg_tables
1017        WHERE (schemaname = 'auth')
1018          AND (tablename IN ('session', 'session_content'))|;
1019
1020   my ($count) = selectrow_query($main::form, $dbh, $query);
1021
1022   $self->{session_tables_present} = 2 == $count;
1023
1024   $main::lxdebug->leave_sub();
1025
1026   return $self->{session_tables_present};
1027 }
1028
1029 # --------------------------------------
1030
1031 sub all_rights_full {
1032   my $locale = $main::locale;
1033
1034   my @all_rights = (
1035     ["--crm",                          $locale->text("CRM optional software")],
1036     ["crm_search",                     $locale->text("CRM search")],
1037     ["crm_new",                        $locale->text("CRM create customers, vendors and contacts")],
1038     ["crm_service",                    $locale->text("CRM services")],
1039     ["crm_admin",                      $locale->text("CRM admin")],
1040     ["crm_adminuser",                  $locale->text("CRM user")],
1041     ["crm_adminstatus",                $locale->text("CRM status")],
1042     ["crm_email",                      $locale->text("CRM send email")],
1043     ["crm_termin",                     $locale->text("CRM termin")],
1044     ["crm_opportunity",                $locale->text("CRM opportunity")],
1045     ["crm_knowhow",                    $locale->text("CRM know how")],
1046     ["crm_follow",                     $locale->text("CRM follow up")],
1047     ["crm_notices",                    $locale->text("CRM notices")],
1048     ["crm_other",                      $locale->text("CRM other")],
1049     ["--master_data",                  $locale->text("Master Data")],
1050     ["customer_vendor_edit",           $locale->text("Create customers and vendors. Edit all vendors. Edit only customers where salesman equals employee (login)")],
1051     ["customer_vendor_all_edit",       $locale->text("Create customers and vendors. Edit all vendors. Edit all customers")],
1052     ["part_service_assembly_edit",     $locale->text("Create and edit parts, services, assemblies")],
1053     ["project_edit",                   $locale->text("Create and edit projects")],
1054     ["--ar",                           $locale->text("AR")],
1055     ["sales_quotation_edit",           $locale->text("Create and edit sales quotations")],
1056     ["sales_order_edit",               $locale->text("Create and edit sales orders")],
1057     ["sales_delivery_order_edit",      $locale->text("Create and edit sales delivery orders")],
1058     ["invoice_edit",                   $locale->text("Create and edit invoices and credit notes")],
1059     ["dunning_edit",                   $locale->text("Create and edit dunnings")],
1060     ["sales_all_edit",                 $locale->text("View/edit all employees sales documents")],
1061     ["edit_prices",                    $locale->text("Edit prices and discount (if not used, textfield is ONLY set readonly)")],
1062     ["--ap",                           $locale->text("AP")],
1063     ["request_quotation_edit",         $locale->text("Create and edit RFQs")],
1064     ["purchase_order_edit",            $locale->text("Create and edit purchase orders")],
1065     ["purchase_delivery_order_edit",   $locale->text("Create and edit purchase delivery orders")],
1066     ["vendor_invoice_edit",            $locale->text("Create and edit vendor invoices")],
1067     ["--warehouse_management",         $locale->text("Warehouse management")],
1068     ["warehouse_contents",             $locale->text("View warehouse content")],
1069     ["warehouse_management",           $locale->text("Warehouse management")],
1070     ["--general_ledger_cash",          $locale->text("General ledger and cash")],
1071     ["general_ledger",                 $locale->text("Transactions, AR transactions, AP transactions")],
1072     ["datev_export",                   $locale->text("DATEV Export")],
1073     ["cash",                           $locale->text("Receipt, payment, reconciliation")],
1074     ["--reports",                      $locale->text('Reports')],
1075     ["report",                         $locale->text('All reports')],
1076     ["advance_turnover_tax_return",    $locale->text('Advance turnover tax return')],
1077     ["--batch_printing",               $locale->text("Batch Printing")],
1078     ["batch_printing",                 $locale->text("Batch Printing")],
1079     ["--configuration",                $locale->text("Configuration")],
1080     ["config",                         $locale->text("Change kivitendo installation settings (most entries in the 'System' menu)")],
1081     ["admin",                          $locale->text("Client administration: configuration, editing templates, task server control, background jobs (remaining entries in the 'System' menu)")],
1082     ["--others",                       $locale->text("Others")],
1083     ["email_bcc",                      $locale->text("May set the BCC field when sending emails")],
1084     ["productivity",                   $locale->text("Productivity")],
1085     ["display_admin_link",             $locale->text("Show administration link")],
1086     );
1087
1088   return @all_rights;
1089 }
1090
1091 sub all_rights {
1092   return grep !/^--/, map { $_->[0] } all_rights_full();
1093 }
1094
1095 sub read_groups {
1096   $main::lxdebug->enter_sub();
1097
1098   my $self = shift;
1099
1100   my $form   = $main::form;
1101   my $groups = {};
1102   my $dbh    = $self->dbconnect();
1103
1104   my $query  = 'SELECT * FROM auth."group"';
1105   my $sth    = prepare_execute_query($form, $dbh, $query);
1106
1107   my ($row, $group);
1108
1109   while ($row = $sth->fetchrow_hashref()) {
1110     $groups->{$row->{id}} = $row;
1111   }
1112   $sth->finish();
1113
1114   $query = 'SELECT * FROM auth.user_group WHERE group_id = ?';
1115   $sth   = prepare_query($form, $dbh, $query);
1116
1117   foreach $group (values %{$groups}) {
1118     my @members;
1119
1120     do_statement($form, $sth, $query, $group->{id});
1121
1122     while ($row = $sth->fetchrow_hashref()) {
1123       push @members, $row->{user_id};
1124     }
1125     $group->{members} = [ uniq @members ];
1126   }
1127   $sth->finish();
1128
1129   $query = 'SELECT * FROM auth.group_rights WHERE group_id = ?';
1130   $sth   = prepare_query($form, $dbh, $query);
1131
1132   foreach $group (values %{$groups}) {
1133     $group->{rights} = {};
1134
1135     do_statement($form, $sth, $query, $group->{id});
1136
1137     while ($row = $sth->fetchrow_hashref()) {
1138       $group->{rights}->{$row->{right}} |= $row->{granted};
1139     }
1140
1141     map { $group->{rights}->{$_} = 0 if (!defined $group->{rights}->{$_}); } all_rights();
1142   }
1143   $sth->finish();
1144
1145   $main::lxdebug->leave_sub();
1146
1147   return $groups;
1148 }
1149
1150 sub save_group {
1151   $main::lxdebug->enter_sub();
1152
1153   my $self  = shift;
1154   my $group = shift;
1155
1156   my $form  = $main::form;
1157   my $dbh   = $self->dbconnect();
1158
1159   $dbh->begin_work;
1160
1161   my ($query, $sth, $row, $rights);
1162
1163   if (!$group->{id}) {
1164     ($group->{id}) = selectrow_query($form, $dbh, qq|SELECT nextval('auth.group_id_seq')|);
1165
1166     $query = qq|INSERT INTO auth."group" (id, name, description) VALUES (?, '', '')|;
1167     do_query($form, $dbh, $query, $group->{id});
1168   }
1169
1170   do_query($form, $dbh, qq|UPDATE auth."group" SET name = ?, description = ? WHERE id = ?|, map { $group->{$_} } qw(name description id));
1171
1172   do_query($form, $dbh, qq|DELETE FROM auth.user_group WHERE group_id = ?|, $group->{id});
1173
1174   $query  = qq|INSERT INTO auth.user_group (user_id, group_id) VALUES (?, ?)|;
1175   $sth    = prepare_query($form, $dbh, $query);
1176
1177   foreach my $user_id (uniq @{ $group->{members} }) {
1178     do_statement($form, $sth, $query, $user_id, $group->{id});
1179   }
1180   $sth->finish();
1181
1182   do_query($form, $dbh, qq|DELETE FROM auth.group_rights WHERE group_id = ?|, $group->{id});
1183
1184   $query = qq|INSERT INTO auth.group_rights (group_id, "right", granted) VALUES (?, ?, ?)|;
1185   $sth   = prepare_query($form, $dbh, $query);
1186
1187   foreach my $right (keys %{ $group->{rights} }) {
1188     do_statement($form, $sth, $query, $group->{id}, $right, $group->{rights}->{$right} ? 't' : 'f');
1189   }
1190   $sth->finish();
1191
1192   $dbh->commit();
1193
1194   $main::lxdebug->leave_sub();
1195 }
1196
1197 sub delete_group {
1198   $main::lxdebug->enter_sub();
1199
1200   my $self = shift;
1201   my $id   = shift;
1202
1203   my $form = $main::form;
1204
1205   my $dbh  = $self->dbconnect();
1206   $dbh->begin_work;
1207
1208   do_query($form, $dbh, qq|DELETE FROM auth.user_group WHERE group_id = ?|, $id);
1209   do_query($form, $dbh, qq|DELETE FROM auth.group_rights WHERE group_id = ?|, $id);
1210   do_query($form, $dbh, qq|DELETE FROM auth."group" WHERE id = ?|, $id);
1211
1212   $dbh->commit();
1213
1214   $main::lxdebug->leave_sub();
1215 }
1216
1217 sub evaluate_rights_ary {
1218   $main::lxdebug->enter_sub(2);
1219
1220   my $ary    = shift;
1221
1222   my $value  = 0;
1223   my $action = '|';
1224
1225   foreach my $el (@{$ary}) {
1226     if (ref $el eq "ARRAY") {
1227       if ($action eq '|') {
1228         $value |= evaluate_rights_ary($el);
1229       } else {
1230         $value &= evaluate_rights_ary($el);
1231       }
1232
1233     } elsif (($el eq '&') || ($el eq '|')) {
1234       $action = $el;
1235
1236     } elsif ($action eq '|') {
1237       $value |= $el;
1238
1239     } else {
1240       $value &= $el;
1241
1242     }
1243   }
1244
1245   $main::lxdebug->leave_sub(2);
1246
1247   return $value;
1248 }
1249
1250 sub _parse_rights_string {
1251   $main::lxdebug->enter_sub(2);
1252
1253   my $self   = shift;
1254
1255   my $login  = shift;
1256   my $access = shift;
1257
1258   my @stack;
1259   my $cur_ary = [];
1260
1261   push @stack, $cur_ary;
1262
1263   while ($access =~ m/^([a-z_0-9]+|\||\&|\(|\)|\s+)/) {
1264     my $token = $1;
1265     substr($access, 0, length $1) = "";
1266
1267     next if ($token =~ /\s/);
1268
1269     if ($token eq "(") {
1270       my $new_cur_ary = [];
1271       push @stack, $new_cur_ary;
1272       push @{$cur_ary}, $new_cur_ary;
1273       $cur_ary = $new_cur_ary;
1274
1275     } elsif ($token eq ")") {
1276       pop @stack;
1277
1278       if (!@stack) {
1279         $main::lxdebug->leave_sub(2);
1280         return 0;
1281       }
1282
1283       $cur_ary = $stack[-1];
1284
1285     } elsif (($token eq "|") || ($token eq "&")) {
1286       push @{$cur_ary}, $token;
1287
1288     } else {
1289       push @{$cur_ary}, $self->{RIGHTS}->{$login}->{$token} * 1;
1290     }
1291   }
1292
1293   my $result = ($access || (1 < scalar @stack)) ? 0 : evaluate_rights_ary($stack[0]);
1294
1295   $main::lxdebug->leave_sub(2);
1296
1297   return $result;
1298 }
1299
1300 sub check_right {
1301   $main::lxdebug->enter_sub(2);
1302
1303   my $self    = shift;
1304   my $login   = shift;
1305   my $right   = shift;
1306   my $default = shift;
1307
1308   $self->{FULL_RIGHTS}           ||= { };
1309   $self->{FULL_RIGHTS}->{$login} ||= { };
1310
1311   if (!defined $self->{FULL_RIGHTS}->{$login}->{$right}) {
1312     $self->{RIGHTS}           ||= { };
1313     $self->{RIGHTS}->{$login} ||= $self->load_rights_for_user($login);
1314
1315     $self->{FULL_RIGHTS}->{$login}->{$right} = $self->_parse_rights_string($login, $right);
1316   }
1317
1318   my $granted = $self->{FULL_RIGHTS}->{$login}->{$right};
1319   $granted    = $default if (!defined $granted);
1320
1321   $main::lxdebug->leave_sub(2);
1322
1323   return $granted;
1324 }
1325
1326 sub assert {
1327   $::lxdebug->enter_sub(2);
1328   my ($self, $right, $dont_abort) = @_;
1329
1330   if ($self->check_right($::myconfig{login}, $right)) {
1331     $::lxdebug->leave_sub(2);
1332     return 1;
1333   }
1334
1335   if (!$dont_abort) {
1336     delete $::form->{title};
1337     $::form->show_generic_error($::locale->text("You do not have the permissions to access this function."));
1338   }
1339
1340   $::lxdebug->leave_sub(2);
1341
1342   return 0;
1343 }
1344
1345 sub load_rights_for_user {
1346   $::lxdebug->enter_sub;
1347
1348   my ($self, $login) = @_;
1349   my $dbh   = $self->dbconnect;
1350   my ($query, $sth, $row, $rights);
1351
1352   $rights = { map { $_ => 0 } all_rights() };
1353
1354   $query =
1355     qq|SELECT gr."right", gr.granted
1356        FROM auth.group_rights gr
1357        WHERE group_id IN
1358          (SELECT ug.group_id
1359           FROM auth.user_group ug
1360           LEFT JOIN auth."user" u ON (ug.user_id = u.id)
1361           WHERE u.login = ?)
1362        AND group_id IN
1363          (SELECT cg.group_id
1364           FROM auth.clients_groups cg
1365           WHERE cg.client_id = ?)|;
1366
1367   $sth = prepare_execute_query($::form, $dbh, $query, $login, $self->client->{id});
1368
1369   while ($row = $sth->fetchrow_hashref()) {
1370     $rights->{$row->{right}} |= $row->{granted};
1371   }
1372   $sth->finish();
1373
1374   $::lxdebug->leave_sub;
1375
1376   return $rights;
1377 }
1378
1379 1;
1380 __END__
1381
1382 =pod
1383
1384 =encoding utf8
1385
1386 =head1 NAME
1387
1388 SL::Auth - Authentication and session handling
1389
1390 =head1 FUNCTIONS
1391
1392 =over 4
1393
1394 =item C<set_session_value @values>
1395
1396 =item C<set_session_value %values>
1397
1398 Store all values of C<@values> or C<%values> in the session. Each
1399 member of C<@values> is tested if it is a hash reference. If it is
1400 then it must contain the keys C<key> and C<value> and can optionally
1401 contain the key C<auto_restore>. In this case C<value> is associated
1402 with C<key> and restored to C<$::form> upon the next request
1403 automatically if C<auto_restore> is trueish or if C<value> is a scalar
1404 value.
1405
1406 If the current member of C<@values> is not a hash reference then it
1407 will be used as the C<key> and the next entry of C<@values> is used as
1408 the C<value> to store. In this case setting C<auto_restore> is not
1409 possible.
1410
1411 Therefore the following two invocations are identical:
1412
1413   $::auth-E<gt>set_session_value(name =E<gt> "Charlie");
1414   $::auth-E<gt>set_session_value({ key =E<gt> "name", value =E<gt> "Charlie" });
1415
1416 All of these values are copied back into C<$::form> for the next
1417 request automatically if they're scalar values or if they have
1418 C<auto_restore> set to trueish.
1419
1420 The values can be any Perl structure. They are stored as YAML dumps.
1421
1422 =item C<get_session_value $key>
1423
1424 Retrieve a value from the session. Returns C<undef> if the value
1425 doesn't exist.
1426
1427 =item C<create_unique_sesion_value $value, %params>
1428
1429 Create a unique key in the session and store C<$value>
1430 there.
1431
1432 Returns the key created in the session.
1433
1434 =item C<save_session>
1435
1436 Stores the session values in the database. This is the only function
1437 that actually stores stuff in the database. Neither the various
1438 setters nor the deleter access the database.
1439
1440 =item <save_form_in_session %params>
1441
1442 Stores the content of C<$params{form}> (default: C<$::form>) in the
1443 session using L</create_unique_sesion_value>.
1444
1445 If C<$params{non_scalars}> is trueish then non-scalar values will be
1446 stored as well. Default is to only store scalar values.
1447
1448 The following keys will never be saved: C<login>, C<password>,
1449 C<stylesheet>, C<titlebar>, C<version>. Additional keys not to save
1450 can be given as an array ref in C<$params{skip_keys}>.
1451
1452 Returns the unique key under which the form is stored.
1453
1454 =item <restore_form_from_session $key, %params>
1455
1456 Restores the form from the session into C<$params{form}> (default:
1457 C<$::form>).
1458
1459 If C<$params{clobber}> is falsish then existing values with the same
1460 key in C<$params{form}> will not be overwritten. C<$params{clobber}>
1461 is on by default.
1462
1463 Returns C<$self>.
1464
1465 =back
1466
1467 =head1 BUGS
1468
1469 Nothing here yet.
1470
1471 =head1 AUTHOR
1472
1473 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
1474
1475 =cut