epic-s6ts
[kivitendo-erp.git] / SL / User.pm
index 7f4ec1a..68fd2c5 100644 (file)
@@ -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
 package User;
 
 use IO::File;
-use Fcntl qw(:seek);
+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();
 
@@ -92,31 +103,78 @@ sub country_codes {
   return %cc;
 }
 
+sub _handle_superuser_privileges {
+  my ($self, $form) = @_;
+
+  if ($form->{database_superuser_username}) {
+    $::auth->set_session_value("database_superuser_username" => $form->{database_superuser_username}, "database_superuser_password" => $form->{database_superuser_password});
+  }
+
+  my %dbconnect_form          = %{ $form };
+  my ($su_user, $su_password) = map { $::auth->get_session_value("database_superuser_$_") } qw(username password);
+
+  if ($su_user) {
+    $dbconnect_form{dbuser}   = $su_user;
+    $dbconnect_form{dbpasswd} = $su_password;
+  }
+
+  dbconnect_vars(\%dbconnect_form, $form->{dbname});
+
+  my %result = (
+    username => $dbconnect_form{dbuser},
+    password => $dbconnect_form{dbpasswd},
+  );
+
+  $::auth->set_session_value("database_superuser_username" => $dbconnect_form{dbuser}, "database_superuser_password" => $dbconnect_form{dbpasswd});
+
+  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;
+
+  my $is_superuser = SL::DBUtils::role_is_superuser($dbh, $dbconnect_form{dbuser});
+
+  $dbh->disconnect;
+
+  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}));
+}
+
 sub login {
   my ($self, $form) = @_;
 
-  return -3 if !$self->{login} || !$::auth->client;
+  return LOGIN_GENERAL_ERROR() if !$self->{login} || !$::auth->client;
 
   my %myconfig = $main::auth->read_user(login => $self->{login});
 
+  # 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);
+
   # check if database is down
-  my $dbh = $form->dbconnect_noauto;
+  my $dbh = SL::DB->client->dbh;
 
   # 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();
+  }
 
   $self->create_schema_info_table($form, $dbh);
 
-  # Auth DB upgrades available?
-  my $dbupdater_auth = SL::DBUpgrade2->new(form => $form, auth => 1)->parse_dbupdate_controls;
-  return -3 if $dbupdater_auth->unapplied_upgrade_scripts($::auth->dbconnect);
+  my $dbupdater        = SL::DBUpgrade2->new(form => $form)->parse_dbupdate_controls;
+  my @unapplied_scripts = $dbupdater->unapplied_upgrade_scripts($dbh);
+#   $dbh->disconnect;
 
-  my $dbupdater = SL::DBUpgrade2->new(form => $form)->parse_dbupdate_controls;
-
-  my $update_available = $dbupdater->update_available($dbversion) || $dbupdater->update2_available($dbh);
-  $dbh->disconnect;
+  if (!@unapplied_scripts) {
+    SL::DB::Manager::Employee->update_entries_for_authorized_users;
+    return LOGIN_OK();
+  }
 
-  return 0 if !$update_available;
+  # 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);
 
   $form->{$_} = $::auth->client->{$_} for qw(dbname dbhost dbport dbuser dbpasswd);
   $form->{$_} = $myconfig{$_}         for qw(datestyle);
