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;
52 use constant LOGIN_OK => 0;
53 use constant LOGIN_BASIC_TABLES_MISSING => -1;
54 use constant LOGIN_DBUPDATE_AVAILABLE => -2;
55 use constant LOGIN_AUTH_DBUPDATE_AVAILABLE => -3;
56 use constant LOGIN_GENERAL_ERROR => -4;
59 $main::lxdebug->enter_sub();
61 my ($type, %params) = @_;
65 if ($params{id} || $params{login}) {
66 my %user_data = $main::auth->read_user(%params);
67 map { $self->{$_} = $user_data{$_} } keys %user_data;
70 $main::lxdebug->leave_sub();
76 $main::lxdebug->enter_sub();
83 # scan the locale directory and read in the LANGUAGE files
84 opendir(DIR, "locale");
86 my @dir = grep(!/(^\.\.?$|\..*)/, readdir(DIR));
88 foreach my $dir (@dir) {
89 next unless open(my $fh, '<:encoding(UTF-8)', "locale/$dir/LANGUAGE");
93 $cc{$dir} = "@language";
98 $main::lxdebug->leave_sub();
104 my ($self, $form) = @_;
106 return LOGIN_GENERAL_ERROR() if !$self->{login} || !$::auth->client;
108 my %myconfig = $main::auth->read_user(login => $self->{login});
110 # Auth DB upgrades available?
111 my $dbupdater_auth = SL::DBUpgrade2->new(form => $form, auth => 1)->parse_dbupdate_controls;
112 return LOGIN_AUTH_DBUPDATE_AVAILABLE() if $dbupdater_auth->unapplied_upgrade_scripts($::auth->dbconnect);
114 # check if database is down
115 my $dbh = $form->dbconnect_noauto;
117 # we got a connection, check the version
118 my ($dbversion) = $dbh->selectrow_array(qq|SELECT version FROM defaults|);
121 return LOGIN_BASIC_TABLES_MISSING();
124 $self->create_schema_info_table($form, $dbh);
126 my $dbupdater = SL::DBUpgrade2->new(form => $form)->parse_dbupdate_controls;
127 my @unapplied_scripts = $dbupdater->unapplied_upgrade_scripts($dbh);
130 if (!@unapplied_scripts) {
131 SL::DB::Manager::Employee->update_entries_for_authorized_users;
135 # Store the fact that we're applying database upgrades at the
136 # moment. That way functions called from the layout modules that may
137 # require updated tables can chose only to use basic features.
138 $::request->applying_database_upgrades(1);
140 $form->{$_} = $::auth->client->{$_} for qw(dbname dbhost dbport dbuser dbpasswd);
141 $form->{$_} = $myconfig{$_} for qw(datestyle);
143 $form->{"title"} = $main::locale->text("Dataset upgrade");
144 $form->header(no_layout => $form->{no_layout});
145 print $form->parse_html_template("dbupgrade/header");
147 $form->{dbupdate} = "db" . $::auth->client->{dbname};
149 if ($form->{"show_dbupdate_warning"}) {
150 print $form->parse_html_template("dbupgrade/warning", { unapplied_scripts => \@unapplied_scripts });
155 SL::System::InstallationLock->lock;
157 # ignore HUP, QUIT in case the webserver times out
158 $SIG{HUP} = 'IGNORE';
159 $SIG{QUIT} = 'IGNORE';
161 $self->dbupdate2(form => $form, updater => $dbupdater, database => $::auth->client->{dbname});
163 # If $self->dbupdate2 returns than this means all upgrade scripts
164 # have been applied successfully, none required user
165 # interaction. Otherwise the deeper layers would have called
166 # ::end_of_request() already, and return would not have returned to
167 # us. Therefore we can now use RDBO instances because their supposed
168 # table structures do match the actual structures. So let's ensure
169 # that the "employee" table contains the appropriate entries for all
170 # users authorized for the current client.
171 SL::DB::Manager::Employee->update_entries_for_authorized_users;
173 SL::System::InstallationLock->unlock;
175 print $form->parse_html_template("dbupgrade/footer");
177 return LOGIN_DBUPDATE_AVAILABLE();
181 $main::lxdebug->enter_sub();
183 my ($form, $db) = @_;
186 'yy-mm-dd' => 'set DateStyle to \'ISO\'',
187 'yyyy-mm-dd' => 'set DateStyle to \'ISO\'',
188 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
189 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
190 'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
193 $form->{dboptions} = $dboptions{ $form->{dateformat} };
194 $form->{dbconnect} = "dbi:Pg:dbname=${db};host=" . ($form->{dbhost} || 'localhost') . ";port=" . ($form->{dbport} || 5432);
196 $main::lxdebug->leave_sub();
200 $main::lxdebug->enter_sub();
202 my ($self, $form) = @_;
207 $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
208 &dbconnect_vars($form, $form->{dbdefault});
210 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
214 qq|SELECT datname FROM pg_database | .
215 qq|WHERE NOT datname IN ('template0', 'template1')|;
216 $sth = $dbh->prepare($query);
217 $sth->execute() || $form->dberror($query);
219 while (my ($db) = $sth->fetchrow_array) {
221 if ($form->{only_acc_db}) {
223 next if ($db =~ /^template/);
225 &dbconnect_vars($form, $db);
226 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
230 qq|SELECT tablename FROM pg_tables | .
231 qq|WHERE (tablename = 'defaults') AND (tableowner = ?)|;
232 my $sth = $dbh->prepare($query);
233 $sth->execute($form->{dbuser}) ||
234 $form->dberror($query . " ($form->{dbuser})");
236 if ($sth->fetchrow_array) {
237 push(@dbsources, $db);
243 push(@dbsources, $db);
249 $main::lxdebug->leave_sub();
255 $main::lxdebug->enter_sub();
257 my ($self, $form) = @_;
259 &dbconnect_vars($form, $form->{dbdefault});
261 SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
263 $form->{db} =~ s/\"//g;
267 push @dboptions, "ENCODING = " . $dbh->quote($form->{"encoding"}) if $form->{"encoding"};
268 if ($form->{"dbdefault"}) {
269 my $dbdefault = $form->{"dbdefault"};
270 $dbdefault =~ s/[^a-zA-Z0-9_\-]//g;
271 push @dboptions, "TEMPLATE = $dbdefault";
274 my $query = qq|CREATE DATABASE "$form->{db}"|;
275 $query .= " WITH " . join(" ", @dboptions) if @dboptions;
277 # Ignore errors if the database exists.
282 &dbconnect_vars($form, $form->{db});
284 # make a shim myconfig so that rose db connections work
285 $::myconfig{$_} = $form->{$_} for qw(dbhost dbport dbuser dbpasswd);
286 $::myconfig{dbname} = $form->{db};
288 $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
291 my $dbupdater = SL::DBUpgrade2->new(form => $form, return_on_error => 1, silent => 1)->parse_dbupdate_controls;
293 $dbupdater->process_query($dbh, "sql/lx-office.sql");
295 # load chart of accounts
296 $dbupdater->process_query($dbh, "sql/$form->{chart}-chart.sql");
298 $query = qq|UPDATE defaults SET coa = ?, accounting_method = ?, profit_determination = ?, inventory_system = ?, curr = ?|;
299 do_query($form, $dbh, $query, map { $form->{$_} } qw(chart accounting_method profit_determination inventory_system defaultcurrency));
303 $main::lxdebug->leave_sub();
307 $main::lxdebug->enter_sub();
309 my ($self, $form) = @_;
310 $form->{db} =~ s/\"//g;
312 &dbconnect_vars($form, $form->{dbdefault});
313 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
315 my $query = qq|DROP DATABASE "$form->{db}"|;
316 do_query($form, $dbh, $query);
320 $main::lxdebug->leave_sub();
324 $main::lxdebug->enter_sub(2);
326 my (@v, $version, $i);
328 @v = split(/\./, $_[0]);
329 while (scalar(@v) < 4) {
333 for ($i = 0; $i < 4; $i++) {
338 $main::lxdebug->leave_sub(2);
342 sub cmp_script_version {
343 my ($a_from, $a_to, $b_from, $b_to);
344 my ($i, $res_a, $res_b);
345 my ($my_a, $my_b) = do { no warnings 'once'; ($a, $b) };
347 $my_a =~ s/.*-upgrade-//;
349 $my_b =~ s/.*-upgrade-//;
351 my ($my_a_from, $my_a_to) = split(/-/, $my_a);
352 my ($my_b_from, $my_b_to) = split(/-/, $my_b);
354 $res_a = calc_version($my_a_from);
355 $res_b = calc_version($my_b_from);
357 if ($res_a == $res_b) {
358 $res_a = calc_version($my_a_to);
359 $res_b = calc_version($my_b_to);
362 return $res_a <=> $res_b;
365 sub create_schema_info_table {
366 $main::lxdebug->enter_sub();
368 my ($self, $form, $dbh) = @_;
370 my $query = "SELECT tag FROM schema_info LIMIT 1";
371 if (!$dbh->do($query)) {
374 qq|CREATE TABLE schema_info (| .
377 qq| itime timestamp DEFAULT now(), | .
378 qq| PRIMARY KEY (tag))|;
379 $dbh->do($query) || $form->dberror($query);
382 $main::lxdebug->leave_sub();
386 my ($self, %params) = @_;
388 my $form = $params{form};
389 my $dbupdater = $params{updater};
390 my $db = $params{database};
391 my $silent = $params{silent};
393 map { $_->{description} = SL::Iconv::convert($_->{charset}, 'UTF-8', $_->{description}) } values %{ $dbupdater->{all_controls} };
395 &dbconnect_vars($form, $db);
397 # Flush potentially held database locks.
398 # $form->get_standard_dbh->commit;
400 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) or $form->dberror;
402 $dbh->do($form->{dboptions}) if ($form->{dboptions});
404 $self->create_schema_info_table($form, $dbh);
406 my @upgradescripts = $dbupdater->unapplied_upgrade_scripts($dbh);
408 foreach my $control (@upgradescripts) {
409 # Apply upgrade. Control will only return to us if the upgrade has
410 # been applied correctly and if the update has not requested user
412 $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $control->{file}");
413 print $form->parse_html_template("dbupgrade/upgrade_message2", $control) unless $silent;
415 $dbupdater->process_file($dbh, "sql/Pg-upgrade2/$control->{file}", $control);
425 sub get_default_myconfig {
426 my ($self_or_class, %user_config) = @_;
430 css_path => 'css', # Needed for menunew, see SL::Layout::Base::get_stylesheet_for_user
431 dateformat => 'dd.mm.yy',
432 numberformat => '1.000,00',
433 stylesheet => 'kivitendo.css',
434 timeformat => 'hh:mm',