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:

bin/mozilla/admin.pl
36 36

  
37 37
use DBI;
38 38
use CGI;
39
use POSIX qw(strftime);
40
use IO::File;
41
use Fcntl;
42 39
use English qw(-no_match_vars);
40
use Fcntl;
41
use File::Copy;
42
use IO::File;
43
use POSIX qw(strftime);
43 44
use Sys::Hostname;
44 45

  
46
use SL::Auth;
45 47
use SL::Form;
46 48
use SL::Mailer;
47 49
use SL::User;
......
51 53
use SL::DBUtils;
52 54

  
53 55
require "bin/mozilla/common.pl";
56
require "bin/mozilla/admin_groups.pl";
54 57

  
55 58
our $cgi = new CGI('');
56 59

  
57 60
$form = new Form;
58
$form->{"root"} = "root login";
59 61

  
60 62
$locale = new Locale $language, "admin";
61 63

  
64
our $auth = SL::Auth->new();
65
if ($auth->session_tables_present()) {
66
  $auth->expire_sessions();
67
  $auth->restore_session();
68
  $auth->set_session_value('rpw', $form->{rpw});
69
}
70

  
62 71
# customization
63 72
if (-f "bin/mozilla/custom_$form->{script}") {
64 73
  eval { require "bin/mozilla/custom_$form->{script}"; };
......
70 79

  
71 80
if ($form->{action}) {
72 81

  
73

  
74 82
  $subroutine = $locale->findsub($form->{action});
75 83

  
76
  if ($subroutine eq 'login') {
77
    if ($form->{rpw}) {
78
      $form->{rpw} = crypt $form->{rpw}, "ro";
79
    }
84
  if ($auth->authenticate_root($form->{rpw}, 0)) {
85
    $form->{error_message} = $locale->text('Incorrect Password!');
86
    adminlogin();
87
    exit;
80 88
  }
81 89

  
82
  check_password();
90
  $auth->create_or_refresh_session() if ($auth->session_tables_present());
83 91

  
84 92
  call_sub($subroutine);
85 93

  
......
89 97
  $form->error($locale->text('No Database Drivers available!'))
90 98
    unless (User->dbdrivers);
91 99

  
92
  # create memberfile
93
  if (!-f $memberfile) {
94
    open(FH, ">$memberfile") or $form->error("$memberfile : $ERRNO");
95
    print FH qq|# SQL-Ledger Accounting members
96

  
97
[root login]
98
password=
99

  
100
|;
101
    close FH;
102
  }
103

  
104 100
  adminlogin();
105 101

  
106 102
}
......
111 107

  
112 108
sub adminlogin {
113 109

  
114
  $form->{title} =
115
    qq|Lx-Office ERP $form->{version} | . $locale->text('Administration');
110
  $form->{title} = qq|Lx-Office ERP $form->{version} | . $locale->text('Administration');
116 111

  
117 112
  $form->header();
118 113
  print $form->parse_html_template('admin/adminlogin');
119 114
}
120 115

  
121 116
sub login {
117
  check_auth_db_and_tables();
122 118
  list_users();
123 119
}
124 120

  
125
sub list_users {
121
sub logout {
122
  $auth->destroy_session();
123
  adminlogin();
124
}
125

  
126
sub check_auth_db_and_tables {
127
  my %params;
126 128

  
127
  $form->error($locale->text('File locked!')) if (-f "${memberfile}.LCK");
129
  map { $params{"db_${_}"} = $auth->{DB_config}->{$_} } keys %{ $auth->{DB_config} };
128 130

  
129
  open(FH, "$memberfile") or $form->error("$memberfile : $ERRNO");
131
  if (!$auth->check_database()) {
132
    $form->{title} = $locale->text('Authentification database creation');
133
    $form->header();
134
    print $form->parse_html_template('admin/check_auth_database', \%params);
135

  
136
    exit 0;
137
  }
130 138

  
131
  my %members;
139
  if (!$auth->check_tables()) {
140
    $form->{title} = $locale->text('Authentification tables creation');
141
    $form->header();
142
    print $form->parse_html_template('admin/check_auth_tables', \%params);
143

  
144
    exit 0;
145
  }
146

  
147
  if (-f $memberfile) {
148
    my $memberdir = "";
149

  
150
    if ($memberfile =~ m|^.*/|) {
151
      $memberdir = $&;
152
    }
153

  
154
    my $backupdir = "${memberdir}member-file-migration";
155

  
156
    $form->{title} = $locale->text('User data migration');
157
    $form->header();
158
    print $form->parse_html_template('admin/user_migration', { 'memberfile' => $memberfile,
159
                                                               'backupdir'  => $backupdir });
160

  
161
    exit 0
162
  }
163
}
164

  
165
sub create_auth_db {
166
  $auth->create_database('superuser'          => $form->{db_superuser},
167
                         'superuser_password' => $form->{db_superuser_password},
168
                         'template'           => $form->{db_template});
169
  login();
170
}
171

  
172
sub create_auth_tables {
173
  $auth->create_tables();
174
  $auth->set_session_value('rpw', $form->{rpw});
175
  $auth->create_or_refresh_session();
176

  
177
  login();
178
}
179

  
180
sub migrate_users {
181
  $lxdebug->enter_sub();
182

  
183
  my $memberdir = "";
184

  
185
  if ($memberfile =~ m|^.*/|) {
186
    $memberdir = $&;
187
  }
132 188

  
133
  while (<FH>) {
189
  my $backupdir = "${memberdir}member-file-migration";
190

  
191
  if (! -d $backupdir && !mkdir $backupdir, 0700) {
192
    $form->error(sprintf($locale->text('The directory "%s" could not be created:\n%s'), $backupdir, $!));
193
  }
194

  
195
  copy $memberfile, "users/member-file-migration/members";
196

  
197
  my $in = IO::File->new($memberfile, "r");
198

  
199
  $form->error($locale->text('Could not open the old memberfile.')) if (!$in);
200

  
201
  my (%members, $login);
202

  
203
  while (<$in>) {
134 204
    chomp;
135 205

  
136
    if (/^\[.*\]/) {
206
    next if (m/^\s*\#/);
207

  
208
    if (m/^\[.*\]/) {
137 209
      $login = $_;
138 210
      $login =~ s/(\[|\])//g;
211
      $login =~ s/^\s*//;
212
      $login =~ s/\s*$//;
139 213

  
140 214
      $members{$login} = { "login" => $login };
215
      next;
141 216
    }
142 217

  
143
    if (/^([a-z]+)=(.*)/) {
144
      $members{$login}->{$1} = $2;
218
    if ($login && m/=/) {
219
      my ($key, $value) = split m/\s*=\s*/, $_, 2;
220
      $key   =~ s|^\s*||;
221
      $value =~ s|\s*$||;
222

  
223
      $value =~ s|\\r||g;
224
      $value =~ s|\\n|\n|g;
225

  
226
      $members{$login}->{$key} = $value;
145 227
    }
146 228
  }
147 229

  
148
  close(FH);
230
  $in->close();
149 231

  
150 232
  delete $members{"root login"};
233

  
234
  map { $_->{dbpasswd} = unpack 'u', $_->{dbpasswd} } values %members;
235

  
236
  while (my ($login, $params) = each %members) {
237
    $auth->save_user($login, %{ $params });
238
    $auth->change_password($login, $params->{password}, 1);
239

  
240
    my $conf_file = "${memberdir}${login}.conf";
241

  
242
    if (-f $conf_file) {
243
      copy   $conf_file, "${backupdir}/${login}.conf";
244
      unlink $conf_file;
245
    }
246
  }
247

  
248
  unlink $memberfile;
249

  
250
  my @member_list = sort { lc $a->{login} cmp lc $b->{login} } values %members;
251

  
252
  $form->{title} = $locale->text('User data migration');
253
  $form->header();
254
  print $form->parse_html_template('admin/user_migration_done', { 'MEMBERS' => \@member_list });
255

  
256
  $lxdebug->leave_sub();
257
}
258

  
259
sub create_standard_group_ask {
260
  $form->{title} = $locale->text('Create a standard group');
261

  
262
  $form->header();
263
  print $form->parse_html_template("admin/create_standard_group_ask");
264
}
265

  
266
sub create_standard_group {
267
  my %members = $auth->read_all_users();
268

  
269
  my $groups = $auth->read_groups();
270

  
271
  foreach my $group (values %{$groups}) {
272
    if (($form->{group_id} != $group->{id})
273
        && ($form->{name} eq $group->{name})) {
274
      $form->show_generic_error($locale->text("A group with that name does already exist."));
275
    }
276
  }
277

  
278
  my $group = {
279
    'name'        => $locale->text('Full Access'),
280
    'description' => $locale->text('Full access to all functions'),
281
    'rights'      => { map { $_ => 1 } SL::Auth::all_rights() },
282
    'members'     => [ map { $_->{id} } values %members ],
283
  };
284

  
285
  $auth->save_group($group);
286

  
287
  user_migration_complete(1);
288
}
289

  
290
sub dont_create_standard_group {
291
  user_migration_complete(0);
292
}
293

  
294
sub user_migration_complete {
295
  my $standard_group_created = shift;
296

  
297
  $form->{title} = $locale->text('User migration complete');
298
  $form->header();
299

  
300
  print $form->parse_html_template('admin/user_migration_complete', { 'standard_group_created' => $standard_group_created });
301
}
302

  
303
sub list_users {
304
  my %members = $auth->read_all_users();
305

  
306
  delete $members{"root login"};
307

  
151 308
  map { $_->{templates} =~ s|.*/||; } values %members;
152 309

  
153
  $form->{title}  = "Lx-Office ERP " . $locale->text('Administration');
154
  $form->{LOCKED} = -e "$userspath/nologin";
310
  $form->{title}   = "Lx-Office ERP " . $locale->text('Administration');
311
  $form->{LOCKED}  = -e "$userspath/nologin";
155 312
  $form->{MEMBERS} = [ @members{sort { lc $a cmp lc $b } keys %members} ];
156 313

  
157 314
  $form->header();
......
177 334
  edit_user_form($myconfig);
178 335
}
179 336

  
180
sub edit {
337
sub edit_user {
181 338

  
182 339
  $form->{title} =
183 340
      "Lx-Office ERP "
......
188 345
  $form->isblank("login", $locale->text("The login is missing."));
189 346

  
190 347
  # get user
191
  my $myconfig = new User "$memberfile", "$form->{login}";
192

  
193
  $myconfig->{signature} =~ s/\\n/\r\n/g;
194
  $myconfig->{address}   =~ s/\\n/\r\n/g;
348
  my $myconfig = new User($form->{login});
195 349

  
196 350
  # strip basedir from templates directory
197 351
  $myconfig->{templates} =~ s|.*/||;
......
255 409

  
256 410
  map { $form->{"myc_${_}"} = $myconfig->{$_} } keys %{ $myconfig };
257 411

  
258
  # access control
259
  my @acsorder = ();
260
  my %acs      = ();
261
  my %excl     = ();
262
  open(FH, $menufile) or $form->error("$menufile : $ERRNO");
263

  
264
  while ($item = <FH>) {
265
    next unless $item =~ /\[/;
266
    next if $item =~ /\#/;
412
  my $groups = [];
267 413

  
268
    $item =~ s/(\[|\])//g;
269
    chomp $item;
414
  if ($form->{edit}) {
415
    my $user_id    = $auth->get_user_id($form->{login});
416
    my $all_groups = $auth->read_groups();
270 417

  
271
    my ($level, $menuitem);
272

  
273
    if ($item =~ /--/) {
274
      ($level, $menuitem) = split /--/, $item, 2;
275
    } else {
276
      $level    = $item;
277
      $menuitem = $item;
278
      push @acsorder, $item;
418
    foreach my $group (values %{ $all_groups }) {
419
      push @{ $groups }, $group if (grep { $user_id == $_ } @{ $group->{members} });
279 420
    }
280 421

  
281
    $acs{$level} ||= [];
282
    push @{ $acs{$level} }, $menuitem;
283

  
284
  }
285

  
286
  foreach $item (split(/;/, $myconfig->{acs})) {
287
    ($key, $value) = split /--/, $item, 2;
288
    $excl{$key}{$value} = 1;
289
  }
290

  
291
  $form->{ACLS}    = [];
292
  $form->{all_acs} = "";
293

  
294
  foreach $key (@acsorder) {
295
    my $acl = { "checked" => $form->{login} ? !$excl{$key}->{$key} : 1,
296
                "name"    => "${key}--${key}",
297
                "title"   => $key,
298
                "SUBACLS" => [], };
299
    $form->{all_acs} .= "${key}--${key};";
300

  
301
    foreach $item (@{ $acs{$key} }) {
302
      next if ($key eq $item);
303

  
304
      my $subacl = { "checked" => $form->{login} ? !$excl{$key}->{$item} : 1,
305
                     "name"    => "${key}--${item}",
306
                     "title"   => $item };
307
      push @{ $acl->{SUBACLS} }, $subacl;
308
      $form->{all_acs} .= "${key}--${item};";
309
    }
310
    push @{ $form->{ACLS} }, $acl;
422
    $groups = [ sort { lc $a->{name} cmp lc $b->{name} } @{ $groups } ];
311 423
  }
312 424

  
313
  chop $form->{all_acs};
425
  $form->{CAN_CHANGE_PASSWORD} = $auth->can_change_password();
314 426

  
315 427
  $form->header();
316
  print $form->parse_html_template("admin/edit_user");
428
  print $form->parse_html_template("admin/edit_user", { 'GROUPS' => $groups });
317 429
}
318 430

  
319
sub save {
320

  
431
sub save_user {
321 432
  $form->{dbdriver} = 'Pg';
322 433

  
323 434
  # no spaces allowed in login name
324
  ($form->{login}) = split / /, $form->{login};
325

  
435
  $form->{login} =~ s|\s||g;
326 436
  $form->isblank("login", $locale->text('Login name missing!'));
327 437

  
328 438
  # check for duplicates
329 439
  if (!$form->{edit}) {
330
    $temp = new User "$memberfile", "$form->{login}";
440
    my %members = $auth->read_all_users();
331 441

  
332
    if ($temp->{login}) {
442
    if ($members{$form->{login}}) {
333 443
      $form->error("$form->{login} " . $locale->text('is already a member!'));
334 444
    }
335 445
  }
......
353 463
  $form->{templates} =~ s|.*/||;
354 464
  $form->{templates} =  "$templates/$form->{templates}";
355 465

  
356
  $myconfig = new User "$memberfile", "$form->{login}";
357

  
358
  # redo acs variable and delete all the acs codes
359
  my @acs;
360
  foreach $item (split m|;|, $form->{all_acs}) {
361
    my $name =  "ACS_${item}";
362
    $name    =~ s| |+|g;
363
    push @acs, $item if !$form->{$name};
364
    delete $form->{$name};
365
  }
366
  $form->{acs} = join ";", @acs;
466
  $myconfig = new User($form->{login});
367 467

  
368 468
  $form->isblank("dbname", $locale->text('Dataset missing!'));
369 469
  $form->isblank("dbuser", $locale->text('Database User missing!'));
......
377 477
    $myconfig->{stylesheet} = $form->{userstylesheet};
378 478
  }
379 479

  
380
  $myconfig->save_member($memberfile, $userspath);
480
  $myconfig->save_member();
481

  
482
  if ($auth->can_change_password()
483
      && defined $form->{new_password}
484
      && ($form->{new_password} ne '********')) {
485
    $auth->change_password($form->{login}, $form->{new_password});
486
  }
381 487

  
382 488
  if ($webdav) {
383 489
    @webdavdirs =
......
457 563

  
458 564
}
459 565

  
460
sub delete {
461
  $form->error($locale->text('File locked!')) if (-f ${memberfile} . LCK);
462
  open(FH, ">${memberfile}.LCK") or $form->error("${memberfile}.LCK : $ERRNO");
463
  close(FH);
566
sub delete_user {
567
  my %members   = $auth->read_all_users();
568
  my $templates = $members{$form->{login}}->{templates};
464 569

  
465
  my $members = Inifile->new($memberfile);
466
  my $templates = $members->{$form->{login}}->{templates};
467
  delete $members->{$form->{login}};
468
  $members->write();
469
  unlink "${memberfile}.LCK";
570
  $auth->delete_user($form->{login});
470 571

  
471 572
  if ($templates) {
472 573
    my $templates_in_use = 0;
473
    foreach $login (keys %{ $members }) {
474
      next if $login =~ m/^[A-Z]+$/;
475
      next if $members->{$login}->{templates} ne $templates;
574

  
575
    foreach $login (keys %members) {
576
      next if $form->{login} eq $login;
577
      next if $members{$login}->{templates} ne $templates;
476 578
      $templates_in_use = 1;
477 579
      last;
478 580
    }
......
483 585
    }
484 586
  }
485 587

  
486
  # delete config file for user
487
  unlink "$userspath/$form->{login}.conf";
488

  
489 588
  $form->redirect($locale->text('User deleted!'));
490 589

  
491 590
}
......
512 611
  $value;
513 612
}
514 613

  
515
sub change_admin_password {
516

  
517
  $form->{title} =
518
      qq|Lx-Office ERP |
519
    . $locale->text('Administration') . " / "
520
    . $locale->text('Change Admin Password');
521

  
522
  $form->header();
523
  print $form->parse_html_template("admin/change_admin_password");
524
}
525

  
526
sub change_password {
527
  if ($form->{"password"} ne $form->{"password_again"}) {
528
    $form->{title} =
529
      qq|Lx-Office ERP |
530
      . $locale->text('Administration') . " / "
531
      . $locale->text('Change Admin Password');
532

  
533
    $form->header();
534
    $form->error($locale->text("The passwords do not match."));
535
  }
536

  
537
  $root->{password} = $form->{password};
538

  
539
  $root->{'root login'} = 1;
540
  $root->save_member($memberfile);
541

  
542
  $form->{callback} =
543
    "$form->{script}?action=list_users&rpw=$root->{password}";
544

  
545
  $form->redirect($locale->text('Password changed!'));
546
}
547

  
548
sub check_password {
549
  $root = new User "$memberfile", $form->{root};
550

  
551
  if (!defined($root->{password}) || ($root->{password} ne $form->{rpw})) {
552
    $form->error($locale->text('Incorrect Password!'));
553
  }
554

  
555
}
556

  
557 614
sub pg_database_administration {
558 615

  
559 616
  $form->{dbdriver} = 'Pg';
......
681 738
}
682 739

  
683 740
sub delete_dataset {
684
  @dbsources = User->dbsources_unused(\%$form, $memberfile);
741
  @dbsources = User->dbsources_unused($form);
685 742
  $form->error($locale->text('Nothing to delete!')) unless @dbsources;
686 743

  
687 744
  $form->{title} =
......
974 1031

  
975 1032
  unlink "$userspath/nologin";
976 1033

  
977
  $form->{callback} =
978
    "$form->{script}?action=list_users&rpw=$root->{password}";
1034
  $form->{callback} = "admin.pl?action=list_users";
979 1035

  
980 1036
  $form->redirect($locale->text('Lockfile removed!'));
981 1037

  
......
987 1043
    or $form->error($locale->text('Cannot create Lock!'));
988 1044
  close(FH);
989 1045

  
990
  $form->{callback} =
991
    "$form->{script}?action=list_users&rpw=$root->{password}";
1046
  $form->{callback} = "admin.pl?action=list_users";
992 1047

  
993 1048
  $form->redirect($locale->text('Lockfile created!'));
994 1049

  
995 1050
}
1051

  
1052
sub yes {
1053
  call_sub($form->{yes_nextsub});
1054
}
1055

  
1056
sub no {
1057
  call_sub($form->{no_nextsub});
1058
}
1059

  
1060
sub add {
1061
  call_sub($form->{add_nextsub});
1062
}
1063

  
1064
sub edit {
1065
  $form->{edit_nextsub} ||= 'edit_user';
1066

  
1067
  call_sub($form->{edit_nextsub});
1068
}
1069

  
1070
sub delete {
1071
  $form->{delete_nextsub} ||= 'delete_user';
1072

  
1073
  call_sub($form->{delete_nextsub});
1074
}
1075

  
1076
sub save {
1077
  $form->{save_nextsub} ||= 'save_user';
1078

  
1079
  call_sub($form->{save_nextsub});
1080
}
1081

  
1082
sub back {
1083
  call_sub($form->{back_nextsub});
1084
}
1085

  
1086
sub dispatcher {
1087
  foreach my $action (qw(create_standard_group dont_create_standard_group)) {
1088
    if ($form->{"action_${action}"}) {
1089
      call_sub($action);
1090
      return;
1091
    }
1092
  }
1093

  
1094
  call_sub($form->{default_action}) if ($form->{default_action});
1095

  
1096
  $form->error($locale->text('No action defined.'));
1097
}
1098

  
1099
1;

Auch abrufbar als: Unified diff