1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
31 # add/edit/delete users
33 #======================================================================
35 $menufile = "menu.ini";
46 require "bin/mozilla/common.pl";
48 our $cgi = new CGI('');
51 $form->{"root"} = "root login";
53 $locale = new Locale $language, "admin";
56 if (-f "bin/mozilla/custom_$form->{script}") {
57 eval { require "bin/mozilla/custom_$form->{script}"; };
58 $form->error($@) if ($@);
61 $form->{stylesheet} = "lx-office-erp.css";
62 $form->{favicon} = "favicon.ico";
64 if ($form->{action}) {
67 $subroutine = $locale->findsub($form->{action});
69 if ($subroutine eq 'login') {
71 $form->{rpw} = crypt $form->{rpw}, "ro";
77 call_sub($subroutine);
81 # if there are no drivers bail out
82 $form->error($locale->text('No Database Drivers available!'))
83 unless (User->dbdrivers);
86 if (!-f $memberfile) {
87 open(FH, ">$memberfile") or $form->error("$memberfile : $!");
88 print FH qq|# SQL-Ledger Accounting members
108 qq|Lx-Office ERP $form->{version} | . $locale->text('Administration');
111 print $form->parse_html_template('admin/adminlogin');
120 $form->error($locale->text('File locked!')) if (-f "${memberfile}.LCK");
122 open(FH, "$memberfile") or $form->error("$memberfile : $!");
131 $login =~ s/(\[|\])//g;
133 $members{$login} = { "login" => $login };
136 if (/^([a-z]+)=(.*)/) {
137 $members{$login}->{$1} = $2;
143 delete $members{"root login"};
144 map { $_->{templates} =~ s|.*/||; } values %members;
146 $form->{title} = "Lx-Office ERP " . $locale->text('Administration');
147 $form->{LOCKED} = -e "$userspath/nologin";
148 $form->{MEMBERS} = [ @members{sort { lc $a cmp lc $b } keys %members} ];
151 print $form->parse_html_template("admin/list_users");
158 . $locale->text('Administration') . " / "
159 . $locale->text('Add User');
163 "countrycode" => "de",
164 "numberformat" => "1000,00",
165 "dateformat" => "dd.mm.yy",
166 "stylesheet" => "lx-office-erp.css",
170 edit_user_form($myconfig);
177 . $locale->text('Administration') . " / "
178 . $locale->text('Edit User');
181 $form->isblank("login", $locale->text("The login is missing."));
184 my $myconfig = new User "$memberfile", "$form->{login}";
186 $myconfig->{signature} =~ s/\\n/\r\n/g;
187 $myconfig->{address} =~ s/\\n/\r\n/g;
189 # strip basedir from templates directory
190 $myconfig->{templates} =~ s|.*/||;
192 edit_user_form($myconfig);
198 my @valid_dateformats = qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd);
199 $form->{ALL_DATEFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{dateformat} } } @valid_dateformats ];
201 my @valid_numberformats = qw(1,000.00 1000.00 1.000,00 1000,00);
202 $form->{ALL_NUMBERFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{numberformat} } } @valid_numberformats ];
204 %countrycodes = User->country_codes;
205 $form->{ALL_COUNTRYCODES} = [];
206 foreach $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
207 push @{ $form->{ALL_COUNTRYCODES} }, { "value" => $countrycode,
208 "name" => $countrycodes{$countrycode},
209 "selected" => $countrycode eq $myconfig->{countrycode} };
212 # is there a templates basedir
213 if (!-d "$templates") {
214 $form->error(sprintf($locale->text("The directory %s does not exist."), $templates));
217 opendir TEMPLATEDIR, "$templates/." or $form->error("$templates : $!");
218 my @all = readdir(TEMPLATEDIR);
219 my @alldir = sort grep { -d "$templates/$_" && !/^\.\.?$/ } @all;
220 my @allhtml = sort grep { -f "$templates/$_" && /\.html$/ } @all;
221 closedir TEMPLATEDIR;
223 @alldir = grep !/\.(html|tex|sty|odt|xml|txb)$/, @alldir;
224 @alldir = grep !/^(webpages|\.svn)$/, @alldir;
226 @allhtml = reverse grep !/Default/, @allhtml;
227 push @allhtml, 'Default';
228 @allhtml = reverse @allhtml;
230 $form->{ALL_TEMPLATES} = [ map { { "name", => $_, "selected" => $_ eq $myconfig->{templates} } } @alldir ];
232 $lastitem = $allhtml[0];
233 $lastitem =~ s/-.*//g;
234 $form->{ALL_MASTER_TEMPLATES} = [ { "name" => $lastitem, "selected" => $lastitem eq "German" } ];
235 foreach $item (@allhtml) {
237 next if ($item eq $lastitem);
239 push @{ $form->{ALL_MASTER_TEMPLATES} }, { "name" => $item, "selected" => $item eq "German" };
243 # css dir has styles that are not intended as general layouts.
244 # reverting to hardcoded list
245 $form->{ALL_STYLESHEETS} = [ map { { "name" => $_, "selected" => $_ eq $myconfig->{stylesheet} } } qw(lx-office-erp.css Win2000.css) ];
247 $form->{"menustyle_" . $myconfig->{menustyle} } = 1;
249 map { $form->{"myc_${_}"} = $myconfig->{$_} } keys %{ $myconfig };
255 open(FH, $menufile) or $form->error("$menufile : $!");
257 while ($item = <FH>) {
258 next unless $item =~ /\[/;
259 next if $item =~ /\#/;
261 $item =~ s/(\[|\])//g;
264 my ($level, $menuitem);
267 ($level, $menuitem) = split /--/, $item, 2;
271 push @acsorder, $item;
275 push @{ $acs{$level} }, $menuitem;
279 foreach $item (split(/;/, $myconfig->{acs})) {
280 ($key, $value) = split /--/, $item, 2;
281 $excl{$key}{$value} = 1;
285 $form->{all_acs} = "";
287 foreach $key (@acsorder) {
288 my $acl = { "checked" => $form->{login} ? !$excl{$key}->{$key} : 1,
289 "name" => "${key}--${key}",
292 $form->{all_acs} .= "${key}--${key};";
294 foreach $item (@{ $acs{$key} }) {
295 next if ($key eq $item);
297 my $subacl = { "checked" => $form->{login} ? !$excl{$key}->{$item} : 1,
298 "name" => "${key}--${item}",
300 push @{ $acl->{SUBACLS} }, $subacl;
301 $form->{all_acs} .= "${key}--${item};";
303 push @{ $form->{ACLS} }, $acl;
306 chop $form->{all_acs};
309 print $form->parse_html_template("admin/edit_user");
314 $form->{dbdriver} = 'Pg';
316 # no spaces allowed in login name
317 ($form->{login}) = split / /, $form->{login};
319 $form->isblank("login", $locale->text('Login name missing!'));
321 # check for duplicates
322 if (!$form->{edit}) {
323 $temp = new User "$memberfile", "$form->{login}";
325 if ($temp->{login}) {
326 $form->error("$form->{login} " . $locale->text('is already a member!'));
330 # no spaces allowed in directories
331 ($form->{newtemplates}) = split / /, $form->{newtemplates};
333 if ($form->{newtemplates}) {
334 $form->{templates} = $form->{newtemplates};
337 ($form->{usetemplates}) ? $form->{usetemplates} : $form->{login};
341 if (!-d "$templates") {
342 $form->error(sprintf($locale->text("The directory %s does not exist."), $templates));
345 # add base directory to $form->{templates}
346 $form->{templates} =~ s|.*/||;
347 $form->{templates} = "$templates/$form->{templates}";
349 $myconfig = new User "$memberfile", "$form->{login}";
351 # redo acs variable and delete all the acs codes
353 foreach $item (split m|;|, $form->{all_acs}) {
354 my $name = "ACS_${item}";
356 push @acs, $item if !$form->{$name};
357 delete $form->{$name};
359 $form->{acs} = join ";", @acs;
361 $form->isblank("dbname", $locale->text('Dataset missing!'));
362 $form->isblank("dbuser", $locale->text('Database User missing!'));
364 foreach $item (keys %{$form}) {
365 $myconfig->{$item} = $form->{$item};
368 delete $myconfig->{stylesheet};
369 if ($form->{userstylesheet}) {
370 $myconfig->{stylesheet} = $form->{userstylesheet};
373 $myconfig->save_member($memberfile, $userspath);
377 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
378 foreach $directory (@webdavdirs) {
379 $file = "webdav/" . $directory . "/webdav-user";
380 if ($form->{$directory}) {
381 if (open(HTACCESS, "$file")) {
383 ($login, $password) = split(/:/, $_);
384 if ($login ne $form->{login}) {
390 open(HTACCESS, "> $file") or die "cannot open $file $!\n";
391 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
392 print(HTACCESS $newfile);
395 $form->{$directory} = 0;
396 if (open(HTACCESS, "$file")) {
398 ($login, $password) = split(/:/, $_);
399 if ($login ne $form->{login}) {
405 open(HTACCESS, "> $file") or die "cannot open $file $!\n";
406 print(HTACCESS $newfile);
412 $form->{templates} =~ s|.*/||;
413 $form->{templates} = "${templates}/$form->{templates}";
414 $form->{mastertemplates} =~ s|.*/||;
416 # create user template directory and copy master files
417 if (!-d "$form->{templates}") {
420 if (mkdir "$form->{templates}", oct("771")) {
424 # copy templates to the directory
425 opendir TEMPLATEDIR, "$templates/." or $form - error("$templates : $!");
426 @templates = grep /$form->{mastertemplates}.*?\.(html|tex|sty|xml|txb)$/,
428 closedir TEMPLATEDIR;
430 foreach $file (@templates) {
431 open(TEMP, "$templates/$file")
432 or $form->error("$templates/$file : $!");
434 $file =~ s/$form->{mastertemplates}-//;
435 open(NEW, ">$form->{templates}/$file")
436 or $form->error("$form->{templates}/$file : $!");
438 while ($line = <TEMP>) {
445 $form->error("$!: $form->{templates}");
449 $form->redirect($locale->text('User saved!'));
454 $form->error($locale->text('File locked!')) if (-f ${memberfile} . LCK);
455 open(FH, ">${memberfile}.LCK") or $form->error("${memberfile}.LCK : $!");
458 my $members = Inifile->new($memberfile);
459 my $templates = $members->{$form->{login}}->{templates};
460 delete $members->{$form->{login}};
462 unlink "${memberfile}.LCK";
465 my $templates_in_use = 0;
466 foreach $login (keys %{ $members }) {
467 next if $login =~ m/^[A-Z]+$/;
468 next if $members->{$login}->{templates} ne $templates;
469 $templates_in_use = 1;
473 if (!$templates_in_use && -d $templates) {
474 unlink <$templates/*>;
479 # delete config file for user
480 unlink "$userspath/$form->{login}.conf";
482 $form->redirect($locale->text('User deleted!'));
490 return ($login) ? $login : undef;
497 my ($null, $value) = split(/=/, $line, 2);
500 $value =~ s/\s#.*//g;
502 # remove any trailing whitespace
503 $value =~ s/^\s*(.*?)\s*$/$1/;
508 sub change_admin_password {
512 . $locale->text('Administration') . " / "
513 . $locale->text('Change Admin Password');
516 print $form->parse_html_template("admin/change_admin_password");
519 sub change_password {
520 if ($form->{"password"} ne $form->{"password_again"}) {
523 . $locale->text('Administration') . " / "
524 . $locale->text('Change Admin Password');
527 $form->error($locale->text("The passwords do not match."));
530 $root->{password} = $form->{password};
532 $root->{'root login'} = 1;
533 $root->save_member($memberfile);
536 "$form->{script}?action=list_users&rpw=$root->{password}";
538 $form->redirect($locale->text('Password changed!'));
542 $root = new User "$memberfile", $form->{root};
544 if (!defined($root->{password}) || ($root->{password} ne $form->{rpw})) {
545 $form->error($locale->text('Incorrect Password!'));
550 sub pg_database_administration {
552 $form->{dbdriver} = 'Pg';
557 sub dbselect_source {
558 $form->{dbport} = '5432';
559 $form->{dbuser} = 'postgres';
560 $form->{dbdefault} = 'template1';
561 $form->{dbhost} = 'localhost';
563 $form->{title} = "Lx-Office ERP / " . $locale->text('Database Administration');
566 print $form->parse_html_template("admin/dbadmin");
570 call_sub($form->{"nextsub"});
576 . $locale->text('Database Administration') . " / "
577 . $locale->text('Update Dataset');
579 my @need_updates = User->dbneedsupdate($form);
580 $form->{NEED_UPDATES} = \@need_updates;
581 $form->{ALL_UPDATED} = !scalar @need_updates;
584 print $form->parse_html_template("admin/update_dataset");
588 $form->{stylesheet} = "lx-office-erp.css";
589 $form->{title} = $locale->text("Dataset upgrade");
592 my $rowcount = $form->{rowcount} * 1;
593 my @update_rows = grep { $form->{"update_$_"} } (1 .. $rowcount);
594 $form->{NOTHING_TO_DO} = !scalar @update_rows;
595 my $saved_form = save_form();
599 print $form->parse_html_template("admin/dbupgrade_all_header");
601 foreach my $i (@update_rows) {
602 restore_form($saved_form);
604 map { $form->{$_} = $form->{"${_}_${i}"} } qw(dbname dbdriver dbhost dbport dbuser dbpasswd);
606 my $controls = parse_dbupdate_controls($form, $form->{dbdriver});
608 print $form->parse_html_template("admin/dbupgrade_header");
610 $form->{dbupdate} = $form->{dbname};
611 $form->{$form->{dbname}} = 1;
613 User->dbupdate($form);
614 User->dbupdate2($form, $controls);
616 print $form->parse_html_template("admin/dbupgrade_footer");
619 print $form->parse_html_template("admin/dbupgrade_all_done");
623 $form->{dbsources} = join " ", map { "[${_}]" } sort User->dbsources(\%$form);
625 $form->{CHARTS} = [];
627 opendir SQLDIR, "sql/." or $form - error($!);
628 foreach $item (sort grep /-chart\.sql\z/, readdir SQLDIR) {
629 next if ($item eq 'Default-chart.sql');
630 $item =~ s/-chart\.sql//;
631 push @{ $form->{CHARTS} }, { "name" => $item,
632 "selected" => $item eq "Germany-DATEV-SKR03EU" };
636 my $default_charset = $dbcharset;
637 $default_charset ||= Common::DEFAULT_CHARSET;
639 $form->{DBENCODINGS} = [];
641 foreach my $encoding (@Common::db_encodings) {
642 push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
643 "label" => $encoding->{label},
644 "selected" => $encoding->{charset} eq $default_charset };
649 . $locale->text('Database Administration') . " / "
650 . $locale->text('Create Dataset');
653 print $form->parse_html_template("admin/create_dataset");
657 $form->isblank("db", $locale->text('Dataset missing!'));
659 User->dbcreate(\%$form);
663 . $locale->text('Database Administration') . " / "
664 . $locale->text('Create Dataset');
667 print $form->parse_html_template("admin/dbcreate");
671 @dbsources = User->dbsources_unused(\%$form, $memberfile);
672 $form->error($locale->text('Nothing to delete!')) unless @dbsources;
676 . $locale->text('Database Administration') . " / "
677 . $locale->text('Delete Dataset');
678 $form->{DBSOURCES} = [ map { { "name", $_ } } sort @dbsources ];
681 print $form->parse_html_template("admin/delete_dataset");
687 $form->error($locale->text('No Dataset selected!'));
690 User->dbdelete(\%$form);
694 . $locale->text('Database Administration') . " / "
695 . $locale->text('Delete Dataset');
698 print $form->parse_html_template("admin/dbdelete");
703 unlink "$userspath/nologin";
706 "$form->{script}?action=list_users&rpw=$root->{password}";
708 $form->redirect($locale->text('Lockfile removed!'));
714 open(FH, ">$userspath/nologin")
715 or $form->error($locale->text('Cannot create Lock!'));
719 "$form->{script}?action=list_users&rpw=$root->{password}";
721 $form->redirect($locale->text('Lockfile created!'));