1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
31 # add/edit/delete users
33 #======================================================================
37 use English qw(-no_match_vars);
45 use POSIX qw(strftime);
49 use SL::Auth::PasswordPolicy;
50 use SL::DB::AuthClient;
62 require "bin/mozilla/common.pl";
63 require "bin/mozilla/admin_printer.pl";
69 # $locale->text('periodic')
70 # $locale->text('income')
71 # $locale->text('perpetual')
72 # $locale->text('balance')
80 $::lxdebug->enter_sub;
81 my $session_result = shift;
87 $::request->{layout} = SL::Layout::Dispatcher->new(style => 'admin');
88 $::request->{layout}->use_stylesheet("lx-office-erp.css");
89 $form->{favicon} = "favicon.ico";
91 if ($form->{action}) {
92 if ($auth->authenticate_root($form->{'{AUTH}admin_password'}) != $auth->OK()) {
93 $auth->punish_wrong_login;
94 $form->{error} = $locale->text('Incorrect Password!');
95 $auth->delete_session_value('admin_password');
98 if ($auth->session_tables_present()) {
99 delete $::form->{'{AUTH}admin_password'};
102 call_sub($locale->findsub($form->{action}));
105 # if there are no drivers bail out
106 $form->error($locale->text('No Database Drivers available!'))
107 unless (User->dbdrivers);
111 $::lxdebug->leave_sub;
115 my $form = $main::form;
116 my $locale = $main::locale;
118 $form->{title} = qq|kivitendo $form->{version} | . $locale->text('Administration');
121 print $form->parse_html_template('admin/adminlogin');
128 return ($login) ? $login : undef;
134 my ($null, $value) = split(/=/, $line, 2);
137 $value =~ s/\s#.*//g;
139 # remove any trailing whitespace
140 $value =~ s/^\s*(.*?)\s*$/$1/;
145 sub pg_database_administration {
146 my $form = $main::form;
148 $form->{dbdriver} = 'Pg';
153 sub dbselect_source {
154 my $form = $main::form;
155 my $locale = $main::locale;
157 $form->{dbport} = $::auth->{DB_config}->{port} || 5432;
158 $form->{dbuser} = $::auth->{DB_config}->{user} || 'lxoffice';
159 $form->{dbdefault} = 'template1';
160 $form->{dbhost} = $::auth->{DB_config}->{host} || 'localhost';
162 $form->{title} = "kivitendo / " . $locale->text('Database Administration');
164 # Intentionnaly disabled unless fixed to work with the authentication DB.
165 $form->{ALLOW_DBBACKUP} = 0; # "$pg_dump_exe" ne "DISABLED";
168 print $form->parse_html_template("admin/dbadmin");
171 sub test_db_connection {
172 my $form = $main::form;
173 my $locale = $main::locale;
175 $form->{dbdriver} = 'Pg';
176 User::dbconnect_vars($form, $form->{dbname});
178 my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd});
180 $form->{connection_ok} = $dbh ? 1 : 0;
181 $form->{errstr} = $DBI::errstr;
183 $dbh->disconnect() if ($dbh);
185 $form->{title} = $locale->text('Database Connection Test');
187 print $form->parse_html_template("admin/test_db_connection");
191 call_sub($main::form->{"nextsub"});
195 my $form = $main::form;
196 my $locale = $main::locale;
198 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Update Dataset');
200 my @need_updates = User->dbneedsupdate($form);
201 $form->{NEED_UPDATES} = \@need_updates;
202 $form->{ALL_UPDATED} = !scalar @need_updates;
205 print $form->parse_html_template("admin/update_dataset");
209 my $form = $main::form;
210 my $locale = $main::locale;
212 $::request->{layout}->use_stylesheet("lx-office-erp.css");
213 $form->{title} = $locale->text("Dataset upgrade");
216 my $rowcount = $form->{rowcount} * 1;
217 my @update_rows = grep { $form->{"update_$_"} } (1 .. $rowcount);
218 $form->{NOTHING_TO_DO} = !scalar @update_rows;
219 my $saved_form = save_form();
223 print $form->parse_html_template("admin/dbupgrade_all_header");
225 foreach my $i (@update_rows) {
226 restore_form($saved_form);
229 map { $form->{$_} = $::myconfig{$_} = $form->{"${_}_${i}"} } qw(dbname dbdriver dbhost dbport dbuser dbpasswd);
231 print $form->parse_html_template("admin/dbupgrade_header");
233 $form->{dbupdate} = $form->{dbname};
234 $form->{$form->{dbname}} = 1;
236 User->dbupdate($form);
237 User->dbupdate2($form, SL::DBUpgrade2->new(form => $form, dbdriver => $form->{dbdriver})->parse_dbupdate_controls);
239 print $form->parse_html_template("admin/dbupgrade_footer");
242 print $form->parse_html_template("admin/dbupgrade_all_done");
246 my $form = $main::form;
247 my $locale = $main::locale;
249 $form->{dbsources} = join " ", map { "[${_}]" } sort User->dbsources($form);
251 $form->{CHARTS} = [];
253 tie my %dir_h, 'IO::Dir', 'sql/';
254 foreach my $item (map { s/-chart\.sql$//; $_ } sort grep { /-chart\.sql\z/ && !/Default-chart.sql\z/ } keys %dir_h) {
255 push @{ $form->{CHARTS} }, { name => $item,
256 selected => $item eq "Germany-DATEV-SKR03EU" };
259 $form->{ACCOUNTING_METHODS} = [ map { { name => $_, selected => $_ eq 'cash' } } qw(accrual cash) ];
260 $form->{INVENTORY_SYSTEMS} = [ map { { name => $_, selected => $_ eq 'periodic' } } qw(perpetual periodic) ];
261 $form->{PROFIT_DETERMINATIONS} = [ map { { name => $_, selected => $_ eq 'income' } } qw(balance income) ];
263 my $default_charset = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET;
265 my $cluster_encoding = User->dbclusterencoding($form);
266 if ($cluster_encoding && ($cluster_encoding =~ m/^(?:UTF-?8|UNICODE)$/i)) {
267 if ($::lx_office_conf{system}->{dbcharset} !~ m/^UTF-?8$/i) {
268 $form->show_generic_error($locale->text('The selected PostgreSQL installation uses UTF-8 as its encoding. ' .
269 'Therefore you have to configure kivitendo to use UTF-8 as well.'),
273 $form->{FORCE_DBENCODING} = 'UNICODE';
276 $form->{DBENCODINGS} = [ map { { %{$_}, selected => $_->{charset} eq $default_charset } } @Common::db_encodings ];
279 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Create Dataset');
282 print $form->parse_html_template("admin/create_dataset");
286 my $form = $main::form;
287 my $locale = $main::locale;
289 $form->isblank("db", $locale->text('Dataset missing!'));
290 $form->isblank("defaultcurrency", $locale->text('Default currency missing!'));
292 User->dbcreate(\%$form);
294 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Create Dataset');
297 print $form->parse_html_template("admin/dbcreate");
301 my $form = $main::form;
302 my $locale = $main::locale;
304 my @dbsources = User->dbsources_unused($form);
305 $form->error($locale->text('Nothing to delete!')) unless @dbsources;
307 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Delete Dataset');
308 $form->{DBSOURCES} = [ map { { "name", $_ } } sort @dbsources ];
311 print $form->parse_html_template("admin/delete_dataset");
315 my $form = $main::form;
316 my $locale = $main::locale;
319 $form->error($locale->text('No Dataset selected!'));
322 User->dbdelete(\%$form);
324 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Delete Dataset');
326 print $form->parse_html_template("admin/dbdelete");
330 my $form = $main::form;
331 my $locale = $main::locale;
333 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset');
335 if ($::lx_office_conf{applications}->{pg_dump} eq "DISABLED") {
336 $form->error($locale->text('Database backups and restorations are disabled in the configuration.'));
339 my @dbsources = sort User->dbsources($form);
340 $form->{DATABASES} = [ map { { "dbname" => $_ } } @dbsources ];
341 $form->{NO_DATABASES} = !scalar @dbsources;
343 my $username = getpwuid $UID || "unknown-user";
344 my $hostname = hostname() || "unknown-host";
345 $form->{from} = "kivitendo Admin <${username}\@${hostname}>";
348 print $form->parse_html_template("admin/backup_dataset");
351 sub backup_dataset_start {
352 my $form = $main::form;
353 my $locale = $main::locale;
355 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset');
357 my $pg_dump_exe = $::lx_office_conf{applications}->{pg_dump} || "pg_dump";
359 if ("$pg_dump_exe" eq "DISABLED") {
360 $form->error($locale->text('Database backups and restorations are disabled in the configuration.'));
363 $form->isblank("dbname", $locale->text('The dataset name is missing.'));
364 $form->isblank("to", $locale->text('The email address is missing.')) if $form->{destination} eq "email";
366 my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
367 mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
369 my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
373 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
376 print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
379 $ENV{HOME} = $tmpdir;
381 my @args = ("-Ft", "-c", "-o", "-h", $form->{dbhost}, "-U", $form->{dbuser});
382 push @args, ("-p", $form->{dbport}) if ($form->{dbport});
383 push @args, $form->{dbname};
385 my $cmd = "$pg_dump_exe " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
386 my $name = "dataset_backup_$form->{dbname}_" . strftime("%Y%m%d", localtime()) . ".tar";
388 if ($form->{destination} ne "email") {
389 my $in = IO::File->new("$cmd |");
392 unlink "${tmpdir}/.pgpass";
395 $form->error($locale->text('The pg_dump process could not be started.'));
398 print "content-type: application/x-tar\n";
399 print "content-disposition: attachment; filename=\"${name}\"\n\n";
401 while (my $line = <$in>) {
407 unlink "${tmpdir}/.pgpass";
411 my $tmp = $tmpdir . "/dump_" . Common::unique_id();
413 if (system("$cmd > $tmp") != 0) {
414 unlink "${tmpdir}/.pgpass", $tmp;
417 $form->error($locale->text('The pg_dump process could not be started.'));
420 my $mail = new Mailer;
422 map { $mail->{$_} = $form->{$_} } qw(from to cc subject message);
424 $mail->{charset} = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET;
425 $mail->{attachments} = [ { "filename" => $tmp, "name" => $name } ];
428 unlink "${tmpdir}/.pgpass", $tmp;
431 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset');
434 print $form->parse_html_template("admin/backup_dataset_email_done");
438 sub restore_dataset {
439 my $form = $main::form;
440 my $locale = $main::locale;
442 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Restore Dataset');
444 if ($::lx_office_conf{applications}->{pg_restore} eq "DISABLED") {
445 $form->error($locale->text('Database backups and restorations are disabled in the configuration.'));
448 my $default_charset = $::lx_office_conf{system}->{dbcharset};
449 $default_charset ||= Common::DEFAULT_CHARSET;
451 $form->{DBENCODINGS} = [];
453 foreach my $encoding (@Common::db_encodings) {
454 push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
455 "label" => $encoding->{label},
456 "selected" => $encoding->{charset} eq $default_charset };
460 print $form->parse_html_template("admin/restore_dataset");
463 sub restore_dataset_start {
464 my $form = $main::form;
465 my $locale = $main::locale;
467 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Restore Dataset');
469 my $pg_restore_exe = $::lx_office_conf{applications}->{pg_restore} || "pg_restore";
471 if ("$pg_restore_exe" eq "DISABLED") {
472 $form->error($locale->text('Database backups and restorations are disabled in the configuration.'));
475 $form->isblank("new_dbname", $locale->text('The dataset name is missing.'));
476 $form->isblank("content", $locale->text('No backup file has been uploaded.'));
478 # Create temporary directories. Write the backup file contents to a temporary
479 # file. Create a .pgpass file with the username and password for the pg_restore
482 my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
483 mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
485 my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
489 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
492 print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{new_dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
495 $ENV{HOME} = $tmpdir;
497 my $tmp = $tmpdir . "/dump_" . Common::unique_id();
500 if (substr($form->{content}, 0, 2) eq "\037\213") {
501 $tmpfile = IO::File->new("| gzip -d > $tmp");
505 $tmpfile = IO::File->new($tmp, O_WRONLY | O_CREAT | O_BINARY, 0600);
509 unlink "${tmpdir}/.pgpass";
512 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
515 print $tmpfile $form->{content};
518 delete $form->{content};
520 # Try to connect to the database. Find out if a database with the same name exists.
521 # If yes, then drop the existing database. Create a new one with the name and encoding
524 User::dbconnect_vars($form, "template1");
526 my %myconfig = map { $_ => $form->{$_} } grep /^db/, keys %{ $form };
527 my $dbh = $form->dbconnect(\%myconfig) || $form->dberror();
531 $form->{new_dbname} =~ s|[^a-zA-Z0-9_\-]||g;
533 $query = qq|SELECT COUNT(*) FROM pg_database WHERE datname = ?|;
534 my ($count) = selectrow_query($form, $dbh, $query, $form->{new_dbname});
536 do_query($form, $dbh, qq|DROP DATABASE $form->{new_dbname}|);
540 foreach my $item (@Common::db_encodings) {
541 if ($item->{dbencoding} eq $form->{dbencoding}) {
546 $form->{dbencoding} = "LATIN9" unless $form->{dbencoding};
548 do_query($form, $dbh, qq|CREATE DATABASE $form->{new_dbname} ENCODING ? TEMPLATE template0|, $form->{dbencoding});
552 # Spawn pg_restore on the temporary file.
554 my @args = ("-h", $form->{dbhost}, "-U", $form->{dbuser}, "-d", $form->{new_dbname});
555 push @args, ("-p", $form->{dbport}) if ($form->{dbport});
558 my $cmd = "$pg_restore_exe " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
560 my $in = IO::File->new("$cmd 2>&1 |");
563 unlink "${tmpdir}/.pgpass", $tmp;
566 $form->error($locale->text('The pg_restore process could not be started.'));
569 $English::AUTOFLUSH = 1;
572 print $form->parse_html_template("admin/restore_dataset_start_header");
574 while (my $line = <$in>) {
579 $form->{retval} = $CHILD_ERROR >> 8;
580 print $form->parse_html_template("admin/restore_dataset_start_footer");
582 unlink "${tmpdir}/.pgpass", $tmp;
587 call_sub($main::form->{yes_nextsub});
591 call_sub($main::form->{no_nextsub});
595 call_sub($main::form->{add_nextsub});
599 my $form = $main::form;
601 $form->{edit_nextsub} ||= 'edit_user';
603 call_sub($form->{edit_nextsub});
607 my $form = $main::form;
609 $form->{delete_nextsub} ||= 'delete_user';
611 call_sub($form->{delete_nextsub});
615 my $form = $main::form;
617 $form->{save_nextsub} ||= 'save_user';
619 call_sub($form->{save_nextsub});
623 call_sub($main::form->{back_nextsub});
627 my $form = $main::form;
628 my $locale = $main::locale;
630 foreach my $action (qw(create_standard_group dont_create_standard_group
631 save_user delete_user save_user_as_new)) {
632 if ($form->{"action_${action}"}) {
638 call_sub($form->{default_action}) if ($form->{default_action});
640 $form->error($locale->text('No action defined.'));
643 sub _search_templates {
644 my %templates = SL::Template->available_templates;
646 return ($templates{print_templates}, $templates{master_templates});