X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FUser.pm;h=68fd2c520ed8971dd88e437d47670e89d22683b1;hb=9447acbeb7b1cd0bb4691aa1da8f533ca9099064;hp=bf649c8cd432664ad3b827d2b727cfb8fe260ae6;hpb=58fcb69f65a255c0db6741ca7aed0a695cae68c8;p=kivitendo-erp.git diff --git a/SL/User.pm b/SL/User.pm index bf649c8cd..68fd2c520 100644 --- a/SL/User.pm +++ b/SL/User.pm @@ -25,7 +25,8 @@ # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1335, USA. #===================================================================== # # user related functions @@ -34,65 +35,63 @@ package User; +use IO::File; +use List::MoreUtils qw(any); + +use SL::DB; +#use SL::Auth; +use SL::DB::AuthClient; +use SL::DB::Employee; +use SL::DBConnect; +use SL::DBUpgrade2; +use SL::DBUtils; +use SL::Iconv; +use SL::Inifile; +use SL::System::InstallationLock; +use SL::DefaultManager; + +use strict; + +use constant LOGIN_OK => 0; +use constant LOGIN_BASIC_TABLES_MISSING => -1; +use constant LOGIN_DBUPDATE_AVAILABLE => -2; +use constant LOGIN_AUTH_DBUPDATE_AVAILABLE => -3; +use constant LOGIN_GENERAL_ERROR => -4; + sub new { $main::lxdebug->enter_sub(); - my ($type, $memfile, $login) = @_; - my $self = {}; - - if ($login ne "") { - &error("", "$memfile locked!") if (-f "${memfile}.LCK"); - - open(MEMBER, "$memfile") or &error("", "$memfile : $!"); - - while () { - if (/^\[$login\]/) { - while () { - last if /^\[/; - next if /^(#|\s)/; - - # remove comments - s/\s#.*//g; - - # remove any trailing whitespace - s/^\s*(.*?)\s*$/$1/; - - ($key, $value) = split /=/, $_, 2; + my ($type, %params) = @_; - if (($key eq "stylesheet") && ($value eq "sql-ledger.css")) { - $value = "lx-office-erp.css"; - } - - $self->{$key} = $value; - } - - $self->{login} = $login; + my $self = {}; - last; - } - } - close MEMBER; + if ($params{id} || $params{login}) { + my %user_data = $main::auth->read_user(%params); + map { $self->{$_} = $user_data{$_} } keys %user_data; } $main::lxdebug->leave_sub(); + bless $self, $type; } sub country_codes { $main::lxdebug->enter_sub(); + local *DIR; + my %cc = (); my @language = (); # scan the locale directory and read in the LANGUAGE files - opendir DIR, "locale"; + opendir(DIR, "locale"); - my @dir = grep !/(^\.\.?$|\..*)/, readdir DIR; + my @dir = grep(!/(^\.\.?$|\..*)/, readdir(DIR)); foreach my $dir (@dir) { - next unless open(FH, "locale/$dir/LANGUAGE"); - @language = ; - close FH; + next unless open(my $fh, '<:encoding(UTF-8)', "locale/$dir/LANGUAGE"); + @language = <$fh>; + close $fh; $cc{$dir} = "@language"; } @@ -104,154 +103,150 @@ sub country_codes { return %cc; } -sub login { - $main::lxdebug->enter_sub(); +sub _handle_superuser_privileges { + my ($self, $form) = @_; - my ($self, $form, $userspath) = @_; + if ($form->{database_superuser_username}) { + $::auth->set_session_value("database_superuser_username" => $form->{database_superuser_username}, "database_superuser_password" => $form->{database_superuser_password}); + } - my $rc = -3; + my %dbconnect_form = %{ $form }; + my ($su_user, $su_password) = map { $::auth->get_session_value("database_superuser_$_") } qw(username password); - if ($self->{login}) { + if ($su_user) { + $dbconnect_form{dbuser} = $su_user; + $dbconnect_form{dbpasswd} = $su_password; + } - if ($self->{password}) { - $form->{password} = crypt $form->{password}, - substr($self->{login}, 0, 2); - if ($self->{password} ne $form->{password}) { - $main::lxdebug->leave_sub(); - return -1; - } - } + dbconnect_vars(\%dbconnect_form, $form->{dbname}); - unless (-e "$userspath/$self->{login}.conf") { - $self->create_config("$userspath/$self->{login}.conf"); - } + my %result = ( + username => $dbconnect_form{dbuser}, + password => $dbconnect_form{dbpasswd}, + ); - do "$userspath/$self->{login}.conf"; - $myconfig{dbpasswd} = unpack 'u', $myconfig{dbpasswd}; - - # check if database is down - my $dbh = - DBI->connect($myconfig{dbconnect}, $myconfig{dbuser}, - $myconfig{dbpasswd}) - or $self->error(DBI::errstr); - - # we got a connection, check the version - my $query = qq|SELECT version FROM defaults|; - my $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); - - my ($dbversion) = $sth->fetchrow_array; - $sth->finish; - - # add login to employee table if it does not exist - # no error check for employee table, ignore if it does not exist - $query = qq|SELECT e.id FROM employee e WHERE e.login = '$self->{login}'|; - $sth = $dbh->prepare($query); - $sth->execute; - - my ($login) = $sth->fetchrow_array; - $sth->finish; - - if (!$login) { - $query = qq|INSERT INTO employee (login, name, workphone, role) - VALUES ('$self->{login}', '$myconfig{name}', - '$myconfig{tel}', 'user')|; - $dbh->do($query); - } - $dbh->disconnect; + $::auth->set_session_value("database_superuser_username" => $dbconnect_form{dbuser}, "database_superuser_password" => $dbconnect_form{dbpasswd}); - $rc = 0; + my $dbh = SL::DBConnect->connect($dbconnect_form{dbconnect}, $dbconnect_form{dbuser}, $dbconnect_form{dbpasswd}, SL::DBConnect->get_options); + return (%result, error => $::locale->text('The credentials (username & password) for connecting database are wrong.')) if !$dbh; - if ($form->{dbversion} ne $dbversion) { + my $is_superuser = SL::DBUtils::role_is_superuser($dbh, $dbconnect_form{dbuser}); - # update the tables - open FH, ">$userspath/nologin" or die " -$!"; + $dbh->disconnect; - map { $form->{$_} = $myconfig{$_} } - qw(dbname dbhost dbport dbdriver dbuser dbpasswd); + return (%result, have_privileges => 1) if $is_superuser; + return (%result) if !$su_user; # no error message if credentials weren't set by the user + return (%result, error => $::locale->text('The database user \'#1\' does not have superuser privileges.', $dbconnect_form{dbuser})); +} - $form->{dbupdate} = "db$myconfig{dbname}"; - $form->{ $form->{dbupdate} } = 1; +sub login { + my ($self, $form) = @_; - $form->info("Upgrading Dataset $myconfig{dbname} ..."); + return LOGIN_GENERAL_ERROR() if !$self->{login} || !$::auth->client; - # required for Oracle - $form->{dbdefault} = $sid; + my %myconfig = $main::auth->read_user(login => $self->{login}); - # ignore HUP, QUIT in case the webserver times out - $SIG{HUP} = 'IGNORE'; - $SIG{QUIT} = 'IGNORE'; + # Auth DB upgrades available? + my $dbupdater_auth = SL::DBUpgrade2->new(form => $form, auth => 1)->parse_dbupdate_controls; + return LOGIN_AUTH_DBUPDATE_AVAILABLE() if $dbupdater_auth->unapplied_upgrade_scripts($::auth->dbconnect); - $self->dbupdate($form); + # check if database is down + my $dbh = SL::DB->client->dbh; - # remove lock file - unlink "$userspath/nologin"; + # we got a connection, check the version + my ($dbversion) = $dbh->selectrow_array(qq|SELECT version FROM defaults|); + if (!$dbversion) { + $dbh->disconnect; + return LOGIN_BASIC_TABLES_MISSING(); + } - $form->info("... done"); + $self->create_schema_info_table($form, $dbh); - $rc = -2; + my $dbupdater = SL::DBUpgrade2->new(form => $form)->parse_dbupdate_controls; + my @unapplied_scripts = $dbupdater->unapplied_upgrade_scripts($dbh); +# $dbh->disconnect; - } + if (!@unapplied_scripts) { + SL::DB::Manager::Employee->update_entries_for_authorized_users; + return LOGIN_OK(); } - $main::lxdebug->leave_sub(); + # Store the fact that we're applying database upgrades at the + # moment. That way functions called from the layout modules that may + # require updated tables can chose only to use basic features. + $::request->applying_database_upgrades(1); - return $rc; -} + $form->{$_} = $::auth->client->{$_} for qw(dbname dbhost dbport dbuser dbpasswd); + $form->{$_} = $myconfig{$_} for qw(datestyle); -sub dbconnect_vars { - $main::lxdebug->enter_sub(); + $form->{"title"} = $main::locale->text("Dataset upgrade"); + $form->header(no_layout => $form->{no_layout}); + print $form->parse_html_template("dbupgrade/header"); - my ($form, $db) = @_; + $form->{dbupdate} = "db" . $::auth->client->{dbname}; - my %dboptions = ( - 'Pg' => { 'yy-mm-dd' => 'set DateStyle to \'ISO\'', - 'yyyy-mm-dd' => 'set DateStyle to \'ISO\'', - 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'', - 'mm-dd-yy' => 'set DateStyle to \'POSTGRES, US\'', - 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'', - 'dd-mm-yy' => 'set DateStyle to \'POSTGRES, EUROPEAN\'', - 'dd.mm.yy' => 'set DateStyle to \'GERMAN\'' - }, - 'Oracle' => { - 'yy-mm-dd' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'YY-MM-DD\'', - 'yyyy-mm-dd' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'YYYY-MM-DD\'', - 'mm/dd/yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'MM/DD/YY\'', - 'mm-dd-yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'MM-DD-YY\'', - 'dd/mm/yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD/MM/YY\'', - 'dd-mm-yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD-MM-YY\'', - 'dd.mm.yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD.MM.YY\'', - }); - - $form->{dboptions} = $dboptions{ $form->{dbdriver} }{ $form->{dateformat} }; - - if ($form->{dbdriver} eq 'Pg') { - $form->{dbconnect} = "dbi:Pg:dbname=$db"; - } + my $show_update_warning = $form->{"show_dbupdate_warning"}; + my %superuser = (need_privileges => (any { $_->{superuser_privileges} } @unapplied_scripts)); - if ($form->{dbdriver} eq 'Oracle') { - $form->{dbconnect} = "dbi:Oracle:sid=$form->{sid}"; + if ($superuser{need_privileges}) { + %superuser = ( + %superuser, + $self->_handle_superuser_privileges($form), + ); + $show_update_warning = 1 if !$superuser{have_privileges}; } - if ($form->{dbhost}) { - $form->{dbconnect} .= ";host=$form->{dbhost}"; - } - if ($form->{dbport}) { - $form->{dbconnect} .= ";port=$form->{dbport}"; + if ($show_update_warning) { + print $form->parse_html_template("dbupgrade/warning", { + unapplied_scripts => \@unapplied_scripts, + superuser => \%superuser, + }); + $::dispatcher->end_request; } - $main::lxdebug->leave_sub(); + # update the tables + SL::System::InstallationLock->lock; + + # ignore HUP, QUIT in case the webserver times out + $SIG{HUP} = 'IGNORE'; + $SIG{QUIT} = 'IGNORE'; + + $self->dbupdate2(form => $form, updater => $dbupdater, database => $::auth->client->{dbname}); + + # If $self->dbupdate2 returns than this means all upgrade scripts + # have been applied successfully, none required user + # interaction. Otherwise the deeper layers would have called + # $::dispatcher->end_request already, and return would not have returned to + # us. Therefore we can now use RDBO instances because their supposed + # table structures do match the actual structures. So let's ensure + # that the "employee" table contains the appropriate entries for all + # users authorized for the current client. + SL::DB::Manager::Employee->update_entries_for_authorized_users; + + SL::System::InstallationLock->unlock; + + print $form->parse_html_template("dbupgrade/footer"); + + return LOGIN_DBUPDATE_AVAILABLE(); } -sub dbdrivers { +sub dbconnect_vars { $main::lxdebug->enter_sub(); - my @drivers = DBI->available_drivers(); + my ($form, $db) = @_; - $main::lxdebug->leave_sub(); + my %dboptions = ( + 'yy-mm-dd' => 'set DateStyle to \'ISO\'', + 'yyyy-mm-dd' => 'set DateStyle to \'ISO\'', + 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'', + 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'', + 'dd.mm.yy' => 'set DateStyle to \'GERMAN\'' + ); + + $form->{dboptions} = $dboptions{ $form->{dateformat} }; + $form->{dbconnect} = "dbi:Pg:dbname=${db};host=" . ($form->{dbhost} || 'localhost') . ";port=" . ($form->{dbport} || 5432); - return (grep { /(Pg|Oracle)/ } @drivers); + $main::lxdebug->leave_sub(); } sub dbsources { @@ -263,62 +258,42 @@ sub dbsources { my ($sth, $query); $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault}; - $form->{sid} = $form->{dbdefault}; &dbconnect_vars($form, $form->{dbdefault}); - my $dbh = - DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) + my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) or $form->dberror; - if ($form->{dbdriver} eq 'Pg') { + $query = + qq|SELECT datname FROM pg_database | . + qq|WHERE NOT datname IN ('template0', 'template1')|; + $sth = $dbh->prepare($query); + $sth->execute() || $form->dberror($query); - $query = qq|SELECT datname FROM pg_database|; - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); + while (my ($db) = $sth->fetchrow_array) { - while (my ($db) = $sth->fetchrow_array) { - - if ($form->{only_acc_db}) { + if ($form->{only_acc_db}) { - next if ($db =~ /^template/); + next if ($db =~ /^template/); - &dbconnect_vars($form, $db); - my $dbh = - DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) - or $form->dberror; + &dbconnect_vars($form, $db); + my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) + or $form->dberror; - $query = qq|SELECT p.tablename FROM pg_tables p - WHERE p.tablename = 'defaults' - AND p.tableowner = '$form->{dbuser}'|; - my $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); + $query = + qq|SELECT tablename FROM pg_tables | . + qq|WHERE (tablename = 'defaults') AND (tableowner = ?)|; + my $sth = $dbh->prepare($query); + $sth->execute($form->{dbuser}) || + $form->dberror($query . " ($form->{dbuser})"); - if ($sth->fetchrow_array) { - push @dbsources, $db; - } - $sth->finish; - $dbh->disconnect; - next; + if ($sth->fetchrow_array) { + push(@dbsources, $db); } - push @dbsources, $db; - } - } - - if ($form->{dbdriver} eq 'Oracle') { - if ($form->{only_acc_db}) { - $query = qq|SELECT o.owner FROM dba_objects o - WHERE o.object_name = 'DEFAULTS' - AND o.object_type = 'TABLE'|; - } else { - $query = qq|SELECT username FROM dba_users|; - } - - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); - - while (my ($db) = $sth->fetchrow_array) { - push @dbsources, $db; + $sth->finish; + $dbh->disconnect; + next; } + push(@dbsources, $db); } $sth->finish; @@ -334,271 +309,110 @@ sub dbcreate { my ($self, $form) = @_; - my %dbcreate = ( - 'Pg' => qq|CREATE DATABASE "$form->{db}"|, - 'Oracle' => - qq|CREATE USER "$form->{db}" DEFAULT TABLESPACE USERS TEMPORARY TABLESPACE TEMP IDENTIFIED BY "$form->{db}"| - ); - - $dbcreate{Pg} .= " WITH ENCODING = '$form->{encoding}'" if $form->{encoding}; - - $form->{sid} = $form->{dbdefault}; &dbconnect_vars($form, $form->{dbdefault}); my $dbh = - DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) + SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) or $form->dberror; - my $query = qq|$dbcreate{$form->{dbdriver}}|; - $dbh->do($query) || $form->dberror($query); + $form->{db} =~ s/\"//g; - if ($form->{dbdriver} eq 'Oracle') { - $query = qq|GRANT CONNECT,RESOURCE TO "$form->{db}"|; - $dbh->do($query) || $form->dberror($query); - } - $dbh->disconnect; + my @dboptions; - # setup variables for the new database - if ($form->{dbdriver} eq 'Oracle') { - $form->{dbuser} = $form->{db}; - $form->{dbpasswd} = $form->{db}; + push @dboptions, "ENCODING = " . $dbh->quote($form->{"encoding"}) if $form->{"encoding"}; + if ($form->{"dbdefault"}) { + my $dbdefault = $form->{"dbdefault"}; + $dbdefault =~ s/[^a-zA-Z0-9_\-]//g; + push @dboptions, "TEMPLATE = $dbdefault"; } - &dbconnect_vars($form, $form->{db}); + my $query = qq|CREATE DATABASE "$form->{db}"|; + $query .= " WITH " . join(" ", @dboptions) if @dboptions; - $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) - or $form->dberror; - - # create the tables - my $filename = qq|sql/lx-office.sql|; - $self->process_query($form, $dbh, $filename); - - # load gifi - ($filename) = split /_/, $form->{chart}; - $filename =~ s/_//; - $self->process_query($form, $dbh, "sql/${filename}-gifi.sql"); - - # load chart of accounts - $filename = qq|sql/$form->{chart}-chart.sql|; - $self->process_query($form, $dbh, $filename); - - # create indices - # Indices sind auch in lx-office.sql - # $filename = qq|sql/$form->{dbdriver}-indices.sql|; - # $self->process_query($form, $dbh, $filename); + # Ignore errors if the database exists. + $dbh->do($query); $dbh->disconnect; - $main::lxdebug->leave_sub(); -} - -sub process_query { - $main::lxdebug->enter_sub(); - - my ($self, $form, $dbh, $filename) = @_; - - # return unless (-f $filename); - - open(FH, "$filename") or $form->error("$filename : $!\n"); - my $query = ""; - my $sth; - my @quote_chars; - - while () { - - # Remove DOS and Unix style line endings. - s/[\r\n]//g; - - # don't add comments or empty lines - next if /^(--.*|\s+)$/; - - for (my $i = 0; $i < length($_); $i++) { - my $char = substr($_, $i, 1); - - # Are we inside a string? - if (@quote_chars) { - if ($char eq $quote_chars[-1]) { - pop(@quote_chars); - } - $query .= $char; - - } else { - if (($char eq "'") || ($char eq "\"")) { - push(@quote_chars, $char); - - } elsif ($char eq ";") { - - # Query is complete. Send it. - - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); - $sth->finish; - - $char = ""; - $query = ""; - } - - $query .= $char; - } - } - } - - close FH; - - $main::lxdebug->leave_sub(); -} - -sub dbdelete { - $main::lxdebug->enter_sub(); - - my ($self, $form) = @_; + &dbconnect_vars($form, $form->{db}); - my %dbdelete = ('Pg' => qq|DROP DATABASE "$form->{db}"|, - 'Oracle' => qq|DROP USER $form->{db} CASCADE|); + # make a shim myconfig so that rose db connections work + $::myconfig{$_} = $form->{$_} for qw(dbhost dbport dbuser dbpasswd); + $::myconfig{dbname} = $form->{db}; - $form->{sid} = $form->{dbdefault}; - &dbconnect_vars($form, $form->{dbdefault}); - my $dbh = - DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) + $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) or $form->dberror; - my $query = qq|$dbdelete{$form->{dbdriver}}|; - $dbh->do($query) || $form->dberror($query); - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub dbsources_unused { - $main::lxdebug->enter_sub(); + my $dbupdater = SL::DBUpgrade2->new(form => $form, return_on_error => 1, silent => 1)->parse_dbupdate_controls; + # create the tables + $dbupdater->process_query($dbh, "sql/lx-office.sql"); + $dbupdater->process_query($dbh, "sql/$form->{chart}-chart.sql"); - my ($self, $form, $memfile) = @_; + $query = qq|UPDATE defaults SET coa = ?|; + do_query($form, $dbh, $query, map { $form->{$_} } qw(chart)); - my @dbexcl = (); - my @dbsources = (); + $dbh->disconnect; - $form->error('File locked!') if (-f "${memfile}.LCK"); + # update new database + $self->dbupdate2(form => $form, updater => $dbupdater, database => $form->{db}, silent => 1); - # open members file - open(FH, "$memfile") or $form->error("$memfile : $!"); + $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) + or $form->dberror; - while () { - if (/^dbname=/) { - my ($null, $item) = split /=/; - push @dbexcl, $item; - } + $query = "SELECT * FROM currencies WHERE name = ?"; + my $curr = selectfirst_hashref_query($form, $dbh, $query, $form->{defaultcurrency}); + if (!$curr->{id}) { + do_query($form, $dbh, "INSERT INTO currencies (name) VALUES (?)", $form->{defaultcurrency}); + $curr = selectfirst_hashref_query($form, $dbh, $query, $form->{defaultcurrency}); } - close FH; - - $form->{only_acc_db} = 1; - my @db = &dbsources("", $form); - - push @dbexcl, $form->{dbdefault}; + $query = qq|UPDATE defaults SET + accounting_method = ?, + profit_determination = ?, + inventory_system = ?, + precision = ?, + currency_id = ?, + feature_balance = ?, + feature_datev = ?, + feature_erfolgsrechnung = ?, + feature_eurechnung = ?, + feature_ustva = ? + |; + do_query($form, $dbh, $query, + $form->{accounting_method}, + $form->{profit_determination}, + $form->{inventory_system}, + $form->parse_amount(\%::myconfig, $form->{precision_as_number}), + $curr->{id}, + $form->{feature_balance}, + $form->{feature_datev}, + $form->{feature_erfolgsrechnung}, + $form->{feature_eurechnung}, + $form->{feature_ustva} + ); - foreach $item (@db) { - unless (grep /$item$/, @dbexcl) { - push @dbsources, $item; - } - } + $dbh->disconnect; $main::lxdebug->leave_sub(); - - return @dbsources; } -sub dbneedsupdate { +sub dbdelete { $main::lxdebug->enter_sub(); my ($self, $form) = @_; + $form->{db} =~ s/\"//g; - my %dbsources = (); - my $query; - - $form->{sid} = $form->{dbdefault}; &dbconnect_vars($form, $form->{dbdefault}); - - my $dbh = - DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) + my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) or $form->dberror; - - if ($form->{dbdriver} eq 'Pg') { - - $query = qq|SELECT d.datname FROM pg_database d, pg_user u - WHERE d.datdba = u.usesysid - AND u.usename = '$form->{dbuser}'|; - my $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); - - while (my ($db) = $sth->fetchrow_array) { - - next if ($db =~ /^template/); - - &dbconnect_vars($form, $db); - - my $dbh = - DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) - or $form->dberror; - - $query = qq|SELECT t.tablename FROM pg_tables t - WHERE t.tablename = 'defaults'|; - my $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); - - if ($sth->fetchrow_array) { - $query = qq|SELECT version FROM defaults|; - my $sth = $dbh->prepare($query); - $sth->execute; - - if (my ($version) = $sth->fetchrow_array) { - $dbsources{$db} = $version; - } - $sth->finish; - } - $sth->finish; - $dbh->disconnect; - } - $sth->finish; - } - - if ($form->{dbdriver} eq 'Oracle') { - $query = qq|SELECT o.owner FROM dba_objects o - WHERE o.object_name = 'DEFAULTS' - AND o.object_type = 'TABLE'|; - - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); - - while (my ($db) = $sth->fetchrow_array) { - - $form->{dbuser} = $db; - &dbconnect_vars($form, $db); - - my $dbh = - DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) - or $form->dberror; - - $query = qq|SELECT version FROM defaults|; - my $sth = $dbh->prepare($query); - $sth->execute; - - if (my ($version) = $sth->fetchrow_array) { - $dbsources{$db} = $version; - } - $sth->finish; - $dbh->disconnect; - } - $sth->finish; - } + my $query = qq|DROP DATABASE "$form->{db}"|; + do_query($form, $dbh, $query); $dbh->disconnect; $main::lxdebug->leave_sub(); - - return %dbsources; } -## LINET sub calc_version { - $main::lxdebug->enter_sub(); + $main::lxdebug->enter_sub(2); my (@v, $version, $i); @@ -612,21 +426,21 @@ sub calc_version { $version += $v[$i]; } - $main::lxdebug->leave_sub(); + $main::lxdebug->leave_sub(2); return $version; } sub cmp_script_version { my ($a_from, $a_to, $b_from, $b_to); my ($i, $res_a, $res_b); - my ($my_a, $my_b) = ($a, $b); + my ($my_a, $my_b) = do { no warnings 'once'; ($a, $b) }; $my_a =~ s/.*-upgrade-//; $my_a =~ s/.sql$//; $my_b =~ s/.*-upgrade-//; $my_b =~ s/.sql$//; - ($my_a_from, $my_a_to) = split(/-/, $my_a); - ($my_b_from, $my_b_to) = split(/-/, $my_b); + my ($my_a_from, $my_a_to) = split(/-/, $my_a); + my ($my_b_from, $my_b_to) = split(/-/, $my_b); $res_a = calc_version($my_a_from); $res_b = calc_version($my_b_from); @@ -638,237 +452,99 @@ sub cmp_script_version { return $res_a <=> $res_b; } -## /LINET -sub dbupdate { +sub create_schema_info_table { $main::lxdebug->enter_sub(); - my ($self, $form) = @_; - - $form->{sid} = $form->{dbdefault}; - - my @upgradescripts = (); - my $query; - my $rc = -2; - - if ($form->{dbupdate}) { - - # read update scripts into memory - opendir SQLDIR, "sql/." or $form - error($!); - ## LINET - @upgradescripts = - sort(cmp_script_version - grep(/$form->{dbdriver}-upgrade-.*?\.sql$/, readdir(SQLDIR))); - ## /LINET - closedir SQLDIR; - } - - foreach my $db (split / /, $form->{dbupdate}) { - - next unless $form->{$db}; - - # strip db from dataset - $db =~ s/^db//; - &dbconnect_vars($form, $db); - - my $dbh = - DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) - or $form->dberror; - - # check version - $query = qq|SELECT version FROM defaults|; - my $sth = $dbh->prepare($query); - - # no error check, let it fall through - $sth->execute; - - my $version = $sth->fetchrow_array; - $sth->finish; - - next unless $version; - - ## LINET - $version = calc_version($version); - ## /LINET - - foreach my $upgradescript (@upgradescripts) { - my $a = $upgradescript; - $a =~ s/^$form->{dbdriver}-upgrade-|\.sql$//g; - - my ($mindb, $maxdb) = split /-/, $a; - ## LINET - $mindb = calc_version($mindb); - $maxdb = calc_version($maxdb); - ## /LINET - - next if ($version >= $maxdb); - - # if there is no upgrade script exit - last if ($version < $mindb); - - # apply upgrade - $self->process_query($form, $dbh, "sql/$upgradescript"); - - $version = $maxdb; - - } - - $rc = 0; - $dbh->disconnect; - + my ($self, $form, $dbh) = @_; + + my $query = "SELECT tag FROM schema_info LIMIT 1"; + if (!$dbh->do($query)) { + $dbh->rollback(); + $query = + qq|CREATE TABLE schema_info (| . + qq| tag text, | . + qq| login text, | . + qq| itime timestamp DEFAULT now(), | . + qq| PRIMARY KEY (tag))|; + $dbh->do($query) || $form->dberror($query); } $main::lxdebug->leave_sub(); - - return $rc; } -sub create_config { - $main::lxdebug->enter_sub(); - - my ($self, $filename) = @_; - - @config = &config_vars; +sub dbupdate2 { + my ($self, %params) = @_; - open(CONF, ">$filename") or $self->error("$filename : $!"); - - # create the config file - print CONF qq|# configuration file for $self->{login} - -\%myconfig = ( -|; - - foreach $key (sort @config) { - $self->{$key} =~ s/\'/\\\'/g; - print CONF qq| $key => '$self->{$key}',\n|; - } - - print CONF qq|);\n\n|; - - close CONF; - - $main::lxdebug->leave_sub(); -} - -sub save_member { - $main::lxdebug->enter_sub(); + my $form = $params{form}; + my $dbupdater = $params{updater}; + my $db = $params{database}; + my $silent = $params{silent}; - my ($self, $memberfile, $userspath) = @_; + map { $_->{description} = SL::Iconv::convert($_->{charset}, 'UTF-8', $_->{description}) } values %{ $dbupdater->{all_controls} }; - my $newmember = 1; + &dbconnect_vars($form, $db); - # format dbconnect and dboptions string - &dbconnect_vars($self, $self->{dbname}); + my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) or $form->dberror; - $self->error('File locked!') if (-f "${memberfile}.LCK"); - open(FH, ">${memberfile}.LCK") or $self->error("${memberfile}.LCK : $!"); - close(FH); + $dbh->do($form->{dboptions}) if ($form->{dboptions}); - open(CONF, "+<$memberfile") or $self->error("$memberfile : $!"); + $self->create_schema_info_table($form, $dbh); - @config = ; + my @upgradescripts = $dbupdater->unapplied_upgrade_scripts($dbh); + my $need_superuser = (any { $_->{superuser_privileges} } @upgradescripts); + my $superuser_dbh; - seek(CONF, 0, 0); - truncate(CONF, 0); + if ($need_superuser) { + my %dbconnect_form = ( + %{ $form }, + dbuser => $::auth->get_session_value("database_superuser_username"), + dbpasswd => $::auth->get_session_value("database_superuser_password"), + ); - while ($line = shift @config) { - if ($line =~ /^\[$self->{login}\]/) { - $newmember = 0; - last; + if ($dbconnect_form{dbuser} ne $form->{dbuser}) { + dbconnect_vars(\%dbconnect_form, $db); + $superuser_dbh = SL::DBConnect->connect($dbconnect_form{dbconnect}, $dbconnect_form{dbuser}, $dbconnect_form{dbpasswd}, SL::DBConnect->get_options) or $form->dberror; } - print CONF $line; } - # remove everything up to next login or EOF - while ($line = shift @config) { - last if ($line =~ /^\[/); - } - - # this one is either the next login or EOF - print CONF $line; - - while ($line = shift @config) { - print CONF $line; - } + $::lxdebug->log_time("DB upgrades commencing"); - print CONF qq|[$self->{login}]\n|; - - if ((($self->{dbpasswd} ne $self->{old_dbpasswd}) || $newmember) - && $self->{root}) { - $self->{dbpasswd} = pack 'u', $self->{dbpasswd}; - chop $self->{dbpasswd}; - } - if (defined($self->{new_password})) { - if ($self->{new_password} ne $self->{old_password}) { - $self->{password} = crypt $self->{new_password}, - substr($self->{login}, 0, 2) - if $self->{new_password}; - } - } else { - if ($self->{password} ne $self->{old_password}) { - $self->{password} = crypt $self->{password}, substr($self->{login}, 0, 2) - if $self->{password}; - } - } + foreach my $control (@upgradescripts) { + # Apply upgrade. Control will only return to us if the upgrade has + # been applied correctly and if the update has not requested user + # interaction. + my $script_dbh = $control->{superuser_privileges} ? ($superuser_dbh // $dbh) : $dbh; - if ($self->{'root login'}) { - @config = ("password"); - } else { - @config = &config_vars; - } + $::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $control->{file}" . ($control->{superuser_privileges} ? " with superuser privileges" : "")); + print $form->parse_html_template("dbupgrade/upgrade_message2", $control) unless $silent; - # replace \r\n with \n - map { $self->{$_} =~ s/\r\n/\\n/g } qw(address signature); - foreach $key (sort @config) { - print CONF qq|$key=$self->{$key}\n|; + $dbupdater->process_file($script_dbh, "sql/Pg-upgrade2/$control->{file}", $control); } - print CONF "\n"; - close CONF; - unlink "${memberfile}.LCK"; + $::lxdebug->log_time("DB upgrades finished"); - # create conf file - $self->create_config("$userspath/$self->{login}.conf") - unless $self->{'root login'}; - - $main::lxdebug->leave_sub(); + $dbh->disconnect; + $superuser_dbh->disconnect if $superuser_dbh; } -sub config_vars { - $main::lxdebug->enter_sub(); - - my @conf = qw(acs address admin businessnumber charset company countrycode - currency dateformat dbconnect dbdriver dbhost dbport dboptions - dbname dbuser dbpasswd email fax name numberformat in_numberformat password - printer role sid signature stylesheet tel templates vclimit angebote bestellungen rechnungen - anfragen lieferantenbestellungen einkaufsrechnungen steuernummer ustid duns menustyle); - - $main::lxdebug->leave_sub(); - - return @conf; +sub data { + +{ %{ $_[0] } } } -sub error { - $main::lxdebug->enter_sub(); - - my ($self, $msg) = @_; - - if ($ENV{HTTP_USER_AGENT}) { - print qq|Content-Type: text/html - - - - - -

Error!

-

$msg|; - - } - - die "Error: $msg\n"; - - $main::lxdebug->leave_sub(); +sub get_default_myconfig { + my ($self_or_class, %user_config) = @_; + my $defaults = SL::DefaultManager->new($::lx_office_conf{system}->{default_manager}); + + return ( + countrycode => $defaults->language('de'), + css_path => 'css', # Needed for menunew, see SL::Layout::Base::get_stylesheet_for_user + dateformat => $defaults->dateformat('dd.mm.yy'), + numberformat => $defaults->numberformat('1.000,00'), + stylesheet => $defaults->stylesheet('kivitendo.css'), + timeformat => $defaults->timeformat('hh:mm'), + %user_config, + ); } 1; -