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";
68 # $locale->text('periodic')
69 # $locale->text('income')
70 # $locale->text('perpetual')
71 # $locale->text('balance')
79 $::lxdebug->enter_sub;
80 my $session_result = shift;
86 $::request->{layout} = SL::Layout::Dispatcher->new(style => 'admin');
87 $::request->{layout}->use_stylesheet("lx-office-erp.css");
88 $form->{favicon} = "favicon.ico";
90 if ($form->{action}) {
91 if ($auth->authenticate_root($form->{'{AUTH}admin_password'}) != $auth->OK()) {
92 $auth->punish_wrong_login;
93 $form->{error} = $locale->text('Incorrect password!');
94 $auth->delete_session_value('admin_password');
97 if ($auth->session_tables_present()) {
98 delete $::form->{'{AUTH}admin_password'};
101 call_sub($locale->findsub($form->{action}));
106 $::lxdebug->leave_sub;
110 my $form = $main::form;
111 my $locale = $main::locale;
113 $form->{title} = qq|kivitendo $form->{version} | . $locale->text('Administration');
116 print $form->parse_html_template('admin/adminlogin');
123 return ($login) ? $login : undef;
129 my ($null, $value) = split(/=/, $line, 2);
132 $value =~ s/\s#.*//g;
134 # remove any trailing whitespace
135 $value =~ s/^\s*(.*?)\s*$/$1/;
140 sub pg_database_administration {
141 my $form = $main::form;
145 sub dbselect_source {
146 my $form = $main::form;
147 my $locale = $main::locale;
149 $form->{dbport} = $::auth->{DB_config}->{port} || 5432;
150 $form->{dbuser} = $::auth->{DB_config}->{user} || 'lxoffice';
151 $form->{dbdefault} = 'template1';
152 $form->{dbhost} = $::auth->{DB_config}->{host} || 'localhost';
154 $form->{title} = "kivitendo / " . $locale->text('Database Administration');
156 # Intentionnaly disabled unless fixed to work with the authentication DB.
157 $form->{ALLOW_DBBACKUP} = 0; # "$pg_dump_exe" ne "DISABLED";
160 print $form->parse_html_template("admin/dbadmin");
163 sub test_db_connection {
164 my $form = $main::form;
165 my $locale = $main::locale;
167 User::dbconnect_vars($form, $form->{dbname});
169 my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd});
171 $form->{connection_ok} = $dbh ? 1 : 0;
172 $form->{errstr} = $DBI::errstr;
174 $dbh->disconnect() if ($dbh);
176 $form->{title} = $locale->text('Database Connection Test');
178 print $form->parse_html_template("admin/test_db_connection");
182 call_sub($main::form->{"nextsub"});
186 my $form = $main::form;
187 my $locale = $main::locale;
189 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Update Dataset');
191 my @need_updates = User->dbneedsupdate($form);
192 $form->{NEED_UPDATES} = \@need_updates;
193 $form->{ALL_UPDATED} = !scalar @need_updates;
196 print $form->parse_html_template("admin/update_dataset");
200 my $form = $main::form;
201 my $locale = $main::locale;
203 $::request->{layout}->use_stylesheet("lx-office-erp.css");
204 $form->{title} = $locale->text("Dataset upgrade");
207 my $rowcount = $form->{rowcount} * 1;
208 my @update_rows = grep { $form->{"update_$_"} } (1 .. $rowcount);
209 $form->{NOTHING_TO_DO} = !scalar @update_rows;
210 my $saved_form = save_form();
214 print $form->parse_html_template("admin/dbupgrade_all_header");
216 foreach my $i (@update_rows) {
217 restore_form($saved_form);
220 map { $form->{$_} = $::myconfig{$_} = $form->{"${_}_${i}"} } qw(dbname dbhost dbport dbuser dbpasswd);
222 print $form->parse_html_template("admin/dbupgrade_header");
224 $form->{dbupdate} = $form->{dbname};
225 $form->{$form->{dbname}} = 1;
227 User->dbupdate($form);
228 User->dbupdate2($form, SL::DBUpgrade2->new(form => $form)->parse_dbupdate_controls);
230 print $form->parse_html_template("admin/dbupgrade_footer");
233 print $form->parse_html_template("admin/dbupgrade_all_done");
237 my $form = $main::form;
238 my $locale = $main::locale;
240 $form->{dbsources} = join " ", map { "[${_}]" } sort User->dbsources($form);
242 $form->{CHARTS} = [];
244 tie my %dir_h, 'IO::Dir', 'sql/';
245 foreach my $item (map { s/-chart\.sql$//; $_ } sort grep { /-chart\.sql\z/ && !/Default-chart.sql\z/ } keys %dir_h) {
246 push @{ $form->{CHARTS} }, { name => $item,
247 selected => $item eq "Germany-DATEV-SKR03EU" };
250 $form->{ACCOUNTING_METHODS} = [ map { { name => $_, selected => $_ eq 'cash' } } qw(accrual cash) ];
251 $form->{INVENTORY_SYSTEMS} = [ map { { name => $_, selected => $_ eq 'periodic' } } qw(perpetual periodic) ];
252 $form->{PROFIT_DETERMINATIONS} = [ map { { name => $_, selected => $_ eq 'income' } } qw(balance income) ];
254 my $default_charset = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET;
256 my $cluster_encoding = User->dbclusterencoding($form);
257 if ($cluster_encoding && ($cluster_encoding =~ m/^(?:UTF-?8|UNICODE)$/i)) {
258 if ($::lx_office_conf{system}->{dbcharset} !~ m/^UTF-?8$/i) {
259 $form->show_generic_error($locale->text('The selected PostgreSQL installation uses UTF-8 as its encoding. ' .
260 'Therefore you have to configure kivitendo to use UTF-8 as well.'),
264 $form->{FORCE_DBENCODING} = 'UNICODE';
267 $form->{DBENCODINGS} = [ map { { %{$_}, selected => $_->{charset} eq $default_charset } } @Common::db_encodings ];
270 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Create Dataset');
273 print $form->parse_html_template("admin/create_dataset");
277 my $form = $main::form;
278 my $locale = $main::locale;
280 $form->isblank("db", $locale->text('Dataset missing!'));
281 $form->isblank("defaultcurrency", $locale->text('Default currency missing!'));
283 User->dbcreate(\%$form);
285 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Create Dataset');
288 print $form->parse_html_template("admin/dbcreate");
292 my $form = $main::form;
293 my $locale = $main::locale;
295 my @dbsources = User->dbsources_unused($form);
296 $form->error($locale->text('Nothing to delete!')) unless @dbsources;
298 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Delete Dataset');
299 $form->{DBSOURCES} = [ map { { "name", $_ } } sort @dbsources ];
302 print $form->parse_html_template("admin/delete_dataset");
306 my $form = $main::form;
307 my $locale = $main::locale;
310 $form->error($locale->text('No Dataset selected!'));
313 User->dbdelete(\%$form);
315 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Delete Dataset');
317 print $form->parse_html_template("admin/dbdelete");
321 my $form = $main::form;
322 my $locale = $main::locale;
324 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset');
326 if ($::lx_office_conf{applications}->{pg_dump} eq "DISABLED") {
327 $form->error($locale->text('Database backups and restorations are disabled in the configuration.'));
330 my @dbsources = sort User->dbsources($form);
331 $form->{DATABASES} = [ map { { "dbname" => $_ } } @dbsources ];
332 $form->{NO_DATABASES} = !scalar @dbsources;
334 my $username = getpwuid $UID || "unknown-user";
335 my $hostname = hostname() || "unknown-host";
336 $form->{from} = "kivitendo Admin <${username}\@${hostname}>";
339 print $form->parse_html_template("admin/backup_dataset");
342 sub backup_dataset_start {
343 my $form = $main::form;
344 my $locale = $main::locale;
346 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset');
348 my $pg_dump_exe = $::lx_office_conf{applications}->{pg_dump} || "pg_dump";
350 if ("$pg_dump_exe" eq "DISABLED") {
351 $form->error($locale->text('Database backups and restorations are disabled in the configuration.'));
354 $form->isblank("dbname", $locale->text('The dataset name is missing.'));
355 $form->isblank("to", $locale->text('The email address is missing.')) if $form->{destination} eq "email";
357 my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
358 mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
360 my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
364 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
367 print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
370 $ENV{HOME} = $tmpdir;
372 my @args = ("-Ft", "-c", "-o", "-h", $form->{dbhost}, "-U", $form->{dbuser});
373 push @args, ("-p", $form->{dbport}) if ($form->{dbport});
374 push @args, $form->{dbname};
376 my $cmd = "$pg_dump_exe " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
377 my $name = "dataset_backup_$form->{dbname}_" . strftime("%Y%m%d", localtime()) . ".tar";
379 if ($form->{destination} ne "email") {
380 my $in = IO::File->new("$cmd |");
383 unlink "${tmpdir}/.pgpass";
386 $form->error($locale->text('The pg_dump process could not be started.'));
389 print "content-type: application/x-tar\n";
390 print "content-disposition: attachment; filename=\"${name}\"\n\n";
392 while (my $line = <$in>) {
398 unlink "${tmpdir}/.pgpass";
402 my $tmp = $tmpdir . "/dump_" . Common::unique_id();
404 if (system("$cmd > $tmp") != 0) {
405 unlink "${tmpdir}/.pgpass", $tmp;
408 $form->error($locale->text('The pg_dump process could not be started.'));
411 my $mail = new Mailer;
413 map { $mail->{$_} = $form->{$_} } qw(from to cc subject message);
415 $mail->{charset} = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET;
416 $mail->{attachments} = [ { "filename" => $tmp, "name" => $name } ];
419 unlink "${tmpdir}/.pgpass", $tmp;
422 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset');
425 print $form->parse_html_template("admin/backup_dataset_email_done");
429 sub restore_dataset {
430 my $form = $main::form;
431 my $locale = $main::locale;
433 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Restore Dataset');
435 if ($::lx_office_conf{applications}->{pg_restore} eq "DISABLED") {
436 $form->error($locale->text('Database backups and restorations are disabled in the configuration.'));
439 my $default_charset = $::lx_office_conf{system}->{dbcharset};
440 $default_charset ||= Common::DEFAULT_CHARSET;
442 $form->{DBENCODINGS} = [];
444 foreach my $encoding (@Common::db_encodings) {
445 push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
446 "label" => $encoding->{label},
447 "selected" => $encoding->{charset} eq $default_charset };
451 print $form->parse_html_template("admin/restore_dataset");
454 sub restore_dataset_start {
455 my $form = $main::form;
456 my $locale = $main::locale;
458 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Restore Dataset');
460 my $pg_restore_exe = $::lx_office_conf{applications}->{pg_restore} || "pg_restore";
462 if ("$pg_restore_exe" eq "DISABLED") {
463 $form->error($locale->text('Database backups and restorations are disabled in the configuration.'));
466 $form->isblank("new_dbname", $locale->text('The dataset name is missing.'));
467 $form->isblank("content", $locale->text('No backup file has been uploaded.'));
469 # Create temporary directories. Write the backup file contents to a temporary
470 # file. Create a .pgpass file with the username and password for the pg_restore
473 my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
474 mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
476 my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
480 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
483 print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{new_dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
486 $ENV{HOME} = $tmpdir;
488 my $tmp = $tmpdir . "/dump_" . Common::unique_id();
491 if (substr($form->{content}, 0, 2) eq "\037\213") {
492 $tmpfile = IO::File->new("| gzip -d > $tmp");
496 $tmpfile = IO::File->new($tmp, O_WRONLY | O_CREAT | O_BINARY, 0600);
500 unlink "${tmpdir}/.pgpass";
503 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
506 print $tmpfile $form->{content};
509 delete $form->{content};
511 # Try to connect to the database. Find out if a database with the same name exists.
512 # If yes, then drop the existing database. Create a new one with the name and encoding
515 User::dbconnect_vars($form, "template1");
517 my %myconfig = map { $_ => $form->{$_} } grep /^db/, keys %{ $form };
518 my $dbh = $form->dbconnect(\%myconfig) || $form->dberror();
522 $form->{new_dbname} =~ s|[^a-zA-Z0-9_\-]||g;
524 $query = qq|SELECT COUNT(*) FROM pg_database WHERE datname = ?|;
525 my ($count) = selectrow_query($form, $dbh, $query, $form->{new_dbname});
527 do_query($form, $dbh, qq|DROP DATABASE $form->{new_dbname}|);
531 foreach my $item (@Common::db_encodings) {
532 if ($item->{dbencoding} eq $form->{dbencoding}) {
537 $form->{dbencoding} = "LATIN9" unless $form->{dbencoding};
539 do_query($form, $dbh, qq|CREATE DATABASE $form->{new_dbname} ENCODING ? TEMPLATE template0|, $form->{dbencoding});
543 # Spawn pg_restore on the temporary file.
545 my @args = ("-h", $form->{dbhost}, "-U", $form->{dbuser}, "-d", $form->{new_dbname});
546 push @args, ("-p", $form->{dbport}) if ($form->{dbport});
549 my $cmd = "$pg_restore_exe " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
551 my $in = IO::File->new("$cmd 2>&1 |");
554 unlink "${tmpdir}/.pgpass", $tmp;
557 $form->error($locale->text('The pg_restore process could not be started.'));
560 $English::AUTOFLUSH = 1;
563 print $form->parse_html_template("admin/restore_dataset_start_header");
565 while (my $line = <$in>) {
570 $form->{retval} = $CHILD_ERROR >> 8;
571 print $form->parse_html_template("admin/restore_dataset_start_footer");
573 unlink "${tmpdir}/.pgpass", $tmp;
578 call_sub($main::form->{yes_nextsub});
582 call_sub($main::form->{no_nextsub});
586 call_sub($main::form->{add_nextsub});
590 my $form = $main::form;
592 $form->{edit_nextsub} ||= 'edit_user';
594 call_sub($form->{edit_nextsub});
598 my $form = $main::form;
600 $form->{delete_nextsub} ||= 'delete_user';
602 call_sub($form->{delete_nextsub});
606 my $form = $main::form;
608 $form->{save_nextsub} ||= 'save_user';
610 call_sub($form->{save_nextsub});
614 call_sub($main::form->{back_nextsub});
618 my $form = $main::form;
619 my $locale = $main::locale;
621 foreach my $action (qw(create_standard_group dont_create_standard_group
622 save_user delete_user save_user_as_new)) {
623 if ($form->{"action_${action}"}) {
629 call_sub($form->{default_action}) if ($form->{default_action});
631 $form->error($locale->text('No action defined.'));
634 sub _search_templates {
635 my %templates = SL::Template->available_templates;
637 return ($templates{print_templates}, $templates{master_templates});