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
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #=====================================================================
31 # user related functions
33 #=====================================================================
41 use SL::DB::AuthClient;
48 use SL::System::InstallationLock;
49 use SL::DefaultManager;
53 use constant LOGIN_OK => 0;
54 use constant LOGIN_BASIC_TABLES_MISSING => -1;
55 use constant LOGIN_DBUPDATE_AVAILABLE => -2;
56 use constant LOGIN_AUTH_DBUPDATE_AVAILABLE => -3;
57 use constant LOGIN_GENERAL_ERROR => -4;
60 $main::lxdebug->enter_sub();
62 my ($type, %params) = @_;
66 if ($params{id} || $params{login}) {
67 my %user_data = $main::auth->read_user(%params);
68 map { $self->{$_} = $user_data{$_} } keys %user_data;
71 $main::lxdebug->leave_sub();
77 $main::lxdebug->enter_sub();
84 # scan the locale directory and read in the LANGUAGE files
85 opendir(DIR, "locale");
87 my @dir = grep(!/(^\.\.?$|\..*)/, readdir(DIR));
89 foreach my $dir (@dir) {
90 next unless open(my $fh, '<:encoding(UTF-8)', "locale/$dir/LANGUAGE");
94 $cc{$dir} = "@language";
99 $main::lxdebug->leave_sub();
105 my ($self, $form) = @_;
107 return LOGIN_GENERAL_ERROR() if !$self->{login} || !$::auth->client;
109 my %myconfig = $main::auth->read_user(login => $self->{login});
111 # Auth DB upgrades available?
112 my $dbupdater_auth = SL::DBUpgrade2->new(form => $form, auth => 1)->parse_dbupdate_controls;
113 return LOGIN_AUTH_DBUPDATE_AVAILABLE() if $dbupdater_auth->unapplied_upgrade_scripts($::auth->dbconnect);
115 # check if database is down
116 my $dbh = $form->dbconnect_noauto;
118 # we got a connection, check the version
119 my ($dbversion) = $dbh->selectrow_array(qq|SELECT version FROM defaults|);
122 return LOGIN_BASIC_TABLES_MISSING();
125 $self->create_schema_info_table($form, $dbh);
127 my $dbupdater = SL::DBUpgrade2->new(form => $form)->parse_dbupdate_controls;
128 my @unapplied_scripts = $dbupdater->unapplied_upgrade_scripts($dbh);
131 if (!@unapplied_scripts) {
132 SL::DB::Manager::Employee->update_entries_for_authorized_users;
136 # Store the fact that we're applying database upgrades at the
137 # moment. That way functions called from the layout modules that may
138 # require updated tables can chose only to use basic features.
139 $::request->applying_database_upgrades(1);
141 $form->{$_} = $::auth->client->{$_} for qw(dbname dbhost dbport dbuser dbpasswd);
142 $form->{$_} = $myconfig{$_} for qw(datestyle);
144 $form->{"title"} = $main::locale->text("Dataset upgrade");
145 $form->header(no_layout => $form->{no_layout});
146 print $form->parse_html_template("dbupgrade/header");
148 $form->{dbupdate} = "db" . $::auth->client->{dbname};
150 if ($form->{"show_dbupdate_warning"}) {
151 print $form->parse_html_template("dbupgrade/warning", { unapplied_scripts => \@unapplied_scripts });
152 $::dispatcher->end_request;
156 SL::System::InstallationLock->lock;
158 # ignore HUP, QUIT in case the webserver times out
159 $SIG{HUP} = 'IGNORE';
160 $SIG{QUIT} = 'IGNORE';
162 $self->dbupdate2(form => $form, updater => $dbupdater, database => $::auth->client->{dbname});
164 # If $self->dbupdate2 returns than this means all upgrade scripts
165 # have been applied successfully, none required user
166 # interaction. Otherwise the deeper layers would have called
167 # $::dispatcher->end_request already, and return would not have returned to
168 # us. Therefore we can now use RDBO instances because their supposed
169 # table structures do match the actual structures. So let's ensure
170 # that the "employee" table contains the appropriate entries for all
171 # users authorized for the current client.
172 SL::DB::Manager::Employee->update_entries_for_authorized_users;
174 SL::System::InstallationLock->unlock;
176 print $form->parse_html_template("dbupgrade/footer");
178 return LOGIN_DBUPDATE_AVAILABLE();
182 $main::lxdebug->enter_sub();
184 my ($form, $db) = @_;
187 'yy-mm-dd' => 'set DateStyle to \'ISO\'',
188 'yyyy-mm-dd' => 'set DateStyle to \'ISO\'',
189 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
190 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
191 'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
194 $form->{dboptions} = $dboptions{ $form->{dateformat} };
195 $form->{dbconnect} = "dbi:Pg:dbname=${db};host=" . ($form->{dbhost} || 'localhost') . ";port=" . ($form->{dbport} || 5432);
197 $main::lxdebug->leave_sub();
201 $main::lxdebug->enter_sub();
203 my ($self, $form) = @_;
208 $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
209 &dbconnect_vars($form, $form->{dbdefault});
211 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
215 qq|SELECT datname FROM pg_database | .
216 qq|WHERE NOT datname IN ('template0', 'template1')|;
217 $sth = $dbh->prepare($query);
218 $sth->execute() || $form->dberror($query);
220 while (my ($db) = $sth->fetchrow_array) {
222 if ($form->{only_acc_db}) {
224 next if ($db =~ /^template/);
226 &dbconnect_vars($form, $db);
227 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
231 qq|SELECT tablename FROM pg_tables | .
232 qq|WHERE (tablename = 'defaults') AND (tableowner = ?)|;
233 my $sth = $dbh->prepare($query);
234 $sth->execute($form->{dbuser}) ||
235 $form->dberror($query . " ($form->{dbuser})");
237 if ($sth->fetchrow_array) {
238 push(@dbsources, $db);
244 push(@dbsources, $db);
250 $main::lxdebug->leave_sub();
256 $main::lxdebug->enter_sub();
258 my ($self, $form) = @_;
260 &dbconnect_vars($form, $form->{dbdefault});
262 SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
264 $form->{db} =~ s/\"//g;
268 push @dboptions, "ENCODING = " . $dbh->quote($form->{"encoding"}) if $form->{"encoding"};
269 if ($form->{"dbdefault"}) {
270 my $dbdefault = $form->{"dbdefault"};
271 $dbdefault =~ s/[^a-zA-Z0-9_\-]//g;
272 push @dboptions, "TEMPLATE = $dbdefault";
275 my $query = qq|CREATE DATABASE "$form->{db}"|;
276 $query .= " WITH " . join(" ", @dboptions) if @dboptions;
278 # Ignore errors if the database exists.
283 &dbconnect_vars($form, $form->{db});
285 # make a shim myconfig so that rose db connections work
286 $::myconfig{$_} = $form->{$_} for qw(dbhost dbport dbuser dbpasswd);
287 $::myconfig{dbname} = $form->{db};
289 $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
292 my $dbupdater = SL::DBUpgrade2->new(form => $form, return_on_error => 1, silent => 1)->parse_dbupdate_controls;
294 $dbupdater->process_query($dbh, "sql/lx-office.sql");
295 $dbupdater->process_query($dbh, "sql/$form->{chart}-chart.sql");
297 $query = qq|UPDATE defaults SET coa = ?|;
298 do_query($form, $dbh, $query, map { $form->{$_} } qw(chart));
302 # update new database
303 $self->dbupdate2(form => $form, updater => $dbupdater, database => $form->{db}, silent => 1);
305 $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
308 $query = "SELECT * FROM currencies WHERE name = ?";
309 my $curr = selectfirst_hashref_query($form, $dbh, $query, $form->{defaultcurrency});
311 do_query($form, $dbh, "INSERT INTO currencies (name) VALUES (?)", $form->{defaultcurrency});
312 $curr = selectfirst_hashref_query($form, $dbh, $query, $form->{defaultcurrency});
315 $query = qq|UPDATE defaults SET accounting_method = ?, profit_determination = ?, inventory_system = ?, precision = ?, currency_id = ?|;
316 do_query($form, $dbh, $query,
317 $form->{accounting_method},
318 $form->{profit_determination},
319 $form->{inventory_system},
320 $form->parse_amount(\%::myconfig, $form->{precision_as_number}),
326 $main::lxdebug->leave_sub();
330 $main::lxdebug->enter_sub();
332 my ($self, $form) = @_;
333 $form->{db} =~ s/\"//g;
335 &dbconnect_vars($form, $form->{dbdefault});
336 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
338 my $query = qq|DROP DATABASE "$form->{db}"|;
339 do_query($form, $dbh, $query);
343 $main::lxdebug->leave_sub();
347 $main::lxdebug->enter_sub(2);
349 my (@v, $version, $i);
351 @v = split(/\./, $_[0]);
352 while (scalar(@v) < 4) {
356 for ($i = 0; $i < 4; $i++) {
361 $main::lxdebug->leave_sub(2);
365 sub cmp_script_version {
366 my ($a_from, $a_to, $b_from, $b_to);
367 my ($i, $res_a, $res_b);
368 my ($my_a, $my_b) = do { no warnings 'once'; ($a, $b) };
370 $my_a =~ s/.*-upgrade-//;
372 $my_b =~ s/.*-upgrade-//;
374 my ($my_a_from, $my_a_to) = split(/-/, $my_a);
375 my ($my_b_from, $my_b_to) = split(/-/, $my_b);
377 $res_a = calc_version($my_a_from);
378 $res_b = calc_version($my_b_from);
380 if ($res_a == $res_b) {
381 $res_a = calc_version($my_a_to);
382 $res_b = calc_version($my_b_to);
385 return $res_a <=> $res_b;
388 sub create_schema_info_table {
389 $main::lxdebug->enter_sub();
391 my ($self, $form, $dbh) = @_;
393 my $query = "SELECT tag FROM schema_info LIMIT 1";
394 if (!$dbh->do($query)) {
397 qq|CREATE TABLE schema_info (| .
400 qq| itime timestamp DEFAULT now(), | .
401 qq| PRIMARY KEY (tag))|;
402 $dbh->do($query) || $form->dberror($query);
405 $main::lxdebug->leave_sub();
409 my ($self, %params) = @_;
411 my $form = $params{form};
412 my $dbupdater = $params{updater};
413 my $db = $params{database};
414 my $silent = $params{silent};
416 map { $_->{description} = SL::Iconv::convert($_->{charset}, 'UTF-8', $_->{description}) } values %{ $dbupdater->{all_controls} };
418 &dbconnect_vars($form, $db);
420 # Flush potentially held database locks.
421 # $form->get_standard_dbh->commit;
423 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) or $form->dberror;
425 $dbh->do($form->{dboptions}) if ($form->{dboptions});
427 $self->create_schema_info_table($form, $dbh);
429 my @upgradescripts = $dbupdater->unapplied_upgrade_scripts($dbh);
431 foreach my $control (@upgradescripts) {
432 # Apply upgrade. Control will only return to us if the upgrade has
433 # been applied correctly and if the update has not requested user
435 $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $control->{file}");
436 print $form->parse_html_template("dbupgrade/upgrade_message2", $control) unless $silent;
438 $dbupdater->process_file($dbh, "sql/Pg-upgrade2/$control->{file}", $control);
448 sub get_default_myconfig {
449 my ($self_or_class, %user_config) = @_;
450 my $defaults = SL::DefaultManager->new($::lx_office_conf{system}->{default_manager});
453 countrycode => $defaults->language('de'),
454 css_path => 'css', # Needed for menunew, see SL::Layout::Base::get_stylesheet_for_user
455 dateformat => $defaults->dateformat('dd.mm.yy'),
456 numberformat => $defaults->numberformat('1.000,00'),
457 stylesheet => $defaults->stylesheet('kivitendo.css'),
458 timeformat => $defaults->timeformat('hh:mm'),