DBO Update - alle normalen Models hinzugefĆ¼gt.
[kivitendo-erp.git] / SL / User.pm
index 1984ca5..46fe855 100644 (file)
@@ -37,56 +37,28 @@ package User;
 use IO::File;
 use Fcntl qw(:seek);
 
+#use SL::Auth;
 use SL::DBUpgrade2;
 use SL::DBUtils;
 use SL::Iconv;
 use SL::Inifile;
 
+use strict;
+
 sub new {
   $main::lxdebug->enter_sub();
 
-  my ($type, $memfile, $login) = @_;
+  my ($type, $login) = @_;
+
   my $self = {};
 
   if ($login ne "") {
-    local *MEMBER;
-
-    $login =~ s|.*/||;
-
-    &error("", "$memfile locked!") if (-f "${memfile}.LCK");
-
-    open(MEMBER, "$memfile") or &error("", "$memfile : $!");
-
-    while (<MEMBER>) {
-      if (/^\[$login\]/) {
-        while (<MEMBER>) {
-          last if m/^\[/;
-          next if m/^(#|\s)/;
-
-          # remove comments
-          s/\s#.*//g;
-
-          # remove any trailing whitespace
-          s/^\s*(.*?)\s*$/$1/;
-
-          ($key, $value) = split(/=/, $_, 2);
-
-          if (($key eq "stylesheet") && ($value eq "sql-ledger.css")) {
-            $value = "lx-office-erp.css";
-          }
-
-          $self->{$key} = $value;
-        }
-
-        $self->{login} = $login;
-
-        last;
-      }
-    }
-    close MEMBER;
+    my %user_data = $main::auth->read_user($login);
+    map { $self->{$_} = $user_data{$_} } keys %user_data;
   }
 
   $main::lxdebug->leave_sub();
+
   bless $self, $type;
 }
 
@@ -121,39 +93,21 @@ sub country_codes {
 sub login {
   $main::lxdebug->enter_sub();
 
-  my ($self, $form, $userspath) = @_;
+  my ($self, $form) = @_;
+  our $sid;
 
   local *FH;
 
   my $rc = -3;
 
   if ($self->{login}) {
-
-    if ($self->{password}) {
-      if ($form->{hashed_password}) {
-        $form->{password} = $form->{hashed_password};
-      } else {
-        $form->{password} = crypt($form->{password},
-                                  substr($self->{login}, 0, 2));
-      }
-      if ($self->{password} ne $form->{password}) {
-        $main::lxdebug->leave_sub();
-        return -1;
-      }
-    }
-
-    unless (-e "$userspath/$self->{login}.conf") {
-      $self->create_config();
-    }
-
-    do "$userspath/$self->{login}.conf";
-    $myconfig{dbpasswd} = unpack('u', $myconfig{dbpasswd});
+    my %myconfig = $main::auth->read_user($self->{login});
 
     # check if database is down
     my $dbh =
       DBI->connect($myconfig{dbconnect}, $myconfig{dbuser},
                    $myconfig{dbpasswd})
-      or $self->error(DBI::errstr);
+      or $self->error($DBI::errstr);
 
     # we got a connection, check the version
     my $query = qq|SELECT version FROM defaults|;
@@ -163,17 +117,7 @@ sub login {
     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 id FROM employee WHERE login = ?|;
-    my ($login) = selectrow_query($form, $dbh, $query, $self->{login});
-
-    if (!$login) {
-      $query = qq|INSERT INTO employee (login, name, workphone, role)| .
-               qq|VALUES (?, ?, ?, ?)|;
-      my @values = ($self->{login}, $myconfig{name}, $myconfig{tel}, "user");
-      do_query($form, $dbh, $query, @values);
-    }
+    $self->create_employee_entry($form, $dbh, \%myconfig);
 
     $self->create_schema_info_table($form, $dbh);
 
@@ -185,7 +129,7 @@ sub login {
       parse_dbupdate_controls($form, $myconfig{"dbdriver"});
 
     map({ $form->{$_} = $myconfig{$_} }
-        qw(dbname dbhost dbport dbdriver dbuser dbpasswd dbconnect));
+        qw(dbname dbhost dbport dbdriver dbuser dbpasswd dbconnect dateformat));
 
     if (update_available($myconfig{"dbdriver"}, $dbversion) ||
         update2_available($form, $controls)) {
@@ -200,11 +144,16 @@ sub login {
 
       if ($form->{"show_dbupdate_warning"}) {
         print $form->parse_html_template("dbupgrade/warning");
-        exit(0);
+        ::end_of_request();
       }
 
       # update the tables
-      open(FH, ">$userspath/nologin") or die("$!");
+      if (!open(FH, ">$main::userspath/nologin")) {
+        $form->show_generic_error($main::locale->text('A temporary file could not be created. ' .
+                                                      'Please verify that the directory "#1" is writeable by the webserver.',
+                                                      $main::userspath),
+                                  'back_button' => 1);
+      }
 
       # required for Oracle
       $form->{dbdefault} = $sid;
@@ -219,11 +168,12 @@ sub login {
       close(FH);
 
       # remove lock file
-      unlink("$userspath/nologin");
+      unlink("$main::userspath/nologin");
 
       my $menufile =
         $self->{"menustyle"} eq "v3" ? "menuv3.pl" :
         $self->{"menustyle"} eq "neu" ? "menunew.pl" :
+        $self->{"menustyle"} eq "js" ? "menujs.pl" :
         $self->{"menustyle"} eq "xml" ? "menuXML.pl" :
         "menu.pl";
 
@@ -370,6 +320,25 @@ sub dbsources {
   return @dbsources;
 }
 
+sub dbclusterencoding {
+  $main::lxdebug->enter_sub();
+
+  my ($self, $form) = @_;
+
+  $form->{dbdefault} ||= $form->{dbuser};
+
+  dbconnect_vars($form, $form->{dbdefault});
+
+  my $dbh                = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) || $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();
 
@@ -403,7 +372,8 @@ sub dbcreate {
   my $query = $dbcreate{$form->{dbdriver}};
   $query .= " WITH " . join(" ", @{$dboptions{"Pg"}}) if (@{$dboptions{"Pg"}});
 
-  do_query($form, $dbh, $query);
+  # Ignore errors if the database exists.
+  $dbh->do($query);
 
   if ($form->{dbdriver} eq 'Oracle') {
     $query = qq|GRANT CONNECT, RESOURCE TO "$form->{db}"|;
@@ -442,7 +412,7 @@ sub dbcreate {
 # Process a Perl script which updates the database.
 # If the script returns 1 then the update was successful.
 # Return code "2" means "needs more interaction; remove
-# users/nologin and exit".
+# users/nologin and end current request".
 # All other return codes are fatal errors.
 sub process_perl_script {
   $main::lxdebug->enter_sub();
@@ -475,14 +445,12 @@ sub process_perl_script {
 
   $dbh->begin_work();
 
+  # setup dbup_ export vars
   my %dbup_myconfig = ();
   map({ $dbup_myconfig{$_} = $form->{$_}; }
       qw(dbname dbuser dbpasswd dbhost dbport dbconnect));
 
-  my $nls_file = $filename;
-  $nls_file =~ s|.*/||;
-  $nls_file =~ s|.pl$||;
-  my $dbup_locale = Locale->new($main::language, $nls_file);
+  my $dbup_locale = $::locale;
 
   my $result = eval($contents);
 
@@ -495,10 +463,10 @@ sub process_perl_script {
     print $form->parse_html_template("dbupgrade/error",
                                      { "file"  => $filename,
                                        "error" => $@ });
-    exit(0);
+    ::end_of_request();
   } elsif (1 != $result) {
     unlink("users/nologin") if (2 == $result);
-    exit(0);
+    ::end_of_request();
   }
 
   if (ref($version_or_control) eq "HASH") {
@@ -584,6 +552,12 @@ sub process_query {
         $query .= $char;
       }
     }
+
+    # Insert a space at the end of each line so that queries split
+    # over multiple lines work properly.
+    if ($query ne '') {
+      $query .= @quote_chars ? "\n" : ' ';
+    }
   }
 
   if (ref($version_or_control) eq "HASH") {
@@ -625,41 +599,21 @@ sub dbdelete {
 sub dbsources_unused {
   $main::lxdebug->enter_sub();
 
-  my ($self, $form, $memfile) = @_;
-
-  local *FH;
-
-  my @dbexcl    = ();
-  my @dbsources = ();
-
-  $form->error('File locked!') if (-f "${memfile}.LCK");
-
-  # open members file
-  open(FH, "$memfile") or $form->error("$memfile : $!");
-
-  while (<FH>) {
-    if (/^dbname=/) {
-      my ($null, $item) = split(/=/);
-      push @dbexcl, $item;
-    }
-  }
-
-  close FH;
+  my ($self, $form) = @_;
 
   $form->{only_acc_db} = 1;
-  my @db = &dbsources("", $form);
 
-  push @dbexcl, $form->{dbdefault};
+  my %members = $main::auth->read_all_users();
+  my %dbexcl  = map { $_ => 1 } grep { $_ } map { $_->{dbname} } values %members;
 
-  foreach $item (@db) {
-    unless (grep /$item$/, @dbexcl) {
-      push @dbsources, $item;
-    }
-  }
+  $dbexcl{$form->{dbdefault}}             = 1;
+  $dbexcl{$main::auth->{DB_config}->{db}} = 1;
+
+  my @dbunused = grep { !$dbexcl{$_} } dbsources("", $form);
 
   $main::lxdebug->leave_sub();
 
-  return @dbsources;
+  return @dbunused;
 }
 
 sub dbneedsupdate {
@@ -667,17 +621,17 @@ sub dbneedsupdate {
 
   my ($self, $form) = @_;
 
-  my $members  = Inifile->new($main::memberfile);
+  my %members  = $main::auth->read_all_users();
   my $controls = parse_dbupdate_controls($form, $form->{dbdriver});
 
   my ($query, $sth, %dbs_needing_updates);
 
-  foreach my $login (grep /[a-z]/, keys %{ $members }) {
-    my $member = $members->{$login};
+  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});
-    $main::lxdebug->dump(0, "form", $form);
+
     my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd});
 
     next unless $dbh;
@@ -734,8 +688,8 @@ sub cmp_script_version {
   $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);
@@ -820,6 +774,8 @@ sub dbupdate {
       DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
       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);
@@ -844,7 +800,7 @@ sub dbupdate {
       last if ($version < $mindb);
 
       # apply upgrade
-      $main::lxdebug->message(DEBUG2, "Applying Update $upgradescript");
+      $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $upgradescript");
       if ($file_type eq "sql") {
         $self->process_query($form, $dbh, "sql/" . $form->{"dbdriver"} .
                              "-upgrade/$upgradescript", $str_maxdb, $db_charset);
@@ -895,6 +851,8 @@ sub dbupdate2 {
       DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
       or $form->dberror;
 
+    $dbh->do($form->{dboptions}) if ($form->{dboptions});
+
     map({ $_->{"applied"} = 0; } @upgradescripts);
 
     $self->create_schema_info_table($form, $dbh);
@@ -926,7 +884,7 @@ sub dbupdate2 {
       my $file_type = $1;
 
       # apply upgrade
-      $main::lxdebug->message(DEBUG2, "Applying Update $control->{file}");
+      $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $control->{file}");
       print $form->parse_html_template("dbupgrade/upgrade_message2", $control);
 
       if ($file_type eq "sql") {
@@ -980,132 +938,69 @@ sub update2_available {
   return 0;
 }
 
-sub create_config {
+sub save_member {
   $main::lxdebug->enter_sub();
 
   my ($self) = @_;
+  my $form   = \%main::form;
 
-  local *CONF;
-
-  @config = config_vars();
-
-  my $userspath = $main::userspath;
-
-  open(CONF, ">", "$userspath/$self->{login}.conf") || $self->error("$userspath/$self->{login}.conf : $!");
+  # format dbconnect and dboptions string
+  dbconnect_vars($self, $self->{dbname});
 
-  # create the config file
-  print CONF qq|# configuration file for $self->{login}
+  map { $self->{$_} =~ s/\r//g; } qw(address signature);
 
-\%myconfig = (
-|;
+  $main::auth->save_user($self->{login}, map { $_, $self->{$_} } config_vars());
 
-  foreach my $key (sort @config) {
-    $self->{$key} =~ s/\'/\\\'/g;
-    print CONF qq|  $key => '$self->{$key}',\n|;
+  my $dbh = DBI->connect($self->{dbconnect}, $self->{dbuser}, $self->{dbpasswd});
+  if ($dbh) {
+    $self->create_employee_entry($form, $dbh, $self, 1);
+    $dbh->disconnect();
   }
 
-  print CONF qq|);\n\n|;
-
-  close CONF;
-
   $main::lxdebug->leave_sub();
 }
 
-sub save_member {
+sub create_employee_entry {
   $main::lxdebug->enter_sub();
 
-  my ($self, $memberfile, $userspath) = @_;
-
-  local (*FH, *CONF);
+  my $self            = shift;
+  my $form            = shift;
+  my $dbh             = shift;
+  my $myconfig        = shift;
+  my $update_existing = shift;
 
-  my $newmember = 1;
-
-  # format dbconnect and dboptions string
-  &dbconnect_vars($self, $self->{dbname});
-
-  $self->error('File locked!') if (-f "${memberfile}.LCK");
-  open(FH, ">${memberfile}.LCK") or $self->error("${memberfile}.LCK : $!");
-  close(FH);
-
-  open(CONF, "+<$memberfile") or $self->error("$memberfile : $!");
-
-  @config = <CONF>;
-
-  seek(CONF, 0, 0);
-  truncate(CONF, 0);
-
-  while ($line = shift @config) {
-    if ($line =~ /^\[\Q$self->{login}\E\]/) {
-      $newmember = 0;
-      last;
-    }
-    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;
+  if (!does_table_exist($dbh, 'employee')) {
+    $main::lxdebug->leave_sub();
+    return;
   }
 
-  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};
-    }
-  }
+  # add login to employee table if it does not exist
+  # no error check for employee table, ignore if it does not exist
+  my ($id)  = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $self->{login});
 
-  if ($self->{'root login'}) {
-    @config = ("password");
-  } else {
-    @config = &config_vars;
-  }
+  if (!$id) {
+    my $query = qq|INSERT INTO employee (login, name, workphone, role) VALUES (?, ?, ?, ?)|;
+    do_query($form, $dbh, $query, ($self->{login}, $myconfig->{name}, $myconfig->{tel}, "user"));
 
-  # 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|;
+  } elsif ($update_existing) {
+    my $query = qq|UPDATE employee SET name = ?, workphone = ?, role = 'user' WHERE id = ?|;
+    do_query($form, $dbh, $query, $myconfig->{name}, $myconfig->{tel}, $id);
   }
 
-  print CONF "\n";
-  close CONF;
-  unlink "${memberfile}.LCK";
-
-  # create conf file
-  $self->create_config() unless $self->{'root login'};
-
   $main::lxdebug->leave_sub();
 }
 
 sub config_vars {
   $main::lxdebug->enter_sub();
 
-  my @conf = qw(acs address admin businessnumber company countrycode
+  my @conf = qw(address admin businessnumber company countrycode
     currency dateformat dbconnect dbdriver dbhost dbport dboptions
     dbname dbuser dbpasswd email fax name numberformat password
     printer role sid signature stylesheet tel templates vclimit angebote
     bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen
     taxnumber co_ustid duns menustyle template_format default_media
-    default_printer_id copies show_form_details favorites);
+    default_printer_id copies show_form_details favorites
+    pdonumber sdonumber hide_cvar_search_options mandatory_departments);
 
   $main::lxdebug->leave_sub();