@@ -127,9 +185,23 @@ sub login {
 
   $form->{dbupdate} = "db" . $::auth->client->{dbname};
 
-  if ($form->{"show_dbupdate_warning"}) {
-    print $form->parse_html_template("dbupgrade/warning");
-    ::end_of_request();
+  my $show_update_warning = $form->{"show_dbupdate_warning"};
+  my %superuser           = (need_privileges => (any { $_->{superuser_privileges} } @unapplied_scripts));
+
+  if ($superuser{need_privileges}) {
+    %superuser = (
+      %superuser,
+      $self->_handle_superuser_privileges($form),
+    );
+    $show_update_warning = 1 if !$superuser{have_privileges};
+  }
+
+  if ($show_update_warning) {
+    print $form->parse_html_template("dbupgrade/warning", {
+      unapplied_scripts => \@unapplied_scripts,
+      superuser         => \%superuser,
+    });
+    $::dispatcher->end_request;
   }
 
   # update the tables
@@ -139,15 +211,23 @@ sub login {
   $SIG{HUP}  = 'IGNORE';
   $SIG{QUIT} = 'IGNORE';
 
-  $self->dbupdate($form);
   $self->dbupdate2(form => $form, updater => $dbupdater, database => $::auth->client->{dbname});
-  SL::DBUpgrade2->new(form => $::form, auth => 1)->apply_admin_dbupgrade_scripts(0);
+
+  # 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 -2;
+  return LOGIN_DBUPDATE_AVAILABLE();
 }
 
 sub dbconnect_vars {
@@ -224,25 +304,6 @@ sub dbsources {
   return @dbsources;
 }
 
-sub dbclusterencoding {
-  $main::lxdebug->enter_sub();
-
-  my ($self, $form) = @_;
-
-  $form->{dbdefault} ||= $form->{dbuser};
-
-  dbconnect_vars($form, $form->{dbdefault});
-
-  my $dbh                = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) || $form->dberror();
-  my $query              = qq|SELECT pg_encoding_to_char(encoding) FROM pg_database WHERE datname = 'template0'|;
-  my ($cluster_encoding) = $dbh->selectrow_array($query);
-  $dbh->disconnect();
-
-  $main::lxdebug->leave_sub();
-
-  return $cluster_encoding;
-}
-
 sub dbcreate {
   $main::lxdebug->enter_sub();
 
@@ -273,29 +334,60 @@ sub dbcreate {
 
   &dbconnect_vars($form, $form->{db});
 
+  # make a shim myconfig so that rose db connections work
+  $::myconfig{$_}     = $form->{$_} for qw(dbhost dbport dbuser dbpasswd);
+  $::myconfig{dbname} = $form->{db};
+
   $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
     or $form->dberror;
 
-  my $db_charset = $Common::db_encoding_to_charset{$form->{encoding}};
-  $db_charset ||= Common::DEFAULT_CHARSET;
-
-  my $dbupdater = SL::DBUpgrade2->new(form => $form);
+  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", undef, $db_charset);
-
-  # load chart of accounts
-  $dbupdater->process_query($dbh, "sql/$form->{chart}-chart.sql", undef, $db_charset);
-
-  $query = "UPDATE defaults SET coa = ?";
-  do_query($form, $dbh, $query, $form->{chart});
-  $query = "UPDATE defaults SET accounting_method = ?";
-  do_query($form, $dbh, $query, $form->{accounting_method});
-  $query = "UPDATE defaults SET profit_determination = ?";
-  do_query($form, $dbh, $query, $form->{profit_determination});
-  $query = "UPDATE defaults SET inventory_system = ?";
-  do_query($form, $dbh, $query, $form->{inventory_system});
-  $query = "UPDATE defaults SET curr = ?";
-  do_query($form, $dbh, $query, $form->{defaultcurrency});
+  $dbupdater->process_query($dbh, "sql/lx-office.sql");
+  $dbupdater->process_query($dbh, "sql/$form->{chart}-chart.sql");
+
+  $query = qq|UPDATE defaults SET coa = ?|;
+  do_query($form, $dbh, $query, map { $form->{$_} } qw(chart));
+
+  $dbh->disconnect;
+
+  # update new database
+  $self->dbupdate2(form => $form, updater => $dbupdater, database => $form->{db}, silent => 1);
+
+  $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
+    or $form->dberror;
+
+  $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});
+  }
+
+  $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}
+  );
 
   $dbh->disconnect;
 
@@ -319,72 +411,6 @@ sub dbdelete {
   $main::lxdebug->leave_sub();
 }
 
