Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 8c7e4493

Von Moritz Bunkus vor fast 17 Jahren hinzugefügt

  • ID 8c7e44938a661e035f62840e1e177353240ace5d
  • Vorgänger 3ced230b
  • Nachfolger ce45d060

Umstellung der Benutzerverwaltung von Dateien im Verzeichnis "users" auf die Verwendung einer Authentifizierungsdatenbank.
Es ist erforderlich, die Dateien doc/UPGRADE und doc/INSTALL/index.html zu lesen und die angesprochenen Punkte auszuführen, um nach einem Upgrade weiter arbeiten zu können.

Unterschiede anzeigen:

SL/User.pm
37 37
use IO::File;
38 38
use Fcntl qw(:seek);
39 39

  
40
use SL::Auth;
40 41
use SL::DBUpgrade2;
41 42
use SL::DBUtils;
42 43
use SL::Iconv;
......
45 46
sub new {
46 47
  $main::lxdebug->enter_sub();
47 48

  
48
  my ($type, $memfile, $login) = @_;
49
  my ($type, $login) = @_;
50

  
49 51
  my $self = {};
50 52

  
51 53
  if ($login ne "") {
52
    local *MEMBER;
53

  
54
    $login =~ s|.*/||;
55

  
56
    &error("", "$memfile locked!") if (-f "${memfile}.LCK");
57

  
58
    open(MEMBER, "$memfile") or &error("", "$memfile : $!");
59

  
60
    while (<MEMBER>) {
61
      if (/^\[$login\]/) {
62
        while (<MEMBER>) {
63
          last if m/^\[/;
64
          next if m/^(#|\s)/;
65

  
66
          # remove comments
67
          s/\s#.*//g;
68

  
69
          # remove any trailing whitespace
70
          s/^\s*(.*?)\s*$/$1/;
71

  
72
          ($key, $value) = split(/=/, $_, 2);
73

  
74
          if (($key eq "stylesheet") && ($value eq "sql-ledger.css")) {
75
            $value = "lx-office-erp.css";
76
          }
77

  
78
          $self->{$key} = $value;
79
        }
80

  
81
        $self->{login} = $login;
82

  
83
        last;
84
      }
85
    }
86
    close MEMBER;
54
    my %user_data = $main::auth->read_user($login);
55
    map { $self->{$_} = $user_data{$_} } keys %user_data;
87 56
  }
88 57

  
89 58
  $main::lxdebug->leave_sub();
59

  
90 60
  bless $self, $type;
91 61
}
92 62

  
......
121 91
sub login {
122 92
  $main::lxdebug->enter_sub();
123 93

  
124
  my ($self, $form, $userspath) = @_;
94
  my ($self, $form) = @_;
125 95

  
126 96
  local *FH;
127 97

  
128 98
  my $rc = -3;
129 99

  
130 100
  if ($self->{login}) {
131

  
132
    if ($self->{password}) {
133
      if ($form->{hashed_password}) {
134
        $form->{password} = $form->{hashed_password};
135
      } else {
136
        $form->{password} = crypt($form->{password},
137
                                  substr($self->{login}, 0, 2));
138
      }
139
      if ($self->{password} ne $form->{password}) {
140
        $main::lxdebug->leave_sub();
141
        return -1;
142
      }
143
    }
144

  
145
    unless (-e "$userspath/$self->{login}.conf") {
146
      $self->create_config();
147
    }
148

  
149
    do "$userspath/$self->{login}.conf";
150
    $myconfig{dbpasswd} = unpack('u', $myconfig{dbpasswd});
101
    my %myconfig = $main::auth->read_user($self->{login});
151 102

  
152 103
    # check if database is down
153 104
    my $dbh =
......
163 114
    my ($dbversion) = $sth->fetchrow_array;
164 115
    $sth->finish;
165 116

  
166
    # add login to employee table if it does not exist
167
    # no error check for employee table, ignore if it does not exist
168
    $query = qq|SELECT id FROM employee WHERE login = ?|;
169
    my ($login) = selectrow_query($form, $dbh, $query, $self->{login});
170

  
171
    if (!$login) {
172
      $query = qq|INSERT INTO employee (login, name, workphone, role)| .
173
               qq|VALUES (?, ?, ?, ?)|;
174
      my @values = ($self->{login}, $myconfig{name}, $myconfig{tel}, "user");
175
      do_query($form, $dbh, $query, @values);
176
    }
117
    $self->create_employee_entry($form, $dbh, \%myconfig);
177 118

  
178 119
    $self->create_schema_info_table($form, $dbh);
179 120

  
......
185 126
      parse_dbupdate_controls($form, $myconfig{"dbdriver"});
186 127

  
187 128
    map({ $form->{$_} = $myconfig{$_} }
188
        qw(dbname dbhost dbport dbdriver dbuser dbpasswd dbconnect));
129
        qw(dbname dbhost dbport dbdriver dbuser dbpasswd dbconnect dateformat));
189 130

  
190 131
    if (update_available($myconfig{"dbdriver"}, $dbversion) ||
191 132
        update2_available($form, $controls)) {
......
204 145
      }
205 146

  
206 147
      # update the tables
207
      open(FH, ">$userspath/nologin") or die("$!");
148
      open(FH, ">$main::userspath/nologin") or die("$!");
208 149

  
209 150
      # required for Oracle
210 151
      $form->{dbdefault} = $sid;
......
219 160
      close(FH);
220 161

  
221 162
      # remove lock file
222
      unlink("$userspath/nologin");
163
      unlink("$main::userspath/nologin");
223 164

  
224 165
      my $menufile =
225 166
        $self->{"menustyle"} eq "v3" ? "menuv3.pl" :
......
625 566
sub dbsources_unused {
626 567
  $main::lxdebug->enter_sub();
627 568

  
628
  my ($self, $form, $memfile) = @_;
629

  
630
  local *FH;
631

  
632
  my @dbexcl    = ();
633
  my @dbsources = ();
634

  
635
  $form->error('File locked!') if (-f "${memfile}.LCK");
636

  
637
  # open members file
638
  open(FH, "$memfile") or $form->error("$memfile : $!");
639

  
640
  while (<FH>) {
641
    if (/^dbname=/) {
642
      my ($null, $item) = split(/=/);
643
      push @dbexcl, $item;
644
    }
645
  }
646

  
647
  close FH;
569
  my ($self, $form) = @_;
648 570

  
649 571
  $form->{only_acc_db} = 1;
650
  my @db = &dbsources("", $form);
651 572

  
652
  push @dbexcl, $form->{dbdefault};
573
  my %members = $main::auth->read_all_users();
574
  my %dbexcl  = map { $_ => 1 } grep { $_ } map { $_->{dbname} } values %members;
653 575

  
654
  foreach $item (@db) {
655
    unless (grep /$item$/, @dbexcl) {
656
      push @dbsources, $item;
657
    }
658
  }
576
  $dbexcl{$form->{dbdefault}}             = 1;
577
  $dbexcl{$main::auth->{DB_config}->{db}} = 1;
578

  
579
  my @dbunused = grep { !$dbexcl{$_} } dbsources("", $form);
659 580

  
660 581
  $main::lxdebug->leave_sub();
661 582

  
662
  return @dbsources;
583
  return @dbunused;
663 584
}
664 585

  
665 586
sub dbneedsupdate {
......
667 588

  
668 589
  my ($self, $form) = @_;
669 590

  
670
  my $members  = Inifile->new($main::memberfile);
591
  my %members  = $main::auth->read_all_users();
671 592
  my $controls = parse_dbupdate_controls($form, $form->{dbdriver});
672 593

  
673 594
  my ($query, $sth, %dbs_needing_updates);
674 595

  
675
  foreach my $login (grep /[a-z]/, keys %{ $members }) {
676
    my $member = $members->{$login};
596
  foreach my $login (grep /[a-z]/, keys %members) {
597
    my $member = $members{$login};
677 598

  
678 599
    map { $form->{$_} = $member->{$_} } qw(dbname dbuser dbpasswd dbhost dbport);
679 600
    dbconnect_vars($form, $form->{dbname});
680
    $main::lxdebug->dump(0, "form", $form);
601

  
681 602
    my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd});
682 603

  
683 604
    next unless $dbh;
......
820 741
      DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
821 742
      or $form->dberror;
822 743

  
744
    $dbh->do($form->{dboptions}) if ($form->{dboptions});
745

  
823 746
    # check version
824 747
    $query = qq|SELECT version FROM defaults|;
825 748
    my ($version) = selectrow_query($form, $dbh, $query);
......
895 818
      DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
896 819
      or $form->dberror;
897 820

  
821
    $dbh->do($form->{dboptions}) if ($form->{dboptions});
822

  
898 823
    map({ $_->{"applied"} = 0; } @upgradescripts);
899 824

  
900 825
    $self->create_schema_info_table($form, $dbh);
......
980 905
  return 0;
981 906
}
982 907

  
983
sub create_config {
908
sub save_member {
984 909
  $main::lxdebug->enter_sub();
985 910

  
986 911
  my ($self) = @_;
987 912

  
988
  local *CONF;
989

  
990
  @config = config_vars();
991

  
992
  my $userspath = $main::userspath;
993

  
994
  open(CONF, ">", "$userspath/$self->{login}.conf") || $self->error("$userspath/$self->{login}.conf : $!");
913
  # format dbconnect and dboptions string
914
  dbconnect_vars($self, $self->{dbname});
995 915

  
996
  # create the config file
997
  print CONF qq|# configuration file for $self->{login}
916
  map { $self->{$_} =~ s/\r//g; } qw(address signature);
998 917

  
999
\%myconfig = (
1000
|;
918
  $main::auth->save_user($self->{login}, map { $_, $self->{$_} } config_vars());
1001 919

  
1002
  foreach my $key (sort @config) {
1003
    $self->{$key} =~ s/\'/\\\'/g;
1004
    print CONF qq|  $key => '$self->{$key}',\n|;
920
  my $dbh = DBI->connect($self->{dbconnect}, $self->{dbuser}, $self->{dbpasswd});
921
  if ($dbh) {
922
    $self->create_employee_entry($form, $dbh, $self);
923
    $dbh->disconnect();
1005 924
  }
1006 925

  
1007
  print CONF qq|);\n\n|;
1008

  
1009
  close CONF;
1010

  
1011 926
  $main::lxdebug->leave_sub();
1012 927
}
1013 928

  
1014
sub save_member {
929
sub create_employee_entry {
1015 930
  $main::lxdebug->enter_sub();
1016 931

  
1017
  my ($self, $memberfile, $userspath) = @_;
1018

  
1019
  local (*FH, *CONF);
1020

  
1021
  my $newmember = 1;
1022

  
1023
  # format dbconnect and dboptions string
1024
  &dbconnect_vars($self, $self->{dbname});
1025

  
1026
  $self->error('File locked!') if (-f "${memberfile}.LCK");
1027
  open(FH, ">${memberfile}.LCK") or $self->error("${memberfile}.LCK : $!");
1028
  close(FH);
1029

  
1030
  open(CONF, "+<$memberfile") or $self->error("$memberfile : $!");
1031

  
1032
  @config = <CONF>;
932
  my $self     = shift;
933
  my $form     = shift;
934
  my $dbh      = shift;
935
  my $myconfig = shift;
1033 936

  
1034
  seek(CONF, 0, 0);
1035
  truncate(CONF, 0);
937
  # add login to employee table if it does not exist
938
  # no error check for employee table, ignore if it does not exist
939
  my ($login)  = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $self->{login});
1036 940

  
1037
  while ($line = shift @config) {
1038
    if ($line =~ /^\[\Q$self->{login}\E\]/) {
1039
      $newmember = 0;
1040
      last;
1041
    }
1042
    print CONF $line;
1043
  }
1044

  
1045
  # remove everything up to next login or EOF
1046
  while ($line = shift @config) {
1047
    last if ($line =~ /^\[/);
941
  if (!$login) {
942
    $query = qq|INSERT INTO employee (login, name, workphone, role) VALUES (?, ?, ?, ?)|;
943
    do_query($form, $dbh, $query, ($self->{login}, $myconfig->{name}, $myconfig->{tel}, "user"));
1048 944
  }
1049 945

  
1050
  # this one is either the next login or EOF
1051
  print CONF $line;
1052

  
1053
  while ($line = shift @config) {
1054
    print CONF $line;
1055
  }
1056

  
1057
  print CONF qq|[$self->{login}]\n|;
1058

  
1059
  if ((($self->{dbpasswd} ne $self->{old_dbpasswd}) || $newmember)
1060
      && $self->{root}) {
1061
    $self->{dbpasswd} = pack 'u', $self->{dbpasswd};
1062
    chop $self->{dbpasswd};
1063
  }
1064
  if (defined($self->{new_password})) {
1065
    if ($self->{new_password} ne $self->{old_password}) {
1066
      $self->{password} = crypt $self->{new_password},
1067
        substr($self->{login}, 0, 2)
1068
        if $self->{new_password};
1069
    }
1070
  } else {
1071
    if ($self->{password} ne $self->{old_password}) {
1072
      $self->{password} = crypt $self->{password}, substr($self->{login}, 0, 2)
1073
        if $self->{password};
1074
    }
1075
  }
1076

  
1077
  if ($self->{'root login'}) {
1078
    @config = ("password");
1079
  } else {
1080
    @config = &config_vars;
1081
  }
1082

  
1083
  # replace \r\n with \n
1084
  map { $self->{$_} =~ s/\r\n/\\n/g } qw(address signature);
1085
  foreach $key (sort @config) {
1086
    print CONF qq|$key=$self->{$key}\n|;
1087
  }
1088

  
1089
  print CONF "\n";
1090
  close CONF;
1091
  unlink "${memberfile}.LCK";
1092

  
1093
  # create conf file
1094
  $self->create_config() unless $self->{'root login'};
1095

  
1096 946
  $main::lxdebug->leave_sub();
1097 947
}
1098 948

  

Auch abrufbar als: Unified diff