4 use constant ERR_PASSWORD => 1;
5 use constant ERR_BACKEND => 100;
7 use Digest::MD5 qw(md5_hex);
9 use Time::HiRes qw(gettimeofday);
18 $main::lxdebug->enter_sub();
25 $self->{SESSION} = { };
27 $self->_read_auth_config();
29 $main::lxdebug->leave_sub();
37 $self->{dbh}->disconnect() if ($self->{dbh});
40 sub _read_auth_config {
41 $main::lxdebug->enter_sub();
45 my $form = $main::form;
46 my $locale = $main::locale;
49 my $in = IO::File->new('config/authentication.pl', 'r');
52 $form->error($locale->text('The config file "config/authentication.pl" was not found.'));
63 $form->error($locale->text('The config file "config/authentication.pl" contained invalid Perl code:') . "\n" . $@);
66 if ($self->{module} eq 'DB') {
67 $self->{authenticator} = SL::Auth::DB->new($self);
69 } elsif ($self->{module} eq 'LDAP') {
70 $self->{authenticator} = SL::Auth::LDAP->new($self);
73 if (!$self->{authenticator}) {
74 $form->error($locale->text('No or an unknown authenticantion module specified in "config/authentication.pl".'));
77 my $cfg = $self->{DB_config};
80 $form->error($locale->text('config/authentication.pl: Key "DB_config" is missing.'));
83 if (!$cfg->{host} || !$cfg->{db} || !$cfg->{user}) {
84 $form->error($locale->text('config/authentication.pl: Missing parameters in "DB_config". Required parameters are "host", "db" and "user".'));
87 $self->{authenticator}->verify_config();
89 $main::lxdebug->leave_sub();
92 sub authenticate_root {
93 $main::lxdebug->enter_sub();
97 my $is_crypted = shift;
99 $password = crypt $password, 'ro' if (!$password || !$is_crypted);
100 my $admin_password = crypt "$self->{admin_password}", 'ro';
102 $main::lxdebug->leave_sub();
104 return $password eq $admin_password ? OK : ERR_PASSWORD;
108 $main::lxdebug->enter_sub();
112 $main::lxdebug->leave_sub();
114 return $self->{authenticator}->authenticate(@_);
118 $main::lxdebug->enter_sub();
121 my $may_fail = shift;
124 $main::lxdebug->leave_sub();
128 my $cfg = $self->{DB_config};
129 my $dsn = 'dbi:Pg:dbname=' . $cfg->{db} . ';host=' . $cfg->{host};
132 $dsn .= ';port=' . $cfg->{port};
135 $main::lxdebug->message(LXDebug::DEBUG1, "Auth::dbconnect DSN: $dsn");
137 $self->{dbh} = DBI->connect($dsn, $cfg->{user}, $cfg->{password}, { 'AutoCommit' => 0 });
139 if (!$may_fail && !$self->{dbh}) {
140 $main::form->error($main::locale->text('The connection to the authentication database failed:') . "\n" . $DBI::errstr);
143 $main::lxdebug->leave_sub();
149 $main::lxdebug->enter_sub();
154 $self->{dbh}->disconnect();
158 $main::lxdebug->leave_sub();
162 $main::lxdebug->enter_sub();
166 my $dbh = $self->dbconnect();
167 my $query = qq|SELECT COUNT(*) FROM pg_tables WHERE (schemaname = 'auth') AND (tablename = 'user')|;
169 my ($count) = $dbh->selectrow_array($query);
171 $main::lxdebug->leave_sub();
177 $main::lxdebug->enter_sub();
181 my $dbh = $self->dbconnect(1);
183 $main::lxdebug->leave_sub();
188 sub create_database {
189 $main::lxdebug->enter_sub();
194 my $cfg = $self->{DB_config};
196 if (!$params{superuser}) {
197 $params{superuser} = $cfg->{user};
198 $params{superuser_password} = $cfg->{password};
201 $params{template} ||= 'template0';
202 $params{template} =~ s|[^a-zA-Z0-9_\-]||g;
204 my $dsn = 'dbi:Pg:dbname=template1;host=' . $cfg->{host};
207 $dsn .= ';port=' . $cfg->{port};
210 $main::lxdebug->message(LXDebug::DEBUG1, "Auth::create_database DSN: $dsn");
212 my $dbh = DBI->connect($dsn, $params{superuser}, $params{superuser_password});
215 $main::form->error($main::locale->text('The connection to the template database failed:') . "\n" . $DBI::errstr);
218 my $charset = $main::charset;
219 $charset ||= Common::DEFAULT_CHARSET;
220 my $encoding = $Common::charset_to_db_encoding{$charset};
221 $encoding ||= 'UNICODE';
223 my $query = qq|CREATE DATABASE "$cfg->{db}" OWNER "$cfg->{user}" TEMPLATE "$params{template}" ENCODING '$encoding'|;
225 $main::lxdebug->message(LXDebug::DEBUG1, "Auth::create_database query: $query");
232 $main::form->error($main::locale->text('The creation of the authentication database failed:') . "\n" . $DBI::errstr);
237 $main::lxdebug->leave_sub();
241 $main::lxdebug->enter_sub();
244 my $dbh = $self->dbconnect();
246 my $charset = $main::charset;
247 $charset ||= Common::DEFAULT_CHARSET;
250 User->process_query($main::form, $dbh, 'sql/auth_db.sql', undef, $charset);
252 $main::lxdebug->leave_sub();
256 $main::lxdebug->enter_sub();
262 my $form = $main::form;
264 my $dbh = $self->dbconnect();
266 my ($sth, $query, $user_id);
268 $query = qq|SELECT id FROM auth."user" WHERE login = ?|;
269 ($user_id) = selectrow_query($form, $dbh, $query, $login);
272 $query = qq|SELECT nextval('auth.user_id_seq')|;
273 ($user_id) = selectrow_query($form, $dbh, $query);
275 $query = qq|INSERT INTO auth."user" (id, login) VALUES (?, ?)|;
276 do_query($form, $dbh, $query, $user_id, $login);
279 $query = qq|DELETE FROM auth.user_config WHERE (user_id = ?)|;
280 do_query($form, $dbh, $query, $user_id);
282 $query = qq|INSERT INTO auth.user_config (user_id, cfg_key, cfg_value) VALUES (?, ?, ?)|;
283 $sth = prepare_query($form, $dbh, $query);
285 while (my ($cfg_key, $cfg_value) = each %params) {
286 next if ($cfg_key eq 'password');
288 do_statement($form, $sth, $query, $user_id, $cfg_key, $cfg_value);
293 $main::lxdebug->leave_sub();
296 sub can_change_password {
299 return $self->{authenticator}->can_change_password();
302 sub change_password {
303 $main::lxdebug->enter_sub();
306 my $result = $self->{authenticator}->change_password(@_);
308 $main::lxdebug->leave_sub();
314 $main::lxdebug->enter_sub();
318 my $dbh = $self->dbconnect();
319 my $query = qq|SELECT u.id, u.login, cfg.cfg_key, cfg.cfg_value
320 FROM auth.user_config cfg
321 LEFT JOIN auth."user" u ON (cfg.user_id = u.id)|;
322 my $sth = prepare_execute_query($main::form, $dbh, $query);
326 while (my $ref = $sth->fetchrow_hashref()) {
327 $users{$ref->{login}} ||= { 'login' => $ref->{login}, 'id' => $ref->{id} };
328 $users{$ref->{login}}->{$ref->{cfg_key}} = $ref->{cfg_value} if (($ref->{cfg_key} ne 'login') && ($ref->{cfg_key} ne 'id'));
333 $main::lxdebug->leave_sub();
339 $main::lxdebug->enter_sub();
344 my $dbh = $self->dbconnect();
345 my $query = qq|SELECT cfg.cfg_key, cfg.cfg_value
346 FROM auth.user_config cfg
347 LEFT JOIN auth."user" u ON (cfg.user_id = u.id)
348 WHERE (u.login = ?)|;
349 my $sth = prepare_execute_query($main::form, $dbh, $query, $login);
353 while (my $ref = $sth->fetchrow_hashref()) {
354 $user_data{$ref->{cfg_key}} = $ref->{cfg_value};
355 $user_data{login} = $login;
360 $main::lxdebug->leave_sub();
366 $main::lxdebug->enter_sub();
371 my $dbh = $self->dbconnect();
372 my ($id) = selectrow_query($main::form, $dbh, qq|SELECT id FROM auth."user" WHERE login = ?|, $login);
374 $main::lxdebug->leave_sub();
380 $main::lxdebug->enter_sub();
385 my $form = $main::form;
387 my $dbh = $self->dbconnect();
388 my $query = qq|SELECT id FROM auth."user" WHERE login = ?|;
390 my ($id) = selectrow_query($form, $dbh, $query, $login);
392 return $main::lxdebug->leave_sub() if (!$id);
394 do_query($form, $dbh, qq|DELETE FROM auth.user_group WHERE user_id = ?|, $id);
395 do_query($form, $dbh, qq|DELETE FROM auth.user_config WHERE user_id = ?|, $id);
399 $main::lxdebug->leave_sub();
402 # --------------------------------------
406 sub restore_session {
407 $main::lxdebug->enter_sub();
411 my $cgi = $main::cgi;
412 $cgi ||= CGI->new('');
414 $session_id = $cgi->cookie($self->get_session_cookie_name());
415 $session_id =~ s|[^0-9a-f]||g;
417 $self->{SESSION} = { };
419 return $main::lxdebug->leave_sub() if (!$session_id);
421 my ($dbh, $query, $sth, $cookie, $ref, $form);
425 $dbh = $self->dbconnect();
426 $query = qq|SELECT *, (mtime < (now() - '24h'::interval)) AS is_expired FROM auth.session WHERE id = ?|;
428 $cookie = selectfirst_hashref_query($form, $dbh, $query, $session_id);
430 if (!$cookie || $cookie->{is_expired} || ($cookie->{ip_address} ne $ENV{REMOTE_ADDR})) {
431 $self->destroy_session();
432 $main::lxdebug->leave_sub();
436 $query = qq|SELECT sess_key, sess_value FROM auth.session_content WHERE session_id = ?|;
437 $sth = prepare_execute_query($form, $dbh, $query, $session_id);
439 while (my $ref = $sth->fetchrow_hashref()) {
440 $self->{SESSION}->{$ref->{sess_key}} = $ref->{sess_value};
441 $form->{$ref->{sess_key}} = $ref->{sess_value} if (!defined $form->{$ref->{sess_key}});
446 $main::lxdebug->leave_sub();
449 sub destroy_session {
450 $main::lxdebug->enter_sub();
455 my $dbh = $self->dbconnect();
457 do_query($main::form, $dbh, qq|DELETE FROM auth.session_content WHERE session_id = ?|, $session_id);
458 do_query($main::form, $dbh, qq|DELETE FROM auth.session WHERE id = ?|, $session_id);
463 $self->{SESSION} = { };
466 $main::lxdebug->leave_sub();
469 sub expire_sessions {
470 $main::lxdebug->enter_sub();
474 my $dbh = $self->dbconnect();
476 qq|DELETE FROM auth.session_content
480 WHERE (mtime < (now() - '24h'::interval)))|;
482 do_query($main::form, $dbh, $query);
485 qq|DELETE FROM auth.session
486 WHERE (mtime < (now() - '24h'::interval))|;
488 do_query($main::form, $dbh, $query);
492 $main::lxdebug->leave_sub();
495 sub _create_session_id {
496 $main::lxdebug->enter_sub();
498 my @secs = gettimeofday();
502 map { push @data, int(rand() * 255); } (1..32);
504 my $id = md5_hex(pack 'C*', @data);
506 $main::lxdebug->leave_sub();
511 sub create_or_refresh_session {
512 $main::lxdebug->enter_sub();
516 $session_id ||= $self->_create_session_id();
518 my ($form, $dbh, $query, $sth, $id);
521 $dbh = $self->dbconnect();
523 $query = qq|SELECT id FROM auth.session WHERE id = ?|;
525 ($id) = selectrow_query($form, $dbh, $query, $session_id);
528 do_query($form, $dbh, qq|UPDATE auth.session SET mtime = now() WHERE id = ?|, $session_id);
529 do_query($form, $dbh, qq|DELETE FROM auth.session_content WHERE session_id = ?|, $session_id);
532 do_query($form, $dbh, qq|INSERT INTO auth.session (id, ip_address, mtime) VALUES (?, ?, now())|, $session_id, $ENV{REMOTE_ADDR});
536 $query = qq|INSERT INTO auth.session_content (session_id, sess_key, sess_value) VALUES (?, ?, ?)|;
537 $sth = prepare_query($form, $dbh, $query);
539 foreach my $key (sort keys %{ $self->{SESSION} }) {
540 do_statement($form, $sth, $query, $session_id, $key, $self->{SESSION}->{$key});
546 $main::lxdebug->leave_sub();
549 sub set_session_value {
550 $main::lxdebug->enter_sub();
554 $self->{SESSION} ||= { };
556 while (2 <= scalar @_) {
560 $self->{SESSION}->{$key} = $value;
563 $main::lxdebug->leave_sub();
566 sub set_cookie_environment_variable {
568 $ENV{HTTP_COOKIE} = $self->get_session_cookie_name() . "=${session_id}";
571 sub get_session_cookie_name {
574 return $self->{cookie_name} || 'lx_office_erp_session_id';
581 sub session_tables_present {
582 $main::lxdebug->enter_sub();
585 my $dbh = $self->dbconnect(1);
588 $main::lxdebug->leave_sub();
595 WHERE (schemaname = 'auth')
596 AND (tablename IN ('session', 'session_content'))|;
598 my ($count) = selectrow_query($main::form, $dbh, $query);
600 $main::lxdebug->leave_sub();
605 # --------------------------------------
607 sub all_rights_full {
608 my $locale = $main::locale;
611 ["--master_data", $locale->text("Master Data")],
612 ["customer_vendor_edit", $locale->text("Create and edit customers and vendors")],
613 ["part_service_assembly_edit", $locale->text("Create and edit parts, services, assemblies")],
614 ["project_edit", $locale->text("Create and edit projects")],
615 ["license_edit", $locale->text("Manage license keys")],
616 ["--ar", $locale->text("AR")],
617 ["sales_quotation_edit", $locale->text("Create and edit sales quotations")],
618 ["sales_order_edit", $locale->text("Create and edit sales orders")],
619 ["sales_delivery_order_edit", $locale->text("Create and edit sales delivery orders")],
620 ["invoice_edit", $locale->text("Create and edit invoices and credit notes")],
621 ["dunning_edit", $locale->text("Create and edit dunnings")],
622 ["--ap", $locale->text("AP")],
623 ["request_quotation_edit", $locale->text("Create and edit RFQs")],
624 ["purchase_order_edit", $locale->text("Create and edit purchase orders")],
625 ["purchase_delivery_order_edit", $locale->text("Create and edit purchase delivery orders")],
626 ["vendor_invoice_edit", $locale->text("Create and edit vendor invoices")],
627 ["--general_ledger_cash", $locale->text("General ledger and cash")],
628 ["general_ledger", $locale->text("Transactions, AR transactions, AP transactions")],
629 ["datev_export", $locale->text("DATEV Export")],
630 ["cash", $locale->text("Receipt, payment, reconciliation")],
631 ["--reports", $locale->text('Reports')],
632 ["report", $locale->text('All reports')],
633 ["advance_turnover_tax_return", $locale->text('Advance turnover tax return')],
634 ["--others", $locale->text("Others")],
635 ["email_bcc", $locale->text("May set the BCC field when sending emails")],
636 ["config", $locale->text("Change Lx-Office installation settings (all menu entries beneath 'System')")],
643 return grep !/^--/, map { $_->[0] } all_rights_full();
647 $main::lxdebug->enter_sub();
651 my $form = $main::form;
653 my $dbh = $self->dbconnect();
655 my $query = 'SELECT * FROM auth."group"';
656 my $sth = prepare_execute_query($form, $dbh, $query);
660 while ($row = $sth->fetchrow_hashref()) {
661 $groups->{$row->{id}} = $row;
665 $query = 'SELECT * FROM auth.user_group WHERE group_id = ?';
666 $sth = prepare_query($form, $dbh, $query);
668 foreach $group (values %{$groups}) {
669 $group->{members} = [];
671 do_statement($form, $sth, $query, $group->{id});
673 while ($row = $sth->fetchrow_hashref()) {
674 push @{$group->{members}}, $row->{user_id};
679 $query = 'SELECT * FROM auth.group_rights WHERE group_id = ?';
680 $sth = prepare_query($form, $dbh, $query);
682 foreach $group (values %{$groups}) {
683 $group->{rights} = {};
685 do_statement($form, $sth, $query, $group->{id});
687 while ($row = $sth->fetchrow_hashref()) {
688 $group->{rights}->{$row->{right}} |= $row->{granted};
691 map { $group->{rights}->{$_} = 0 if (!defined $group->{rights}->{$_}); } all_rights();
695 $main::lxdebug->leave_sub();
701 $main::lxdebug->enter_sub();
706 my $form = $main::form;
707 my $dbh = $self->dbconnect();
709 my ($query, $sth, $row, $rights);
712 ($group->{id}) = selectrow_query($form, $dbh, qq|SELECT nextval('auth.group_id_seq')|);
714 $query = qq|INSERT INTO auth."group" (id, name, description) VALUES (?, '', '')|;
715 do_query($form, $dbh, $query, $group->{id});
718 do_query($form, $dbh, qq|UPDATE auth."group" SET name = ?, description = ? WHERE id = ?|, map { $group->{$_} } qw(name description id));
720 do_query($form, $dbh, qq|DELETE FROM auth.user_group WHERE group_id = ?|, $group->{id});
722 $query = qq|INSERT INTO auth.user_group (user_id, group_id) VALUES (?, ?)|;
723 $sth = prepare_query($form, $dbh, $query);
725 foreach my $user_id (@{ $group->{members} }) {
726 do_statement($form, $sth, $query, $user_id, $group->{id});
730 do_query($form, $dbh, qq|DELETE FROM auth.group_rights WHERE group_id = ?|, $group->{id});
732 $query = qq|INSERT INTO auth.group_rights (group_id, "right", granted) VALUES (?, ?, ?)|;
733 $sth = prepare_query($form, $dbh, $query);
735 foreach my $right (keys %{ $group->{rights} }) {
736 do_statement($form, $sth, $query, $group->{id}, $right, $group->{rights}->{$right} ? 't' : 'f');
742 $main::lxdebug->leave_sub();
746 $main::lxdebug->enter_sub();
751 my $form = $main::from;
753 my $dbh = $self->dbconnect();
755 do_query($form, $dbh, qq|DELETE FROM auth.user_group WHERE group_id = ?|, $id);
756 do_query($form, $dbh, qq|DELETE FROM auth.group_rights WHERE group_id = ?|, $id);
757 do_query($form, $dbh, qq|DELETE FROM auth."group" WHERE id = ?|, $id);
761 $main::lxdebug->leave_sub();
764 sub evaluate_rights_ary {
765 $main::lxdebug->enter_sub(2);
772 foreach my $el (@{$ary}) {
773 if (ref $el eq "ARRAY") {
774 if ($action eq '|') {
775 $value |= evaluate_rights_ary($el);
777 $value &= evaluate_rights_ary($el);
780 } elsif (($el eq '&') || ($el eq '|')) {
783 } elsif ($action eq '|') {
792 $main::lxdebug->enter_sub(2);
797 sub _parse_rights_string {
798 $main::lxdebug->enter_sub(2);
808 push @stack, $cur_ary;
810 while ($access =~ m/^([a-z_0-9]+|\||\&|\(|\)|\s+)/) {
812 substr($access, 0, length $1) = "";
814 next if ($token =~ /\s/);
817 my $new_cur_ary = [];
818 push @stack, $new_cur_ary;
819 push @{$cur_ary}, $new_cur_ary;
820 $cur_ary = $new_cur_ary;
822 } elsif ($token eq ")") {
826 $main::lxdebug->enter_sub(2);
830 $cur_ary = $stack[-1];
832 } elsif (($token eq "|") || ($token eq "&")) {
833 push @{$cur_ary}, $token;
836 push @{$cur_ary}, $self->{RIGHTS}->{$login}->{$token} * 1;
840 my $result = ($access || (1 < scalar @stack)) ? 0 : evaluate_rights_ary($stack[0]);
842 $main::lxdebug->enter_sub(2);
848 $main::lxdebug->enter_sub(2);
855 $self->{FULL_RIGHTS} ||= { };
856 $self->{FULL_RIGHTS}->{$login} ||= { };
858 if (!defined $self->{FULL_RIGHTS}->{$login}->{$right}) {
859 $self->{RIGHTS} ||= { };
860 $self->{RIGHTS}->{$login} ||= $self->load_rights_for_user($login);
862 $self->{FULL_RIGHTS}->{$login}->{$right} = $self->_parse_rights_string($login, $right);
865 my $granted = $self->{FULL_RIGHTS}->{$login}->{$right};
866 $granted = $default if (!defined $granted);
868 $main::lxdebug->leave_sub(2);
874 $main::lxdebug->enter_sub(2);
878 my $dont_abort = shift;
880 my $form = $main::form;
882 if ($self->check_right($form->{login}, $right)) {
883 $main::lxdebug->leave_sub(2);
888 delete $form->{title};
889 $form->show_generic_error($main::locale->text("You do not have the permissions to access this function."));
892 $main::lxdebug->leave_sub(2);
897 sub load_rights_for_user {
898 $main::lxdebug->enter_sub();
903 my $form = $main::form;
904 my $dbh = $self->dbconnect();
906 my ($query, $sth, $row, $rights);
911 qq|SELECT gr."right", gr.granted
912 FROM auth.group_rights gr
915 FROM auth.user_group ug
916 LEFT JOIN auth."user" u ON (ug.user_id = u.id)
919 $sth = prepare_execute_query($form, $dbh, $query, $login);
921 while ($row = $sth->fetchrow_hashref()) {
922 $rights->{$row->{right}} |= $row->{granted};
926 map({ $rights->{$_} = 0 unless (defined $rights->{$_}); } SL::Auth::all_rights());
928 $main::lxdebug->leave_sub();