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 User->dbupdate($form);
225 User->dbupdate2(form => $form, updater => SL::DBUpgrade2->new(form => $form)->parse_dbupdate_controls, database => $form->{dbname});
227 print $form->parse_html_template("admin/dbupgrade_footer");
230 print $form->parse_html_template("admin/dbupgrade_all_done");
234 my $form = $main::form;
235 my $locale = $main::locale;
237 $form->{dbsources} = join " ", map { "[${_}]" } sort User->dbsources($form);
239 $form->{CHARTS} = [];
241 tie my %dir_h, 'IO::Dir', 'sql/';
242 foreach my $item (map { s/-chart\.sql$//; $_ } sort grep { /-chart\.sql\z/ && !/Default-chart.sql\z/ } keys %dir_h) {
243 push @{ $form->{CHARTS} }, { name => $item,
244 selected => $item eq "Germany-DATEV-SKR03EU" };
247 $form->{ACCOUNTING_METHODS} = [ map { { name => $_, selected => $_ eq 'cash' } } qw(accrual cash) ];
248 $form->{INVENTORY_SYSTEMS} = [ map { { name => $_, selected => $_ eq 'periodic' } } qw(perpetual periodic) ];
249 $form->{PROFIT_DETERMINATIONS} = [ map { { name => $_, selected => $_ eq 'income' } } qw(balance income) ];
251 my $default_charset = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET;
253 my $cluster_encoding = User->dbclusterencoding($form);
254 if ($cluster_encoding && ($cluster_encoding =~ m/^(?:UTF-?8|UNICODE)$/i)) {
255 if ($::lx_office_conf{system}->{dbcharset} !~ m/^UTF-?8$/i) {
256 $form->show_generic_error($locale->text('The selected PostgreSQL installation uses UTF-8 as its encoding. ' .
257 'Therefore you have to configure kivitendo to use UTF-8 as well.'),
261 $form->{FORCE_DBENCODING} = 'UNICODE';
264 $form->{DBENCODINGS} = [ map { { %{$_}, selected => $_->{charset} eq $default_charset } } @Common::db_encodings ];
267 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Create Dataset');
270 print $form->parse_html_template("admin/create_dataset");
274 my $form = $main::form;
275 my $locale = $main::locale;
277 $form->isblank("db", $locale->text('Dataset missing!'));
278 $form->isblank("defaultcurrency", $locale->text('Default currency missing!'));
280 User->dbcreate(\%$form);
282 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Create Dataset');
285 print $form->parse_html_template("admin/dbcreate");
289 my $form = $main::form;
290 my $locale = $main::locale;
292 my @dbsources = User->dbsources_unused($form);
293 $form->error($locale->text('Nothing to delete!')) unless @dbsources;
295 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Delete Dataset');
296 $form->{DBSOURCES} = [ map { { "name", $_ } } sort @dbsources ];
299 print $form->parse_html_template("admin/delete_dataset");
303 my $form = $main::form;
304 my $locale = $main::locale;
307 $form->error($locale->text('No Dataset selected!'));
310 User->dbdelete(\%$form);
312 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Delete Dataset');
314 print $form->parse_html_template("admin/dbdelete");
318 my $form = $main::form;
319 my $locale = $main::locale;
321 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset');
323 if ($::lx_office_conf{applications}->{pg_dump} eq "DISABLED") {
324 $form->error($locale->text('Database backups and restorations are disabled in the configuration.'));
327 my @dbsources = sort User->dbsources($form);
328 $form->{DATABASES} = [ map { { "dbname" => $_ } } @dbsources ];
329 $form->{NO_DATABASES} = !scalar @dbsources;
331 my $username = getpwuid $UID || "unknown-user";
332 my $hostname = hostname() || "unknown-host";
333 $form->{from} = "kivitendo Admin <${username}\@${hostname}>";
336 print $form->parse_html_template("admin/backup_dataset");
339 sub backup_dataset_start {
340 my $form = $main::form;
341 my $locale = $main::locale;
343 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset');
345 my $pg_dump_exe = $::lx_office_conf{applications}->{pg_dump} || "pg_dump";
347 if ("$pg_dump_exe" eq "DISABLED") {
348 $form->error($locale->text('Database backups and restorations are disabled in the configuration.'));
351 $form->isblank("dbname", $locale->text('The dataset name is missing.'));
352 $form->isblank("to", $locale->text('The email address is missing.')) if $form->{destination} eq "email";
354 my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
355 mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
357 my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
361 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
364 print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
367 $ENV{HOME} = $tmpdir;
369 my @args = ("-Ft", "-c", "-o", "-h", $form->{dbhost}, "-U", $form->{dbuser});
370 push @args, ("-p", $form->{dbport}) if ($form->{dbport});
371 push @args, $form->{dbname};
373 my $cmd = "$pg_dump_exe " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
374 my $name = "dataset_backup_$form->{dbname}_" . strftime("%Y%m%d", localtime()) . ".tar";
376 if ($form->{destination} ne "email") {
377 my $in = IO::File->new("$cmd |");
380 unlink "${tmpdir}/.pgpass";
383 $form->error($locale->text('The pg_dump process could not be started.'));
386 print "content-type: application/x-tar\n";
387 print "content-disposition: attachment; filename=\"${name}\"\n\n";
389 while (my $line = <$in>) {
395 unlink "${tmpdir}/.pgpass";
399 my $tmp = $tmpdir . "/dump_" . Common::unique_id();
401 if (system("$cmd > $tmp") != 0) {
402 unlink "${tmpdir}/.pgpass", $tmp;
405 $form->error($locale->text('The pg_dump process could not be started.'));
408 my $mail = new Mailer;
410 map { $mail->{$_} = $form->{$_} } qw(from to cc subject message);
412 $mail->{charset} = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET;
413 $mail->{attachments} = [ { "filename" => $tmp, "name" => $name } ];
416 unlink "${tmpdir}/.pgpass", $tmp;
419 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset');
422 print $form->parse_html_template("admin/backup_dataset_email_done");
426 sub restore_dataset {
427 my $form = $main::form;
428 my $locale = $main::locale;
430 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Restore Dataset');
432 if ($::lx_office_conf{applications}->{pg_restore} eq "DISABLED") {
433 $form->error($locale->text('Database backups and restorations are disabled in the configuration.'));
436 my $default_charset = $::lx_office_conf{system}->{dbcharset};
437 $default_charset ||= Common::DEFAULT_CHARSET;
439 $form->{DBENCODINGS} = [];
441 foreach my $encoding (@Common::db_encodings) {
442 push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
443 "label" => $encoding->{label},
444 "selected" => $encoding->{charset} eq $default_charset };
448 print $form->parse_html_template("admin/restore_dataset");
451 sub restore_dataset_start {
452 my $form = $main::form;
453 my $locale = $main::locale;
455 $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Restore Dataset');
457 my $pg_restore_exe = $::lx_office_conf{applications}->{pg_restore} || "pg_restore";
459 if ("$pg_restore_exe" eq "DISABLED") {
460 $form->error($locale->text('Database backups and restorations are disabled in the configuration.'));
463 $form->isblank("new_dbname", $locale->text('The dataset name is missing.'));
464 $form->isblank("content", $locale->text('No backup file has been uploaded.'));
466 # Create temporary directories. Write the backup file contents to a temporary
467 # file. Create a .pgpass file with the username and password for the pg_restore
470 my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
471 mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
473 my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
477 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
480 print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{new_dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
483 $ENV{HOME} = $tmpdir;
485 my $tmp = $tmpdir . "/dump_" . Common::unique_id();
488 if (substr($form->{content}, 0, 2) eq "\037\213") {
489 $tmpfile = IO::File->new("| gzip -d > $tmp");
493 $tmpfile = IO::File->new($tmp, O_WRONLY | O_CREAT | O_BINARY, 0600);
497 unlink "${tmpdir}/.pgpass";
500 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
503 print $tmpfile $form->{content};
506 delete $form->{content};
508 # Try to connect to the database. Find out if a database with the same name exists.
509 # If yes, then drop the existing database. Create a new one with the name and encoding
512 User::dbconnect_vars($form, "template1");
514 my %myconfig = map { $_ => $form->{$_} } grep /^db/, keys %{ $form };
515 my $dbh = $form->dbconnect(\%myconfig) || $form->dberror();
519 $form->{new_dbname} =~ s|[^a-zA-Z0-9_\-]||g;
521 $query = qq|SELECT COUNT(*) FROM pg_database WHERE datname = ?|;
522 my ($count) = selectrow_query($form, $dbh, $query, $form->{new_dbname});
524 do_query($form, $dbh, qq|DROP DATABASE $form->{new_dbname}|);
528 foreach my $item (@Common::db_encodings) {
529 if ($item->{dbencoding} eq $form->{dbencoding}) {
534 $form->{dbencoding} = "LATIN9" unless $form->{dbencoding};
536 do_query($form, $dbh, qq|CREATE DATABASE $form->{new_dbname} ENCODING ? TEMPLATE template0|, $form->{dbencoding});
540 # Spawn pg_restore on the temporary file.
542 my @args = ("-h", $form->{dbhost}, "-U", $form->{dbuser}, "-d", $form->{new_dbname});
543 push @args, ("-p", $form->{dbport}) if ($form->{dbport});
546 my $cmd = "$pg_restore_exe " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
548 my $in = IO::File->new("$cmd 2>&1 |");
551 unlink "${tmpdir}/.pgpass", $tmp;
554 $form->error($locale->text('The pg_restore process could not be started.'));
557 $English::AUTOFLUSH = 1;
560 print $form->parse_html_template("admin/restore_dataset_start_header");
562 while (my $line = <$in>) {
567 $form->{retval} = $CHILD_ERROR >> 8;
568 print $form->parse_html_template("admin/restore_dataset_start_footer");
570 unlink "${tmpdir}/.pgpass", $tmp;
575 call_sub($main::form->{yes_nextsub});
579 call_sub($main::form->{no_nextsub});
583 call_sub($main::form->{add_nextsub});
587 my $form = $main::form;
589 $form->{edit_nextsub} ||= 'edit_user';
591 call_sub($form->{edit_nextsub});
595 my $form = $main::form;
597 $form->{delete_nextsub} ||= 'delete_user';
599 call_sub($form->{delete_nextsub});
603 my $form = $main::form;
605 $form->{save_nextsub} ||= 'save_user';
607 call_sub($form->{save_nextsub});
611 call_sub($main::form->{back_nextsub});
615 my $form = $main::form;
616 my $locale = $main::locale;
618 foreach my $action (qw(create_standard_group dont_create_standard_group
619 save_user delete_user save_user_as_new)) {
620 if ($form->{"action_${action}"}) {
626 call_sub($form->{default_action}) if ($form->{default_action});
628 $form->error($locale->text('No action defined.'));
631 sub _search_templates {
632 my %templates = SL::Template->available_templates;
634 return ($templates{print_templates}, $templates{master_templates});