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 $form->{$_} = $::auth->client->{$_} for qw(dbname dbhost dbport dbuser dbpasswd);
137 $form->{$_} = $myconfig{$_} for qw(datestyle);
139 $form->{"title"} = $main::locale->text("Dataset upgrade");
140 $form->header(no_layout => $form->{no_layout});
141 print $form->parse_html_template("dbupgrade/header");
143 $form->{dbupdate} = "db" . $::auth->client->{dbname};
145 if ($form->{"show_dbupdate_warning"}) {
146 print $form->parse_html_template("dbupgrade/warning", { unapplied_scripts => \@unapplied_scripts });
151 SL::System::InstallationLock->lock;
153 # ignore HUP, QUIT in case the webserver times out
154 $SIG{HUP} = 'IGNORE';
155 $SIG{QUIT} = 'IGNORE';
157 $self->dbupdate2(form => $form, updater => $dbupdater, database => $::auth->client->{dbname});
159 # If $self->dbupdate2 returns than this means all upgrade scripts
160 # have been applied successfully, none required user
161 # interaction. Otherwise the deeper layers would have called
162 # ::end_of_request() already, and return would not have returned to
163 # us. Therefore we can now use RDBO instances because their supposed
164 # table structures do match the actual structures. So let's ensure
165 # that the "employee" table contains the appropriate entries for all
166 # users authorized for the current client.
167 SL::DB::Manager::Employee->update_entries_for_authorized_users;
169 SL::System::InstallationLock->unlock;
171 print $form->parse_html_template("dbupgrade/footer");
173 return LOGIN_DBUPDATE_AVAILABLE();
177 $main::lxdebug->enter_sub();
179 my ($form, $db) = @_;
182 'yy-mm-dd' => 'set DateStyle to \'ISO\'',
183 'yyyy-mm-dd' => 'set DateStyle to \'ISO\'',
184 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
185 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
186 'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
189 $form->{dboptions} = $dboptions{ $form->{dateformat} };
190 $form->{dbconnect} = "dbi:Pg:dbname=${db};host=" . ($form->{dbhost} || 'localhost') . ";port=" . ($form->{dbport} || 5432);
192 $main::lxdebug->leave_sub();
196 $main::lxdebug->enter_sub();
198 my ($self, $form) = @_;
203 $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
204 &dbconnect_vars($form, $form->{dbdefault});
206 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
210 qq|SELECT datname FROM pg_database | .
211 qq|WHERE NOT datname IN ('template0', 'template1')|;
212 $sth = $dbh->prepare($query);
213 $sth->execute() || $form->dberror($query);
215 while (my ($db) = $sth->fetchrow_array) {
217 if ($form->{only_acc_db}) {
219 next if ($db =~ /^template/);
221 &dbconnect_vars($form, $db);
222 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
226 qq|SELECT tablename FROM pg_tables | .
227 qq|WHERE (tablename = 'defaults') AND (tableowner = ?)|;
228 my $sth = $dbh->prepare($query);
229 $sth->execute($form->{dbuser}) ||
230 $form->dberror($query . " ($form->{dbuser})");
232 if ($sth->fetchrow_array) {
233 push(@dbsources, $db);
239 push(@dbsources, $db);
245 $main::lxdebug->leave_sub();
251 $main::lxdebug->enter_sub();
253 my ($self, $form) = @_;
255 &dbconnect_vars($form, $form->{dbdefault});
257 SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
259 $form->{db} =~ s/\"//g;
263 push @dboptions, "ENCODING = " . $dbh->quote($form->{"encoding"}) if $form->{"encoding"};
264 if ($form->{"dbdefault"}) {
265 my $dbdefault = $form->{"dbdefault"};
266 $dbdefault =~ s/[^a-zA-Z0-9_\-]//g;
267 push @dboptions, "TEMPLATE = $dbdefault";
270 my $query = qq|CREATE DATABASE "$form->{db}"|;
271 $query .= " WITH " . join(" ", @dboptions) if @dboptions;
273 # Ignore errors if the database exists.
278 &dbconnect_vars($form, $form->{db});
280 $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
283 my $dbupdater = SL::DBUpgrade2->new(form => $form);
285 $dbupdater->process_query($dbh, "sql/lx-office.sql");
287 # process update-scripts needed before 1st user-login
288 $self->create_schema_info_table($form, $dbh);
289 $dbupdater->process_query($dbh, "sql/Pg-upgrade2/defaults_add_precision.sql");
290 $dbh->do("INSERT INTO schema_info (tag, login) VALUES ('defaults_add_precision', 'admin')");
291 $dbupdater->process_query($dbh, "sql/Pg-upgrade2/defaults_add_country_mode.sql");
292 $dbh->do("INSERT INTO schema_info (tag, login) VALUES ('defaults_add_country_mode', 'admin')");
294 # load chart of accounts
295 $dbupdater->process_query($dbh, "sql/$form->{chart}-chart.sql");
297 $query = qq|UPDATE defaults SET coa = ?, accounting_method = ?, profit_determination = ?, inventory_system = ?, curr = ?, precision = ?, country_mode = ?|;
298 do_query($form, $dbh, $query, map { $form->{$_} } qw(chart accounting_method profit_determination inventory_system defaultcurrency precision countrymode));
302 $main::lxdebug->leave_sub();
306 $main::lxdebug->enter_sub();
308 my ($self, $form) = @_;
309 $form->{db} =~ s/\"//g;
311 &dbconnect_vars($form, $form->{dbdefault});
312 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
314 my $query = qq|DROP DATABASE "$form->{db}"|;
315 do_query($form, $dbh, $query);
319 $main::lxdebug->leave_sub();
323 $main::lxdebug->enter_sub(2);
325 my (@v, $version, $i);
327 @v = split(/\./, $_[0]);
328 while (scalar(@v) < 4) {
332 for ($i = 0; $i < 4; $i++) {
337 $main::lxdebug->leave_sub(2);
341 sub cmp_script_version {
342 my ($a_from, $a_to, $b_from, $b_to);
343 my ($i, $res_a, $res_b);
344 my ($my_a, $my_b) = do { no warnings 'once'; ($a, $b) };
346 $my_a =~ s/.*-upgrade-//;
348 $my_b =~ s/.*-upgrade-//;
350 my ($my_a_from, $my_a_to) = split(/-/, $my_a);
351 my ($my_b_from, $my_b_to) = split(/-/, $my_b);
353 $res_a = calc_version($my_a_from);
354 $res_b = calc_version($my_b_from);
356 if ($res_a == $res_b) {
357 $res_a = calc_version($my_a_to);
358 $res_b = calc_version($my_b_to);
361 return $res_a <=> $res_b;
364 sub create_schema_info_table {
365 $main::lxdebug->enter_sub();
367 my ($self, $form, $dbh) = @_;
369 my $query = "SELECT tag FROM schema_info LIMIT 1";
370 if (!$dbh->do($query)) {
373 qq|CREATE TABLE schema_info (| .
376 qq| itime timestamp DEFAULT now(), | .
377 qq| PRIMARY KEY (tag))|;
378 $dbh->do($query) || $form->dberror($query);
381 $main::lxdebug->leave_sub();
385 my ($self, %params) = @_;
387 my $form = $params{form};
388 my $dbupdater = $params{updater};
389 my $db = $params{database};
391 map { $_->{description} = SL::Iconv::convert($_->{charset}, 'UTF-8', $_->{description}) } values %{ $dbupdater->{all_controls} };
393 &dbconnect_vars($form, $db);
395 # Flush potentially held database locks.
396 $form->get_standard_dbh->commit;
398 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) or $form->dberror;
400 $dbh->do($form->{dboptions}) if ($form->{dboptions});
402 $self->create_schema_info_table($form, $dbh);
404 my @upgradescripts = $dbupdater->unapplied_upgrade_scripts($dbh);
406 foreach my $control (@upgradescripts) {
407 # Apply upgrade. Control will only return to us if the upgrade has
408 # been applied correctly and if the update has not requested user
410 $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $control->{file}");
411 print $form->parse_html_template("dbupgrade/upgrade_message2", $control);
413 $dbupdater->process_file($dbh, "sql/Pg-upgrade2/$control->{file}", $control);
423 sub get_default_myconfig {
424 my ($self_or_class, %user_config) = @_;
425 my $defaults = SL::DefaultManager->new($::lx_office_conf{system}->{default_manager});
428 countrycode => $defaults->language('de'),
429 css_path => 'css', # Needed for menunew, see SL::Layout::Base::get_stylesheet_for_user
430 dateformat => $defaults->dateformat('dd.mm.yy'),
431 numberformat => $defaults->numberformat('1.000,00'),
432 stylesheet => 'kivitendo.css',
433 timeformat => $defaults->timeformat('hh:mm'),