-sub add_user {
- $::form->{title} = "kivitendo " . $::locale->text('Administration') . " / " . $::locale->text('Add User');
-
- # User does not have a well behaved new constructor, so we'll just have to build one ourself
- my $user = bless {
- "vclimit" => 200,
- "countrycode" => "de",
- "numberformat" => "1.000,00",
- "dateformat" => "dd.mm.yy",
- "stylesheet" => "kivitendo.css",
- "menustyle" => "neu",
- dbport => $::auth->{DB_config}->{port} || 5432,
- dbuser => $::auth->{DB_config}->{user} || 'lxoffice',
- dbhost => $::auth->{DB_config}->{host} || 'localhost',
- }, 'User';
-
- edit_user_form($user);
-}
-
-sub edit_user {
- $::form->{title} = "kivitendo " . $::locale->text('Administration') . " / " . $::locale->text('Edit User');
- $::form->{edit} = 1;
-
- # get user
- my $user = User->new(id => $::form->{user}{id});
-
- edit_user_form($user);
-}
-
-sub edit_user_form {
- my ($user) = @_;
-
- my %cc = $user->country_codes;
- my @all_countrycodes = map { id => $_, title => $cc{$_} }, sort { $cc{$a} cmp $cc{$b} } keys %cc;
- my ($all_dir, $all_master) = _search_templates();
- my $groups = [];
-
- if ($::form->{edit}) {
- my $user_id = $::auth->get_user_id($user->{login});
- my $all_groups = $::auth->read_groups();
-
- for my $group (values %{ $all_groups }) {
- push @{ $groups }, $group if (grep { $user_id == $_ } @{ $group->{members} });
- }
-
- $groups = [ sort { lc $a->{name} cmp lc $b->{name} } @{ $groups } ];
- }
-
- $::form->header;
- print $::form->parse_html_template("admin/edit_user", {
- GROUPS => $groups,
- CAN_CHANGE_PASSWORD => $::auth->can_change_password,
- user => $user->data,
- all_stylesheets => \@all_stylesheets,
- all_numberformats => \@valid_numberformats,
- all_dateformats => \@valid_dateformats,
- all_countrycodes => \@all_countrycodes,
- all_menustyles => \@all_menustyles,
- all_templates => $all_dir,
- all_master_templates => $all_master,
- });
-}
-
-sub save_user {
- my $form = $main::form;
- my $locale = $main::locale;
-
- my $user = $form->{user};
-
- $user->{dbdriver} = 'Pg';
-
- if (!$::form->{edit}) {
- # no spaces allowed in login name
- $user->{login} =~ s/\s//g;
- $::form->show_generic_error($::locale->text('Login name missing!')) unless $user->{login};
-
- # check for duplicates
- my %members = $::auth->read_all_users;
- if ($members{$user->{login}}) {
- $::form->show_generic_error($locale->text('Another user with the login #1 does already exist.', $user->{login}), 'back_button' => 1);
- }
- }
-
- # no spaces allowed in directories
- ($::form->{newtemplates}) = split / /, $::form->{newtemplates};
- $user->{templates} = $::form->{newtemplates} || $::form->{usetemplates} || $user->{login};
-
- # is there a basedir
- if (!-d $::lx_office_conf{paths}->{templates}) {
- $::form->error(sprintf($::locale->text("The directory %s does not exist."), $::lx_office_conf{paths}->{templates}));
- }
-
- # add base directory to $form->{templates}
- $user->{templates} =~ s|.*/||;
- $user->{templates} = $::lx_office_conf{paths}->{templates} . "/$user->{templates}";
-
- my $myconfig = new User(id => $user->{id});
-
- $::form->show_generic_error($::locale->text('Dataset missing!')) unless $user->{dbname};
- $::form->show_generic_error($::locale->text('Database User missing!')) unless $user->{dbuser};
-
- foreach my $item (keys %{$user}) {
- $myconfig->{$item} = $user->{$item};
- }
-
- $myconfig->save_member;
-
- $user->{templates} =~ s|.*/||;
- $user->{templates} = $::lx_office_conf{paths}->{templates} . "/$user->{templates}";
- $::form->{mastertemplates} =~ s|.*/||;
-
- # create user template directory and copy master files
- if (!-d "$user->{templates}") {
- umask(002);
-
- if (mkdir "$user->{templates}", oct("771")) {
-
- umask(007);
-
- # copy templates to the directory
-
- my $oldcurrdir = getcwd();
- if (!chdir("$::lx_office_conf{paths}->{templates}/print/$::form->{mastertemplates}")) {
- $form->error("$ERRNO: chdir $::lx_office_conf{paths}->{templates}/print/$::form->{mastertemplates}");
- }
-
- my $newdir = File::Spec->catdir($oldcurrdir, $user->{templates});
-
- find(
- sub
- {
- next if ($_ eq ".");
-
- if (-d $_) {
- if (!mkdir (File::Spec->catdir($newdir, $File::Find::name))) {
- chdir($oldcurrdir);
- $form->error("$ERRNO: mkdir $File::Find::name");
- }
- } elsif (-l $_) {
- if (!symlink (readlink($_),
- File::Spec->catfile($newdir, $File::Find::name))) {
- chdir($oldcurrdir);
- $form->error("$ERRNO: symlink $File::Find::name");
- }
- } elsif (-f $_) {
- if (!copy($_, File::Spec->catfile($newdir, $File::Find::name))) {
- chdir($oldcurrdir);
- $form->error("$ERRNO: cp $File::Find::name");
- }
- }
- }, "./");
-
- chdir($oldcurrdir);
-
- } else {
- $form->error("$ERRNO: $user->{templates}");
- }
- }
-
- # Add new user to his groups.
- if (ref $form->{new_user_group_ids} eq 'ARRAY') {
- my $all_groups = $main::auth->read_groups();
- my %user = $main::auth->read_user(login => $myconfig->{login});
-
- foreach my $group_id (@{ $form->{new_user_group_ids} }) {
- my $group = $all_groups->{$group_id};
-
- next if !$group;
-
- push @{ $group->{members} }, $user{id};
- $main::auth->save_group($group);
- }
- }
-
- if ($main::auth->can_change_password()
- && defined $::form->{new_password}
- && ($::form->{new_password} ne '********')) {
- my $verifier = SL::Auth::PasswordPolicy->new;
- my $result = $verifier->verify($::form->{new_password}, 1);
-
- if ($result != SL::Auth::PasswordPolicy->OK()) {
- $form->error($::locale->text('The settings were saved, but the password was not changed.') . ' ' . join(' ', $verifier->errors($result)));
- }
-
- $main::auth->change_password($myconfig->{login}, $::form->{new_password});
- }
-
- $::form->redirect($::locale->text('User saved!'));
-}
-
-sub save_user_as_new {
- my $form = $main::form;
-
- $form->{user}{login} = $::form->{new_user_login};
- delete $form->{user}{id};
- delete @{$form}{qw(id edit new_user_login)};
-
- save_user();
-}
-
-sub delete_user {
- my $form = $main::form;
- my $locale = $main::locale;
-
- my $user = $::form->{user} || {};
-
- $::form->show_generic_error($::locale->text('Missing user id!')) unless $user->{id};
-
- my $loaded_user = User->new(id => $user->{id});
-
- my %members = $main::auth->read_all_users();
- my $templates = $members{$loaded_user->{login}}->{templates};
-
- $main::auth->delete_user($loaded_user->{login});
-
- if ($templates) {
- my $templates_in_use = 0;
-
- foreach my $login (keys %members) {
- next if $loaded_user->{login} eq $login;
- next if $members{$login}->{templates} ne $templates;
- $templates_in_use = 1;
- last;
- }
-
- if (!$templates_in_use && -d $templates) {
- unlink <$templates/*>;
- rmdir $templates;
- }
- }
-
- $form->redirect($locale->text('User deleted!'));
-
-}
-