-sub dbsources_unused {
-  $main::lxdebug->enter_sub();
-
-  my ($self, $form) = @_;
-
-  $form->{only_acc_db} = 1;
-
-  my %members = $main::auth->read_all_users();
-  my %dbexcl  = map { $_ => 1 } grep { $_ } map { $_->{dbname} } values %members;
-
-  $dbexcl{$form->{dbdefault}}             = 1;
-  $dbexcl{$main::auth->{DB_config}->{db}} = 1;
-
-  my @dbunused = grep { !$dbexcl{$_} } dbsources("", $form);
-
-  $main::lxdebug->leave_sub();
-
-  return @dbunused;
-}
-
-sub dbneedsupdate {
-  $main::lxdebug->enter_sub();
-
-  my ($self, $form) = @_;
-
-  my %members   = $main::auth->read_all_users();
-  my $dbupdater = SL::DBUpgrade2->new(form => $form)->parse_dbupdate_controls;
-
-  my ($query, $sth, %dbs_needing_updates);
-
-  foreach my $login (grep /[a-z]/, keys %members) {
-    my $member = $members{$login};
-
-    map { $form->{$_} = $member->{$_} } qw(dbname dbuser dbpasswd dbhost dbport);
-    dbconnect_vars($form, $form->{dbname});
-
-    my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options);
-
-    next unless $dbh;
-
-    my $version;
-
-    $query = qq|SELECT version FROM defaults|;
-    $sth = prepare_query($form, $dbh, $query);
-    if ($sth->execute()) {
-      ($version) = $sth->fetchrow_array();
-    }
-    $sth->finish();
-
-    $dbh->disconnect and next unless $version;
-
-    my $update_available = $dbupdater->update_available($version) || $dbupdater->update2_available($dbh);
-    $dbh->disconnect;
-
-   if ($update_available) {
-      my $dbinfo = {};
-      map { $dbinfo->{$_} = $member->{$_} } grep /^db/, keys %{ $member };
-      $dbs_needing_updates{$member->{dbhost} . "::" . $member->{dbname}} = $dbinfo;
-    }
-  }
-
-  $main::lxdebug->leave_sub();
-
-  return values %dbs_needing_updates;
-}
-
 sub calc_version {
   $main::lxdebug->enter_sub(2);
 
@@ -407,7 +433,7 @@ sub calc_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$//;
@@ -447,99 +473,15 @@ sub create_schema_info_table {
   $main::lxdebug->leave_sub();
 }
 
-sub dbupdate {
-  $main::lxdebug->enter_sub();
-
-  my ($self, $form) = @_;
-
-  local *SQLDIR;
-
-  my @upgradescripts = ();
-  my $query;
-  my $rc = -2;
-
-  if ($form->{dbupdate}) {
-
-    # read update scripts into memory
-    opendir(SQLDIR, "sql/Pg-upgrade")
-      or &error("", "sql/Pg-upgrade : $!");
-    @upgradescripts =
-      sort(cmp_script_version
-           grep(/Pg-upgrade-.*?\.(sql|pl)$/,
-                readdir(SQLDIR)));
-    closedir(SQLDIR);
-  }
-
-  my $db_charset = $::lx_office_conf{system}->{dbcharset};
-  $db_charset ||= Common::DEFAULT_CHARSET;
-
-  my $dbupdater = SL::DBUpgrade2->new(form => $form);
-
-  foreach my $db (split(/ /, $form->{dbupdate})) {
-
-    next unless $form->{$db};
-
-    # strip db from dataset
-    $db =~ s/^db//;
-    &dbconnect_vars($form, $db);
-
-    my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
-      or $form->dberror;
-
-    $dbh->do($form->{dboptions}) if ($form->{dboptions});
-
-    # check version
-    $query = qq|SELECT version FROM defaults|;
-    my ($version) = selectrow_query($form, $dbh, $query);
-
-    next unless $version;
-
-    $version = calc_version($version);
-
-    foreach my $upgradescript (@upgradescripts) {
-      my $a = $upgradescript;
-      $a =~ s/^Pg-upgrade-|\.(sql|pl)$//g;
-
-      my ($mindb, $maxdb) = split /-/, $a;
-      my $str_maxdb = $maxdb;
-      $mindb = calc_version($mindb);
-      $maxdb = calc_version($maxdb);
-
-      next if ($version >= $maxdb);
-
-      # if there is no upgrade script exit
-      last if ($version < $mindb);
-
-      # apply upgrade
-      $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $upgradescript");
-      $dbupdater->process_file($dbh, "sql/Pg-upgrade/$upgradescript", $str_maxdb, $db_charset);
-
-      $version = $maxdb;
-
-    }
-
-    $rc = 0;
-    $dbh->disconnect;
-
-  }
-
-  $main::lxdebug->leave_sub();
-
-  return $rc;
-}
-
 sub dbupdate2 {
-  $main::lxdebug->enter_sub();
-
   my ($self, %params) = @_;
 
   my $form            = $params{form};
   my $dbupdater       = $params{updater};
   my $db              = $params{database};
-  my $rc              = -2;
-  my $db_charset      = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET;
+  my $silent          = $params{silent};
 
-  map { $_->{description} = SL::Iconv::convert($_->{charset}, $db_charset, $_->{description}) } values %{ $dbupdater->{all_controls} };
+  map { $_->{description} = SL::Iconv::convert($_->{charset}, 'UTF-8', $_->{description}) } values %{ $dbupdater->{all_controls} };
 
   &dbconnect_vars($form, $db);
 
@@ -550,27 +492,59 @@ sub dbupdate2 {
   $self->create_schema_info_table($form, $dbh);
 
   my @upgradescripts = $dbupdater->unapplied_upgrade_scripts($dbh);
+  my $need_superuser = (any { $_->{superuser_privileges} } @upgradescripts);
+  my $superuser_dbh;
+
+  if ($need_superuser) {
+    my %dbconnect_form = (
+      %{ $form },
+      dbuser   => $::auth->get_session_value("database_superuser_username"),
+      dbpasswd => $::auth->get_session_value("database_superuser_password"),
+    );
+
+    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;
+    }
+  }
 
-  $dbh->disconnect and next if !@upgradescripts;
+  $::lxdebug->log_time("DB upgrades commencing");
 
   foreach my $control (@upgradescripts) {
-    # apply upgrade
-    $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $control->{file}");
-    print $form->parse_html_template("dbupgrade/upgrade_message2", $control);
+    # 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;
 
-    $dbupdater->process_file($dbh, "sql/Pg-upgrade2/$control->{file}", $control, $db_charset);
-  }
+    $::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;
 
-  $rc = 0;
-  $dbh->disconnect;
+    $dbupdater->process_file($script_dbh, "sql/Pg-upgrade2/$control->{file}", $control);
+  }
 
-  $main::lxdebug->leave_sub();
+  $::lxdebug->log_time("DB upgrades finished");
 
-  return $rc;
+  $dbh->disconnect;
+  $superuser_dbh->disconnect if $superuser_dbh;
 }
 
 sub data {
   +{ %{ $_[0] } }
 }
 
+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;